View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19
    20  package org.apache.myfaces.orchestra.lib;
    21
    22  import javax.servlet.FilterConfig;
    23  import javax.servlet.ServletRequest;
    24  import javax.servlet.http.HttpServletRequest;
    25
    26  /**
    27   * Provides the ability to test the url of a servlet-request against a set of criteria to determine
    28   * whether to "accept" the url or not.
    29   * <p>
    30   * This can be used for a number of purposes. One example is that servlet filters can use this to
    31   * extend the very primitive "filter-mapping" facilities of the web.xml configuration file, giving
    32   * the ability for the filter to be matched more precisely to the incoming url.
    33   * <p>
    34   * Initially the matching facilities are fairly primitive; just a single "excludePrefix" and
    35   * "excludeSuffix" string can be defined. However this class can be extended later and classes which
    36   * use this to filter their urls will transparently get the new features.
    37   */
    38  public class _UrlMatcher
    39  {
    40      private final String excludePrefix;
    41      private final String excludeSuffix;
    42
    43      public _UrlMatcher(FilterConfig config)
    44      {
    45          this.excludePrefix = config.getInitParameter("excludePrefix");
    46          this.excludeSuffix = config.getInitParameter("excludeSuffix");
    47      }
    48
    49      public boolean accept(ServletRequest req)
    50      {
    51          if ((excludePrefix == null) && (excludeSuffix == null))
    52          {
    53              return true;
    54          }
    55
    56          if (req instanceof HttpServletRequest == false)
    57          {
    58              return true;
    59          }
    60
    61          HttpServletRequest hreq = (HttpServletRequest) req;
    62          String pathInfo = hreq.getPathInfo();
    63          String servletPath = hreq.getServletPath();
    64
    65          if (excludePrefix != null)
    66          {
    67              if (servletPath.startsWith(excludePrefix))
    68              {
    69                  return false;
    70              }
    71          }
    72
    73          if (excludeSuffix != null)
    74          {
    75              if ((pathInfo == null) && servletPath.endsWith(excludeSuffix))
    76              {
    77                  return false;
    78              }
    79
    80              if ((pathInfo != null) && pathInfo.endsWith(excludeSuffix))
    81              {
    82                  return false;
    83              }
    84          }
    85
    86          return true;
    87      }
    88  }