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