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