001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.struts;
022:
023: import com.liferay.portal.kernel.servlet.BrowserSniffer;
024:
025: import java.io.IOException;
026:
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import javax.portlet.PortletContext;
033: import javax.portlet.PortletRequest;
034:
035: import javax.servlet.RequestDispatcher;
036: import javax.servlet.ServletContext;
037: import javax.servlet.ServletException;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import javax.servlet.jsp.PageContext;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044: import org.apache.struts.Globals;
045:
046: /**
047: * <a href="StrutsUtil.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class StrutsUtil {
053:
054: public static final String STRUTS_PACKAGE = "org.apache.struts.";
055:
056: public static final String TEXT_HTML_DIR = "/html";
057:
058: public static final String TEXT_WAP_DIR = "/wap";
059:
060: public static void forward(String uri, ServletContext ctx,
061: HttpServletRequest req, HttpServletResponse res)
062: throws ServletException {
063:
064: if (_log.isDebugEnabled()) {
065: _log.debug("Forward URI " + uri);
066: }
067:
068: if (uri.equals(ActionConstants.COMMON_NULL)) {
069: return;
070: }
071:
072: if (!res.isCommitted()) {
073: String path = TEXT_HTML_DIR + uri;
074:
075: if (BrowserSniffer.is_wap_xhtml(req)) {
076: path = TEXT_WAP_DIR + uri;
077: }
078:
079: if (_log.isDebugEnabled()) {
080: _log.debug("Forward path " + path);
081: }
082:
083: RequestDispatcher rd = ctx.getRequestDispatcher(path);
084:
085: try {
086: rd.forward(req, res);
087: } catch (IOException ioe1) {
088: _log.warn(ioe1, ioe1);
089: } catch (ServletException se1) {
090: req.setAttribute(PageContext.EXCEPTION, se1
091: .getRootCause());
092:
093: String errorPath = TEXT_HTML_DIR
094: + ActionConstants.COMMON_ERROR;
095:
096: if (BrowserSniffer.is_wap_xhtml(req)) {
097: path = TEXT_WAP_DIR + ActionConstants.COMMON_ERROR;
098: }
099:
100: rd = ctx.getRequestDispatcher(errorPath);
101:
102: try {
103: rd.forward(req, res);
104: } catch (IOException ioe2) {
105: _log.warn(ioe2, ioe2);
106: } catch (ServletException se2) {
107: throw se2;
108: }
109: }
110: } else {
111: _log.warn(uri + " is already committed");
112: }
113: }
114:
115: public static void include(String uri, ServletContext ctx,
116: HttpServletRequest req, HttpServletResponse res)
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 (BrowserSniffer.is_wap_xhtml(req)) {
126: path = TEXT_WAP_DIR + uri;
127: }
128:
129: if (_log.isDebugEnabled()) {
130: _log.debug("Include path " + path);
131: }
132:
133: RequestDispatcher rd = ctx.getRequestDispatcher(path);
134:
135: try {
136: rd.include(req, res);
137: } catch (IOException ioe) {
138: _log.warn(ioe, ioe);
139: }
140: }
141:
142: public static Map removeStrutsAttributes(
143: PortletContext portletContext, PortletRequest req) {
144:
145: Map strutsAttributes = new HashMap();
146:
147: Enumeration enu = req.getAttributeNames();
148:
149: while (enu.hasMoreElements()) {
150: String attributeName = (String) enu.nextElement();
151:
152: if (attributeName.startsWith(STRUTS_PACKAGE)) {
153: strutsAttributes.put(attributeName, req
154: .getAttribute(attributeName));
155: }
156: }
157:
158: Iterator itr = strutsAttributes.keySet().iterator();
159:
160: while (itr.hasNext()) {
161: String attributeName = (String) itr.next();
162:
163: req.setAttribute(attributeName, null);
164: }
165:
166: Object moduleConfig = portletContext
167: .getAttribute(Globals.MODULE_KEY);
168:
169: req.setAttribute(Globals.MODULE_KEY, moduleConfig);
170:
171: return strutsAttributes;
172: }
173:
174: public static void setStrutsAttributes(PortletRequest req,
175: Map strutsAttributes) {
176:
177: Iterator itr = strutsAttributes.entrySet().iterator();
178:
179: while (itr.hasNext()) {
180: Map.Entry entry = (Map.Entry) itr.next();
181:
182: String key = (String) entry.getKey();
183: Object value = entry.getValue();
184:
185: req.setAttribute(key, value);
186: }
187: }
188:
189: private static Log _log = LogFactory.getLog(StrutsUtil.class);
190:
191: }
|