1
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
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 }