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