1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.struts;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
20  import com.liferay.portal.kernel.servlet.SessionErrors;
21  import com.liferay.portal.kernel.servlet.SessionMessages;
22  import com.liferay.portal.kernel.util.JavaConstants;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.portlet.PortletConfigImpl;
32  
33  import java.io.IOException;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.PortletContext;
39  import javax.portlet.PortletRequest;
40  import javax.portlet.PortletRequestDispatcher;
41  import javax.portlet.PortletResponse;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  import javax.portlet.ResourceRequest;
45  import javax.portlet.ResourceResponse;
46  
47  import javax.servlet.ServletContext;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.struts.Globals;
52  import org.apache.struts.action.Action;
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  import org.apache.struts.config.ModuleConfig;
57  import org.apache.struts.util.MessageResources;
58  
59  /**
60   * <a href="PortletAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   */
64  public class PortletAction extends Action {
65  
66      public static String getForwardKey(HttpServletRequest request) {
67          PortletConfigImpl portletConfig =
68              (PortletConfigImpl)request.getAttribute(
69                  JavaConstants.JAVAX_PORTLET_CONFIG);
70  
71          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
72              WebKeys.PORTLET_STRUTS_FORWARD;
73      }
74  
75      public static String getForwardKey(PortletRequest portletRequest) {
76          PortletConfigImpl portletConfig =
77              (PortletConfigImpl)portletRequest.getAttribute(
78                  JavaConstants.JAVAX_PORTLET_CONFIG);
79  
80          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
81              WebKeys.PORTLET_STRUTS_FORWARD;
82      }
83  
84      public ActionForward execute(
85              ActionMapping mapping, ActionForm form, HttpServletRequest request,
86              HttpServletResponse response)
87          throws Exception {
88  
89          PortletConfig portletConfig = (PortletConfig)request.getAttribute(
90              JavaConstants.JAVAX_PORTLET_CONFIG);
91  
92          PortletRequest portletRequest = (PortletRequest)request.getAttribute(
93              JavaConstants.JAVAX_PORTLET_REQUEST);
94  
95          PortletResponse portletResponse = (PortletResponse)request.getAttribute(
96              JavaConstants.JAVAX_PORTLET_RESPONSE);
97  
98          Boolean strutsExecute = (Boolean)request.getAttribute(
99              WebKeys.PORTLET_STRUTS_EXECUTE);
100 
101         if ((strutsExecute != null) && strutsExecute.booleanValue()) {
102             return strutsExecute(mapping, form, request, response);
103         }
104         else if (portletRequest instanceof RenderRequest) {
105             return render(
106                 mapping, form, portletConfig, (RenderRequest)portletRequest,
107                 (RenderResponse)portletResponse);
108         }
109         else {
110             serveResource(
111                 mapping, form, portletConfig, (ResourceRequest)portletRequest,
112                 (ResourceResponse)portletResponse);
113 
114             return mapping.findForward(ActionConstants.COMMON_NULL);
115         }
116     }
117 
118     public ActionForward strutsExecute(
119             ActionMapping mapping, ActionForm form, HttpServletRequest request,
120             HttpServletResponse response)
121         throws Exception {
122 
123         return super.execute(mapping, form, request, response);
124     }
125 
126     public void processAction(
127             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
128             ActionRequest actionRequest, ActionResponse actionResponse)
129         throws Exception {
130     }
131 
132     public ActionForward render(
133             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
134             RenderRequest renderRequest, RenderResponse renderResponse)
135         throws Exception {
136 
137         if (_log.isDebugEnabled()) {
138             _log.debug("Forward to " + getForward(renderRequest));
139         }
140 
141         return mapping.findForward(getForward(renderRequest));
142     }
143 
144     public void serveResource(
145             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
146             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
147         throws Exception {
148 
149         String resourceId = resourceRequest.getResourceID();
150 
151         if (Validator.isNotNull(resourceId)) {
152             PortletContext portletContext = portletConfig.getPortletContext();
153 
154             PortletRequestDispatcher portletRequestDispatcher =
155                 portletContext.getRequestDispatcher(resourceId);
156 
157             if (portletRequestDispatcher != null) {
158                 portletRequestDispatcher.forward(
159                     resourceRequest, resourceResponse);
160             }
161         }
162     }
163 
164     protected void addSuccessMessage(
165         ActionRequest actionRequest, ActionResponse actionResponse) {
166 
167         String successMessage = ParamUtil.getString(
168             actionRequest, "successMessage");
169 
170         SessionMessages.add(actionRequest, "request_processed", successMessage);
171     }
172 
173     protected String getForward(PortletRequest portletRequest) {
174         return getForward(portletRequest, null);
175     }
176 
177     protected String getForward(
178         PortletRequest portletRequest, String defaultValue) {
179 
180         String forward = (String)portletRequest.getAttribute(
181             getForwardKey(portletRequest));
182 
183         if (forward == null) {
184             return defaultValue;
185         }
186         else {
187             return forward;
188         }
189     }
190 
191     protected void setForward(PortletRequest portletRequest, String forward) {
192         portletRequest.setAttribute(getForwardKey(portletRequest), forward);
193     }
194 
195     protected ModuleConfig getModuleConfig(PortletRequest portletRequest) {
196         return (ModuleConfig)portletRequest.getAttribute(Globals.MODULE_KEY);
197     }
198 
199     protected MessageResources getResources() {
200         ServletContext servletContext = getServlet().getServletContext();
201 
202         return (MessageResources)servletContext.getAttribute(
203             Globals.MESSAGES_KEY);
204     }
205 
206     protected MessageResources getResources(HttpServletRequest request) {
207         return getResources();
208     }
209 
210     protected MessageResources getResources(PortletRequest portletRequest) {
211         return getResources();
212     }
213 
214     protected boolean isCheckMethodOnProcessAction() {
215         return _CHECK_METHOD_ON_PROCESS_ACTION;
216     }
217 
218     protected void sendRedirect(
219             ActionRequest actionRequest, ActionResponse actionResponse)
220         throws IOException {
221 
222         sendRedirect(actionRequest, actionResponse, null);
223     }
224 
225     protected void sendRedirect(
226             ActionRequest actionRequest, ActionResponse actionResponse,
227             String redirect)
228         throws IOException {
229 
230         if (SessionErrors.isEmpty(actionRequest)) {
231             addSuccessMessage(actionRequest, actionResponse);
232         }
233 
234         if (Validator.isNull(redirect)) {
235             redirect = ParamUtil.getString(actionRequest, "redirect");
236         }
237 
238         if (Validator.isNotNull(redirect)) {
239 
240             // LPS-1928
241 
242             HttpServletRequest request = PortalUtil.getHttpServletRequest(
243                 actionRequest);
244 
245             if ((BrowserSnifferUtil.isIe(request)) &&
246                 (BrowserSnifferUtil.getMajorVersion(request) == 6.0) &&
247                 (redirect.contains(StringPool.POUND))) {
248 
249                 String redirectToken = "&#";
250 
251                 if (!redirect.contains(StringPool.QUESTION)) {
252                     redirectToken = StringPool.QUESTION + redirectToken;
253                 }
254 
255                 redirect = StringUtil.replace(
256                     redirect, StringPool.POUND, redirectToken);
257             }
258 
259             actionResponse.sendRedirect(redirect);
260         }
261     }
262 
263     protected boolean redirectToLogin(
264             ActionRequest actionRequest, ActionResponse actionResponse)
265         throws IOException {
266 
267         if (actionRequest.getRemoteUser() == null) {
268             HttpServletRequest request = PortalUtil.getHttpServletRequest(
269                 actionRequest);
270 
271             SessionErrors.add(request, PrincipalException.class.getName());
272 
273             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
274                 WebKeys.THEME_DISPLAY);
275 
276             actionResponse.sendRedirect(themeDisplay.getURLSignIn());
277 
278             return true;
279         }
280         else {
281             return false;
282         }
283     }
284 
285     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
286 
287     private static Log _log = LogFactoryUtil.getLog(PortletAction.class);
288 
289 }