1 // ======================================================================== 2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd. 3 // ------------------------------------------------------------------------ 4 // All rights reserved. This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v1.0 6 // and Apache License v2.0 which accompanies this distribution. 7 // The Eclipse Public License is available at 8 // http://www.eclipse.org/legal/epl-v10.html 9 // The Apache License v2.0 is available at 10 // http://www.opensource.org/licenses/apache2.0.php 11 // You may elect to redistribute this code under either of these licenses. 12 // ======================================================================== 13 package org.eclipse.jetty.rewrite.handler; 14 15 import java.io.IOException; 16 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 20 21 /** 22 * Sets the header in the response whenever the rule finds a match. 23 */ 24 public class HeaderPatternRule extends PatternRule 25 { 26 private String _name; 27 private String _value; 28 private boolean _add=false; 29 30 /* ------------------------------------------------------------ */ 31 public HeaderPatternRule() 32 { 33 _handling = false; 34 _terminating = false; 35 } 36 37 /* ------------------------------------------------------------ */ 38 /** 39 * Sets the header name. 40 * 41 * @param name name of the header field 42 */ 43 public void setName(String name) 44 { 45 _name = name; 46 } 47 48 /* ------------------------------------------------------------ */ 49 /** 50 * Sets the header value. The value can be either a <code>String</code> or <code>int</code> value. 51 * 52 * @param value of the header field 53 */ 54 public void setValue(String value) 55 { 56 _value = value; 57 } 58 59 /* ------------------------------------------------------------ */ 60 /** 61 * Sets the Add flag. 62 * @param add If true, the header is added to the response, otherwise the header it is set on the response. 63 */ 64 public void setAdd(boolean add) 65 { 66 _add = add; 67 } 68 69 /* ------------------------------------------------------------ */ 70 /** 71 * Invokes this method when a match found. If the header had already been set, 72 * the new value overwrites the previous one. Otherwise, it adds the new 73 * header name and value. 74 * 75 *@see org.eclipse.jetty.rewrite.handler.Rule#matchAndApply(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) 76 */ 77 public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException 78 { 79 // process header 80 if (_add) 81 response.addHeader(_name, _value); 82 else 83 response.setHeader(_name, _value); 84 return target; 85 } 86 87 88 89 /* ------------------------------------------------------------ */ 90 /** 91 * Returns the header name. 92 * @return the header name. 93 */ 94 public String getName() 95 { 96 return _name; 97 } 98 99 /* ------------------------------------------------------------ */ 100 /** 101 * Returns the header value. 102 * @return the header value. 103 */ 104 public String getValue() 105 { 106 return _value; 107 } 108 109 /* ------------------------------------------------------------ */ 110 /** 111 * Returns the add flag value. 112 */ 113 public boolean isAdd() 114 { 115 return _add; 116 } 117 118 /* ------------------------------------------------------------ */ 119 /** 120 * Returns the header contents. 121 */ 122 public String toString() 123 { 124 return super.toString()+"["+_name+","+_value+"]"; 125 } 126 }