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