KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > DispatcherUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet;
4
5 import jodd.servlet.filter.CharArrayResponseWrapper;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.ServletRequest JavaDoc;
10 import javax.servlet.ServletResponse JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.RequestDispatcher JavaDoc;
13 import javax.servlet.ServletContext JavaDoc;
14 import java.io.IOException JavaDoc;
15
16 /**
17  * Request utilities for path, uri, query strings etc..
18  */

19 public class DispatcherUtil {
20
21     // ---------------------------------------------------------------- include
22

23     /**
24      * Include page which path is relative to the current HTTP request.
25      */

26     public static boolean include(ServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
27         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(page);
28         if (dispatcher != null) {
29             dispatcher.include(request, response);
30             return true;
31         }
32         return false;
33     }
34
35     /**
36      * Include named resource.
37      */

38     public static boolean includeNamed(HttpServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
39         return includeNamed(request.getSession().getServletContext(), request, response, resource);
40     }
41
42
43     /**
44      * Include named resource.
45      */

46     public static boolean includeNamed(ServletContext JavaDoc context, ServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
47         RequestDispatcher JavaDoc dispatcher = context.getNamedDispatcher(page);
48         if (dispatcher != null) {
49             dispatcher.include(request, response);
50             return true;
51         }
52         return false;
53     }
54
55
56
57     /**
58      * Include page which path relative to the root of the ServletContext.
59      */

60     public static boolean includeAbsolute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
61         return includeAbsolute(request.getSession().getServletContext(), request, response, page);
62     }
63
64
65     /**
66      * Include page which path relative to the root of the ServletContext.
67      */

68     public static boolean includeAbsolute(ServletContext JavaDoc context, ServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
69         RequestDispatcher JavaDoc dispatcher = context.getRequestDispatcher(page);
70         if (dispatcher != null) {
71             dispatcher.include(request, response);
72             return true;
73         }
74         return false;
75     }
76
77
78     // ---------------------------------------------------------------- wrap
79

80     public static CharArrayResponseWrapper wrap(ServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
81         CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
82         include(request, wrapper, page);
83         return wrapper;
84     }
85
86     public static CharArrayResponseWrapper wrapNamed(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
87         CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
88         includeNamed(request, wrapper, resource);
89         return wrapper;
90     }
91
92     public static CharArrayResponseWrapper wrapNamed(ServletContext JavaDoc context, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
93         CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
94         includeNamed(context, request, wrapper, resource);
95         return wrapper;
96     }
97
98     public static CharArrayResponseWrapper wrapAbsolute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
99         CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
100         includeAbsolute(request, wrapper, page);
101         return wrapper;
102     }
103
104
105     public static CharArrayResponseWrapper wrapAbsolute(ServletContext JavaDoc context, ServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
106         CharArrayResponseWrapper wrapper = new CharArrayResponseWrapper(response);
107         includeAbsolute(context, request, wrapper, page);
108         return wrapper;
109     }
110
111
112     // ---------------------------------------------------------------- forward
113

114     /**
115      * Forward to page which path is relative to the current HTTP request.
116      */

117     public static boolean forward(ServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
118         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(page);
119         if (dispatcher != null) {
120             dispatcher.forward(request, response);
121             return true;
122         }
123         return false;
124     }
125
126     /**
127      * Forward to named resource.
128      */

129     public static boolean forwardNamed(HttpServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
130         return forwardNamed(request.getSession().getServletContext(), request, response, resource);
131     }
132
133     /**
134      * Forward to named resource.
135      */

136     public static boolean forwardNamed(ServletContext JavaDoc context, ServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
137         RequestDispatcher JavaDoc dispatcher = context.getNamedDispatcher(resource);
138         if (dispatcher != null) {
139             dispatcher.forward(request, response);
140             return true;
141         }
142         return false;
143     }
144
145     /**
146      * Forward to page path relative to the root of the ServletContext.
147      */

