Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-07-22 16:29:10 -0400
committerJoakim Erdfelt2015-07-22 16:29:10 -0400
commit7bf6cec74e6d1eb1f7caff628b5d59b5047384b2 (patch)
treeaf05e397d17dad4e36a81f8ff73a508f654fe642 /jetty-rewrite
parenta469e78bdb6559db53990127c72ad18f55652e93 (diff)
downloadorg.eclipse.jetty.project-7bf6cec74e6d1eb1f7caff628b5d59b5047384b2.tar.gz
org.eclipse.jetty.project-7bf6cec74e6d1eb1f7caff628b5d59b5047384b2.tar.xz
org.eclipse.jetty.project-7bf6cec74e6d1eb1f7caff628b5d59b5047384b2.zip
473309 - Add special (non-replacement) Terminating rules to jetty-rewrite
+ Adding TerminatingPatternRule (and test) + Adding TerminatingRegexRule (and test)
Diffstat (limited to 'jetty-rewrite')
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java51
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java52
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRuleTest.java103
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRuleTest.java103
4 files changed, 309 insertions, 0 deletions
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java
new file mode 100644
index 0000000000..40dacca639
--- /dev/null
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java
@@ -0,0 +1,51 @@
+//
+// ========================================================================
+// 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;
+
+/**
+ * If this rule matches, terminate the processing of other rules.
+ * Allowing the request to be processed by the handlers after the rewrite rules.
+ */
+public class TerminatingPatternRule extends PatternRule
+{
+ public TerminatingPatternRule()
+ {
+ super.setTerminating(true);
+ }
+
+ @Override
+ public void setTerminating(boolean terminating)
+ {
+ if (!terminating)
+ {
+ throw new RuntimeException("Not allowed to disable terminating on a " + TerminatingPatternRule.class.getName());
+ }
+ }
+
+ @Override
+ protected String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
+ return target;
+ }
+} \ No newline at end of file
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java
new file mode 100644
index 0000000000..fc67c6a94f
--- /dev/null
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java
@@ -0,0 +1,52 @@
+//
+// ========================================================================
+// 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;
+
+/**
+ * If this rule matches, terminate the processing of other rules.
+ * Allowing the request to be processed by the handlers after the rewrite rules.
+ */
+public class TerminatingRegexRule extends RegexRule
+{
+ public TerminatingRegexRule()
+ {
+ super.setTerminating(true);
+ }
+
+ @Override
+ public void setTerminating(boolean terminating)
+ {
+ if (!terminating)
+ {
+ throw new RuntimeException("Not allowed to disable terminating on a " + TerminatingRegexRule.class.getName());
+ }
+ }
+
+ @Override
+ public String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
+ {
+ return target;
+ }
+} \ No newline at end of file
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRuleTest.java
new file mode 100644
index 0000000000..e2631328e3
--- /dev/null
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRuleTest.java
@@ -0,0 +1,103 @@
+//
+// ========================================================================
+// 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 javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.http.HttpHeader;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TerminatingPatternRuleTest extends AbstractRuleTestCase
+{
+ private RewriteHandler rewriteHandler;
+
+ @Before
+ public void init() throws Exception
+ {
+ rewriteHandler = new RewriteHandler();
+ rewriteHandler.setServer(_server);
+ rewriteHandler.setHandler(new AbstractHandler()
+ {
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
+ ServletException
+ {
+ response.setStatus(HttpStatus.CREATED_201);
+ request.setAttribute("target",target);
+ request.setAttribute("URI",request.getRequestURI());
+ request.setAttribute("info",request.getPathInfo());
+ }
+ });
+ rewriteHandler.start();
+
+ TerminatingPatternRule rule1 = new TerminatingPatternRule();
+ rule1.setPattern("/login.jsp");
+ rewriteHandler.addRule(rule1);
+ RedirectRegexRule rule2 = new RedirectRegexRule();
+ rule2.setRegex("^/login.*$");
+ rule2.setReplacement("http://login.company.com/");
+ rewriteHandler.addRule(rule2);
+
+ start(false);
+ }
+
+ private void assertIsRedirect(int expectedStatus, String expectedLocation)
+ {
+ assertThat("Response Status",_response.getStatus(),is(expectedStatus));
+ assertThat("Response Location Header",_response.getHeader(HttpHeader.LOCATION.asString()),is(expectedLocation));
+ }
+
+ private void assertIsRequest(String expectedRequestPath)
+ {
+ assertThat("Response Status",_response.getStatus(),is(HttpStatus.CREATED_201));
+ assertThat("Request Target",_request.getAttribute("target"),is(expectedRequestPath));
+ }
+
+ @Test
+ public void testTerminatingEarly() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login.jsp",_request,_request,_response);
+ assertIsRequest("/login.jsp");
+ }
+
+ @Test
+ public void testNoTerminationDo() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login.do",_request,_request,_response);
+ assertIsRedirect(HttpStatus.MOVED_TEMPORARILY_302,"http://login.company.com/");
+ }
+
+ @Test
+ public void testNoTerminationDir() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login/",_request,_request,_response);
+ assertIsRedirect(HttpStatus.MOVED_TEMPORARILY_302,"http://login.company.com/");
+ }
+}
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRuleTest.java
new file mode 100644
index 0000000000..7420931346
--- /dev/null
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRuleTest.java
@@ -0,0 +1,103 @@
+//
+// ========================================================================
+// 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 javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.http.HttpHeader;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TerminatingRegexRuleTest extends AbstractRuleTestCase
+{
+ private RewriteHandler rewriteHandler;
+
+ @Before
+ public void init() throws Exception
+ {
+ rewriteHandler = new RewriteHandler();
+ rewriteHandler.setServer(_server);
+ rewriteHandler.setHandler(new AbstractHandler()
+ {
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
+ ServletException
+ {
+ response.setStatus(HttpStatus.CREATED_201);
+ request.setAttribute("target",target);
+ request.setAttribute("URI",request.getRequestURI());
+ request.setAttribute("info",request.getPathInfo());
+ }
+ });
+ rewriteHandler.start();
+
+ TerminatingRegexRule rule1 = new TerminatingRegexRule();
+ rule1.setRegex("^/login.jsp$");
+ rewriteHandler.addRule(rule1);
+ RedirectRegexRule rule2 = new RedirectRegexRule();
+ rule2.setRegex("^/login.*$");
+ rule2.setReplacement("http://login.company.com/");
+ rewriteHandler.addRule(rule2);
+
+ start(false);
+ }
+
+ private void assertIsRedirect(int expectedStatus, String expectedLocation)
+ {
+ assertThat("Response Status",_response.getStatus(),is(expectedStatus));
+ assertThat("Response Location Header",_response.getHeader(HttpHeader.LOCATION.asString()),is(expectedLocation));
+ }
+
+ private void assertIsRequest(String expectedRequestPath)
+ {
+ assertThat("Response Status",_response.getStatus(),is(HttpStatus.CREATED_201));
+ assertThat("Request Target",_request.getAttribute("target"),is(expectedRequestPath));
+ }
+
+ @Test
+ public void testTerminatingEarly() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login.jsp",_request,_request,_response);
+ assertIsRequest("/login.jsp");
+ }
+
+ @Test
+ public void testNoTerminationDo() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login.do",_request,_request,_response);
+ assertIsRedirect(HttpStatus.MOVED_TEMPORARILY_302,"http://login.company.com/");
+ }
+
+ @Test
+ public void testNoTerminationDir() throws IOException, ServletException
+ {
+ rewriteHandler.handle("/login/",_request,_request,_response);
+ assertIsRedirect(HttpStatus.MOVED_TEMPORARILY_302,"http://login.company.com/");
+ }
+}

Back to the top