From 4d7ea8127efe477a9f15099953046281703c3bff Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 11 Dec 2014 13:47:07 +0100 Subject: [PATCH] Revert "453829 removed code with yahoo copyright" This reverts commit 65c7b621f62ee1ecae27739f5fbf1df235bb676b. The yahoo copyright messages were removed by the contributor in the bugzilla/pullrequest --- .../rewrite/handler/HeaderRegexRule.java | 128 +++++++++++++++++ .../rewrite/handler/HeaderRegexRuleTest.java | 131 ++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java create mode 100644 jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRuleTest.java diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java new file mode 100644 index 00000000000..87d7c179abf --- /dev/null +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java @@ -0,0 +1,128 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.rewrite.handler; + +import java.io.IOException; +import java.util.regex.Matcher; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +/* ------------------------------------------------------------ */ +/** Rule to add a header based on a Regex match + */ +public class HeaderRegexRule extends RegexRule +{ + + private String _name; + private String _value; + private boolean _add=false; + + /* ------------------------------------------------------------ */ + public HeaderRegexRule() + { + _handling = false; + _terminating = false; + } + + /* ------------------------------------------------------------ */ + /** + * Sets the header name. + * + * @param name name of the header field + */ + public void setName(String name) + { + _name = name; + } + + /* ------------------------------------------------------------ */ + /** + * Sets the header value. The value can be either a String or int value. + * + * @param value of the header field + */ + public void setValue(String value) + { + _value = value; + } + + /* ------------------------------------------------------------ */ + /** + * Sets the Add flag. + * @param add If true, the header is added to the response, otherwise the header it is set on the response. + */ + public void setAdd(boolean add) + { + _add = add; + } + + /* ------------------------------------------------------------ */ + @Override + protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) + throws IOException + { + // process header + if (_add) + response.addHeader(_name, _value); + else + response.setHeader(_name, _value); + return target; + } + + /* ------------------------------------------------------------ */ + /** + * Returns the header name. + * @return the header name. + */ + public String getName() + { + return _name; + } + + /* ------------------------------------------------------------ */ + /** + * Returns the header value. + * @return the header value. + */ + public String getValue() + { + return _value; + } + + /* ------------------------------------------------------------ */ + /** + * Returns the add flag value. + */ + public boolean isAdd() + { + return _add; + } + + /* ------------------------------------------------------------ */ + /** + * Returns the header contents. + */ + @Override + public String toString() + { + return super.toString()+"["+_name+","+_value+"]"; + } +} diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRuleTest.java new file mode 100644 index 00000000000..0edfde24c2c --- /dev/null +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRuleTest.java @@ -0,0 +1,131 @@ +// +// ======================================================================== +// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.rewrite.handler; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.util.Iterator; + +public class HeaderRegexRuleTest extends AbstractRuleTestCase +{ + + private HeaderRegexRule _rule; + + @Before + public void init() throws Exception + { + start(false); + _rule = new HeaderRegexRule(); + _rule.setRegex("\\*"); + } + + @Test + public void testHeaderWithTextValues() throws IOException + { + // different keys + String headers[][] = + { + { "hnum#1", "test1" }, + { "hnum#2", "2test2" }, + { "hnum#3", "test3" } }; + assertHeaders(headers); + } + + @Test + public void testHeaderWithNumberValues() throws IOException + { + String headers[][] = + { + { "hello", "1" }, + { "hello", "-1" }, + { "hello", "100" }, + { "hello", "100" }, + { "hello", "100" }, + { "hello", "100" }, + { "hello", "100" }, + { "hello1", "200" } }; + assertHeaders(headers); + } + + @Test + public void testHeaderOverwriteValues() throws IOException + { + String headers[][] = + { + { "size", "100" }, + { "size", "200" }, + { "size", "300" }, + { "size", "400" }, + { "size", "500" }, + { "title", "abc" }, + { "title", "bac" }, + { "title", "cba" }, + { "title1", "abba" }, + { "title1", "abba1" }, + { "title1", "abba" }, + { "title1", "abba1" } }; + assertHeaders(headers); + Iterator e = _response.getHeaders("size").iterator(); + int count = 0; + while (e.hasNext()) + { + e.next(); + count++; + } + assertEquals(1,count); + assertEquals("500",_response.getHeader("size")); + assertEquals("cba",_response.getHeader("title")); + assertEquals("abba1",_response.getHeader("title1")); + } + + @Test + public void testMatch() throws Exception + { + _rule.setRegex("/my/dir/file/(.*)$"); + _rule.setName("cache-control"); + _rule.setValue("no-store"); + _rule.matchAndApply("/my/dir/file/",_request,_response); + assertEquals("no-store",_response.getHeader("cache-control")); + } + + @Test + public void testNotMatch() throws Exception + { + reset(); + _rule.setRegex("/my/dir/file/(.*)$"); + _rule.setName("cache-control"); + _rule.setValue("no-store"); + _rule.matchAndApply("/my/dir/file_not_match/",_request,_response); + assertEquals(null,_response.getHeader("cache-control")); + } + + private void assertHeaders(String headers[][]) throws IOException + { + for (String[] header : headers) + { + _rule.setName(header[0]); + _rule.setValue(header[1]); + _rule.apply(null,_request,_response,null); + assertEquals(header[1],_response.getHeader(header[0])); + } + } +}