View Javadoc

1   //========================================================================
2   //$Id: HeaderPatternRule.java 966 2008-04-17 13:53:44Z gregw $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
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  /**
24   * Sets the header in the response whenever the rule finds a match.
25   */
26  public class HeaderPatternRule extends PatternRule
27  {
28      private String _name;
29      private String _value;
30      private boolean _add=false;
31  
32      /* ------------------------------------------------------------ */
33      public HeaderPatternRule()
34      {
35          _handling = false;
36          _terminating = false;
37      }
38  
39      /* ------------------------------------------------------------ */
40      /**
41       * Sets the header name.
42       * 
43       * @param name name of the header field
44       */
45      public void setName(String name)
46      {
47          _name = name;
48      }
49  
50      /* ------------------------------------------------------------ */
51      /**
52       * Sets the header value. The value can be either a <code>String</code> or <code>int</code> value.
53       * 
54       * @param value of the header field
55       */
56      public void setValue(String value)
57      {
58          _value = value;
59      }
60  
61      /* ------------------------------------------------------------ */
62      /**
63       * Sets the Add flag. 
64       * @param add If true, the header is added to the response, otherwise the header it is set on the response.
65       */
66      public void setAdd(boolean add)
67      {
68          _add = add;
69      }
70  
71      /* ------------------------------------------------------------ */
72      /**
73       * Invokes this method when a match found. If the header had already been set, 
74       * the new value overwrites the previous one. Otherwise, it adds the new 
75       * header name and value.
76       * 
77       *@see org.mortbay.jetty.handler.rewrite.Rule#matchAndApply(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
78       */
79      public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
80      {
81          // process header
82          if (_add)
83              response.addHeader(_name, _value);
84          else
85              response.setHeader(_name, _value); 
86          return target;
87      }
88      
89      
90  
91      /* ------------------------------------------------------------ */
92      /**
93       * Returns the header name.
94       * @return the header name.
95       */
96      public String getName()
97      {
98          return _name;
99      }
100 
101     /* ------------------------------------------------------------ */
102     /**
103      * Returns the header value.
104      * @return the header value.
105      */
106     public String getValue()
107     {
108         return _value;
109     }
110 
111     /* ------------------------------------------------------------ */
112     /**
113      * Returns the add flag value.
114      */
115     public boolean isAdd()
116     {
117         return _add;
118     }
119 
120     /* ------------------------------------------------------------ */
121     /**
122      * Returns the header contents.
123      */
124     public String toString()
125     {
126         return super.toString()+"["+_name+","+_value+"]";
127     }
128 }