1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet;
21  
22  import com.liferay.portal.kernel.portlet.LiferayPortlet;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.security.permission.PermissionChecker;
26  import com.liferay.portal.security.permission.PermissionThreadLocal;
27  import com.liferay.portal.struts.PortletRequestProcessor;
28  import com.liferay.portal.struts.StrutsUtil;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.io.IOException;
33  
34  import java.util.Map;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletException;
40  import javax.portlet.PortletRequest;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  import javax.portlet.ResourceRequest;
44  import javax.portlet.ResourceResponse;
45  
46  import javax.servlet.ServletException;
47  
48  /**
49   * <a href="StrutsPortlet.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class StrutsPortlet extends LiferayPortlet {
55  
56      public void doAbout(
57              RenderRequest renderRequest, RenderResponse renderResponse)
58          throws IOException, PortletException {
59  
60          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, aboutAction);
61  
62          include(renderRequest, renderResponse);
63      }
64  
65      public void doConfig(
66              RenderRequest renderRequest, RenderResponse renderResponse)
67          throws IOException, PortletException {
68  
69          renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, configAction);
70  
71          include(renderRequest, renderResponse);
72      }
73  
74      public void doEdit(
75              RenderRequest renderRequest, RenderResponse renderResponse)
76          throws IOException, PortletException {
77  
78          if (renderRequest.getPreferences() == null) {
79              super.doEdit(renderRequest, renderResponse);
80          }
81          else {
82              renderRequest.setAttribute(
83                  WebKeys.PORTLET_STRUTS_ACTION, editAction);
84  
85              include(renderRequest, renderResponse);
86          }
87      }
88  
89      public void doEditDefaults(
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws IOException, PortletException {
92  
93          if (renderRequest.getPreferences() == null) {
94              super.doEdit(renderRequest, renderResponse);
95          }
96          else {
97              renderRequest.setAttribute(
98                  WebKeys.PORTLET_STRUTS_ACTION, editDefaultsAction);
99  
100             include(renderRequest, renderResponse);
101         }
102     }
103 
104     public void doEditGuest(
105             RenderRequest renderRequest, RenderResponse renderResponse)
106         throws IOException, PortletException {
107 
108         if (renderRequest.getPreferences() == null) {
109             super.doEdit(renderRequest, renderResponse);
110         }
111         else {
112             renderRequest.setAttribute(
113                 WebKeys.PORTLET_STRUTS_ACTION, editGuestAction);
114 
115             include(renderRequest, renderResponse);
116         }
117     }
118 
119     public void doHelp(
120             RenderRequest renderRequest, RenderResponse renderResponse)
121         throws IOException, PortletException {
122 
123         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, helpAction);
124 
125         include(renderRequest, renderResponse);
126     }
127 
128     public void doPreview(
129             RenderRequest renderRequest, RenderResponse renderResponse)
130         throws IOException, PortletException {
131 
132         renderRequest.setAttribute(
133             WebKeys.PORTLET_STRUTS_ACTION, previewAction);
134 
135         include(renderRequest, renderResponse);
136     }
137 
138     public void doPrint(
139             RenderRequest renderRequest, RenderResponse renderResponse)
140         throws IOException, PortletException {
141 
142         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, printAction);
143 
144         include(renderRequest, renderResponse);
145     }
146 
147     public void doView(
148             RenderRequest renderRequest, RenderResponse renderResponse)
149         throws IOException, PortletException {
150 
151         renderRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
152 
153         include(renderRequest, renderResponse);
154     }
155 
156     public void init(PortletConfig portletConfig) throws PortletException {
157         super.init(portletConfig);
158 
159         aboutAction = getInitParameter("about-action");
160         configAction = getInitParameter("config-action");
161         editAction = getInitParameter("edit-action");
162         editDefaultsAction = getInitParameter("edit-defaults-action");
163         editGuestAction = getInitParameter("edit-guest-action");
164         helpAction = getInitParameter("help-action");
165         previewAction = getInitParameter("preview-action");
166         printAction = getInitParameter("print-action");
167         viewAction = getInitParameter("view-action");
168 
169         copyRequestParameters = GetterUtil.getBoolean(
170             getInitParameter("copy-request-parameters"), true);
171 
172         _portletConfig = (PortletConfigImpl)portletConfig;
173     }
174 
175     public void processAction(
176             ActionRequest actionRequest, ActionResponse actionResponse)
177         throws IOException, PortletException {
178 
179         String path = actionRequest.getParameter("struts_action");
180 
181         if (Validator.isNotNull(path)) {
182 
183             // Call processAction of com.liferay.portal.struts.PortletAction
184 
185             PermissionChecker permissionChecker =
186                 PermissionThreadLocal.getPermissionChecker();
187 
188             try {
189                 permissionChecker.setValues(actionRequest);
190 
191                 PortletRequestProcessor processor =
192                     _getPortletRequestProcessor(actionRequest);
193 
194                 processor.process(actionRequest, actionResponse, path);
195             }
196             catch (ServletException se) {
197                 throw new PortletException(se);
198             }
199             finally {
200                 permissionChecker.resetValues();
201             }
202         }
203 
204         if (copyRequestParameters) {
205             PortalUtil.copyRequestParameters(actionRequest, actionResponse);
206         }
207     }
208 
209     public void serveResource(
210             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
211         throws IOException, PortletException {
212 
213         resourceRequest.setAttribute(WebKeys.PORTLET_STRUTS_ACTION, viewAction);
214 
215         // Call serveResource of com.liferay.portal.struts.PortletAction
216 
217         PermissionChecker permissionChecker =
218             PermissionThreadLocal.getPermissionChecker();
219 
220         try {
221             permissionChecker.setValues(resourceRequest);
222 
223             PortletRequestProcessor processor =
224                 _getPortletRequestProcessor(resourceRequest);
225 
226             processor.process(resourceRequest, resourceResponse);
227         }
228         catch (ServletException se) {
229             throw new PortletException(se);
230         }
231         finally {
232             permissionChecker.resetValues();
233         }
234     }
235 
236     protected void include(
237             RenderRequest renderRequest, RenderResponse renderResponse)
238         throws IOException, PortletException {
239 
240         // Call render of com.liferay.portal.struts.PortletAction
241 
242         Map<String, Object> strutsAttributes = null;
243 
244         if (_portletConfig.isWARFile()) {
245             strutsAttributes = StrutsUtil.removeStrutsAttributes(
246                 getPortletContext(), renderRequest);
247         }
248 
249         PermissionChecker permissionChecker =
250             PermissionThreadLocal.getPermissionChecker();
251 
252         try {
253             permissionChecker.setValues(renderRequest);
254 
255             PortletRequestProcessor processor =
256                 _getPortletRequestProcessor(renderRequest);
257 
258             processor.process(renderRequest, renderResponse);
259         }
260         catch (ServletException se) {
261             throw new PortletException(se);
262         }
263         finally {
264             permissionChecker.resetValues();
265 
266             if (_portletConfig.isWARFile()) {
267                 StrutsUtil.setStrutsAttributes(renderRequest, strutsAttributes);
268             }
269         }
270 
271         if (copyRequestParameters) {
272             PortalUtil.clearRequestParameters(renderRequest);
273         }
274     }
275 
276     private PortletRequestProcessor _getPortletRequestProcessor(
277         PortletRequest portletRequest) {
278 
279         return (PortletRequestProcessor)getPortletContext().getAttribute(
280             WebKeys.PORTLET_STRUTS_PROCESSOR);
281     }
282 
283     protected String aboutAction;
284     protected String configAction;
285     protected String editAction;
286     protected String editDefaultsAction;
287     protected String editGuestAction;
288     protected String helpAction;
289     protected String previewAction;
290     protected String printAction;
291     protected String viewAction;
292     protected boolean copyRequestParameters;
293 
294     private PortletConfigImpl _portletConfig;
295 
296 }