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.SessionErrors;
28  import com.liferay.portal.kernel.servlet.SessionMessages;
29  import com.liferay.portal.kernel.util.JavaConstants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.PortletConfigImpl;
37  
38  import java.io.IOException;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.PortletContext;
44  import javax.portlet.PortletRequest;
45  import javax.portlet.PortletRequestDispatcher;
46  import javax.portlet.PortletResponse;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  import javax.portlet.ResourceRequest;
50  import javax.portlet.ResourceResponse;
51  
52  import javax.servlet.ServletContext;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.struts.Globals;
57  import org.apache.struts.action.Action;
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  import org.apache.struts.config.ModuleConfig;
62  import org.apache.struts.util.MessageResources;
63  
64  /**
65   * <a href="PortletAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   *
69   */
70  public class PortletAction extends Action {
71  
72      public static String getForwardKey(HttpServletRequest request) {
73          PortletConfigImpl portletConfig =
74              (PortletConfigImpl)request.getAttribute(
75                  JavaConstants.JAVAX_PORTLET_CONFIG);
76  
77          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
78              WebKeys.PORTLET_STRUTS_FORWARD;
79      }
80  
81      public static String getForwardKey(PortletRequest portletRequest) {
82          PortletConfigImpl portletConfig =
83              (PortletConfigImpl)portletRequest.getAttribute(
84                  JavaConstants.JAVAX_PORTLET_CONFIG);
85  
86          return PortalUtil.getPortletNamespace(portletConfig.getPortletId()) +
87              WebKeys.PORTLET_STRUTS_FORWARD;
88      }
89  
90      public ActionForward execute(
91              ActionMapping mapping, ActionForm form, HttpServletRequest request,
92              HttpServletResponse response)
93          throws Exception {
94  
95          PortletConfig portletConfig = (PortletConfig)request.getAttribute(
96              JavaConstants.JAVAX_PORTLET_CONFIG);
97  
98          PortletRequest portletRequest = (PortletRequest)request.getAttribute(
99              JavaConstants.JAVAX_PORTLET_REQUEST);
100 
101         PortletResponse portletResponse = (PortletResponse)request.getAttribute(
102             JavaConstants.JAVAX_PORTLET_RESPONSE);
103 
104         Boolean strutsExecute = (Boolean)request.getAttribute(
105             WebKeys.PORTLET_STRUTS_EXECUTE);
106 
107         if ((strutsExecute != null) && strutsExecute.booleanValue()) {
108             return strutsExecute(mapping, form, request, response);
109         }
110         else if (portletRequest instanceof RenderRequest) {
111             return render(
112                 mapping, form, portletConfig, (RenderRequest)portletRequest,
113                 (RenderResponse)portletResponse);
114         }
115         else {
116             serveResource(
117                 mapping, form, portletConfig, (ResourceRequest)portletRequest,
118                 (ResourceResponse)portletResponse);
119 
120             return mapping.findForward(ActionConstants.COMMON_NULL);
121         }
122     }
123 
124     public ActionForward strutsExecute(
125             ActionMapping mapping, ActionForm form, HttpServletRequest request,
126             HttpServletResponse response)
127         throws Exception {
128 
129         return super.execute(mapping, form, request, response);
130     }
131 
132     public void processAction(
133             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
134             ActionRequest actionRequest, ActionResponse actionResponse)
135         throws Exception {
136     }
137 
138     public ActionForward render(
139             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
140             RenderRequest renderRequest, RenderResponse renderResponse)
141         throws Exception {
142 
143         if (_log.isDebugEnabled()) {
144             _log.debug("Forward to " + getForward(renderRequest));
145         }
146 
147         return mapping.findForward(getForward(renderRequest));
148     }
149 
150     public void serveResource(
151             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
152             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
153         throws Exception {
154 
155         String resourceId = resourceRequest.getResourceID();
156 
157         if (Validator.isNotNull(resourceId)) {
158             PortletContext portletContext = portletConfig.getPortletContext();
159 
160             PortletRequestDispatcher portletRequestDispatcher =
161                 portletContext.getRequestDispatcher(resourceId);
162 
163             if (portletRequestDispatcher != null) {
164                 portletRequestDispatcher.forward(
165                     resourceRequest, resourceResponse);
166             }
167         }
168     }
169 
170     protected String getForward(PortletRequest portletRequest) {
171         return getForward(portletRequest, null);
172     }
173 
174     protected String getForward(
175         PortletRequest portletRequest, String defaultValue) {
176 
177         String forward = (String)portletRequest.getAttribute(
178             getForwardKey(portletRequest));
179 
180         if (forward == null) {
181             return defaultValue;
182         }
183         else {
184             return forward;
185         }
186     }
187 
188     protected void setForward(PortletRequest portletRequest, String forward) {
189         portletRequest.setAttribute(getForwardKey(portletRequest), forward);
190     }
191 
192     protected ModuleConfig getModuleConfig(PortletRequest portletRequest) {
193         return (ModuleConfig)portletRequest.getAttribute(Globals.MODULE_KEY);
194     }
195 
196     protected MessageResources getResources() {
197         ServletContext servletContext = getServlet().getServletContext();
198 
199         return (MessageResources)servletContext.getAttribute(
200             Globals.MESSAGES_KEY);
201     }
202 
203     protected MessageResources getResources(HttpServletRequest request) {
204         return getResources();
205     }
206 
207     protected MessageResources getResources(PortletRequest portletRequest) {
208         return getResources();
209     }
210 
211     protected boolean isCheckMethodOnProcessAction() {
212         return _CHECK_METHOD_ON_PROCESS_ACTION;
213     }
214 
215     protected void sendRedirect(
216             ActionRequest actionRequest, ActionResponse actionResponse)
217         throws IOException {
218 
219         sendRedirect(actionRequest, actionResponse, null);
220     }
221 
222     protected void sendRedirect(
223             ActionRequest actionRequest, ActionResponse actionResponse,
224             String redirect)
225         throws IOException {
226 
227         if (SessionErrors.isEmpty(actionRequest)) {
228             String successMessage = ParamUtil.getString(
229                 actionRequest, "successMessage");
230 
231             SessionMessages.add(
232                 actionRequest, "request_processed", successMessage);
233         }
234 
235         if (redirect == null) {
236             redirect = ParamUtil.getString(actionRequest, "redirect");
237         }
238 
239         if (Validator.isNotNull(redirect)) {
240             actionResponse.sendRedirect(redirect);
241         }
242     }
243 
244     protected boolean redirectToLogin(
245             ActionRequest actionRequest, ActionResponse actionResponse)
246         throws IOException {
247 
248         if (actionRequest.getRemoteUser() == null) {
249             HttpServletRequest request = PortalUtil.getHttpServletRequest(
250                 actionRequest);
251 
252             SessionErrors.add(request, PrincipalException.class.getName());
253 
254             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
255                 WebKeys.THEME_DISPLAY);
256 
257             actionResponse.sendRedirect(themeDisplay.getURLSignIn());
258 
259             return true;
260         }
261         else {
262             return false;
263         }
264     }
265 
266     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
267 
268     private static Log _log = LogFactoryUtil.getLog(PortletAction.class);
269 
270 }