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