1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.bridges.jsp;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.LiferayPortlet;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  
30  import java.io.IOException;
31  
32  import javax.portlet.PortletException;
33  import javax.portlet.PortletRequest;
34  import javax.portlet.PortletRequestDispatcher;
35  import javax.portlet.PortletResponse;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  import javax.portlet.ResourceRequest;
39  import javax.portlet.ResourceResponse;
40  
41  /**
42   * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class JSPPortlet extends LiferayPortlet {
48  
49      public void init() {
50          aboutJSP = getInitParameter("about-jsp");
51          configJSP = getInitParameter("config-jsp");
52          editJSP = getInitParameter("edit-jsp");
53          editDefaultsJSP = getInitParameter("edit-defaults-jsp");
54          editGuestJSP = getInitParameter("edit-guest-jsp");
55          helpJSP = getInitParameter("help-jsp");
56          previewJSP = getInitParameter("preview-jsp");
57          printJSP = getInitParameter("print-jsp");
58          viewJSP = getInitParameter("view-jsp");
59  
60          clearRequestParameters = GetterUtil.getBoolean(
61              getInitParameter("clear-request-parameters"));
62      }
63  
64      public void doAbout(
65              RenderRequest renderRequest, RenderResponse renderResponse)
66          throws IOException, PortletException {
67  
68          include(aboutJSP, renderRequest, renderResponse);
69      }
70  
71      public void doConfig(
72              RenderRequest renderRequest, RenderResponse renderResponse)
73          throws IOException, PortletException {
74  
75          include(configJSP, renderRequest, renderResponse);
76      }
77  
78      public void doEdit(
79              RenderRequest renderRequest, RenderResponse renderResponse)
80          throws IOException, PortletException {
81  
82          if (renderRequest.getPreferences() == null) {
83              super.doEdit(renderRequest, renderResponse);
84          }
85          else {
86              include(editJSP, renderRequest, renderResponse);
87          }
88      }
89  
90      public void doEditDefaults(
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws IOException, PortletException {
93  
94          if (renderRequest.getPreferences() == null) {
95              super.doEdit(renderRequest, renderResponse);
96          }
97          else {
98              include(editDefaultsJSP, renderRequest, renderResponse);
99          }
100     }
101 
102     public void doEditGuest(
103             RenderRequest renderRequest, RenderResponse renderResponse)
104         throws IOException, PortletException {
105 
106         if (renderRequest.getPreferences() == null) {
107             super.doEdit(renderRequest, renderResponse);
108         }
109         else {
110             include(editGuestJSP, renderRequest, renderResponse);
111         }
112     }
113 
114     public void doHelp(
115             RenderRequest renderRequest, RenderResponse renderResponse)
116         throws IOException, PortletException {
117 
118         include(helpJSP, renderRequest, renderResponse);
119     }
120 
121     public void doPreview(
122             RenderRequest renderRequest, RenderResponse renderResponse)
123         throws IOException, PortletException {
124 
125         include(previewJSP, renderRequest, renderResponse);
126     }
127 
128     public void doPrint(
129             RenderRequest renderRequest, RenderResponse renderResponse)
130         throws IOException, PortletException {
131 
132         include(printJSP, renderRequest, renderResponse);
133     }
134 
135     public void doView(
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws IOException, PortletException {
138 
139         include(viewJSP, renderRequest, renderResponse);
140     }
141 
142     public void serveResource(
143             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
144         throws IOException, PortletException {
145 
146         String jspPage = resourceRequest.getParameter("jspPage");
147 
148         if (jspPage != null) {
149             include(
150                 jspPage, resourceRequest, resourceResponse,
151                 PortletRequest.RESOURCE_PHASE);
152         }
153         else {
154             super.serveResource(resourceRequest, resourceResponse);
155         }
156     }
157 
158     protected void doDispatch(
159             RenderRequest renderRequest, RenderResponse renderResponse)
160         throws IOException, PortletException {
161 
162         String jspPage = renderRequest.getParameter("jspPage");
163 
164         if (jspPage != null) {
165             include(jspPage, renderRequest, renderResponse);
166         }
167         else {
168             super.doDispatch(renderRequest, renderResponse);
169         }
170     }
171 
172     protected void include(
173             String path, PortletRequest portletRequest,
174             PortletResponse portletResponse)
175         throws IOException, PortletException {
176 
177         include(
178             path, portletRequest, portletResponse, PortletRequest.RENDER_PHASE);
179     }
180 
181     protected void include(
182             String path, PortletRequest portletRequest,
183             PortletResponse portletResponse, String lifecycle)
184         throws IOException, PortletException {
185 
186         PortletRequestDispatcher portletRequestDispatcher =
187             getPortletContext().getRequestDispatcher(path);
188 
189         if (portletRequestDispatcher == null) {
190             _log.error(path + " is not a valid include");
191         }
192         else {
193             portletRequestDispatcher.include(portletRequest, portletResponse);
194         }
195 
196         if (clearRequestParameters) {
197             if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
198                 portletResponse.setProperty("clear-request-parameters", "true");
199             }
200         }
201     }
202 
203     protected String aboutJSP;
204     protected String configJSP;
205     protected String editJSP;
206     protected String editDefaultsJSP;
207     protected String editGuestJSP;
208     protected String helpJSP;
209     protected String previewJSP;
210     protected String printJSP;
211     protected String viewJSP;
212     protected boolean clearRequestParameters;
213 
214     private static Log _log = LogFactoryUtil.getLog(JSPPortlet.class);
215 
216 }