View Javadoc

    1   //========================================================================
    2   //$Id: TestFilter.java,v 1.5 2005/11/01 11:42:53 gregwilkins Exp $
    3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package com.acme;
    17
    18  import java.io.IOException;
    19
    20  import javax.servlet.Filter;
    21  import javax.servlet.FilterChain;
    22  import javax.servlet.FilterConfig;
    23  import javax.servlet.ServletContext;
    24  import javax.servlet.ServletException;
    25  import javax.servlet.ServletRequest;
    26  import javax.servlet.ServletRequestWrapper;
    27  import javax.servlet.ServletResponse;
    28  import javax.servlet.http.HttpServletRequest;
    29  import javax.servlet.http.HttpServletRequestWrapper;
    30
    31  /* ------------------------------------------------------------ */
    32  /** TestFilter.
    33   * @author gregw
    34   *
    35   */
    36  public class TestFilter implements Filter
    37  {
    38      private ServletContext _context;
    39
    40      /* ------------------------------------------------------------ */
    41      /*
    42       * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
    43       */
    44      public void init(FilterConfig filterConfig) throws ServletException
    45      {
    46          _context= filterConfig.getServletContext();
    47      }
    48
    49      /* ------------------------------------------------------------ */
    50      /*
    51       * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
    52       */
    53      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    54              throws IOException, ServletException
    55      {
    56          Integer old_value=null;
    57          ServletRequest r = request;
    58          while (r instanceof ServletRequestWrapper)
    59              r=((ServletRequestWrapper)r).getRequest();
    60
    61          try
    62          {
    63              old_value=(Integer)request.getAttribute("testFilter");
    64
    65              Integer value=(old_value==null)?new Integer(1):new Integer(old_value.intValue()+1);
    66
    67              request.setAttribute("testFilter", value);
    68
    69              String qString = ((HttpServletRequest)request).getQueryString();
    70              if (qString != null && qString.indexOf("wrap")>0)
    71              {
    72                  request=new HttpServletRequestWrapper((HttpServletRequest)request);
    73              }
    74              _context.setAttribute("request"+r.hashCode(),value);
    75
    76              chain.doFilter(request, response);
    77          }
    78          finally
    79          {
    80              request.setAttribute("testFilter", old_value);
    81              _context.setAttribute("request"+r.hashCode(),old_value);
    82          }
    83      }
    84
    85      /* ------------------------------------------------------------ */
    86      /*
    87       * @see javax.servlet.Filter#destroy()
    88       */
    89      public void destroy()
    90      {
    91      }
    92
    93  }