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