1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.BrowserSnifferUtil;
28  
29  import java.io.IOException;
30  
31  import java.util.Enumeration;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  import javax.portlet.PortletContext;
37  import javax.portlet.PortletRequest;
38  
39  import javax.servlet.RequestDispatcher;
40  import javax.servlet.ServletContext;
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.jsp.PageContext;
45  
46  import org.apache.struts.Globals;
47  
48  /**
49   * <a href="StrutsUtil.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   */
53  public class StrutsUtil {
54  
55      public static final String STRUTS_PACKAGE = "org.apache.struts.";
56  
57      public static final String TEXT_HTML_DIR = "/html";
58  
59      public static final String TEXT_WAP_DIR = "/wap";
60  
61      public static void forward(
62              String uri, ServletContext servletContext,
63              HttpServletRequest request, HttpServletResponse response)
64          throws ServletException {
65  
66          if (_log.isDebugEnabled()) {
67              _log.debug("Forward URI " + uri);
68          }
69  
70          if (uri.equals(ActionConstants.COMMON_NULL)) {
71              return;
72          }
73  
74          if (!response.isCommitted()) {
75              String path = TEXT_HTML_DIR + uri;
76  
77              if (BrowserSnifferUtil.isWap(request)) {
78                  path = TEXT_WAP_DIR + uri;
79              }
80  
81              if (_log.isDebugEnabled()) {
82                  _log.debug("Forward path " + path);
83              }
84  
85              RequestDispatcher requestDispatcher =
86                  servletContext.getRequestDispatcher(path);
87  
88              try {
89                  requestDispatcher.forward(request, response);
90              }
91              catch (IOException ioe1) {
92                  _log.warn(ioe1, ioe1);
93              }
94              catch (ServletException se1) {
95                  request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
96  
97                  String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
98  
99                  if (BrowserSnifferUtil.isWap(request)) {
100                     path = TEXT_WAP_DIR + "/common/error.jsp";
101                 }
102 
103                 requestDispatcher = servletContext.getRequestDispatcher(
104                     errorPath);
105 
106                 try {
107                     requestDispatcher.forward(request, response);
108                 }
109                 catch (IOException ioe2) {
110                     _log.warn(ioe2, ioe2);
111                 }
112                 catch (ServletException se2) {
113                     throw se2;
114                 }
115             }
116         }
117         else {
118             _log.warn(uri + " is already committed");
119         }
120     }
121 
122     public static void include(
123             String uri, ServletContext servletContext,
124             HttpServletRequest request, HttpServletResponse response)
125         throws ServletException {
126 
127         if (_log.isDebugEnabled()) {
128             _log.debug("Include URI " + uri);
129         }
130 
131         String path = TEXT_HTML_DIR + uri;
132 
133         if (BrowserSnifferUtil.isWap(request)) {
134             path = TEXT_WAP_DIR + uri;
135         }
136 
137         if (_log.isDebugEnabled()) {
138             _log.debug("Include path " + path);
139         }
140 
141         RequestDispatcher requestDispatcher =
142             servletContext.getRequestDispatcher(path);
143 
144         try {
145             requestDispatcher.include(request, response);
146         }
147         catch (IOException ioe) {
148             _log.warn(ioe, ioe);
149         }
150     }
151 
152     public static Map<String, Object> removeStrutsAttributes(
153         PortletContext portletContext, PortletRequest portletRequest) {
154 
155         Map<String, Object> strutsAttributes = new HashMap<String, Object>();
156 
157         Enumeration<String> enu = portletRequest.getAttributeNames();
158 
159         while (enu.hasMoreElements()) {
160             String attributeName = enu.nextElement();
161 
162             if (attributeName.startsWith(STRUTS_PACKAGE)) {
163                 strutsAttributes.put(
164                     attributeName, portletRequest.getAttribute(attributeName));
165             }
166         }
167 
168         Iterator<String> itr = strutsAttributes.keySet().iterator();
169 
170         while (itr.hasNext()) {
171             String attributeName = itr.next();
172 
173             portletRequest.setAttribute(attributeName, null);
174         }
175 
176         Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
177 
178         portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
179 
180         return strutsAttributes;
181     }
182 
183     public static void setStrutsAttributes(
184         PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
185 
186         for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
187             String key = entry.getKey();
188             Object value = entry.getValue();
189 
190             portletRequest.setAttribute(key, value);
191         }
192     }
193 
194     private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
195 
196 }