1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.struts;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
20  
21  import java.io.IOException;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletRequest;
30  
31  import javax.servlet.RequestDispatcher;
32  import javax.servlet.ServletContext;
33  import javax.servlet.ServletException;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.jsp.PageContext;
37  
38  import org.apache.struts.Globals;
39  
40  /**
41   * <a href="StrutsUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class StrutsUtil {
46  
47      public static final String STRUTS_PACKAGE = "org.apache.struts.";
48  
49      public static final String TEXT_HTML_DIR = "/html";
50  
51      public static final String TEXT_WAP_DIR = "/wap";
52  
53      public static void forward(
54              String uri, ServletContext servletContext,
55              HttpServletRequest request, HttpServletResponse response)
56          throws ServletException {
57  
58          if (_log.isDebugEnabled()) {
59              _log.debug("Forward URI " + uri);
60          }
61  
62          if (uri.equals(ActionConstants.COMMON_NULL)) {
63              return;
64          }
65  
66          if (!response.isCommitted()) {
67              String path = TEXT_HTML_DIR + uri;
68  
69              if (BrowserSnifferUtil.isWap(request)) {
70                  path = TEXT_WAP_DIR + uri;
71              }
72  
73              if (_log.isDebugEnabled()) {
74                  _log.debug("Forward path " + path);
75              }
76  
77              RequestDispatcher requestDispatcher =
78                  servletContext.getRequestDispatcher(path);
79  
80              try {
81                  requestDispatcher.forward(request, response);
82              }
83              catch (IOException ioe1) {
84                  _log.warn(ioe1, ioe1);
85              }
86              catch (ServletException se1) {
87                  request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
88  
89                  String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
90  
91                  if (BrowserSnifferUtil.isWap(request)) {
92                      path = TEXT_WAP_DIR + "/common/error.jsp";
93                  }
94  
95                  requestDispatcher = servletContext.getRequestDispatcher(
96                      errorPath);
97  
98                  try {
99                      requestDispatcher.forward(request, response);
100                 }
101                 catch (IOException ioe2) {
102                     _log.warn(ioe2, ioe2);
103                 }
104                 catch (ServletException se2) {
105                     throw se2;
106                 }
107             }
108         }
109         else {
110             _log.warn(uri + " is already committed");
111         }
112     }
113 
114     public static void include(
115             String uri, ServletContext servletContext,
116             HttpServletRequest request, HttpServletResponse response)
117         throws ServletException {
118 
119         if (_log.isDebugEnabled()) {
120             _log.debug("Include URI " + uri);
121         }
122 
123         String path = TEXT_HTML_DIR + uri;
124 
125         if (BrowserSnifferUtil.isWap(request)) {
126             path = TEXT_WAP_DIR + uri;
127         }
128 
129         if (_log.isDebugEnabled()) {
130             _log.debug("Include path " + path);
131         }
132 
133         RequestDispatcher requestDispatcher =
134             servletContext.getRequestDispatcher(path);
135 
136         try {
137             requestDispatcher.include(request, response);
138         }
139         catch (IOException ioe) {
140             _log.warn(ioe, ioe);
141         }
142     }
143 
144     public static Map<String, Object> removeStrutsAttributes(
145         PortletContext portletContext, PortletRequest portletRequest) {
146 
147         Map<String, Object> strutsAttributes = new HashMap<String, Object>();
148 
149         Enumeration<String> enu = portletRequest.getAttributeNames();
150 
151         while (enu.hasMoreElements()) {
152             String attributeName = enu.nextElement();
153 
154             if (attributeName.startsWith(STRUTS_PACKAGE)) {
155                 strutsAttributes.put(
156                     attributeName, portletRequest.getAttribute(attributeName));
157             }
158         }
159 
160         Iterator<String> itr = strutsAttributes.keySet().iterator();
161 
162         while (itr.hasNext()) {
163             String attributeName = itr.next();
164 
165             portletRequest.setAttribute(attributeName, null);
166         }
167 
168         Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
169 
170         portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
171 
172         return strutsAttributes;
173     }
174 
175     public static void setStrutsAttributes(
176         PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
177 
178         for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
179             String key = entry.getKey();
180             Object value = entry.getValue();
181 
182             portletRequest.setAttribute(key, value);
183         }
184     }
185 
186     private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
187 
188 }