001
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
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 if (_log.isWarnEnabled()) {
082 _log.warn(ioe1, ioe1);
083 }
084 }
085 catch (ServletException se1) {
086 request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
087
088 String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
089
090 if (BrowserSnifferUtil.isWap(request)) {
091 path = TEXT_WAP_DIR + "/common/error.jsp";
092 }
093
094 requestDispatcher = servletContext.getRequestDispatcher(
095 errorPath);
096
097 try {
098 requestDispatcher.forward(request, response);
099 }
100 catch (IOException ioe2) {
101 if (_log.isWarnEnabled()) {
102 _log.warn(ioe2, ioe2);
103 }
104 }
105 catch (ServletException se2) {
106 throw se2;
107 }
108 }
109 }
110 else if (_log.isWarnEnabled()) {
111 _log.warn(uri + " is already committed");
112 }
113 }
114
115 public static void include(
116 String uri, ServletContext servletContext,
117 HttpServletRequest request, HttpServletResponse response)
118 throws ServletException {
119
120 if (_log.isDebugEnabled()) {
121 _log.debug("Include URI " + uri);
122 }
123
124 String path = TEXT_HTML_DIR + uri;
125
126 if (BrowserSnifferUtil.isWap(request)) {
127 path = TEXT_WAP_DIR + uri;
128 }
129
130 if (_log.isDebugEnabled()) {
131 _log.debug("Include path " + path);
132 }
133
134 RequestDispatcher requestDispatcher =
135 servletContext.getRequestDispatcher(path);
136
137 try {
138 requestDispatcher.include(request, response);
139 }
140 catch (IOException ioe) {
141 if (_log.isWarnEnabled()) {
142 _log.warn(ioe, ioe);
143 }
144 }
145 }
146
147 public static Map<String, Object> removeStrutsAttributes(
148 PortletContext portletContext, PortletRequest portletRequest) {
149
150 Map<String, Object> strutsAttributes = new HashMap<String, Object>();
151
152 Enumeration<String> enu = portletRequest.getAttributeNames();
153
154 while (enu.hasMoreElements()) {
155 String attributeName = enu.nextElement();
156
157 if (attributeName.startsWith(STRUTS_PACKAGE)) {
158 strutsAttributes.put(
159 attributeName, portletRequest.getAttribute(attributeName));
160 }
161 }
162
163 for (String attributeName : strutsAttributes.keySet()) {
164 portletRequest.setAttribute(attributeName, null);
165 }
166
167 Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
168
169 portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
170
171 return strutsAttributes;
172 }
173
174 public static void setStrutsAttributes(
175 PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
176
177 for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
178 String key = entry.getKey();
179 Object value = entry.getValue();
180
181 portletRequest.setAttribute(key, value);
182 }
183 }
184
185 private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
186
187 }