View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.rewrite.handler;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  /**
27   * Abstract rule that matches against request headers.
28   */
29  
30  public abstract class HeaderRule extends Rule
31  {
32      private String _header;
33      private String _headerValue;
34  
35      /* ------------------------------------------------------------ */
36      public String getHeader()
37      {
38          return _header;
39      }
40  
41      /* ------------------------------------------------------------ */
42      /**
43       * @param header
44       *                the header name to check for
45       */
46      public void setHeader(String header)
47      {
48          _header = header;
49      }
50  
51      /* ------------------------------------------------------------ */
52      public String getHeaderValue()
53      {
54          return _headerValue;
55      }
56  
57      /* ------------------------------------------------------------ */
58      /**
59       * @param headerValue
60       *                the header value to match against. If null, then the
61       *                presence of the header is enough to match
62       */
63      public void setHeaderValue(String headerValue)
64      {
65          _headerValue = headerValue;
66      }
67  
68      /* ------------------------------------------------------------ */
69      @Override
70      public String matchAndApply(String target, HttpServletRequest request,
71              HttpServletResponse response) throws IOException
72      {
73          String requestHeaderValue = request.getHeader(_header);
74          
75          if (requestHeaderValue != null)
76              if (_headerValue == null || _headerValue.equals(requestHeaderValue))
77                  apply(target, requestHeaderValue, request, response);
78          
79          return null;
80      }
81  
82      /* ------------------------------------------------------------ */
83      /**
84       * Apply the rule to the request
85       * 
86       * @param target
87       *                field to attempt match
88       * @param value 
89       *                header value found
90       * @param request
91       *                request object
92       * @param response
93       *                response object
94       * @return The target (possible updated)
95       * @throws IOException
96       *                 exceptions dealing with operating on request or response
97       *                 objects
98       */
99      protected abstract String apply(String target, String value, HttpServletRequest request, HttpServletResponse response) throws IOException;
100 
101     /* ------------------------------------------------------------ */
102     @Override
103     public String toString()
104     {
105         return super.toString() + "[" + _header + ":" + _headerValue + "]";
106     }
107 
108 }