1   /**
2    * Copyright (c) 2000-2009 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.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   */
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 servletContext,
64              HttpServletRequest request, HttpServletResponse response)
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 (!response.isCommitted()) {
76              String path = TEXT_HTML_DIR + uri;
77  
78              if (BrowserSnifferUtil.isWap(request)) {
79                  path = TEXT_WAP_DIR + uri;
80              }
81  
82              if (_log.isDebugEnabled()) {
83                  _log.debug("Forward path " + path);
84              }
85  
86              RequestDispatcher requestDispatcher =
87                  servletContext.getRequestDispatcher(path);
88  
89              try {
90                  requestDispatcher.forward(request, response);
91              }
92              catch (IOException ioe1) {
93                  _log.warn(ioe1, ioe1);
94              }
95              catch (ServletException se1) {
96                  request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
97  
98                  String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
99  
100                 if (BrowserSnifferUtil.isWap(request)) {
101                     path = TEXT_WAP_DIR + "/common/error.jsp";
102                 }
103 
104                 requestDispatcher = servletContext.getRequestDispatcher(
105                     errorPath);
106 
107                 try {
108                     requestDispatcher.forward(request, response);
109                 }
110                 catch (IOException ioe2) {
111                     _log.warn(ioe2, ioe2);
112                 }
113                 catch (ServletException se2) {
114                     throw se2;
115                 }
116             }
117         }
118         else {
119             _log.warn(uri + " is already committed");
120         }
121     }
122 
123     public static void include(
124             String uri, ServletContext servletContext,
125             HttpServletRequest request, HttpServletResponse response)
126         throws ServletException {
127 
128         if (_log.isDebugEnabled()) {
129             _log.debug("Include URI " + uri);
130         }
131 
132         String path = TEXT_HTML_DIR + uri;
133 
134         if (BrowserSnifferUtil.isWap(request)) {
135             path = TEXT_WAP_DIR + uri;
136         }
137 
138         if (_log.isDebugEnabled()) {
139             _log.debug("Include path " + path);
140         }
141 
142         RequestDispatcher requestDispatcher =
143             servletContext.getRequestDispatcher(path);
144 
145         try {
146             requestDispatcher.include(request, response);
147         }
148         catch (IOException ioe) {
149             _log.warn(ioe, ioe);
150         }
151     }
152 
153     public static Map<String, Object> removeStrutsAttributes(
154         PortletContext portletContext, PortletRequest portletRequest) {
155 
156         Map<String, Object> strutsAttributes = new HashMap<String, Object>();
157 
158         Enumeration<String> enu = portletRequest.getAttributeNames();
159 
160         while (enu.hasMoreElements()) {
161             String attributeName = enu.nextElement();
162 
163             if (attributeName.startsWith(STRUTS_PACKAGE)) {
164                 strutsAttributes.put(
165                     attributeName, portletRequest.getAttribute(attributeName));
166             }
167         }
168 
169         Iterator<String> itr = strutsAttributes.keySet().iterator();
170 
171         while (itr.hasNext()) {
172             String attributeName = itr.next();
173 
174             portletRequest.setAttribute(attributeName, null);
175         }
176 
177         Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
178 
179         portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
180 
181         return strutsAttributes;
182     }
183 
184     public static void setStrutsAttributes(
185         PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
186 
187         for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
188             String key = entry.getKey();
189             Object value = entry.getValue();
190 
191             portletRequest.setAttribute(key, value);
192         }
193     }
194 
195     private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
196 
197 }