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  /**
28   * Sets the header in the response whenever the rule finds a match.
29   */
30  public class HeaderPatternRule extends PatternRule
31  {
32      private String _name;
33      private String _value;
34      private boolean _add=false;
35  
36      /* ------------------------------------------------------------ */
37      public HeaderPatternRule()
38      {
39          _handling = false;
40          _terminating = false;
41      }
42  
43      /* ------------------------------------------------------------ */
44      /**
45       * Sets the header name.
46       * 
47       * @param name name of the header field
48       */
49      public void setName(String name)
50      {
51          _name = name;
52      }
53  
54      /* ------------------------------------------------------------ */
55      /**
56       * Sets the header value. The value can be either a <code>String</code> or <code>int</code> value.
57       * 
58       * @param value of the header field
59       */
60      public void setValue(String value)
61      {
62          _value = value;
63      }
64  
65      /* ------------------------------------------------------------ */
66      /**
67       * Sets the Add flag. 
68       * @param add If true, the header is added to the response, otherwise the header it is set on the response.
69       */
70      public void setAdd(boolean add)
71      {
72          _add = add;
73      }
74  
75      /* ------------------------------------------------------------ */
76      /**
77       * Invokes this method when a match found. If the header had already been set, 
78       * the new value overwrites the previous one. Otherwise, it adds the new 
79       * header name and value.
80       * 
81       *@see org.eclipse.jetty.rewrite.handler.Rule#matchAndApply(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
82       */
83      @Override
84      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
85      {
86          // process header
87          if (_add)
88              response.addHeader(_name, _value);
89          else
90              response.setHeader(_name, _value); 
91          return target;
92      }
93      
94      
95  
96      /* ------------------------------------------------------------ */
97      /**
98       * Returns the header name.
99       * @return the header name.
100      */
101     public String getName()
102     {
103         return _name;
104     }
105 
106     /* ------------------------------------------------------------ */
107     /**
108      * Returns the header value.
109      * @return the header value.
110      */
111     public String getValue()
112     {
113         return _value;
114     }
115 
116     /* ------------------------------------------------------------ */
117     /**
118      * Returns the add flag value.
119      * @return true if add flag set
120      */
121     public boolean isAdd()
122     {
123         return _add;
124     }
125 
126     /* ------------------------------------------------------------ */
127     /**
128      * Returns the header contents.
129      */
130     @Override
131     public String toString()
132     {
133         return super.toString()+"["+_name+","+_value+"]";
134     }
135 }