View Javadoc

    1   package org.mortbay.jetty.handler.rewrite;
    2
    3   import java.io.IOException;
    4
    5   import javax.servlet.http.HttpServletRequest;
    6   import javax.servlet.http.HttpServletResponse;
    7
    8   import org.mortbay.jetty.HttpConnection;
    9   import org.mortbay.jetty.Request;
    10  import org.mortbay.jetty.servlet.PathMap;
    11  import org.mortbay.log.Log;
    12  import org.mortbay.util.LazyList;
    13
    14  /**
    15   * Base container to group rules. Can be extended so that the contained rules
    16   * will only be applied under certain conditions
    17   *
    18   * @author Athena Yao
    19   */
    20
    21  public class RuleContainer extends Rule
    22  {
    23      protected Rule[] _rules;
    24      protected boolean _handled;
    25
    26      protected String _originalPathAttribute;
    27      protected boolean _rewriteRequestURI=true;
    28      protected boolean _rewritePathInfo=true;
    29
    30      protected LegacyRule _legacy;
    31
    32      /* ------------------------------------------------------------ */
    33      private LegacyRule getLegacyRule()
    34      {
    35          if (_legacy==null)
    36          {
    37              _legacy= new LegacyRule();
    38              addRule(_legacy);
    39          }
    40          return _legacy;
    41      }
    42
    43
    44      /* ------------------------------------------------------------ */
    45      /**
    46       * To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
    47       * originalPathAttribute
    48       *
    49       * @param legacyRule old style rewrite rule
    50       */
    51      public void setLegacyRule(LegacyRule legacyRule)
    52      {
    53          _legacy = legacyRule;
    54      }
    55
    56      /* ------------------------------------------------------------ */
    57      /**
    58       * Returns the list of rules.
    59       * @return an array of {@link Rule}.
    60       */
    61      public Rule[] getRules()
    62      {
    63          return _rules;
    64      }
    65
    66      /* ------------------------------------------------------------ */
    67      /**
    68       * Assigns the rules to process.
    69       * @param rules an array of {@link Rule}.
    70       */
    71      public void setRules(Rule[] rules)
    72      {
    73          if (_legacy==null)
    74              _rules = rules;
    75          else
    76          {
    77              _rules=null;
    78              addRule(_legacy);
    79              if (rules!=null)
    80                  for (Rule rule:rules)
    81                      addRule(rule);
    82          }
    83      }
    84
    85      /* ------------------------------------------------------------ */
    86      /**
    87       * Add a Rule
    88       * @param rule The rule to add to the end of the rules array
    89       */
    90      public void addRule(Rule rule)
    91      {
    92          _rules = (Rule[])LazyList.addToArray(_rules,rule,Rule.class);
    93      }
    94
    95
    96      /* ------------------------------------------------------------ */
    97      /**
    98       * @return the rewriteRequestURI If true, this handler will rewrite the value
    99       * returned by {@link HttpServletRequest#getRequestURI()}.
    100      */
    101     public boolean isRewriteRequestURI()
    102     {
    103         return _rewriteRequestURI;
    104     }
    105
    106     /* ------------------------------------------------------------ */
    107     /**
    108      * @param rewriteRequestURI true if this handler will rewrite the value
    109      * returned by {@link HttpServletRequest#getRequestURI()}.
    110      */
    111     public void setRewriteRequestURI(boolean rewriteRequestURI)
    112     {
    113         _rewriteRequestURI=rewriteRequestURI;
    114     }
    115
    116     /* ------------------------------------------------------------ */
    117     /**
    118      * @return true if this handler will rewrite the value
    119      * returned by {@link HttpServletRequest#getPathInfo()}.
    120      */
    121     public boolean isRewritePathInfo()
    122     {
    123         return _rewritePathInfo;
    124     }
    125
    126     /* ------------------------------------------------------------ */
    127     /**
    128      * @param rewritePathInfo true if this handler will rewrite the value
    129      * returned by {@link HttpServletRequest#getPathInfo()}.
    130      */
    131     public void setRewritePathInfo(boolean rewritePathInfo)
    132     {
    133         _rewritePathInfo=rewritePathInfo;
    134     }
    135
    136     /* ------------------------------------------------------------ */
    137     /**
    138      * @return the originalPathAttribte. If non null, this string will be used
    139      * as the attribute name to store the original request path.
    140      */
    141     public String getOriginalPathAttribute()
    142     {
    143         return _originalPathAttribute;
    144     }
    145
    146     /* ------------------------------------------------------------ */
    147     /**
    148      * @param originalPathAttribte If non null, this string will be used
    149      * as the attribute name to store the original request path.
    150      */
    151     public void setOriginalPathAttribute(String originalPathAttribte)
    152     {
    153         _originalPathAttribute=originalPathAttribte;
    154     }
    155
    156
    157     /* ------------------------------------------------------------ */
    158     /**
    159      * @deprecated
    160      */
    161     public PathMap getRewrite()
    162     {
    163         return getLegacyRule().getRewrite();
    164     }
    165
    166     /* ------------------------------------------------------------ */
    167     /**
    168      * @deprecated
    169      */
    170     public void setRewrite(PathMap rewrite)
    171     {
    172         getLegacyRule().setRewrite(rewrite);
    173     }
    174
    175     /* ------------------------------------------------------------ */
    176     /**
    177      * @deprecated
    178      */
    179     public void addRewriteRule(String pattern, String prefix)
    180     {
    181         getLegacyRule().addRewriteRule(pattern,prefix);
    182     }
    183
    184
    185     /**
    186      * @see http://jira.codehaus.org/browse/JETTY-1287
    187      *
    188      * @return handled true if one of the rules within the rule container is handling the request
    189      *
    190      * @deprecated not thread safe, better to rely on baseRequest.isHandled()
    191      */
    192     public boolean isHandled()
    193     {
    194         return _handled;
    195     }
    196
    197     /*------------------------------------------------------------ */
    198     /**
    199      * @param handled true if one of the rules within the rule container is handling the request
    200      *
    201      * @deprecated best to use the baseRequest.isHandled()
    202      */
    203     public void setHandled(boolean handled)
    204     {
    205         _handled=handled;
    206     }
    207
    208
    209     /**
    210      * Process the contained rules
    211      * @param target target field to pass on to the contained rules
    212      * @param request request object to pass on to the contained rules
    213      * @param response response object to pass on to the contained rules
    214      */
    215     @Override
    216     public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    217     {
    218         return apply(target, request, response);
    219     }
    220
    221     /**
    222      * Process the contained rules (called by matchAndApply)
    223      * @param target target field to pass on to the contained rules
    224      * @param request request object to pass on to the contained rules
    225      * @param response response object to pass on to the contained rules
    226      */
    227     protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    228     {
    229         _handled=false;
    230
    231         boolean original_set=_originalPathAttribute==null;
    232
    233         for (Rule rule : _rules)
    234         {
    235             String applied=rule.matchAndApply(target,request, response);
    236             if (applied!=null)
    237             {
    238                 Log.debug("applied {}",rule);
    239                 if (!target.equals(applied))
    240                 {
    241                     Log.debug("rewrote {} to {}",target,applied);
    242                     if (!original_set)
    243                     {
    244                         original_set=true;
    245                         request.setAttribute(_originalPathAttribute, target);
    246                     }
    247
    248                     if (_rewriteRequestURI)
    249                         ((Request)request).setRequestURI(applied);
    250
    251                     if (_rewritePathInfo)
    252                         ((Request)request).setPathInfo(applied);
    253
    254                     target=applied;
    255                 }
    256
    257                 if (rule.isHandling())
    258                 {
    259                     Log.debug("handling {}",rule);
    260                     _handled=true;// leaving for historical purposes - http://jira.codehaus.org/browse/JETTY-1287
    261                     (request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest()).setHandled(true);
    262                 }
    263
    264                 if (rule.isTerminating())
    265                 {
    266                     Log.debug("terminating {}",rule);
    267                     break;
    268                 }
    269             }
    270         }
    271
    272         return target;
    273     }
    274 }