473309 - Add special (non-replacement) Terminating rules to jetty-rewrite

+ Adding TerminatingPatternRule (and test)
+ Adding TerminatingRegexRule (and test)
This commit is contained in:
Joakim Erdfelt 2015-07-22 13:29:10 -07:00
parent a469e78bdb
commit 7bf6cec74e
4 changed files with 309 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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/");
}
}

View File

@ -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/");
}
}