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