001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.struts;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
020    
021    import java.io.IOException;
022    
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import javax.portlet.PortletContext;
028    import javax.portlet.PortletRequest;
029    
030    import javax.servlet.RequestDispatcher;
031    import javax.servlet.ServletContext;
032    import javax.servlet.ServletException;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    import javax.servlet.jsp.PageContext;
036    
037    import org.apache.struts.Globals;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class StrutsUtil {
043    
044            public static final String STRUTS_PACKAGE = "org.apache.struts.";
045    
046            public static final String TEXT_HTML_DIR = "/html";
047    
048            public static final String TEXT_WAP_DIR = "/wap";
049    
050            public static void forward(
051                            String uri, ServletContext servletContext,
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws ServletException {
054    
055                    if (_log.isDebugEnabled()) {
056                            _log.debug("Forward URI " + uri);
057                    }
058    
059                    if (uri.equals(ActionConstants.COMMON_NULL)) {
060                            return;
061                    }
062    
063                    if (!response.isCommitted()) {
064                            String path = TEXT_HTML_DIR + uri;
065    
066                            if (BrowserSnifferUtil.isWap(request)) {
067                                    path = TEXT_WAP_DIR + uri;
068                            }
069    
070                            if (_log.isDebugEnabled()) {
071                                    _log.debug("Forward path " + path);
072                            }
073    
074                            RequestDispatcher requestDispatcher =
075                                    servletContext.getRequestDispatcher(path);
076    
077                            try {
078                                    requestDispatcher.forward(request, response);
079                            }
080                            catch (IOException ioe1) {
081                                    _log.warn(ioe1, ioe1);
082                            }
083                            catch (ServletException se1) {
084                                    request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
085    
086                                    String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
087    
088                                    if (BrowserSnifferUtil.isWap(request)) {
089                                            path = TEXT_WAP_DIR + "/common/error.jsp";
090                                    }
091    
092                                    requestDispatcher = servletContext.getRequestDispatcher(
093                                            errorPath);
094    
095                                    try {
096                                            requestDispatcher.forward(request, response);
097                                    }
098                                    catch (IOException ioe2) {
099                                            _log.warn(ioe2, ioe2);
100                                    }
101                                    catch (ServletException se2) {
102                                            throw se2;
103                                    }
104                            }
105                    }
106                    else {
107                            _log.warn(uri + " is already committed");
108                    }
109            }
110    
111            public static void include(
112                            String uri, ServletContext servletContext,
113                            HttpServletRequest request, HttpServletResponse response)
114                    throws ServletException {
115    
116                    if (_log.isDebugEnabled()) {
117                            _log.debug("Include URI " + uri);
118                    }
119    
120                    String path = TEXT_HTML_DIR + uri;
121    
122                    if (BrowserSnifferUtil.isWap(request)) {
123                            path = TEXT_WAP_DIR + uri;
124                    }
125    
126                    if (_log.isDebugEnabled()) {
127                            _log.debug("Include path " + path);
128                    }
129    
130                    RequestDispatcher requestDispatcher =
131                            servletContext.getRequestDispatcher(path);
132    
133                    try {
134                            requestDispatcher.include(request, response);
135                    }
136                    catch (IOException ioe) {
137                            _log.warn(ioe, ioe);
138                    }
139            }
140    
141            public static Map<String, Object> removeStrutsAttributes(
142                    PortletContext portletContext, PortletRequest portletRequest) {
143    
144                    Map<String, Object> strutsAttributes = new HashMap<String, Object>();
145    
146                    Enumeration<String> enu = portletRequest.getAttributeNames();
147    
148                    while (enu.hasMoreElements()) {
149                            String attributeName = enu.nextElement();
150    
151                            if (attributeName.startsWith(STRUTS_PACKAGE)) {
152                                    strutsAttributes.put(
153                                            attributeName, portletRequest.getAttribute(attributeName));
154                            }
155                    }
156    
157                    for (String attributeName : strutsAttributes.keySet()) {
158                            portletRequest.setAttribute(attributeName, null);
159                    }
160    
161                    Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
162    
163                    portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
164    
165                    return strutsAttributes;
166            }
167    
168            public static void setStrutsAttributes(
169                    PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
170    
171                    for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
172                            String key = entry.getKey();
173                            Object value = entry.getValue();
174    
175                            portletRequest.setAttribute(key, value);
176                    }
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
180    
181    }