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      * @return handled true if one of the rules within the rule container is handling the request
    187      */
    188     public boolean isHandled()
    189     {
    190         return _handled;
    191     }
    192
    193     /*------------------------------------------------------------ */
    194     /**
    195      * @param handled true if one of the rules within the rule container is handling the request
    196      */
    197     public void setHandled(boolean handled)
    198     {
    199         _handled=handled;
    200     }
    201
    202
    203     /**
    204      * Process the contained rules
    205      * @param target target field to pass on to the contained rules
    206      * @param request request object to pass on to the contained rules
    207      * @param response response object to pass on to the contained rules
    208      */
    209     @Override
    210     public String matchAndApply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    211     {
    212         return apply(target, request, response);
    213     }
    214
    215     /**
    216      * Process the contained rules (called by matchAndApply)
    217      * @param target target field to pass on to the contained rules
    218      * @param request request object to pass on to the contained rules
    219      * @param response response object to pass on to the contained rules
    220      */
    221     protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
    222     {
    223         _handled=false;
    224
    225         boolean original_set=_originalPathAttribute==null;
    226
    227         for (Rule rule : _rules)
    228         {
    229             String applied=rule.matchAndApply(target,request, response);
    230             if (applied!=null)
    231             {
    232                 Log.debug("applied {}",rule);
    233                 if (!target.equals(applied))
    234                 {
    235                     Log.debug("rewrote {} to {}",target,applied);
    236                     if (!original_set)
    237                     {
    238                         original_set=true;
    239                         request.setAttribute(_originalPathAttribute, target);
    240                     }
    241
    242                     if (_rewriteRequestURI)
    243                         ((Request)request).setRequestURI(applied);
    244
    245                     if (_rewritePathInfo)
    246                         ((Request)request).setPathInfo(applied);
    247
    248                     target=applied;
    249                 }
    250
    251                 if (rule.isHandling())
    252                 {
    253                     Log.debug("handling {}",rule);
    254                     _handled=true;
    255                     (request instanceof Request?(Request)request:HttpConnection.getCurrentConnection().getRequest()).setHandled(true);
    256                 }
    257
    258                 if (rule.isTerminating())
    259                 {
    260                     Log.debug("terminating {}",rule);
    261                     break;
    262                 }
    263             }
    264         }
    265
    266         return target;
    267     }
    268 }