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  import org.mortbay.log.Log;
    35
    36
    37  /* ------------------------------------------------------------ */
    38  /** TestFilter.
    39   *
    40   * This filter checks for a none local request, and if the init parameter
    41   * "remote" is not set to true, then all non local requests are forwarded
    42   * to /remote.html
    43   *
    44   */
    45  public class TestFilter implements Filter
    46  {
    47      private boolean _remote;
    48      private ServletContext _context;
    49      private final Set _allowed = new HashSet();
    50
    51      /* ------------------------------------------------------------ */
    52      /*
    53       * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
    54       */
    55      public void init(FilterConfig filterConfig) throws ServletException
    56      {
    57          _context= filterConfig.getServletContext();
    58          _remote=Boolean.parseBoolean(filterConfig.getInitParameter("remote"));
    59          _allowed.add("/favicon.ico");
    60          _allowed.add("/jetty_banner.gif");
    61
    62          Log.debug("TestFilter#remote="+_remote);
    63      }
    64
    65      /* ------------------------------------------------------------ */
    66      /*
    67       * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
    68       */
    69      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    70              throws IOException, ServletException
    71      {
    72          String from = request.getRemoteHost();
    73          String to = request.getServerName();
    74          String path=((HttpServletRequest)request).getServletPath();
    75
    76          if (!_remote && !_allowed.contains(path) && (
    77               !from.equals("localhost") && !from.startsWith("127.") && from.indexOf(":1")<0 ||
    78               !to.equals("localhost")&&!to.startsWith("127.0.0.") && to.indexOf(":1")<0))
    79          {
    80              System.err.println("REMOTE "+from+" "+to+" "+path);
    81              if ("/".equals(path))
    82                  _context.getRequestDispatcher("/remote.html").forward(request,response);
    83              else
    84                  ((HttpServletResponse)response).sendRedirect("/remote.html");
    85              return;
    86          }
    87
    88          Integer old_value=null;
    89          ServletRequest r = request;
    90          while (r instanceof ServletRequestWrapper)
    91              r=((ServletRequestWrapper)r).getRequest();
    92
    93          try
    94          {
    95              old_value=(Integer)request.getAttribute("testFilter");
    96
    97              Integer value=(old_value==null)?new Integer(1):new Integer(old_value.intValue()+1);
    98
    99              request.setAttribute("testFilter", value);
    100
    101             String qString = ((HttpServletRequest)request).getQueryString();
    102             if (qString != null && qString.indexOf("wrap")>=0)
    103             {
    104                 request=new HttpServletRequestWrapper((HttpServletRequest)request);
    105             }
    106             _context.setAttribute("request"+r.hashCode(),value);
    107
    108             chain.doFilter(request, response);
    109         }
    110         finally
    111         {
    112             request.setAttribute("testFilter", old_value);
    113             _context.setAttribute("request"+r.hashCode(),old_value);
    114         }
    115     }
    116
    117     /* ------------------------------------------------------------ */
    118     /*
    119      * @see javax.servlet.Filter#destroy()
    120      */
    121     public void destroy()
    122     {
    123     }
    124
    125 }