Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-07-22 14:31:56 -0400
committerJoakim Erdfelt2015-07-22 14:31:56 -0400
commita469e78bdb6559db53990127c72ad18f55652e93 (patch)
treee8cf7e90a438afe6753f82a2f574b979c7c0c5d6 /jetty-rewrite
parentfa92938243c8c02fe84adc918f8ee477b0db62f5 (diff)
downloadorg.eclipse.jetty.project-a469e78bdb6559db53990127c72ad18f55652e93.tar.gz
org.eclipse.jetty.project-a469e78bdb6559db53990127c72ad18f55652e93.tar.xz
org.eclipse.jetty.project-a469e78bdb6559db53990127c72ad18f55652e93.zip
473307 - Add 301 Moved Permanently Rules to jetty-rewrite
+ Adding MovedPermanentlyRegexRule (and test) + Adding MovedPermanentlyPatternRule (and test)
Diffstat (limited to 'jetty-rewrite')
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRule.java40
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRule.java46
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java2
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java2
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRuleTest.java66
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRuleTest.java84
6 files changed, 238 insertions, 2 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
new file mode 100644
index 0000000000..73a513babc
--- /dev/null
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRule.java
@@ -0,0 +1,40 @@
+//
+// ========================================================================
+// 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
new file mode 100644
index 0000000000..0f463a8ef8
--- /dev/null
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRule.java
@@ -0,0 +1,46 @@
+//
+// ========================================================================
+// 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 de434a08fa..08223f2e4d 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
@@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class RedirectPatternRule extends PatternRule
{
- private String _location;
+ protected String _location;
/* ------------------------------------------------------------ */
public RedirectPatternRule()
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 b734e7d9b8..b1a20e8ce5 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
@@ -30,7 +30,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class RedirectRegexRule extends RegexRule
{
- private String _replacement;
+ protected String _replacement;
public RedirectRegexRule()
{
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
new file mode 100644
index 0000000000..7f1a7b798e
--- /dev/null
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyPatternRuleTest.java
@@ -0,0 +1,66 @@
+//
+// ========================================================================
+// 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
new file mode 100644
index 0000000000..e7b8bec972
--- /dev/null
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/MovedPermanentlyRegexRuleTest.java
@@ -0,0 +1,84 @@
+//
+// ========================================================================
+// 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"));
+ }
+}

Back to the top