1   /**
2    * Copyright (c) 2000-2009 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.portal.struts;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
28  import com.liferay.portal.kernel.servlet.SessionErrors;
29  import com.liferay.portal.kernel.servlet.SessionMessages;
30  import com.liferay.portal.kernel.util.JavaConstants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.PortletConfigImpl;
40  
41  import java.io.IOException;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.PortletContext;
47  import javax.portlet.PortletRequest;
48  import javax.portlet.PortletRequestDispatcher;
49  import javax.portlet.PortletResponse;
50  import javax.portlet.RenderRequest;
51  import javax.portlet.RenderResponse;
52  import javax.portlet.ResourceRequest;
53  import javax.portlet.ResourceResponse;
54  
55  import javax.servlet.ServletContext;
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  import org.apache.struts.Globals;
60  import org.apache.struts.action.Action;
61  import org.apache.struts.action.ActionForm;
62  import org.apache.struts.action.ActionForward;
63  import org.apache.struts.action.ActionMapping;
64  import org.apache.struts.config.ModuleConfig;
65  import org.apache.struts.util.MessageResources;
66  
67  /**
68   * <a href="PortletAction.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   *
72   */
73  public class PortletAction extends Action {
74  
75      public static String getForwardKey(HttpServletRequest request) {
76          PortletConfigImpl portletConfig =
77              (PortletConfigImpl)request.getAttribute(
78                  JavaConstants.JAVAX_PORTLET_CONFIG);
79  
80          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
81              WebKeys.PORTLET_STRUTS_FORWARD;
82      }
83  
84      public static String getForwardKey(PortletRequest portletRequest) {
85          PortletConfigImpl portletConfig =
86              (PortletConfigImpl)portletRequest.getAttribute(
87                  JavaConstants.JAVAX_PORTLET_CONFIG);
88  
89          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
90              WebKeys.PORTLET_STRUTS_FORWARD;
91      }
92  
93      public ActionForward execute(
94              ActionMapping mapping, ActionForm form, HttpServletRequest request,
95              HttpServletResponse response)
96          throws Exception {
97  
98          PortletConfig portletConfig = (PortletConfig)request.getAttribute(
99              JavaConstants.JAVAX_PORTLET_CONFIG);
100 
101         PortletRequest portletRequest = (PortletRequest)request.getAttribute(
102             JavaConstants.JAVAX_PORTLET_REQUEST);
103 
104         PortletResponse portletResponse = (PortletResponse)request.getAttribute(
105             JavaConstants.JAVAX_PORTLET_RESPONSE);
106 
107         Boolean strutsExecute = (Boolean)request.getAttribute(
108             WebKeys.PORTLET_STRUTS_EXECUTE);
109 
110         if ((strutsExecute != null) && strutsExecute.booleanValue()) {
111             return strutsExecute(mapping, form, request, response);
112         }
113         else if (portletRequest instanceof RenderRequest) {
114             return render(
115                 mapping, form, portletConfig, (RenderRequest)portletRequest,
116                 (RenderResponse)portletResponse);
117         }
118         else {
119             serveResource(
120                 mapping, form, portletConfig, (ResourceRequest)portletRequest,
121                 (ResourceResponse)portletResponse);
122 
123             return mapping.findForward(ActionConstants.COMMON_NULL);
124         }
125     }
126 
127     public ActionForward strutsExecute(
128             ActionMapping mapping, ActionForm form, HttpServletRequest request,
129             HttpServletResponse response)
130         throws Exception {
131 
132         return super.execute(mapping, form, request, response);
133     }
134 
135     public void processAction(
136             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
137             ActionRequest actionRequest, ActionResponse actionResponse)
138         throws Exception {
139     }
140 
141     public ActionForward render(
142             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
143             RenderRequest renderRequest, RenderResponse renderResponse)
144         throws Exception {
145 
146         if (_log.isDebugEnabled()) {
147             _log.debug("Forward to " + getForward(renderRequest));
148         }
149 
150         return mapping.findForward(getForward(renderRequest));
151     }
152 
153     public void serveResource(
154             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
155             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
156         throws Exception {
157 
158         String resourceId = resourceRequest.getResourceID();
159 
160         if (Validator.isNotNull(resourceId)) {
161             PortletContext portletContext = portletConfig.getPortletContext();
162 
163             PortletRequestDispatcher portletRequestDispatcher =
164                 portletContext.getRequestDispatcher(resourceId);
165 
166             if (portletRequestDispatcher != null) {
167                 portletRequestDispatcher.forward(
168                     resourceRequest, resourceResponse);
169             }
170         }
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             String successMessage = ParamUtil.getString(
232                 actionRequest, "successMessage");
233 
234             SessionMessages.add(
235                 actionRequest, "request_processed", successMessage);
236         }
237 
238         if (redirect == null) {
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 
252                 redirect = StringUtil.replace(redirect, StringPool.POUND, "&#");
253             }
254 
255             actionResponse.sendRedirect(redirect);
256         }
257     }
258 
259     protected boolean redirectToLogin(
260             ActionRequest actionRequest, ActionResponse actionResponse)
261         throws IOException {
262 
263         if (actionRequest.getRemoteUser() == null) {
264             HttpServletRequest request = PortalUtil.getHttpServletRequest(
265                 actionRequest);
266 
267             SessionErrors.add(request, PrincipalException.class.getName());
268 
269             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
270                 WebKeys.THEME_DISPLAY);
271 
272             actionResponse.sendRedirect(themeDisplay.getURLSignIn());
273 
274             return true;
275         }
276         else {
277             return false;
278         }
279     }
280 
281     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
282 
283     private static Log _log = LogFactoryUtil.getLog(PortletAction.class);
284 
285 }