View Javadoc

    1   //========================================================================
    2   //$Id: RegexRule.java 966 2008-04-17 13:53:44Z gregw $
    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  package org.mortbay.jetty.handler.rewrite;
    16
    17  import java.io.IOException;
    18  import java.util.regex.Matcher;
    19  import java.util.regex.Pattern;
    20
    21  import javax.servlet.http.HttpServletRequest;
    22  import javax.servlet.http.HttpServletResponse;
    23
    24
    25  /**
    26   * Abstract rule to use as a base class for rules that match with a regular expression.
    27   */
    28  public abstract class RegexRule extends Rule
    29  {
    30      protected Pattern _regex;
    31
    32      /* ------------------------------------------------------------ */
    33      /**
    34       * Sets the regular expression string used to match with string URI.
    35       *
    36       * @param regex the regular expression.
    37       */
    38      public void setRegex(String regex)
    39      {
    40          _regex=Pattern.compile(regex);
    41      }
    42
    43      /* ------------------------------------------------------------ */
    44      /**
    45       * @return get the regular expression
    46       */
    47      public String getRegex()
    48      {
    49          return _regex==null?null:_regex.pattern();
    50      }
    51
    52
    53      /* ------------------------------------------------------------ */
    54      public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    55      {
    56          Matcher matcher=_regex.matcher(target);
    57          boolean matches = matcher.matches();
    58          if (matches)
    59              return apply(target,request,response, matcher);
    60          return null;
    61      }
    62
    63      /* ------------------------------------------------------------ */
    64      /**
    65       * Apply this rule to the request/response pair.
    66       * Called by {@link #matchAndApply(String, HttpServletRequest, HttpServletResponse)} if the regex matches.
    67       * @param target field to attempt match
    68       * @param request request object
    69       * @param response response object
    70       * @param matcher The Regex matcher that matched the request (with capture groups available for replacement).
    71       * @return The target (possible updated).
    72       * @throws IOException exceptions dealing with operating on request or response objects
    73       */
    74      protected abstract String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException;
    75
    76
    77      /* ------------------------------------------------------------ */
    78      /**
    79       * Returns the regular expression string.
    80       */
    81      public String toString()
    82      {
    83          return super.toString()+"["+_regex+"]";
    84      }
    85  }