1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.struts;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
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.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
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 
241             // LPS-1928
242 
243             HttpServletRequest request = PortalUtil.getHttpServletRequest(
244                 actionRequest);
245 
246             if (BrowserSnifferUtil.isIe(request) &&
247                 (BrowserSnifferUtil.getMajorVersion(request) == 6.0)) {
248 
249                 redirect = StringUtil.replace(redirect, StringPool.POUND, "&#");
250             }
251 
252             actionResponse.sendRedirect(redirect);
253         }
254     }
255 
256     protected boolean redirectToLogin(
257             ActionRequest actionRequest, ActionResponse actionResponse)
258         throws IOException {
259 
260         if (actionRequest.getRemoteUser() == null) {
261             HttpServletRequest request = PortalUtil.getHttpServletRequest(
262                 actionRequest);
263 
264             SessionErrors.add(request, PrincipalException.class.getName());
265 
266             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
267                 WebKeys.THEME_DISPLAY);
268 
269             actionResponse.sendRedirect(themeDisplay.getURLSignIn());
270 
271             return true;
272         }
273         else {
274             return false;
275         }
276     }
277 
278     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = true;
279 
280     private static Log _log = LogFactoryUtil.getLog(PortletAction.class);
281 
282 }