KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > servlet > ForwardDispatchFilter


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.console.servlet;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.Filter JavaDoc;
23 import javax.servlet.FilterChain JavaDoc;
24 import javax.servlet.FilterConfig JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.ServletRequest JavaDoc;
27 import javax.servlet.ServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
30
31 /**
32  * Filter that wrappers HTTP requests forwarded from a separate
33  * context via a named request dispatcher. The wrapped HTTP
34  * requests return attributes from the original request instead
35  * of the forwarded instance of the request. Deployment
36  * descriptors that use this filter should specify "FORWARD" as
37  * the dispatcher type in their filter-mapping elements, as per
38  * the 2.4 Servlet Specification. e.g.
39  *
40  * <pre>
41  * <filter-mapping>
42  * <filter-name>myfilter</filter-name>
43  * <servlet-name>myservlet</servlet-name>
44  * <dispatcher>FORWARD</dispatcher>
45  * </filter-mapping>
46  * </pre>
47  *
48  */

49 public class ForwardDispatchFilter implements Filter JavaDoc {
50
51     protected FilterConfig JavaDoc filterConfig;
52     
53     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
54         filterConfig = config;
55     }
56
57     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
58         if (request instanceof HttpServletRequest JavaDoc) {
59             chain.doFilter(new ForwardRequest((HttpServletRequest JavaDoc)request), response);
60         } else {
61             throw new ServletException JavaDoc("ServletRequest is not an instance of HttpServletRequest");
62         }
63     }
64
65     public void destroy() {}
66     
67     /* An HTTP servlet request wrapper that maps the following
68      * attributes from the original request
69      *
70      * # javax.servlet.forward.request_uri
71      * # javax.servlet.forward.context_path
72      * # javax.servlet.forward.servlet_path
73      * # javax.servlet.forward.path_info
74      * # javax.servlet.forward.query_string
75      */

76     protected class ForwardRequest extends HttpServletRequestWrapper JavaDoc {
77         HttpServletRequest JavaDoc request;
78         public ForwardRequest(HttpServletRequest JavaDoc req) {
79             super(req);
80             request = req;
81         }
82         public String JavaDoc getRequestURI() {
83             return String.valueOf(request.getAttribute("javax.servlet.forward.request_uri"));
84         }
85         public String JavaDoc getContextPath() {
86             return String.valueOf(request.getAttribute("javax.servlet.forward.context_path"));
87         }
88         public String JavaDoc getServletPath() {
89             return String.valueOf(request.getAttribute("javax.servlet.forward.servlet_path"));
90         }
91         public String JavaDoc getPathInfo() {
92             return String.valueOf(request.getAttribute("javax.servlet.forward.path_info"));
93         }
94         public String JavaDoc getQueryString() {
95             return String.valueOf(request.getAttribute("javax.servlet.forward.query_string"));
96         }
97     }
98 }
99
Popular Tags