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.BrowserSniffer;
26  
27  import java.io.IOException;
28  
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.Map;
33  
34  import javax.portlet.PortletContext;
35  import javax.portlet.PortletRequest;
36  
37  import javax.servlet.RequestDispatcher;
38  import javax.servlet.ServletContext;
39  import javax.servlet.ServletException;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.jsp.PageContext;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
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   */
54  public class StrutsUtil {
55  
56      public static final String STRUTS_PACKAGE = "org.apache.struts.";
57  
58      public static final String TEXT_HTML_DIR = "/html";
59  
60      public static final String TEXT_WAP_DIR = "/wap";
61  
62      public static void forward(
63              String uri, ServletContext ctx, HttpServletRequest req,
64              HttpServletResponse res)
65          throws ServletException {
66  
67          if (_log.isDebugEnabled()) {
68              _log.debug("Forward URI " + uri);
69          }
70  
71          if (uri.equals(ActionConstants.COMMON_NULL)) {
72              return;
73          }
74  
75          if (!res.isCommitted()) {
76              String path = TEXT_HTML_DIR + uri;
77  
78              if (BrowserSniffer.is_wap_xhtml(req)) {
79                  path = TEXT_WAP_DIR + uri;
80              }
81  
82              if (_log.isDebugEnabled()) {
83                  _log.debug("Forward path " + path);
84              }
85  
86              RequestDispatcher rd = ctx.getRequestDispatcher(path);
87  
88              try {
89                  rd.forward(req, res);
90              }
91              catch (IOException ioe1) {
92                  _log.warn(ioe1, ioe1);
93              }
94              catch (ServletException se1) {
95                  req.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
96  
97                  String errorPath = TEXT_HTML_DIR + ActionConstants.COMMON_ERROR;
98  
99                  if (BrowserSniffer.is_wap_xhtml(req)) {
100                     path = TEXT_WAP_DIR + ActionConstants.COMMON_ERROR;
101                 }
102 
103                 rd = ctx.getRequestDispatcher(errorPath);
104 
105                 try {
106                     rd.forward(req, res);
107                 }
108                 catch (IOException ioe2) {
109                     _log.warn(ioe2, ioe2);
110                 }
111                 catch (ServletException se2) {
112                     throw se2;
113                 }
114             }
115         }
116         else {
117             _log.warn(uri + " is already committed");
118         }
119     }
120 
121     public static void include(
122             String uri, ServletContext ctx, HttpServletRequest req,
123             HttpServletResponse res)
124         throws ServletException {
125 
126         if (_log.isDebugEnabled()) {
127             _log.debug("Include URI " + uri);
128         }
129 
130         String path = TEXT_HTML_DIR + uri;
131 
132         if (BrowserSniffer.is_wap_xhtml(req)) {
133             path = TEXT_WAP_DIR + uri;
134         }
135 
136         if (_log.isDebugEnabled()) {
137             _log.debug("Include path " + path);
138         }
139 
140         RequestDispatcher rd = ctx.getRequestDispatcher(path);
141 
142         try {
143             rd.include(req, res);
144         }
145         catch (IOException ioe) {
146             _log.warn(ioe, ioe);
147         }
148     }
149 
150     public static Map<String, Object> removeStrutsAttributes(
151         PortletContext portletContext, PortletRequest req) {
152 
153         Map<String, Object> strutsAttributes = new HashMap<String, Object>();
154 
155         Enumeration<String> enu = req.getAttributeNames();
156 
157         while (enu.hasMoreElements()) {
158             String attributeName = enu.nextElement();
159 
160             if (attributeName.startsWith(STRUTS_PACKAGE)) {
161                 strutsAttributes.put(
162                     attributeName, req.getAttribute(attributeName));
163             }
164         }
165 
166         Iterator<String> itr = strutsAttributes.keySet().iterator();
167 
168         while (itr.hasNext()) {
169             String attributeName = itr.next();
170 
171             req.setAttribute(attributeName, null);
172         }
173 
174         Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
175 
176         req.setAttribute(Globals.MODULE_KEY, moduleConfig);
177 
178         return strutsAttributes;
179     }
180 
181     public static void setStrutsAttributes(
182         PortletRequest req, Map<String, Object> strutsAttributes) {
183 
184         for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
185             String key = entry.getKey();
186             Object value = entry.getValue();
187 
188             req.setAttribute(key, value);
189         }
190     }
191 
192     private static Log _log = LogFactory.getLog(StrutsUtil.class);
193 
194 }