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