473307 - Add 301 Moved Permanently Rules to jetty-rewrite

+ Adding MovedPermanentlyRegexRule (and test)
+ Adding MovedPermanentlyPatternRule (and test)
This commit is contained in:
Joakim Erdfelt 2015-07-22 11:31:56 -07:00
parent fa92938243
commit a469e78bdb
6 changed files with 238 additions and 2 deletions

View File

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

View File

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

View File

@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class RedirectPatternRule extends PatternRule
{
private String _location;
protected String _location;
/* ------------------------------------------------------------ */
public RedirectPatternRule()

View File

@ -30,7 +30,7 @@ import javax.servlet.http.HttpServletResponse;
*/
public class RedirectRegexRule extends RegexRule
{
private String _replacement;
protected String _replacement;
public RedirectRegexRule()
{

View File

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

View File

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