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