1
19
20 package com.liferay.portal.struts;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
25
26 import java.io.IOException;
27
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32
33 import javax.portlet.PortletContext;
34 import javax.portlet.PortletRequest;
35
36 import javax.servlet.RequestDispatcher;
37 import javax.servlet.ServletContext;
38 import javax.servlet.ServletException;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import javax.servlet.jsp.PageContext;
42
43 import org.apache.struts.Globals;
44
45
51 public class StrutsUtil {
52
53 public static final String STRUTS_PACKAGE = "org.apache.struts.";
54
55 public static final String TEXT_HTML_DIR = "/html";
56
57 public static final String TEXT_WAP_DIR = "/wap";
58
59 public static void forward(
60 String uri, ServletContext servletContext,
61 HttpServletRequest request, HttpServletResponse response)
62 throws ServletException {
63
64 if (_log.isDebugEnabled()) {
65 _log.debug("Forward URI " + uri);
66 }
67
68 if (uri.equals(ActionConstants.COMMON_NULL)) {
69 return;
70 }
71
72 if (!response.isCommitted()) {
73 String path = TEXT_HTML_DIR + uri;
74
75 if (BrowserSnifferUtil.isWap(request)) {
76 path = TEXT_WAP_DIR + uri;
77 }
78
79 if (_log.isDebugEnabled()) {
80 _log.debug("Forward path " + path);
81 }
82
83 RequestDispatcher requestDispatcher =
84 servletContext.getRequestDispatcher(path);
85
86 try {
87 requestDispatcher.forward(request, response);
88 }
89 catch (IOException ioe1) {
90 _log.warn(ioe1, ioe1);
91 }
92 catch (ServletException se1) {
93 request.setAttribute(PageContext.EXCEPTION, se1.getRootCause());
94
95 String errorPath = TEXT_HTML_DIR + "/common/error.jsp";
96
97 if (BrowserSnifferUtil.isWap(request)) {
98 path = TEXT_WAP_DIR + "/common/error.jsp";
99 }
100
101 requestDispatcher = servletContext.getRequestDispatcher(
102 errorPath);
103
104 try {
105 requestDispatcher.forward(request, response);
106 }
107 catch (IOException ioe2) {
108 _log.warn(ioe2, ioe2);
109 }
110 catch (ServletException se2) {
111 throw se2;
112 }
113 }
114 }
115 else {
116 _log.warn(uri + " is already committed");
117 }
118 }
119
120 public static void include(
121 String uri, ServletContext servletContext,
122 HttpServletRequest request, HttpServletResponse response)
123 throws ServletException {
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("Include URI " + uri);
127 }
128
129 String path = TEXT_HTML_DIR + uri;
130
131 if (BrowserSnifferUtil.isWap(request)) {
132 path = TEXT_WAP_DIR + uri;
133 }
134
135 if (_log.isDebugEnabled()) {
136 _log.debug("Include path " + path);
137 }
138
139 RequestDispatcher requestDispatcher =
140 servletContext.getRequestDispatcher(path);
141
142 try {
143 requestDispatcher.include(request, response);
144 }
145 catch (IOException ioe) {
146 _log.warn(ioe, ioe);
147 }
148 }
149
150 public static Map<String, Object> removeStrutsAttributes(
151 PortletContext portletContext, PortletRequest portletRequest) {
152
153 Map<String, Object> strutsAttributes = new HashMap<String, Object>();
154
155 Enumeration<String> enu = portletRequest.getAttributeNames();
156
157 while (enu.hasMoreElements()) {
158 String attributeName = enu.nextElement();
159
160 if (attributeName.startsWith(STRUTS_PACKAGE)) {
161 strutsAttributes.put(
162 attributeName, portletRequest.getAttribute(attributeName));
163 }
164 }
165
166 Iterator<String> itr = strutsAttributes.keySet().iterator();
167
168 while (itr.hasNext()) {
169 String attributeName = itr.next();
170
171 portletRequest.setAttribute(attributeName, null);
172 }
173
174 Object moduleConfig = portletContext.getAttribute(Globals.MODULE_KEY);
175
176 portletRequest.setAttribute(Globals.MODULE_KEY, moduleConfig);
177
178 return strutsAttributes;
179 }
180
181 public static void setStrutsAttributes(
182 PortletRequest portletRequest, Map<String, Object> strutsAttributes) {
183
184 for (Map.Entry<String, Object> entry : strutsAttributes.entrySet()) {
185 String key = entry.getKey();
186 Object value = entry.getValue();
187
188 portletRequest.setAttribute(key, value);
189 }
190 }
191
192 private static Log _log = LogFactoryUtil.getLog(StrutsUtil.class);
193
194 }