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  import java.util.HashSet;
    20  import java.util.Set;
    21
    22  import javax.servlet.Filter;
    23  import javax.servlet.FilterChain;
    24  import javax.servlet.FilterConfig;
    25  import javax.servlet.ServletContext;
    26  import javax.servlet.ServletException;
    27  import javax.servlet.ServletRequest;
    28  import javax.servlet.ServletRequestWrapper;
    29  import javax.servlet.ServletResponse;
    30  import javax.servlet.http.HttpServletRequest;
    31  import javax.servlet.http.HttpServletRequestWrapper;
    32  import javax.servlet.http.HttpServletResponse;
    33
    34  /* ------------------------------------------------------------ */
    35  /** TestFilter.
    36   * @author gregw
    37   *
    38   */
    39  public class TestFilter implements Filter
    40  {
    41      private boolean _remote;
    42      private ServletContext _context;
    43      private final Set _allowed = new HashSet();
    44
    45      /* ------------------------------------------------------------ */
    46      /*
    47       * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
    48       */
    49      public void init(FilterConfig filterConfig) throws ServletException
    50      {
    51          _context= filterConfig.getServletContext();
    52          _remote=Boolean.parseBoolean(_context.getInitParameter("remote"));
    53          _allowed.add("/favicon.ico");
    54          _allowed.add("/jetty_banner.gif");
    55          _allowed.add("/remote.html");
    56      }
    57
    58      /* ------------------------------------------------------------ */
    59      /*
    60       * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
    61       */
    62      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    63              throws IOException, ServletException
    64      {
    65          String from = request.getRemoteHost();
    66          String to = request.getServerName();
    67
    68          if ((!_remote&&!from.equals("localhost")&&!from.startsWith("127.0.0.")||
    69               !to.equals("localhost")&&!to.startsWith("127.0.0.")) &&
    70               !_allowed.contains(((HttpServletRequest)request).getServletPath()))
    71          {
    72              ((HttpServletResponse)response).sendRedirect("/remote.html");
    73              return;
    74          }
    75
    76          Integer old_value=null;
    77          ServletRequest r = request;
    78          while (r instanceof ServletRequestWrapper)
    79              r=((ServletRequestWrapper)r).getRequest();
    80
    81          try
    82          {
    83              old_value=(Integer)request.getAttribute("testFilter");
    84
    85              Integer value=(old_value==null)?new Integer(1):new Integer(old_value.intValue()+1);
    86
    87              request.setAttribute("testFilter", value);
    88
    89              String qString = ((HttpServletRequest)request).getQueryString();
    90              if (qString != null && qString.indexOf("wrap")>0)
    91              {
    92                  request=new HttpServletRequestWrapper((HttpServletRequest)request);
    93              }
    94              _context.setAttribute("request"+r.hashCode(),value);
    95
    96              chain.doFilter(request, response);
    97          }
    98          finally
    99          {
    100             request.setAttribute("testFilter", old_value);
    101             _context.setAttribute("request"+r.hashCode(),old_value);
    102         }
    103     }
    104
    105     /* ------------------------------------------------------------ */
    106     /*
    107      * @see javax.servlet.Filter#destroy()
    108      */
    109     public void destroy()
    110     {
    111     }
    112
    113 }