-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
DispatcherUtil.java
364 lines (308 loc) · 12.5 KB
/
DispatcherUtil.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package jodd.servlet;
import io.github.pixee.security.Newlines;
import jodd.util.StringPool;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Request utilities for path, uri, query strings etc..
*/
public class DispatcherUtil {
// ---------------------------------------------------------------- include
/**
* Include page which path is relative to the current HTTP request.
*/
public static boolean include(final ServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null) {
dispatcher.include(request, response);
return true;
}
return false;
}
/**
* Include named resource.
*/
public static boolean includeNamed(final HttpServletRequest request, final ServletResponse response, final String resource) throws IOException, ServletException {
return includeNamed(request.getServletContext(), request, response, resource);
}
/**
* Include named resource.
*/
public static boolean includeNamed(final ServletContext context, final ServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
RequestDispatcher dispatcher = context.getNamedDispatcher(page);
if (dispatcher != null) {
dispatcher.include(request, response);
return true;
}
return false;
}
/**
* Include page which path relative to the root of the ServletContext.
*/
public static boolean includeAbsolute(final HttpServletRequest request, final HttpServletResponse response, final String page) throws IOException, ServletException {
return includeAbsolute(request.getServletContext(), request, response, page);
}
/**
* Include page which path relative to the root of the ServletContext.
*/
public static boolean includeAbsolute(final ServletContext context, final ServletRequest request, final HttpServletResponse response, final String page) throws IOException, ServletException {
RequestDispatcher dispatcher = context.getRequestDispatcher(page);
if (dispatcher != null) {
dispatcher.include(request, response);
return true;
}
return false;
}
// ---------------------------------------------------------------- forward
/**
* Forward to page which path is relative to the current HTTP request.
*/
public static boolean forward(final ServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null) {
dispatcher.forward(request, response);
return true;
}
return false;
}
/**
* Forward to named resource.
*/
public static boolean forwardNamed(final HttpServletRequest request, final ServletResponse response, final String resource) throws IOException, ServletException {
return forwardNamed(request.getServletContext(), request, response, resource);
}
/**
* Forward to named resource.
*/
public static boolean forwardNamed(final ServletContext context, final ServletRequest request, final ServletResponse response, final String resource) throws IOException, ServletException {
RequestDispatcher dispatcher = context.getNamedDispatcher(resource);
if (dispatcher != null) {
dispatcher.forward(request, response);
return true;
}
return false;
}
/**
* Forward to page path relative to the root of the ServletContext.
*/
public static boolean forwardAbsolute(final HttpServletRequest request, final ServletResponse response, final String page) throws IOException, ServletException {
return forwardAbsolute(request.getServletContext(), request, response, page);
}
/**
* Forward to page path relative to the root of the ServletContext.
*/
public static boolean forwardAbsolute(final ServletContext context, final ServletRequest request, final ServletResponse response, final String resource) throws IOException, ServletException {
RequestDispatcher dispatcher = context.getRequestDispatcher(resource);
if (dispatcher != null) {
dispatcher.forward(request, response);
return true;
}
return false;
}
// ---------------------------------------------------------------- redirect
/**
* Performs redirection (302) to specified url.
*/
public static void redirect(final HttpServletRequest request, final HttpServletResponse response, String url) throws IOException {
if (url.startsWith(StringPool.SLASH)) {
url = ServletUtil.getContextPath(request) + url;
}
response.sendRedirect(response.encodeRedirectURL(url));
}
/**
* Performs permanent redirection (301) to specified url.
*/
public static void redirectPermanent(final HttpServletRequest request, final HttpServletResponse response, String url) {
if (url.startsWith(StringPool.SLASH)) {
url = ServletUtil.getContextPath(request) + url;
}
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", Newlines.stripAll(url));
}
// ---------------------------------------------------------------- full urls
/**
* Returns full URL: uri + query string, including the context path.
*/
public static String getFullUrl(final HttpServletRequest request) {
String url = request.getRequestURI();
String query = request.getQueryString();
if ((query != null) && (query.length() != 0)) {
url += '?' + query;
}
return url;
}
/**
* Returns url, without context path, convenient for request dispatcher.
*/
public static String getUrl(final HttpServletRequest request) {
String servletPath = request.getServletPath();
String query = request.getQueryString();
if ((query != null) && (query.length() != 0)) {
servletPath += '?' + query;
}
return servletPath;
}
// ---------------------------------------------------------------- include
public static final String INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";
public static final String INCLUDE_CONTEXT_PATH = "javax.servlet.include.context_path";
public static final String INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
public static final String INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
public static final String INCLUDE_QUERY_STRING = "javax.servlet.include.query_string";
public static String getIncludeRequestUri(final HttpServletRequest request) {
return (String) request.getAttribute(INCLUDE_REQUEST_URI);
}
public static String getIncludeContextPath(final HttpServletRequest request) {
return (String) request.getAttribute(INCLUDE_CONTEXT_PATH);
}
public static String getIncludeServletPath(final HttpServletRequest request) {
return (String) request.getAttribute(INCLUDE_SERVLET_PATH);
}
public static String getIncludePathInfo(final HttpServletRequest request) {
return (String) request.getAttribute(INCLUDE_PATH_INFO);
}
public static String getIncludeQueryString(final HttpServletRequest request) {
return (String) request.getAttribute(INCLUDE_QUERY_STRING);
}
// ---------------------------------------------------------------- forward
public static final String FORWARD_REQUEST_URI = "javax.servlet.forward.request_uri";
public static final String FORWARD_CONTEXT_PATH = "javax.servlet.forward.context_path";
public static final String FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path";
public static final String FORWARD_PATH_INFO = "javax.servlet.forward.path_info";
public static final String FORWARD_QUERY_STRING = "javax.servlet.forward.query_string";
public static String getForwardRequestUri(final HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_REQUEST_URI);
}
public static String getForwardContextPath(final HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_CONTEXT_PATH);
}
public static String getForwardServletPath(final HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_SERVLET_PATH);
}
public static String getForwardPathInfo(final HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_PATH_INFO);
}
public static String getForwardQueryString(final HttpServletRequest request) {
return (String) request.getAttribute(FORWARD_QUERY_STRING);
}
// ---------------------------------------------------------------- misc
/**
* Returns <code>true</code> if current page is included.
*/
public static boolean isPageIncluded(final HttpServletRequest request, final HttpServletResponse response) {
return (response.isCommitted() || (getIncludeServletPath(request) != null));
}
/**
* Returns <code>true</code> if request is a top-level one, i.e. previously
* not included or forwarded.
*/
public static boolean isTopLevelRequest(final HttpServletRequest request) {
return (getForwardRequestUri(request) == null) && (getIncludeRequestUri(request) == null);
}
// ---------------------------------------------------------------- smarter info
/**
* Returns the base (top-level) uri.
*/
public static String getBaseRequestUri(final HttpServletRequest request) {
String result = getForwardRequestUri(request);
if (result == null) {
result = request.getRequestURI();
}
return result;
}
/**
* Get current request uri.
*/
public static String getRequestUri(final HttpServletRequest request) {
String result = getIncludeRequestUri(request);
if (result == null) {
result = request.getRequestURI();
}
return result;
}
public static String getBaseContextPath(final HttpServletRequest request) {
String result = getForwardContextPath(request);
if (result == null) {
result = ServletUtil.getContextPath(request);
}
return result;
}
public static String getContextPath(final HttpServletRequest request) {
String result = getIncludeContextPath(request);
if (result == null) {
result = ServletUtil.getContextPath(request);
}
return result;
}
public static String getBaseServletPath(final HttpServletRequest request) {
String result = getForwardServletPath(request);
if (result == null) {
result = request.getServletPath();
}
return result;
}
public static String getServletPath(final HttpServletRequest request) {
String result = getIncludeServletPath(request);
if (result == null) {
result = request.getServletPath();
}
return result;
}
public static String getBasePathInfo(final HttpServletRequest request) {
String result = getForwardPathInfo(request);
if (result == null) {
result = request.getPathInfo();
}
return result;
}
public static String getPathInfo(final HttpServletRequest request) {
String result = getIncludePathInfo(request);
if (result == null) {
result = request.getPathInfo();
}
return result;
}
public static String getBaseQueryString(final HttpServletRequest request) {
String result = getForwardQueryString(request);
if (result == null) {
result = request.getQueryString();
}
return result;
}
public static String getQueryString(final HttpServletRequest request) {
String result = getIncludeQueryString(request);
if (result == null) {
result = request.getQueryString();
}
return result;
}
}