001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.servlet;
019    
020    import java.io.IOException;
021    
022    import javax.servlet.Filter;
023    import javax.servlet.FilterChain;
024    import javax.servlet.FilterConfig;
025    import javax.servlet.ServletException;
026    import javax.servlet.ServletRequest;
027    import javax.servlet.ServletResponse;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletRequestWrapper;
030    
031    /**
032     * Filter that wrappers HTTP requests forwarded from a separate
033     * context via a named request dispatcher.  The wrapped HTTP
034     * requests return attributes from the original request instead
035     * of the forwarded instance of the request. Deployment
036     * descriptors that use this filter should specify "FORWARD" as
037     * the dispatcher type in their filter-mapping elements, as per
038     * the 2.4 Servlet Specification. e.g.
039     * 
040     *     <pre>
041     *     <filter-mapping>
042     *        <filter-name>myfilter</filter-name>
043     *        <servlet-name>myservlet</servlet-name>
044     *        <dispatcher>FORWARD</dispatcher>
045     *     </filter-mapping>
046     *     </pre>
047     *     
048     */
049    public class ForwardDispatchFilter implements Filter {
050    
051        protected FilterConfig filterConfig;
052        
053        public void init(FilterConfig config) throws ServletException {
054            filterConfig = config;
055        }
056    
057        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
058            if (request instanceof HttpServletRequest) {
059                chain.doFilter(new ForwardRequest((HttpServletRequest)request), response);
060            } else {
061                throw new ServletException("ServletRequest is not an instance of HttpServletRequest");
062            }
063        }
064    
065        public void destroy() {}
066        
067        /* An HTTP servlet request wrapper that maps the following
068         * attributes from the original request
069         *  
070         * # javax.servlet.forward.request_uri
071         * # javax.servlet.forward.context_path
072         * # javax.servlet.forward.servlet_path
073         * # javax.servlet.forward.path_info
074         * # javax.servlet.forward.query_string
075         */
076        protected static class ForwardRequest extends HttpServletRequestWrapper {
077            private final HttpServletRequest request;
078            public ForwardRequest(HttpServletRequest req) {
079                super(req);
080                request = req;
081            }
082            public String getRequestURI() {
083                return String.valueOf(request.getAttribute("javax.servlet.forward.request_uri")); 
084            }
085            public String getContextPath() {
086                return String.valueOf(request.getAttribute("javax.servlet.forward.context_path")); 
087            }
088            public String getServletPath() {
089                return String.valueOf(request.getAttribute("javax.servlet.forward.servlet_path")); 
090            }
091            public String getPathInfo() {
092                return String.valueOf(request.getAttribute("javax.servlet.forward.path_info")); 
093            }
094            public String getQueryString() {
095                return String.valueOf(request.getAttribute("javax.servlet.forward.query_string")); 
096            }
097        }
098    }