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