1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import java.util.Enumeration;
37 import java.util.LinkedHashMap;
38 import javax.servlet.FilterConfig;
39 import javax.servlet.ServletConfig;
40 import javax.servlet.ServletRequest;
41 import javax.servlet.ServletRequestWrapper;
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.apache.commons.lang3.StringUtils;
45
46
47
48
49
50
51 public abstract class ServletUtil {
52
53 public static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri";
54 public static final String FORWARD_QUERY_STRING_ATTRIBUTE = "javax.servlet.forward.query_string";
55
56 public static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri";
57 public static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.include.context_path";
58
59 public static final String ERROR_REQUEST_STATUS_CODE_ATTRIBUTE = "javax.servlet.error.status_code";
60
61
62
63
64
65 public static LinkedHashMap<String, String> initParametersToMap(ServletConfig config) {
66 LinkedHashMap<String, String> initParameters = new LinkedHashMap<String, String>();
67 Enumeration parameterNames = config.getInitParameterNames();
68 while (parameterNames.hasMoreElements()) {
69 String parameterName = (String) parameterNames.nextElement();
70 initParameters.put(parameterName, config.getInitParameter(parameterName));
71 }
72 return initParameters;
73 }
74
75
76
77
78
79 public static LinkedHashMap<String, String> initParametersToMap(FilterConfig config) {
80 LinkedHashMap<String, String> initParameters = new LinkedHashMap<String, String>();
81 Enumeration parameterNames = config.getInitParameterNames();
82 while (parameterNames.hasMoreElements()) {
83 String parameterName = (String) parameterNames.nextElement();
84 initParameters.put(parameterName, config.getInitParameter(parameterName));
85 }
86 return initParameters;
87 }
88
89
90
91
92 public static <T extends ServletRequest> T getWrappedRequest(ServletRequest request, Class<T> clazz) {
93 while (request != null) {
94 if (clazz.isAssignableFrom(request.getClass())) {
95 return (T) request;
96 }
97 request = (request instanceof ServletRequestWrapper) ? ((ServletRequestWrapper) request).getRequest() : null;
98 }
99 return null;
100 }
101
102
103
104
105 public static boolean isMultipart(HttpServletRequest request) {
106 String contentType = request.getContentType();
107 return contentType != null && contentType.toLowerCase().startsWith("multipart/");
108 }
109
110
111
112
113
114 public static boolean isForward(HttpServletRequest request) {
115 return request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) != null && !isInclude(request);
116 }
117
118
119
120
121 public static boolean isInclude(HttpServletRequest request) {
122 return request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null;
123 }
124
125
126
127
128
129
130 public static boolean isError(HttpServletRequest request) {
131 return request.getAttribute(ERROR_REQUEST_STATUS_CODE_ATTRIBUTE) != null;
132 }
133
134
135
136
137
138 public static DispatcherType getDispatcherType(HttpServletRequest request) {
139
140 if (request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null) {
141 return DispatcherType.INCLUDE;
142 }
143 if (request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
144 return DispatcherType.FORWARD;
145 }
146 if (request.getAttribute(ERROR_REQUEST_STATUS_CODE_ATTRIBUTE) != null) {
147 return DispatcherType.ERROR;
148 }
149 return DispatcherType.REQUEST;
150 }
151
152
153
154
155
156 public static String getOriginalRequestURI(HttpServletRequest request) {
157 String uri = (String) request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE);
158 if (uri == null) {
159 uri = request.getRequestURI();
160 }
161 return stripPathParameters(uri);
162 }
163
164
165
166
167
168 public static String getOriginalRequestURLIncludingQueryString(HttpServletRequest request) {
169 if (request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE) != null) {
170 StringBuilder builder = new StringBuilder();
171
172 String scheme = request.getScheme();
173 builder.append(scheme).append("://").append(request.getServerName());
174
175 int port = request.getServerPort();
176 if ((scheme.equalsIgnoreCase("http") && port == 80) || (scheme.equalsIgnoreCase("https") && port == 443)) {
177
178 } else {
179 builder.append(":").append(port);
180 }
181
182 String requestUri = (String) request.getAttribute(FORWARD_REQUEST_URI_ATTRIBUTE);
183 builder.append(requestUri);
184
185 String queryString = (String) request.getAttribute(FORWARD_QUERY_STRING_ATTRIBUTE);
186 if (StringUtils.isNotEmpty(queryString)) {
187 builder.append("?").append(queryString);
188 }
189
190 return builder.toString();
191 }
192 StringBuilder builder = new StringBuilder();
193 builder.append(request.getRequestURL());
194 String queryString = request.getQueryString();
195 if (StringUtils.isNotEmpty(queryString)) {
196 builder.append("?").append(queryString);
197 }
198 return builder.toString();
199 }
200
201
202
203
204
205 public static String getRequestUri(HttpServletRequest request) {
206 String uri = (String) request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE);
207 if (uri == null) {
208 uri = request.getRequestURI();
209 }
210 return stripPathParameters(uri);
211 }
212
213
214
215
216
217
218 public static String stripPathParameters(String uri) {
219 int semicolonIndex = uri.indexOf(';');
220 return semicolonIndex != -1 ? uri.substring(0, semicolonIndex) : uri;
221 }
222
223
224
225
226
227 public static String getContextRelativePath(HttpServletRequest request) {
228 String uri = getRequestUri(request);
229 String contextPath = getContextPath(request);
230
231 if (uri.startsWith(contextPath + "/")) {
232 return uri.substring(contextPath.length());
233 }
234 return uri;
235 }
236
237 public static String getContextPath(HttpServletRequest request) {
238 String contextPath = (String) request.getAttribute(INCLUDE_CONTEXT_PATH_ATTRIBUTE);
239 if (contextPath == null) {
240 contextPath = request.getContextPath();
241 }
242
243 if ("/".equals(contextPath)) {
244 contextPath = "";
245 }
246 return contextPath;
247 }
248 }