148     public static boolean forwardAbsolute(HttpServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc page) throws IOException JavaDoc, ServletException JavaDoc {
149         return forwardAbsolute(request.getSession().getServletContext(), request, response, page);
150     }
151
152     /**
153      * Forward to page path relative to the root of the ServletContext.
154      */

155     public static boolean forwardAbsolute(ServletContext JavaDoc context, ServletRequest JavaDoc request, ServletResponse JavaDoc response, String JavaDoc resource) throws IOException JavaDoc, ServletException JavaDoc {
156         RequestDispatcher JavaDoc dispatcher = context.getRequestDispatcher(resource);
157         if (dispatcher != null) {
158             dispatcher.forward(request, response);
159             return true;
160         }
161         return false;
162     }
163
164     // ---------------------------------------------------------------- redirect
165

166
167     /**
168      * Performs redirection to specified page.
169      */

170     public static void redirect(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc url) throws IOException JavaDoc {
171         if (url.startsWith("/") == true) {
172             url = request.getContextPath() + url;
173         }
174         response.sendRedirect(response.encodeRedirectURL(url));
175     }
176
177
178     // ---------------------------------------------------------------- action path
179

180     /**
181      * Returns full action path: uri + query string.
182      */

183     public static String JavaDoc getActionPath(HttpServletRequest JavaDoc request) {
184         return makeActionPath(request.getRequestURI(), request.getQueryString());
185     }
186
187     /**
188      * Builds action path from uri and query.
189      */

190     public static String JavaDoc makeActionPath(String JavaDoc uri, String JavaDoc query) {
191         if ((query != null) && (query.length() != 0)) {
192             uri += '?' + query;
193         }
194         return uri;
195     }
196
197
198     // ---------------------------------------------------------------- include
199

200     public static final String JavaDoc INCLUDE_REQUEST_URI = "javax.servlet.include.request_uri";
201     public static final String JavaDoc INCLUDE_CONTEXT_PATH = "javax.servlet.include.context_path";
202     public static final String JavaDoc INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
203     public static final String JavaDoc INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
204     public static final String JavaDoc INCLUDE_QUERY_STRING = "javax.servlet.include.query_string";
205
206
207     public static String JavaDoc getIncludeRequestUri(HttpServletRequest JavaDoc request) {
208         return (String JavaDoc) request.getAttribute(INCLUDE_REQUEST_URI);
209     }
210
211     public static String JavaDoc getIncludeContextPath(HttpServletRequest JavaDoc request) {
212         return (String JavaDoc) request.getAttribute(INCLUDE_CONTEXT_PATH);
213     }
214
215     public static String JavaDoc getIncludeServletPath(HttpServletRequest JavaDoc request) {
216         return (String JavaDoc) request.getAttribute(INCLUDE_SERVLET_PATH);
217     }
218
219     public static String JavaDoc getIncludePathInfo(HttpServletRequest JavaDoc request) {
220         return (String JavaDoc) request.getAttribute(INCLUDE_PATH_INFO);
221     }
222
223     public static String JavaDoc getIncludeQueryString(HttpServletRequest JavaDoc request) {
224         return (String JavaDoc) request.getAttribute(INCLUDE_QUERY_STRING);
225     }
226
227     // ---------------------------------------------------------------- forward
228

229     public static final String JavaDoc FORWARD_REQUEST_URI = "javax.servlet.forward.request_uri";
230     public static final String JavaDoc FORWARD_CONTEXT_PATH = "javax.servlet.forward.context_path";
231     public static final String JavaDoc FORWARD_SERVLET_PATH = "javax.servlet.forward.servlet_path";
232     public static final String JavaDoc FORWARD_PATH_INFO = "javax.servlet.forward.path_info";
233     public static final String JavaDoc FORWARD_QUERY_STRING = "javax.servlet.forward.query_string";
234
235     public static String JavaDoc getForwardRequestUri(HttpServletRequest JavaDoc request) {
236         return (String JavaDoc) request.getAttribute(FORWARD_REQUEST_URI);
237     }
238
239     public static String JavaDoc getForwardContextPath(HttpServletRequest JavaDoc request) {
240         return (String JavaDoc) request.getAttribute(FORWARD_CONTEXT_PATH);
241     }
242
243     public static String JavaDoc getForwardServletPath(HttpServletRequest JavaDoc request) {
244         return (String JavaDoc) request.getAttribute(FORWARD_SERVLET_PATH);
245     }
246
247     public static String JavaDoc getForwardPathInfo(HttpServletRequest JavaDoc request) {
248         return (String JavaDoc) request.getAttribute(FORWARD_PATH_INFO);
249     }
250
251     public static String JavaDoc getForwardQueryString(HttpServletRequest JavaDoc request) {
252         return (String JavaDoc) request.getAttribute(FORWARD_QUERY_STRING);
253     }
254
255     // ---------------------------------------------------------------- misc
256

