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