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