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.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.PermissionCheckerImpl;
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(RenderRequest req, RenderResponse res)
60          throws IOException, PortletException {
61  
62          req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
63  
64          include(req, res);
65      }
66  
67      public void doConfig(RenderRequest req, RenderResponse res)
68          throws IOException, PortletException {
69  
70          req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
71  
72          include(req, res);
73      }
74  
75      public void doEdit(RenderRequest req, RenderResponse res)
76          throws IOException, PortletException {
77  
78          if (req.getPreferences() == null) {
79              super.doEdit(req, res);
80          }
81          else {
82              req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editAction);
83  
84              include(req, res);
85          }
86      }
87  
88      public void doEditDefaults(RenderRequest req, RenderResponse res)
89          throws IOException, PortletException {
90  
91          if (req.getPreferences() == null) {
92              super.doEdit(req, res);
93          }
94          else {
95              req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
96  
97              include(req, res);
98          }
99      }
100 
101     public void doEditGuest(RenderRequest req, RenderResponse res)
102         throws IOException, PortletException {
103 
104         if (req.getPreferences() == null) {
105             super.doEdit(req, res);
106         }
107         else {
108             req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
109 
110             include(req, res);
111         }
112     }
113 
114     public void doHelp(RenderRequest req, RenderResponse res)
115         throws IOException, PortletException {
116 
117         req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
118 
119         include(req, res);
120     }
121 
122     public void doPreview(RenderRequest req, RenderResponse res)
123         throws IOException, PortletException {
124 
125         req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, previewAction);
126 
127         include(req, res);
128     }
129 
130     public void doPrint(RenderRequest req, RenderResponse res)
131         throws IOException, PortletException {
132 
133         req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
134 
135         include(req, res);
136     }
137 
138     public void doView(RenderRequest req, RenderResponse res)
139         throws IOException, PortletException {
140 
141         req.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
142 
143         include(req, res);
144     }
145 
146     public void init(PortletConfig config) throws PortletException {
147         super.init(config);
148 
149         aboutAction = getInitParameter("about-action");
150         configAction = getInitParameter("config-action");
151         editAction = getInitParameter("edit-action");
152         editDefaultsAction = getInitParameter("edit-defaults-action");
153         editGuestAction = getInitParameter("edit-guest-action");
154         helpAction = getInitParameter("help-action");
155         previewAction = getInitParameter("preview-action");
156         printAction = getInitParameter("print-action");
157         viewAction = getInitParameter("view-action");
158 
159         copyRequestParameters = GetterUtil.getBoolean(
160             getInitParameter("copy-request-parameters"), true);
161 
162         _portletConfig = (PortletConfigImpl)config;
163     }
164 
165     public void processAction(ActionRequest req, ActionResponse res)
166         throws IOException, PortletException {
167 
168         String path = req.getParameter("struts_action");
169 
170         if (Validator.isNotNull(path)) {
171 
172             // Call processAction of com.liferay.portal.struts.PortletAction
173 
174             PermissionCheckerImpl permissionChecker = (PermissionCheckerImpl)
175                 PermissionThreadLocal.getPermissionChecker();
176 
177             try {
178                 permissionChecker.setValues(req);
179 
180                 PortletRequestProcessor processor =
181                     _getPortletRequestProcessor(req);
182 
183                 processor.process(req, res, path);
184             }
185             catch (ServletException se) {
186                 throw new PortletException(se);
187             }
188             finally {
189                 permissionChecker.resetValues();
190             }
191         }
192 
193         if (copyRequestParameters) {
194             PortalUtil.copyRequestParameters(req, res);
195         }
196     }
197 
198     public void serveResource(ResourceRequest req, ResourceResponse res)
199         throws IOException, PortletException {
200 
201         // Call serveResource of com.liferay.portal.struts.PortletAction
202 
203         PermissionCheckerImpl permissionChecker =
204             (PermissionCheckerImpl)PermissionThreadLocal.getPermissionChecker();
205 
206         try {
207             permissionChecker.setValues(req);
208 
209             PortletRequestProcessor processor =
210                 _getPortletRequestProcessor(req);
211 
212             processor.process(req, res);
213         }
214         catch (IOException ioe) {
215             throw ioe;
216         }
217         catch (ServletException se) {
218             throw new PortletException(se);
219         }
220         finally {
221             permissionChecker.resetValues();
222         }
223     }
224 
225     protected void include(RenderRequest req, RenderResponse res)
226         throws IOException, PortletException {
227 
228         // Call render of com.liferay.portal.struts.PortletAction
229 
230         Map<String, Object> strutsAttributes = null;
231 
232         if (_portletConfig.isWARFile()) {
233             strutsAttributes =
234                 StrutsUtil.removeStrutsAttributes(getPortletContext(), req);
235         }
236 
237         PermissionCheckerImpl permissionChecker =
238             (PermissionCheckerImpl)PermissionThreadLocal.getPermissionChecker();
239 
240         try {
241             permissionChecker.setValues(req);
242 
243             PortletRequestProcessor processor =
244                 _getPortletRequestProcessor(req);
245 
246             processor.process(req, res);
247         }
248         catch (IOException ioe) {
249             throw ioe;
250         }
251         catch (ServletException se) {
252             throw new PortletException(se);
253         }
254         finally {
255             permissionChecker.resetValues();
256 
257             if (_portletConfig.isWARFile()) {
258                 StrutsUtil.setStrutsAttributes(req, strutsAttributes);
259             }
260         }
261 
262         if (copyRequestParameters) {
263             PortalUtil.clearRequestParameters(req);
264         }
265     }
266 
267     private PortletRequestProcessor _getPortletRequestProcessor(
268         PortletRequest req) {
269 
270         return (PortletRequestProcessor)getPortletContext().getAttribute(
271             WebKeys.PORTLET_STRUTS_PROCESSOR);
272     }
273 
274     protected String aboutAction;
275     protected String configAction;
276     protected String editAction;
277     protected String editDefaultsAction;
278     protected String editGuestAction;
279     protected String helpAction;
280     protected String previewAction;
281     protected String printAction;
282     protected String viewAction;
283     protected boolean copyRequestParameters;
284 
285     private PortletConfigImpl _portletConfig;
286 
287 }