1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at 
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.mortbay.jetty.handler.rewrite;
16  
17  import java.io.IOException;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  /**
23   * Abstract rule that matches against request headers.
24   */
25  
26  public abstract class HeaderRule extends Rule
27  {
28      private String _header;
29      private String _headerValue;
30  
31      /* ------------------------------------------------------------ */
32      public String getHeader()
33      {
34          return _header;
35      }
36  
37      /* ------------------------------------------------------------ */
38      /**
39       * @param header
40       *                the header name to check for
41       */
42      public void setHeader(String header)
43      {
44          _header = header;
45      }
46  
47      /* ------------------------------------------------------------ */
48      public String getHeaderValue()
49      {
50          return _headerValue;
51      }
52  
53      /* ------------------------------------------------------------ */
54      /**
55       * @param headerValue
56       *                the header value to match against. If null, then the
57       *                presence of the header is enough to match
58       */
59      public void setHeaderValue(String headerValue)
60      {
61          _headerValue = headerValue;
62      }
63  
64      /* ------------------------------------------------------------ */
65      @Override
66      public String matchAndApply(String target, HttpServletRequest request,
67              HttpServletResponse response) throws IOException
68      {
69          String requestHeaderValue = request.getHeader(_header);
70          
71          if (requestHeaderValue != null)
72              if (_headerValue == null || _headerValue.equals(requestHeaderValue))
73                  apply(target, requestHeaderValue, request, response);
74          
75          return null;
76      }
77  
78      /* ------------------------------------------------------------ */
79      /**
80       * Apply the rule to the request
81       * 
82       * @param target
83       *                field to attempt match
84       * @param value 
85       *                header value found
86       * @param request
87       *                request object
88       * @param response
89       *                response object
90       * @return The target (possible updated)
91       * @throws IOException
92       *                 exceptions dealing with operating on request or response
93       *                 objects
94       */
95      protected abstract String apply(String target, String value, HttpServletRequest request, HttpServletResponse response) throws IOException;
96  
97      /* ------------------------------------------------------------ */
98      public String toString()
99      {
100         return super.toString() + "[" + _header + ":" + _headerValue + "]";
101     }
102 
103 }