diff options
author | Joakim Erdfelt | 2015-07-23 16:11:20 +0000 |
---|---|---|
committer | Joakim Erdfelt | 2015-07-23 16:11:20 +0000 |
commit | 6cea37aec977c0a7dbfca83f19fab4640b902374 (patch) | |
tree | a0b40816fd84f97edec0f35cd61e699081697e9d /jetty-rewrite | |
parent | 32e63eb0fdc78f318d0940986498a16e4b08b536 (diff) | |
download | org.eclipse.jetty.project-6cea37aec977c0a7dbfca83f19fab4640b902374.tar.gz org.eclipse.jetty.project-6cea37aec977c0a7dbfca83f19fab4640b902374.tar.xz org.eclipse.jetty.project-6cea37aec977c0a7dbfca83f19fab4640b902374.zip |
473319 - Parameterize status code on Redirect Rules for alternate use
+ Both RedirectRegexRule and RedirectPatternRule support any 3xx
redirection status code.
+ Deleted MovedPermanently Rules in favor of this more centralized
approach.
Diffstat (limited to 'jetty-rewrite')
8 files changed, 142 insertions, 286 deletions
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRule.java deleted file mode 100644 index 73a513babc..0000000000 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRule.java +++ /dev/null @@ -1,40 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 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 javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.rewrite.handler.RedirectPatternRule; - -/** - * Issues a 301 Moved Permanently Redirects to location if pattern matches - */ -public class MovedPermanentlyPatternRule extends RedirectPatternRule -{ - @Override - public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException - { - response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - response.setHeader("Location",response.encodeRedirectURL(_location)); - return target; - } -}
\ No newline at end of file diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRule.java deleted file mode 100644 index 0f463a8ef8..0000000000 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRule.java +++ /dev/null @@ -1,46 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 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; - -/** - * Issues a 301 Moved Permanently Redirects to location if pattern matches - */ -public class MovedPermanentlyRegexRule extends RedirectRegexRule -{ - @Override - protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException - { - target = _replacement; - for (int g = 1; g <= matcher.groupCount(); g++) - { - String group = matcher.group(g); - target = target.replaceAll("\\$" + g,group); - } - - response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - response.setHeader("Location",response.encodeRedirectURL(target)); - return target; - } -}
\ No newline at end of file diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java index 08223f2e4d..5735012814 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java @@ -23,21 +23,26 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpStatus; + /** - * Redirects the response whenever the rule finds a match. + * Issues a (3xx) Redirect response whenever the rule finds a match. + * <p> + * All redirects are part of the <a href="http://tools.ietf.org/html/rfc7231#section-6.4"><code>3xx Redirection</code> status code set</a>. + * <p> + * Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3"><code>302 Found</code></a> */ public class RedirectPatternRule extends PatternRule { - protected String _location; - - /* ------------------------------------------------------------ */ + private String _location; + private int _statusCode = HttpStatus.FOUND_302; + public RedirectPatternRule() { _handling = true; _terminating = true; } - /* ------------------------------------------------------------ */ /** * Sets the redirect location. * @@ -47,26 +52,43 @@ public class RedirectPatternRule extends PatternRule { _location = value; } - - /* ------------------------------------------------------------ */ - /* - * (non-Javadoc) - * @see org.eclipse.jetty.server.server.handler.rules.RuleBase#apply(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + + /** + * Sets the redirect status code. + * + * @param statusCode the 3xx redirect status code */ + public void setStatusCode(int statusCode) + { + if ((300 <= statusCode) || (statusCode >= 399)) + { + _statusCode = statusCode; + } + else + { + throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)"); + } + } + @Override public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException { - response.sendRedirect(response.encodeRedirectURL(_location)); + response.setHeader("Location",response.encodeRedirectURL(_location)); + response.sendError(_statusCode); return target; } - /* ------------------------------------------------------------ */ /** - * Returns the redirect location. + * Returns the redirect status code and location. */ @Override public String toString() { - return super.toString()+"["+_location+"]"; + StringBuilder str = new StringBuilder(); + str.append(super.toString()); + str.append('[').append(_statusCode); + str.append('>').append(_location); + str.append(']'); + return str.toString(); } } diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java index b1a20e8ce5..aca659eab1 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java @@ -24,13 +24,21 @@ import java.util.regex.Matcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.http.HttpStatus; + /** - * Redirects the response by matching with a regular expression. + * Issues a (3xx) Redirect response whenever the rule finds a match via regular expression. + * <p> * The replacement string may use $n" to replace the nth capture group. + * <p> + * All redirects are part of the <a href="http://tools.ietf.org/html/rfc7231#section-6.4"><code>3xx Redirection</code> status code set</a>. + * <p> + * Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3"><code>302 Found</code></a> */ public class RedirectRegexRule extends RegexRule { protected String _replacement; + private int _statusCode = HttpStatus.FOUND_302; public RedirectRegexRule() { @@ -48,6 +56,23 @@ public class RedirectRegexRule extends RegexRule _replacement = replacement; } + /** + * Sets the redirect status code. + * + * @param statusCode the 3xx redirect status code + */ + public void setStatusCode(int statusCode) + { + if ((300 <= statusCode) || (statusCode >= 399)) + { + _statusCode = statusCode; + } + else + { + throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)"); + } + } + @Override protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException @@ -59,7 +84,23 @@ public class RedirectRegexRule extends RegexRule target=target.replaceAll("\\$"+g,group); } - response.sendRedirect(response.encodeRedirectURL(target)); + response.setHeader("Location",response.encodeRedirectURL(target)); + response.sendError(_statusCode); return target; } + + /** + * Returns the redirect status code and replacement. + */ + @Override + public String toString() + { + StringBuilder str = new StringBuilder(); + str.append(super.toString()); + str.append('[').append(_statusCode); + str.append('>').append(_replacement); + str.append(']'); + return str.toString(); + } + } diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRuleTest.java deleted file mode 100644 index 7f1a7b798e..0000000000 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRuleTest.java +++ /dev/null @@ -1,66 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 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 static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import java.io.IOException; - -import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.http.HttpStatus; -import org.junit.Before; -import org.junit.Test; - -public class MovedPermanentlyPatternRuleTest extends AbstractRuleTestCase -{ - @Before - public void init() throws Exception - { - start(false); - } - - @Test - public void testGlobPattern() throws IOException - { - MovedPermanentlyPatternRule rule = new MovedPermanentlyPatternRule(); - rule.setPattern("*"); - - String location = "http://new.company.com"; - rule.setLocation(location); - rule.apply("/", _request, _response); - - assertThat("Response Status", _response.getStatus(), is(HttpStatus.MOVED_PERMANENTLY_301)); - assertThat("Response Location Header", _response.getHeader(HttpHeader.LOCATION.asString()), is(location)); - } - - @Test - public void testPrefixPattern() throws IOException - { - MovedPermanentlyPatternRule rule = new MovedPermanentlyPatternRule(); - rule.setPattern("/api/*"); - - String location = "http://api.company.com/"; - rule.setLocation(location); - rule.apply("/api/docs", _request, _response); - - assertThat("Response Status", _response.getStatus(), is(HttpStatus.MOVED_PERMANENTLY_301)); - assertThat("Response Location Header", _response.getHeader(HttpHeader.LOCATION.asString()), is(location)); - } -} diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRuleTest.java deleted file mode 100644 index e7b8bec972..0000000000 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRuleTest.java +++ /dev/null @@ -1,84 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2015 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 static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import java.io.IOException; - -import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.http.HttpStatus; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class MovedPermanentlyRegexRuleTest extends AbstractRuleTestCase -{ - private MovedPermanentlyRegexRule _rule; - - @Before - public void init() throws Exception - { - start(false); - _rule = new MovedPermanentlyRegexRule(); - } - - @After - public void destroy() - { - _rule = null; - } - - @Test - public void testLocationWithReplacementGroupEmpty() throws IOException - { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); - - // Resource is dir - _rule.matchAndApply("/my/dir/file/", _request, _response); - assertThat("Response Status", _response.getStatus(), is(HttpStatus.MOVED_PERMANENTLY_301)); - assertThat("Response Location Header", _response.getHeader(HttpHeader.LOCATION.asString()), is("http://www.mortbay.org/")); - } - - @Test - public void testLocationWithReplacmentGroupSimple() throws IOException - { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); - - // Resource is an image - _rule.matchAndApply("/my/dir/file/image.png", _request, _response); - assertThat("Response Status", _response.getStatus(), is(HttpStatus.MOVED_PERMANENTLY_301)); - assertThat("Response Location Header", _response.getHeader(HttpHeader.LOCATION.asString()), is("http://www.mortbay.org/image.png")); - } - - @Test - public void testLocationWithReplacementGroupDeepWithParams() throws IOException - { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); - - // Resource is api with parameters - _rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response); - assertThat("Response Status", _response.getStatus(), is(HttpStatus.MOVED_PERMANENTLY_301)); - assertThat("Response Location Header", _response.getHeader(HttpHeader.LOCATION.asString()), is("http://www.mortbay.org/api/rest/foo?id=100&sort=date")); - } -} diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRuleTest.java index b4a5e45c55..e5e92758fa 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRuleTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRuleTest.java @@ -18,39 +18,54 @@ package org.eclipse.jetty.rewrite.handler; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; import java.io.IOException; import org.eclipse.jetty.http.HttpHeader; -import org.junit.After; +import org.eclipse.jetty.http.HttpStatus; import org.junit.Before; import org.junit.Test; public class RedirectPatternRuleTest extends AbstractRuleTestCase { - private RedirectPatternRule _rule; - @Before public void init() throws Exception { start(false); - _rule = new RedirectPatternRule(); - _rule.setPattern("*"); } - @After - public void destroy() + private void assertRedirectResponse(int expectedStatusCode, String expectedLocation) throws IOException { - _rule = null; + assertThat("Response status code",_response.getStatus(),is(expectedStatusCode)); + assertThat("Response location",_response.getHeader(HttpHeader.LOCATION.asString()),is(expectedLocation)); } @Test - public void testLocation() throws IOException + public void testGlobPattern() throws IOException { String location = "http://eclipse.com"; - _rule.setLocation(location); - _rule.apply(null, _request, _response); - assertEquals(location, _response.getHeader(HttpHeader.LOCATION.asString())); + + RedirectPatternRule rule = new RedirectPatternRule(); + rule.setPattern("*"); + rule.setLocation(location); + + rule.apply("/",_request,_response); + assertRedirectResponse(HttpStatus.FOUND_302,location); + } + + @Test + public void testPrefixPattern() throws IOException + { + String location = "http://api.company.com/"; + + RedirectPatternRule rule = new RedirectPatternRule(); + rule.setPattern("/api/*"); + rule.setLocation(location); + rule.setStatusCode(HttpStatus.MOVED_PERMANENTLY_301); + + rule.apply("/api/rest?foo=1",_request,_response); + assertRedirectResponse(HttpStatus.MOVED_PERMANENTLY_301,location); } } diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java index f18839a361..68b3ea9f65 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java @@ -18,62 +18,76 @@ package org.eclipse.jetty.rewrite.handler; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; import java.io.IOException; import org.eclipse.jetty.http.HttpHeader; -import org.junit.After; +import org.eclipse.jetty.http.HttpStatus; import org.junit.Before; import org.junit.Test; public class RedirectRegexRuleTest extends AbstractRuleTestCase { - private RedirectRegexRule _rule; - @Before public void init() throws Exception { start(false); - _rule = new RedirectRegexRule(); } - - @After - public void destroy() + + private void assertRedirectResponse(int expectedStatusCode, String expectedLocation) throws IOException { - _rule = null; + assertThat("Response status code", _response.getStatus(), is(expectedStatusCode)); + assertThat("Response location", _response.getHeader(HttpHeader.LOCATION.asString()), is(expectedLocation)); } @Test public void testLocationWithReplacementGroupEmpty() throws IOException { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); + RedirectRegexRule rule = new RedirectRegexRule(); + rule.setRegex("/my/dir/file/(.*)$"); + rule.setReplacement("http://www.mortbay.org/$1"); // Resource is dir - _rule.matchAndApply("/my/dir/file/", _request, _response); - assertEquals("http://www.mortbay.org/", _response.getHeader(HttpHeader.LOCATION.asString())); + rule.matchAndApply("/my/dir/file/", _request, _response); + assertRedirectResponse(HttpStatus.FOUND_302,"http://www.mortbay.org/"); } @Test public void testLocationWithReplacmentGroupSimple() throws IOException { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); + RedirectRegexRule rule = new RedirectRegexRule(); + rule.setRegex("/my/dir/file/(.*)$"); + rule.setReplacement("http://www.mortbay.org/$1"); // Resource is an image - _rule.matchAndApply("/my/dir/file/image.png", _request, _response); - assertEquals("http://www.mortbay.org/image.png", _response.getHeader(HttpHeader.LOCATION.asString())); + rule.matchAndApply("/my/dir/file/image.png", _request, _response); + assertRedirectResponse(HttpStatus.FOUND_302,"http://www.mortbay.org/image.png"); } @Test public void testLocationWithReplacementGroupDeepWithParams() throws IOException { - _rule.setRegex("/my/dir/file/(.*)$"); - _rule.setReplacement("http://www.mortbay.org/$1"); + RedirectRegexRule rule = new RedirectRegexRule(); + rule.setRegex("/my/dir/file/(.*)$"); + rule.setReplacement("http://www.mortbay.org/$1"); + + // Resource is api with parameters + rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response); + assertRedirectResponse(HttpStatus.FOUND_302,"http://www.mortbay.org/api/rest/foo?id=100&sort=date"); + } + + @Test + public void testMovedPermanently() throws IOException + { + RedirectRegexRule rule = new RedirectRegexRule(); + rule.setRegex("/api/(.*)$"); + rule.setReplacement("http://api.company.com/$1"); + rule.setStatusCode(HttpStatus.MOVED_PERMANENTLY_301); // Resource is api with parameters - _rule.matchAndApply("/my/dir/file/api/rest/foo?id=100&sort=date", _request, _response); - assertEquals("http://www.mortbay.org/api/rest/foo?id=100&sort=date", _response.getHeader(HttpHeader.LOCATION.asString())); + rule.matchAndApply("/api/rest/foo?id=100&sort=date", _request, _response); + assertRedirectResponse(HttpStatus.MOVED_PERMANENTLY_301,"http://api.company.com/rest/foo?id=100&sort=date"); } } |