1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.io.IOException;
35  
36  import java.util.List;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletException;
41  import javax.portlet.PortletRequest;
42  import javax.portlet.PortletRequestDispatcher;
43  import javax.portlet.PortletResponse;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  import javax.portlet.ResourceRequest;
47  import javax.portlet.ResourceResponse;
48  
49  /**
50   * <a href="MVCPortlet.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class MVCPortlet extends LiferayPortlet {
55  
56      public void init() {
57          aboutJSP = getInitParameter("about-jsp");
58          configJSP = getInitParameter("config-jsp");
59          editJSP = getInitParameter("edit-jsp");
60          editDefaultsJSP = getInitParameter("edit-defaults-jsp");
61          editGuestJSP = getInitParameter("edit-guest-jsp");
62          helpJSP = getInitParameter("help-jsp");
63          previewJSP = getInitParameter("preview-jsp");
64          printJSP = getInitParameter("print-jsp");
65          viewJSP = getInitParameter("view-jsp");
66  
67          clearRequestParameters = GetterUtil.getBoolean(
68              getInitParameter("clear-request-parameters"));
69          copyRequestParameters = GetterUtil.getBoolean(
70              getInitParameter("copy-request-parameters"));
71  
72          String packagePrefix = getInitParameter(
73              ActionCommandCache.ACTION_PACKAGE_NAME);
74  
75          if (Validator.isNotNull(packagePrefix)) {
76              _actionCommandCache = new ActionCommandCache(packagePrefix);
77          }
78      }
79  
80      public void doAbout(
81              RenderRequest renderRequest, RenderResponse renderResponse)
82          throws IOException, PortletException {
83  
84          include(aboutJSP, renderRequest, renderResponse);
85      }
86  
87      public void doConfig(
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws IOException, PortletException {
90  
91          include(configJSP, renderRequest, renderResponse);
92      }
93  
94      public void doEdit(
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws IOException, PortletException {
97  
98          if (renderRequest.getPreferences() == null) {
99              super.doEdit(renderRequest, renderResponse);
100         }
101         else {
102             include(editJSP, renderRequest, renderResponse);
103         }
104     }
105 
106     public void doEditDefaults(
107             RenderRequest renderRequest, RenderResponse renderResponse)
108         throws IOException, PortletException {
109 
110         if (renderRequest.getPreferences() == null) {
111             super.doEdit(renderRequest, renderResponse);
112         }
113         else {
114             include(editDefaultsJSP, renderRequest, renderResponse);
115         }
116     }
117 
118     public void doEditGuest(
119             RenderRequest renderRequest, RenderResponse renderResponse)
120         throws IOException, PortletException {
121 
122         if (renderRequest.getPreferences() == null) {
123             super.doEdit(renderRequest, renderResponse);
124         }
125         else {
126             include(editGuestJSP, renderRequest, renderResponse);
127         }
128     }
129 
130     public void doHelp(
131             RenderRequest renderRequest, RenderResponse renderResponse)
132         throws IOException, PortletException {
133 
134         include(helpJSP, renderRequest, renderResponse);
135     }
136 
137     public void doPreview(
138             RenderRequest renderRequest, RenderResponse renderResponse)
139         throws IOException, PortletException {
140 
141         include(previewJSP, renderRequest, renderResponse);
142     }
143 
144     public void doPrint(
145             RenderRequest renderRequest, RenderResponse renderResponse)
146         throws IOException, PortletException {
147 
148         include(printJSP, renderRequest, renderResponse);
149     }
150 
151     public void doView(
152             RenderRequest renderRequest, RenderResponse renderResponse)
153         throws IOException, PortletException {
154 
155         include(viewJSP, renderRequest, renderResponse);
156     }
157 
158     public void processAction(
159             ActionRequest actionRequest, ActionResponse actionResponse)
160         throws IOException, PortletException {
161 
162         super.processAction(actionRequest, actionResponse);
163 
164         if (copyRequestParameters) {
165             PortalUtil.copyRequestParameters(actionRequest, actionResponse);
166         }
167     }
168 
169     public void serveResource(
170             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
171         throws IOException, PortletException {
172 
173         String jspPage = resourceRequest.getParameter("jspPage");
174 
175         if (jspPage != null) {
176             include(
177                 jspPage, resourceRequest, resourceResponse,
178                 PortletRequest.RESOURCE_PHASE);
179         }
180         else {
181             super.serveResource(resourceRequest, resourceResponse);
182         }
183     }
184 
185     protected boolean callActionMethod(
186             ActionRequest request, ActionResponse response)
187         throws PortletException {
188 
189         if ((_actionCommandCache == null) || _actionCommandCache.isEmpty()) {
190             return super.callActionMethod(request, response);
191         }
192 
193         String actionName = ParamUtil.getString(
194             request, ActionRequest.ACTION_NAME);
195 
196         if (!actionName.contains(StringPool.COMMA)) {
197             ActionCommand actionCommand = _actionCommandCache.getActionCommand(
198                 actionName);
199 
200             if (actionCommand != ActionCommandCache.EMPTY) {
201                 return actionCommand.processCommand(request, response);
202             }
203         }
204         else {
205             List<ActionCommand> actionCommands =
206                 _actionCommandCache.getActionCommandChain(actionName);
207 
208             if (actionCommands.isEmpty()) {
209                 return false;
210             }
211 
212             for (ActionCommand actionCommand : actionCommands) {
213                 if (!actionCommand.processCommand(request, response)) {
214                     return false;
215                 }
216             }
217 
218             return true;
219         }
220 
221         return false;
222     }
223 
224     protected void doDispatch(
225             RenderRequest renderRequest, RenderResponse renderResponse)
226         throws IOException, PortletException {
227 
228         String jspPage = renderRequest.getParameter("jspPage");
229 
230         if (jspPage != null) {
231             include(jspPage, renderRequest, renderResponse);
232         }
233         else {
234             super.doDispatch(renderRequest, renderResponse);
235         }
236     }
237 
238     protected void include(
239             String path, PortletRequest portletRequest,
240             PortletResponse portletResponse)
241         throws IOException, PortletException {
242 
243         include(
244             path, portletRequest, portletResponse, PortletRequest.RENDER_PHASE);
245     }
246 
247     protected void include(
248             String path, PortletRequest portletRequest,
249             PortletResponse portletResponse, String lifecycle)
250         throws IOException, PortletException {
251 
252         PortletRequestDispatcher portletRequestDispatcher =
253             getPortletContext().getRequestDispatcher(path);
254 
255         if (portletRequestDispatcher == null) {
256             _log.error(path + " is not a valid include");
257         }
258         else {
259             portletRequestDispatcher.include(portletRequest, portletResponse);
260         }
261 
262         if (clearRequestParameters) {
263             if (lifecycle.equals(PortletRequest.RENDER_PHASE)) {
264                 portletResponse.setProperty("clear-request-parameters", "true");
265             }
266         }
267     }
268 
269     protected ActionCommandCache _actionCommandCache;
270     protected String aboutJSP;
271     protected String configJSP;
272     protected String editJSP;
273     protected String editDefaultsJSP;
274     protected String editGuestJSP;
275     protected String helpJSP;
276     protected String previewJSP;
277     protected String printJSP;
278     protected String viewJSP;
279     protected boolean clearRequestParameters;
280     protected boolean copyRequestParameters;
281 
282     private static Log _log = LogFactoryUtil.getLog(MVCPortlet.class);
283 
284 }