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   /**
    9    * Abstract rule that matches against request headers.
    10   */
    11
    12  public abstract class HeaderRule extends Rule
    13  {
    14      private String _header;
    15      private String _headerValue;
    16
    17      /* ------------------------------------------------------------ */
    18      public String getHeader()
    19      {
    20          return _header;
    21      }
    22
    23      /* ------------------------------------------------------------ */
    24      /**
    25       * @param header
    26       *                the header name to check for
    27       */
    28      public void setHeader(String header)
    29      {
    30          _header = header;
    31      }
    32
    33      /* ------------------------------------------------------------ */
    34      public String getHeaderValue()
    35      {
    36          return _headerValue;
    37      }
    38
    39      /* ------------------------------------------------------------ */
    40      /**
    41       * @param headerValue
    42       *                the header value to match against. If null, then the
    43       *                presence of the header is enough to match
    44       */
    45      public void setHeaderValue(String headerValue)
    46      {
    47          _headerValue = headerValue;
    48      }
    49
    50      /* ------------------------------------------------------------ */
    51      @Override
    52      public String matchAndApply(String target, HttpServletRequest request,
    53              HttpServletResponse response) throws IOException
    54      {
    55          String requestHeaderValue = request.getHeader(_header);
    56
    57          if (requestHeaderValue != null)
    58              if (_headerValue == null || _headerValue.equals(requestHeaderValue))
    59                  apply(target, requestHeaderValue, request, response);
    60
    61          return null;
    62      }
    63
    64      /* ------------------------------------------------------------ */
    65      /**
    66       * Apply the rule to the request
    67       *
    68       * @param target
    69       *                field to attempt match
    70       * @param value
    71       *                header value found
    72       * @param request
    73       *                request object
    74       * @param response
    75       *                response object
    76       * @return The target (possible updated)
    77       * @throws IOException
    78       *                 exceptions dealing with operating on request or response
    79       *                 objects
    80       */
    81      protected abstract String apply(String target, String value, HttpServletRequest request, HttpServletResponse response) throws IOException;
    82
    83      /* ------------------------------------------------------------ */
    84      public String toString()
    85      {
    86          return super.toString() + "[" + _header + ":" + _headerValue + "]";
    87      }
    88
    89  }