001    /**
002     * Copyright (c) 2000-2013 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.Iterator;
026    import java.util.Map;
027    
028    import javax.portlet.PortletContext;
029    import javax.portlet.PortletRequest;
030    
031    import javax.servlet.RequestDispatcher;
032    import javax.servlet.ServletContext;
033    import javax.servlet.ServletException;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    import javax.servlet.jsp.PageContext;
037    
038    import org.apache.struts.Globals;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class StrutsUtil {
044    
045            public static final String STRUTS_PACKAGE = "org.apache.struts.";
046    
047            public static final String TEXT_HTML_DIR = "/html";
048    
049            public static final String TEXT_WAP_DIR = "/wap";
050    
051            public static void forward(
052                            String uri, ServletContext servletContext,
053                            HttpServletRequest request, HttpServletResponse response)
054                    throws ServletException {
055    
056                    if (_log.isDebugEnabled()) {
057                            _log.debug("Forward URI " + uri);
058                    }
059    
060                    if (uri.equals(ActionConstants.COMMON_NULL)) {
061                            return;
062                    }
063    
064                    if (!response.isCommitted()) {
065                            String path = TEXT_HTML_DIR + uri;
066    
067                            if (BrowserSnifferUtil.isWap(request)) {
068                                    path = TEXT_WAP_DIR + uri;
069                            }
070    
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug("Forward path " + path);
073                            }
074    
075                            RequestDispatcher requestDispatcher =
076                                    servletContext.getRequestDispatcher(path);
077    
078                            try {
079                                    requestDispatcher.forward(request, response);
080                            }
081                            catch (IOException ioe1) {
082                                    _log.warn(ioe1, ioe1);
083                            }
084                            catch (ServletException se1) {
085                                    request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
086    
087                                    String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
088    
089                                    if (BrowserSnifferUtil.isWap(request)) {
090                                            path = TEXT_WAP_DIR + "/common/error.jsp";
091                                    }
092    
093                                    requestDispatcher = servletContext.getRequestDispatcher(
094                                            errorPath);
095    
096                                    try {
097                                            requestDispatcher.forward(request, response);
098                                    }
099                                    catch (IOException ioe2) {
100                                            _log.warn(ioe2, ioe2);
101                                    }
102                                    catch (ServletException se2) {
103                                            throw se2;
104                                    }
105                            }
106                    }
107                    else {
108                            _log.warn(uri + " is already committed");
109                    }
110            }
111    
112            public static void include(
113                            String uri, ServletContext servletContext,
114                            HttpServletRequest request, HttpServletResponse response)
115                    throws ServletException {
116    
117                    if (_log.isDebugEnabled()) {
118                            _log.debug("Include URI " + uri);
119                    }
120    
121                    String path = TEXT_HTML_DIR + uri;
122    
123                    if (BrowserSnifferUtil.isWap(request)) {
124                            path = TEXT_WAP_DIR + uri;
125                    }
126    
127                    if (_log.isDebugEnabled()) {
128                            _log.debug("Include path " + path);
129                    }
130    
131                    RequestDispatcher requestDispatcher =
132                            servletContext.getRequestDispatcher(path);
133    
134                    try {
135                            requestDispatcher.include(request, response);
136                    }
137                    catch (IOException ioe) {
138                            _log.warn(ioe, ioe);
139                    }
140            }
141    
142            public static Map<String, Object> removeStrutsAttributes(
143                    PortletContext portletContext, PortletRequest portletRequest) {
144    
145                    Map<String, Object> strutsAttributes = new HashMap<String, Object>();
146    
147                    Enumeration<String> enu = portletRequest.getAttributeNames();
148    
149                    while (enu.hasMoreElements()) {
150                            String attributeName = enu.nextElement();
151    
152                            if (attributeName.startsWith(STRUTS_PACKAGE)) {
153                                    strutsAttributes.put(
154                                            attributeName, portletRequest.getAttribute(attributeName));
155                            }
156                    }
157    
158                    Iterator<String> itr = strutsAttributes.keySet().iterator();
159    
160                    while (itr.hasNext()) {
161                            String attributeName = itr.next();
162    
163                            portletRequest.setAttribute(attributeName, null);
164                    }
165    
166                    Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
167    
168                    portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
169    
170                    return strutsAttributes;
171            }
172    
173            public static void setStrutsAttributes(
174                    PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
175    
176                    for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
177                            String key = entry.getKey();
178                            Object value = entry.getValue();
179    
180                            portletRequest.setAttribute(key, value);
181                    }
182            }
183    
184            private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
185    
186    }