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.portlet;
24  
25  import com.liferay.portal.kernel.portlet.LiferayPortlet;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.security.permission.PermissionThreadLocal;
30  import com.liferay.portal.struts.PortletRequestProcessor;
31  import com.liferay.portal.struts.StrutsUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.io.IOException;
36  
37  import java.util.Map;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletException;
43  import javax.portlet.PortletRequest;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  import javax.portlet.ResourceRequest;
47  import javax.portlet.ResourceResponse;
48  
49  import javax.servlet.ServletException;
50  
51  /**
52   * <a href="StrutsPortlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class StrutsPortlet extends LiferayPortlet {
58  
59      public void doAbout(
60              RenderRequest renderRequest, RenderResponse renderResponse)
61          throws IOException, PortletException {
62  
63          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
64  
65          include(renderRequest, renderResponse);
66      }
67  
68      public void doConfig(
69              RenderRequest renderRequest, RenderResponse renderResponse)
70          throws IOException, PortletException {
71  
72          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
73  
74          include(renderRequest, renderResponse);
75      }
76  
77      public void doEdit(
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws IOException, PortletException {
80  
81          if (renderRequest.getPreferences() == null) {
82              super.doEdit(renderRequest, renderResponse);
83          }
84          else {
85              renderRequest.setAttribute(
86                  WebKeys.PORTLET_STRUTS_ACTION, editAction);
87  
88              include(renderRequest, renderResponse);
89          }
90      }
91  
92      public void doEditDefaults(
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws IOException, PortletException {
95  
96          if (renderRequest.getPreferences() == null) {
97              super.doEdit(renderRequest, renderResponse);
98          }
99          else {
100             renderRequest.setAttribute(
101                 WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
102 
103             include(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             renderRequest.setAttribute(
116                 WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
117 
118             include(renderRequest, renderResponse);
119         }
120     }
121 
122     public void doHelp(
123             RenderRequest renderRequest, RenderResponse renderResponse)
124         throws IOException, PortletException {
125 
126         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
127 
128         include(renderRequest, renderResponse);
129     }
130 
131     public void doPreview(
132             RenderRequest renderRequest, RenderResponse renderResponse)
133         throws IOException, PortletException {
134 
135         renderRequest.setAttribute(
136             WebKeys.PORTLET_STRUTS_ACTION, previewAction);
137 
138         include(renderRequest, renderResponse);
139     }
140 
141     public void doPrint(
142             RenderRequest renderRequest, RenderResponse renderResponse)
143         throws IOException, PortletException {
144 
145         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
146 
147         include(renderRequest, renderResponse);
148     }
149 
150     public void doView(
151             RenderRequest renderRequest, RenderResponse renderResponse)
152         throws IOException, PortletException {
153 
154         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
155 
156         include(renderRequest, renderResponse);
157     }
158 
159     public void init(PortletConfig portletConfig) throws PortletException {
160         super.init(portletConfig);
161 
162         aboutAction = getInitParameter("about-action");
163         configAction = getInitParameter("config-action");
164         editAction = getInitParameter("edit-action");
165         editDefaultsAction = getInitParameter("edit-defaults-action");
166         editGuestAction = getInitParameter("edit-guest-action");
167         helpAction = getInitParameter("help-action");
168         previewAction = getInitParameter("preview-action");
169         printAction = getInitParameter("print-action");
170         viewAction = getInitParameter("view-action");
171 
172         copyRequestParameters = GetterUtil.getBoolean(
173             getInitParameter("copy-request-parameters"), true);
174 
175         _portletConfig = (PortletConfigImpl)portletConfig;
176     }
177 
178     public void processAction(
179             ActionRequest actionRequest, ActionResponse actionResponse)
180         throws IOException, PortletException {
181 
182         String path = actionRequest.getParameter("struts_action");
183 
184         if (Validator.isNotNull(path)) {
185 
186             // Call processAction of com.liferay.portal.struts.PortletAction
187 
188             PermissionChecker permissionChecker =
189                 PermissionThreadLocal.getPermissionChecker();
190 
191             try {
192                 permissionChecker.setValues(actionRequest);
193 
194                 PortletRequestProcessor processor =
195                     _getPortletRequestProcessor(actionRequest);
196 
197                 processor.process(actionRequest, actionResponse, path);
198             }
199             catch (ServletException se) {
200                 throw new PortletException(se);
201             }
202             finally {
203                 permissionChecker.resetValues();
204             }
205         }
206 
207         if (copyRequestParameters) {
208             PortalUtil.copyRequestParameters(actionRequest, actionResponse);
209         }
210     }
211 
212     public void serveResource(
213             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
214         throws IOException, PortletException {
215 
216         resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
217 
218         // Call serveResource of com.liferay.portal.struts.PortletAction
219 
220         PermissionChecker permissionChecker =
221             PermissionThreadLocal.getPermissionChecker();
222 
223         try {
224             permissionChecker.setValues(resourceRequest);
225 
226             PortletRequestProcessor processor =
227                 _getPortletRequestProcessor(resourceRequest);
228 
229             processor.process(resourceRequest, resourceResponse);
230         }
231         catch (ServletException se) {
232             throw new PortletException(se);
233         }
234         finally {
235             permissionChecker.resetValues();
236         }
237     }
238 
239     protected void include(
240             RenderRequest renderRequest, RenderResponse renderResponse)
241         throws IOException, PortletException {
242 
243         // Call render of com.liferay.portal.struts.PortletAction
244 
245         Map<String, Object> strutsAttributes = null;
246 
247         if (_portletConfig.isWARFile()) {
248             strutsAttributes = StrutsUtil.removeStrutsAttributes(
249                 getPortletContext(), renderRequest);
250         }
251 
252         PermissionChecker permissionChecker =
253             PermissionThreadLocal.getPermissionChecker();
254 
255         try {
256             permissionChecker.setValues(renderRequest);
257 
258             PortletRequestProcessor processor =
259                 _getPortletRequestProcessor(renderRequest);
260 
261             processor.process(renderRequest, renderResponse);
262         }
263         catch (ServletException se) {
264             throw new PortletException(se);
265         }
266         finally {
267             permissionChecker.resetValues();
268 
269             if (_portletConfig.isWARFile()) {
270                 StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
271             }
272         }
273 
274         if (copyRequestParameters) {
275             PortalUtil.clearRequestParameters(renderRequest);
276         }
277     }
278 
279     private PortletRequestProcessor _getPortletRequestProcessor(
280         PortletRequest portletRequest) {
281 
282         return (PortletRequestProcessor)getPortletContext().getAttribute(
283             WebKeys.PORTLET_STRUTS_PROCESSOR);
284     }
285 
286     protected String aboutAction;
287     protected String configAction;
288     protected String editAction;
289     protected String editDefaultsAction;
290     protected String editGuestAction;
291     protected String helpAction;
292     protected String previewAction;
293     protected String printAction;
294     protected String viewAction;
295     protected boolean copyRequestParameters;
296 
297     private PortletConfigImpl _portletConfig;
298 
299 }