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 String getForward(PortletRequest portletRequest) {
165         return getForward(portletRequest, null);
166     }
167 
168     protected String getForward(
169         PortletRequest portletRequest, String defaultValue) {
170 
171         String forward = (String)portletRequest.getAttribute(
172             getForwardKey(portletRequest));
173 
174         if (forward == null) {
175             return defaultValue;
176         }
177         else {
178             return forward;
179         }
180     }
181 
182     protected void setForward(PortletRequest portletRequest, String forward) {
183         portletRequest.setAttribute(getForwardKey(portletRequest), forward);
184     }
185 
186     protected ModuleConfig getModuleConfig(PortletRequest portletRequest) {
187         return (ModuleConfig)portletRequest.getAttribute(Globals.MODULE_KEY);
188     }
189 
190     protected MessageResources getResources() {
191         ServletContext servletContext = getServlet().getServletContext();
192 
193         return (MessageResources)servletContext.getAttribute(
194             Globals.MESSAGES_KEY);
195     }
196 
197     protected MessageResources getResources(HttpServletRequest request) {
198         return getResources();
199     }
200 
201     protected MessageResources getResources(PortletRequest portletRequest) {
202         return getResources();
203     }
204 
205     protected boolean isCheckMethodOnProcessAction() {
206         return _CHECK_METHOD_ON_PROCESS_ACTION;
207     }
208 
209     protected void sendRedirect(
210             ActionRequest actionRequest, ActionResponse actionResponse)
211         throws IOException {
212 
213         sendRedirect(actionRequest, actionResponse, null);
214     }
215 
216     protected void sendRedirect(
217             ActionRequest actionRequest, ActionResponse actionResponse,
218             String redirect)
219         throws IOException {
220 
221         if (SessionErrors.isEmpty(actionRequest)) {
222             String successMessage = ParamUtil.getString(
223                 actionRequest, "successMessage");
224 
225             SessionMessages.add(
226                 actionRequest, "request_processed", successMessage);
227         }
228 
229         if (Validator.isNull(redirect)) {
230             redirect = ParamUtil.getString(actionRequest, "redirect");
231         }
232 
233         if (Validator.isNotNull(redirect)) {
234 
235             // LPS-1928
236 
237             HttpServletRequest request = PortalUtil.getHttpServletRequest(
238                 actionRequest);
239 
240             if ((BrowserSnifferUtil.isIe(request)) &&
241                 (BrowserSnifferUtil.getMajorVersion(request) == 6.0) &&
242                 (redirect.contains(StringPool.POUND))) {
243 
244                 String redirectToken = "&#";
245 
246                 if (!redirect.contains(StringPool.QUESTION)) {
247                     redirectToken = StringPool.QUESTION + redirectToken;
248                 }
249 
250                 redirect = StringUtil.replace(
251                     redirect, StringPool.POUND, redirectToken);
252             }
253 
254             actionResponse.sendRedirect(redirect);
255         }
256     }
257 
258     protected boolean redirectToLogin(
259             ActionRequest actionRequest, ActionResponse actionResponse)
260         throws IOException {
261 
262         if (actionRequest.getRemoteUser() == null) {
263             HttpServletRequest request = PortalUtil.getHttpServletRequest(
264                 actionRequest);
265 
266             SessionErrors.add(request, PrincipalException.class.getName());
267 
268             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
269                 WebKeys.THEME_DISPLAY);
270 
271             actionResponse.sendRedirect(themeDisplay.getURLSignIn());
272 
273             return true;
274         }
275         else {
276             return false;
277         }
278     }
279 
280     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
281 
282     private static Log _log = LogFactoryUtil.getLog(PortletAction.class);
283 
284 }