257     /**
258      * Returns <code>true</code> if current page is included.
259      */

260     public static boolean isPageIncluded(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
261         return (response.isCommitted() || (getIncludeServletPath(request) != null));
262     }
263
264     /**
265      * Returns <code>true</code> if request is a top-level one, i.e. previously
266      * not included or forwarded.
267      */

268     public static boolean isTopLevelRequest(HttpServletRequest JavaDoc request) {
269         return (getForwardRequestUri(request) == null) && (getIncludeRequestUri(request) == null);
270     }
271
272     // ---------------------------------------------------------------- smarter info
273

274     /**
275      * Returns the base (top-level) uri.
276      */

277     public static String JavaDoc getBaseRequestUri(HttpServletRequest JavaDoc request) {
278         String JavaDoc result = getForwardRequestUri(request);
279         if (result == null) {
280             result = request.getRequestURI();
281         }
282         return result;
283     }
284     /**
285      * Get current request uri.
286      */

287     public static String JavaDoc getRequestUri(HttpServletRequest JavaDoc request) {
288         String JavaDoc result = getIncludeRequestUri(request);
289         if (result == null) {
290             result = request.getRequestURI();
291         }
292         return result;
293     }
294
295     public static String JavaDoc getBaseContextPath(HttpServletRequest JavaDoc request) {
296         String JavaDoc result = getForwardContextPath(request);
297         if (result == null) {
298             result = request.getContextPath();
299         }
300         return result;
301     }
302     public static String JavaDoc getContextPath(HttpServletRequest JavaDoc request) {
303         String JavaDoc result = getIncludeContextPath(request);
304         if (result == null) {
305             result = request.getContextPath();
306         }
307         return result;
308     }
309
310
311     public static String JavaDoc getBaseServletPath(HttpServletRequest JavaDoc request) {
312         String JavaDoc result = getForwardServletPath(request);
313         if (result == null) {
314             result = request.getServletPath();
315         }
316         return result;
317     }
318     public static String JavaDoc getServletPath(HttpServletRequest JavaDoc request) {
319         String JavaDoc result = getIncludeServletPath(request);
320         if (result == null) {
321             result = request.getServletPath();
322         }
323         return result;
324     }
325
326
327     public static String JavaDoc getBasePathInfo(HttpServletRequest JavaDoc request) {
328         String JavaDoc result = getForwardPathInfo(request);
329         if (result == null) {
330             result = request.getPathInfo();
331         }
332         return result;
333     }
334     public static String JavaDoc getPathInfo(HttpServletRequest JavaDoc request) {
335         String JavaDoc result = getIncludePathInfo(request);
336         if (result == null) {
337             result = request.getPathInfo();
338         }
339         return result;
340     }
341
342
343     public static String JavaDoc getBaseQueryString(HttpServletRequest JavaDoc request) {
344         String JavaDoc result = getForwardQueryString(request);
345         if (result == null) {
346             result = request.getQueryString();
347         }
348         return result;
349     }
350     public static String JavaDoc getQueryString(HttpServletRequest JavaDoc request) {
351         String JavaDoc result = getIncludeQueryString(request);
352         if (result == null) {
353             result = request.getQueryString();
354         }
355         return result;
356     }
357 }
358
Popular Tags