Home » geronimo-2.2-source-release » org.apache.geronimo.console.servlet » [javadoc | source]

    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;
   21   
   22   import javax.servlet.Filter;
   23   import javax.servlet.FilterChain;
   24   import javax.servlet.FilterConfig;
   25   import javax.servlet.ServletException;
   26   import javax.servlet.ServletRequest;
   27   import javax.servlet.ServletResponse;
   28   import javax.servlet.http.HttpServletRequest;
   29   import javax.servlet.http.HttpServletRequestWrapper;
   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 {
   50   
   51       protected FilterConfig filterConfig;
   52       
   53       public void init(FilterConfig config) throws ServletException {
   54           filterConfig = config;
   55       }
   56   
   57       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
   58           if (request instanceof HttpServletRequest) {
   59               chain.doFilter(new ForwardRequest((HttpServletRequest)request), response);
   60           } else {
   61               throw new ServletException("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 static class ForwardRequest extends HttpServletRequestWrapper {
   77           private final HttpServletRequest request;
   78           public ForwardRequest(HttpServletRequest req) {
   79               super(req);
   80               request = req;
   81           }
   82           public String getRequestURI() {
   83               return String.valueOf(request.getAttribute("javax.servlet.forward.request_uri")); 
   84           }
   85           public String getContextPath() {
   86               return String.valueOf(request.getAttribute("javax.servlet.forward.context_path")); 
   87           }
   88           public String getServletPath() {
   89               return String.valueOf(request.getAttribute("javax.servlet.forward.servlet_path")); 
   90           }
   91           public String getPathInfo() {
   92               return String.valueOf(request.getAttribute("javax.servlet.forward.path_info")); 
   93           }
   94           public String getQueryString() {
   95               return String.valueOf(request.getAttribute("javax.servlet.forward.query_string")); 
   96           }
   97       }
   98   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.servlet » [javadoc | source]