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  import java.util.regex.Matcher;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.util.annotation.Name;
28  
29  
30  /* ------------------------------------------------------------ */
31  /** Rule to add a header based on a Regex match
32   */
33  public class HeaderRegexRule extends RegexRule
34  {
35      private String _name;
36      private String _value;
37      private boolean _add=false;
38  
39      /* ------------------------------------------------------------ */
40      public HeaderRegexRule()
41      {
42          this(null,null,null);
43      }
44  
45      /* ------------------------------------------------------------ */
46      public HeaderRegexRule(@Name("regex") String regex, @Name("name") String name, @Name("value") String value)
47      {
48          super(regex);
49          setHandling(false);
50          setTerminating(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      @Override
89      protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
90              throws IOException 
91      {
92          // process header
93          if (_add)
94              response.addHeader(_name, _value);
95          else
96              response.setHeader(_name, _value); 
97          return target;
98      }
99      
100     /* ------------------------------------------------------------ */
101     /**
102      * Returns the header name.
103      * @return the header name.
104      */
105     public String getName()
106     {
107         return _name;
108     }
109 
110     /* ------------------------------------------------------------ */
111     /**
112      * Returns the header value.
113      * @return the header value.
114      */
115     public String getValue()
116     {
117         return _value;
118     }
119 
120     /* ------------------------------------------------------------ */
121     /**
122      * @return the add flag value.
123      */
124     public boolean isAdd()
125     {
126         return _add;
127     }
128 
129     /* ------------------------------------------------------------ */
130     /**
131      * @return the header contents.
132      */
133     @Override
134     public String toString()
135     {
136         return super.toString()+"["+_name+","+_value+"]";
137     }
138 }