1   /**
2    * Copyright (c) 2000-2008 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.portlet.LiferayPortlet;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  
28  import java.io.IOException;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
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  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class JSPPortlet extends LiferayPortlet {
51  
52      public void init() {
53          aboutJSP = getInitParameter("about-jsp");
54          configJSP = getInitParameter("config-jsp");
55          editJSP = getInitParameter("edit-jsp");
56          editDefaultsJSP = getInitParameter("edit-defaults-jsp");
57          editGuestJSP = getInitParameter("edit-guest-jsp");
58          helpJSP = getInitParameter("help-jsp");
59          previewJSP = getInitParameter("preview-jsp");
60          printJSP = getInitParameter("print-jsp");
61          viewJSP = getInitParameter("view-jsp");
62  
63          clearRequestParameters = GetterUtil.getBoolean(
64              getInitParameter("clear-request-parameters"));
65      }
66  
67      public void doAbout(
68              RenderRequest renderRequest, RenderResponse renderResponse)
69          throws IOException, PortletException {
70  
71          include(aboutJSP, renderRequest, renderResponse);
72      }
73  
74      public void doConfig(
75              RenderRequest renderRequest, RenderResponse renderResponse)
76          throws IOException, PortletException {
77  
78          include(configJSP, renderRequest, renderResponse);
79      }
80  
81      public void doEdit(
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws IOException, PortletException {
84  
85          if (renderRequest.getPreferences() == null) {
86              super.doEdit(renderRequest, renderResponse);
87          }
88          else {
89              include(editJSP, renderRequest, renderResponse);
90          }
91      }
92  
93      public void doEditDefaults(
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws IOException, PortletException {
96  
97          if (renderRequest.getPreferences() == null) {
98              super.doEdit(renderRequest, renderResponse);
99          }
100         else {
101             include(editDefaultsJSP, renderRequest, renderResponse);
102         }
103     }
104 
105     public void doEditGuest(
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws IOException, PortletException {
108 
109         if (renderRequest.getPreferences() == null) {
110             super.doEdit(renderRequest, renderResponse);
111         }
112         else {
113             include(editGuestJSP, renderRequest, renderResponse);
114         }
115     }
116 
117     public void doHelp(
118             RenderRequest renderRequest, RenderResponse renderResponse)
119         throws IOException, PortletException {
120 
121         include(helpJSP, renderRequest, renderResponse);
122     }
123 
124     public void doPreview(
125             RenderRequest renderRequest, RenderResponse renderResponse)
126         throws IOException, PortletException {
127 
128         include(previewJSP, renderRequest, renderResponse);
129     }
130 
131     public void doPrint(
132             RenderRequest renderRequest, RenderResponse renderResponse)
133         throws IOException, PortletException {
134 
135         include(printJSP, renderRequest, renderResponse);
136     }
137 
138     public void doView(
139             RenderRequest renderRequest, RenderResponse renderResponse)
140         throws IOException, PortletException {
141 
142         include(viewJSP, renderRequest, renderResponse);
143     }
144 
145     public void processAction(
146             ActionRequest actionRequest, ActionResponse actionResponse)
147         throws IOException, PortletException {
148     }
149 
150     public void serveResource(
151             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
152         throws IOException, PortletException {
153 
154         String jspPage = resourceRequest.getParameter("jspPage");
155 
156         if (jspPage != null) {
157             include(
158                 jspPage, resourceRequest, resourceResponse,
159                 PortletRequest.RESOURCE_PHASE);
160         }
161         else {
162             super.serveResource(resourceRequest, resourceResponse);
163         }
164     }
165 
166     protected void doDispatch(
167             RenderRequest renderRequest, RenderResponse renderResponse)
168         throws IOException, PortletException {
169 
170         String jspPage = renderRequest.getParameter("jspPage");
171 
172         if (jspPage != null) {
173             include(jspPage, renderRequest, renderResponse);
174         }
175         else {
176             super.doDispatch(renderRequest, renderResponse);
177         }
178     }
179 
180     protected void include(
181             String path, PortletRequest portletRequest,
182             PortletResponse portletResponse)
183         throws IOException, PortletException {
184 
185         include(
186             path, portletRequest, portletResponse, PortletRequest.RENDER_PHASE);
187     }
188 
189     protected void include(
190             String path, PortletRequest portletRequest,
191             PortletResponse portletResponse, String lifecycle)
192         throws IOException, PortletException {
193 
194         PortletRequestDispatcher prd =
195             getPortletContext().getRequestDispatcher(path);
196 
197         if (prd == null) {
198             _log.error(path + " is not a valid include");
199         }
200         else {
201             prd.include(portletRequest, portletResponse);
202         }
203 
204         if (clearRequestParameters) {
205             if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
206                 portletResponse.setProperty("clear-request-parameters", "true");
207             }
208         }
209     }
210 
211     protected String aboutJSP;
212     protected String configJSP;
213     protected String editJSP;
214     protected String editDefaultsJSP;
215     protected String editGuestJSP;
216     protected String helpJSP;
217     protected String previewJSP;
218     protected String printJSP;
219     protected String viewJSP;
220     protected boolean clearRequestParameters;
221 
222     private static Log _log = LogFactory.getLog(JSPPortlet.class);
223 
224 }