View Javadoc

    1   //========================================================================
    2   //$Id: RewriteHandler.java 1427 2009-02-10 11:24:41Z ayao $
    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
    19  import javax.servlet.ServletException;
    20  import javax.servlet.http.HttpServletRequest;
    21  import javax.servlet.http.HttpServletResponse;
    22
    23  import org.mortbay.jetty.HttpConnection;
    24  import org.mortbay.jetty.Request;
    25  import org.mortbay.jetty.handler.HandlerWrapper;
    26  import org.mortbay.jetty.servlet.PathMap;
    27  import org.mortbay.log.Log;
    28  import org.mortbay.util.LazyList;
    29
    30  /* ------------------------------------------------------------ */
    31  /**
    32   *<p> The RewriteHandler is responsible for managing a list of rules to be applied to requests.
    33   * Its capabilities are not only limited to url rewrites such as RewritePatternRule or RewriteRegexRule;
    34   * there is also handling for cookies, headers, redirection, setting status or error codes
    35   * whenever the rule finds a match.
    36   *
    37   * <p> The rules can be matched by the ff. options: pattern matching of PathMap
    38   * (class PatternRule), regular expressions (class RegexRule) or certain conditions set
    39   * (e.g. MsieSslRule - the requests must be in SSL mode).
    40   *
    41   * Here are the list of rules:
    42   * <ul>
    43   * <li> CookiePatternRule - adds a new cookie in response. </li>
    44   * <li> HeaderPatternRule - adds/modifies the HTTP headers in response. </li>
    45   * <li> RedirectPatternRule - sets the redirect location. </li>
    46   * <li> ResponsePatternRule - sets the status/error codes. </li>
    47   * <li> RewritePatternRule - rewrites the requested URI. </li>
    48   * <li> RewriteRegexRule - rewrites the requested URI using regular expression for pattern matching. </li>
    49   * <li> MsieSslRule - disables the keep alive on SSL for IE5 and IE6. </li>
    50   * <li> LegacyRule - the old version of rewrite. </li>
    51   * <li> ForwardedSchemeHeaderRule - set the scheme according to the headers present. </li>
    52   * </ul>
    53   *
    54   * <p> The rules can be grouped into rule containers (class RuleContainerRule), and will only
    55   * be applied if the request matches the conditions for their container
    56   * (e.g., by virtual host name)
    57   *
    58   * Here are a list of rule containers:
    59   * <ul>
    60   * <li> VirtualHostRuleContainer - checks whether the request matches one of a set of virtual host names.</li>
    61   * <li> LowThreadsRuleContainer - checks whether the threadpool is low on threads</li>
    62   * </ul>
    63   *
    64   */
    65  public class RewriteHandler extends HandlerWrapper
    66  {
    67
    68      private RuleContainer _rules;
    69
    70      /* ------------------------------------------------------------ */
    71      public RewriteHandler()
    72      {
    73          _rules = new RuleContainer();
    74      }
    75
    76      /* ------------------------------------------------------------ */
    77      /**
    78       * To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
    79       * originalPathAttribute
    80       *
    81       * @param legacyRule old style rewrite rule
    82       */
    83      public void setLegacyRule(LegacyRule legacyRule)
    84      {
    85          _rules.setLegacyRule(legacyRule);
    86      }
    87
    88      /* ------------------------------------------------------------ */
    89      /**
    90       * Returns the list of rules.
    91       * @return an array of {@link Rule}.
    92       */
    93      public Rule[] getRules()
    94      {
    95          return _rules.getRules();
    96      }
    97
    98      /* ------------------------------------------------------------ */
    99      /**
    100      * Assigns the rules to process.
    101      * @param rules an array of {@link Rule}.
    102      */
    103     public void setRules(Rule[] rules)
    104     {
    105         _rules.setRules(rules);
    106     }
    107
    108     /*------------------------------------------------------------ */
    109     /**
    110      * Assigns the rules to process.
    111      * @param rules a {@link RuleContainer} containing other rules to process
    112      */
    113     public void setRules(RuleContainer rules)
    114     {
    115         _rules = rules;
    116     }
    117
    118     /* ------------------------------------------------------------ */
    119     /**
    120      * Add a Rule
    121      * @param rule The rule to add to the end of the rules array
    122      */
    123     public void addRule(Rule rule)
    124     {
    125         _rules.addRule(rule);
    126     }
    127
    128
    129     /* ------------------------------------------------------------ */
    130     /**
    131      * @return the rewriteRequestURI If true, this handler will rewrite the value
    132      * returned by {@link HttpServletRequest#getRequestURI()}.
    133      */
    134     public boolean isRewriteRequestURI()
    135     {
    136         return _rules.isRewriteRequestURI();
    137     }
    138
    139     /* ------------------------------------------------------------ */
    140     /**
    141      * @param rewriteRequestURI true if this handler will rewrite the value
    142      * returned by {@link HttpServletRequest#getRequestURI()}.
    143      */
    144     public void setRewriteRequestURI(boolean rewriteRequestURI)
    145     {
    146         _rules.setRewriteRequestURI(rewriteRequestURI);
    147     }
    148
    149     /* ------------------------------------------------------------ */
    150     /**
    151      * @return true if this handler will rewrite the value
    152      * returned by {@link HttpServletRequest#getPathInfo()}.
    153      */
    154     public boolean isRewritePathInfo()
    155     {
    156         return _rules.isRewritePathInfo();
    157     }
    158
    159     /* ------------------------------------------------------------ */
    160     /**
    161      * @param rewritePathInfo true if this handler will rewrite the value
    162      * returned by {@link HttpServletRequest#getPathInfo()}.
    163      */
    164     public void setRewritePathInfo(boolean rewritePathInfo)
    165     {
    166         _rules.setRewritePathInfo(rewritePathInfo);
    167     }
    168
    169     /* ------------------------------------------------------------ */
    170     /**
    171      * @return the originalPathAttribute. If non null, this string will be used
    172      * as the attribute name to store the original request path.
    173      */
    174     public String getOriginalPathAttribute()
    175     {
    176         return _rules.getOriginalPathAttribute();
    177     }
    178
    179     /* ------------------------------------------------------------ */
    180     /**
    181      * @param originalPathAttribute If non null, this string will be used
    182      * as the attribute name to store the original request path.
    183      */
    184     public void setOriginalPathAttribute(String originalPathAttribute)
    185     {
    186         _rules.setOriginalPathAttribute(originalPathAttribute);
    187     }
    188
    189
    190     /* ------------------------------------------------------------ */
    191     /**
    192      * @deprecated
    193      */
    194     public PathMap getRewrite()
    195     {
    196         return _rules.getRewrite();
    197     }
    198
    199     /* ------------------------------------------------------------ */
    200     /**
    201      * @deprecated
    202      */
    203     public void setRewrite(PathMap rewrite)
    204     {
    205         _rules.setRewrite(rewrite);
    206     }
    207
    208     /* ------------------------------------------------------------ */
    209     /**
    210      * @deprecated
    211      */
    212     public void addRewriteRule(String pattern, String prefix)
    213     {
    214         _rules.addRewriteRule(pattern,prefix);
    215     }
    216
    217     /* ------------------------------------------------------------ */
    218     /* (non-Javadoc)
    219      * @see org.mortbay.jetty.handler.HandlerWrapper#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
    220      */
    221     public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException
    222     {
    223         if (isStarted())
    224         {
    225             String returned = _rules.matchAndApply(target, request, response);
    226             target = (returned == null) ? target : returned;
    227
    228             if (!_rules.isHandled())
    229             {
    230                 super.handle(target, request, response, dispatch);
    231             }
    232         }
    233     }
    234
    235 }