1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 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 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 84 { 85 // process header 86 if (_add) 87 response.addHeader(_name, _value); 88 else 89 response.setHeader(_name, _value); 90 return target; 91 } 92 93 94 95 /* ------------------------------------------------------------ */ 96 /** 97 * Returns the header name. 98 * @return the header name. 99 */ 100 public String getName() 101 { 102 return _name; 103 } 104 105 /* ------------------------------------------------------------ */ 106 /** 107 * Returns the header value. 108 * @return the header value. 109 */ 110 public String getValue() 111 { 112 return _value; 113 } 114 115 /* ------------------------------------------------------------ */ 116 /** 117 * Returns the add flag value. 118 */ 119 public boolean isAdd() 120 { 121 return _add; 122 } 123 124 /* ------------------------------------------------------------ */ 125 /** 126 * Returns the header contents. 127 */ 128 public String toString() 129 { 130 return super.toString()+"["+_name+","+_value+"]"; 131 } 132 }