This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename UriRedirect -> PathRedirect and add new UriRedirect functional…
…ity.
- Loading branch information
Brandon Beck
committed
Jul 25, 2014
1 parent
2259be9
commit 47e6531
Showing
5 changed files
with
247 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/bazaarvoice/dropwizard/redirect/PathRedirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.bazaarvoice.dropwizard.redirect; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Map; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** Redirects requests coming on a specific source path to a target path. */ | ||
public class PathRedirect implements Redirect { | ||
private final Map<String, String> pathMapping; | ||
private final boolean keepParameters; | ||
|
||
public PathRedirect(String sourceUri, String targetUri) { | ||
this(sourceUri, targetUri, true); | ||
} | ||
|
||
public PathRedirect(String sourceUri, String targetUri, boolean keepParameters) { | ||
checkNotNull(sourceUri); | ||
checkNotNull(targetUri); | ||
|
||
pathMapping = ImmutableMap.of(sourceUri, targetUri); | ||
this.keepParameters = keepParameters; | ||
} | ||
|
||
public PathRedirect(Map<String, String> uriMap) { | ||
this(uriMap, true); | ||
} | ||
|
||
public PathRedirect(Map<String, String> uriMap, boolean keepParameters) { | ||
checkNotNull(uriMap); | ||
|
||
pathMapping = ImmutableMap.copyOf(uriMap); | ||
this.keepParameters = keepParameters; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public String getRedirect(HttpServletRequest request) { | ||
String uri = pathMapping.get(request.getRequestURI()); | ||
if (uri == null) { | ||
return null; | ||
} | ||
|
||
StringBuilder redirect = new StringBuilder(uri); | ||
if (keepParameters) { | ||
String query = request.getQueryString(); | ||
if (query != null) { | ||
redirect.append('?'); | ||
redirect.append(query); | ||
} | ||
} | ||
|
||
return redirect.toString(); | ||
} | ||
} |
81 changes: 51 additions & 30 deletions
81
src/main/java/com/bazaarvoice/dropwizard/redirect/UriRedirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,78 @@ | ||
package com.bazaarvoice.dropwizard.redirect; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** Redirects requests coming on a specific source URI to a target URI. */ | ||
/** Regular expression based redirect. Has access to the full URI. */ | ||
public class UriRedirect implements Redirect { | ||
private final Map<String, String> uriMapping; | ||
private final boolean keepParameters; | ||
private final List<Entry> entries; | ||
|
||
public UriRedirect(String sourceUri, String targetUri) { | ||
this(sourceUri, targetUri, true); | ||
} | ||
|
||
public UriRedirect(String sourceUri, String targetUri, boolean keepParameters) { | ||
checkNotNull(sourceUri); | ||
checkNotNull(targetUri); | ||
public UriRedirect(String regex, String replacement) { | ||
checkNotNull(regex); | ||
checkNotNull(replacement); | ||
|
||
uriMapping = ImmutableMap.of(sourceUri, targetUri); | ||
this.keepParameters = keepParameters; | ||
entries = ImmutableList.of(new Entry(Pattern.compile(regex), replacement)); | ||
} | ||
|
||
public UriRedirect(Map<String, String> uriMap) { | ||
this(uriMap, true); | ||
} | ||
|
||
public UriRedirect(Map<String, String> uriMap, boolean keepParameters) { | ||
checkNotNull(uriMap); | ||
|
||
uriMapping = ImmutableMap.copyOf(uriMap); | ||
this.keepParameters = keepParameters; | ||
entries = Lists.newArrayList(); | ||
for (Map.Entry<String, String> entry : uriMap.entrySet()) { | ||
String regex = entry.getKey(); | ||
String replacement = entry.getValue(); | ||
entries.add(new Entry(Pattern.compile(regex), replacement)); | ||
} | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public String getRedirect(HttpServletRequest request) { | ||
String uri = uriMapping.get(request.getRequestURI()); | ||
if (uri == null) { | ||
return null; | ||
String uri = getFullURI(request); | ||
for (Entry entry : entries) { | ||
Matcher matcher = entry.getRegex().matcher(uri); | ||
if (matcher.matches()) { | ||
return matcher.replaceAll(entry.getReplacement()); | ||
} | ||
} | ||
|
||
StringBuilder redirect = new StringBuilder(uri); | ||
if (keepParameters) { | ||
String query = request.getQueryString(); | ||
if (query != null) { | ||
redirect.append('?'); | ||
redirect.append(query); | ||
} | ||
return null; | ||
} | ||
|
||
private static String getFullURI(HttpServletRequest request) { | ||
StringBuffer requestURL = request.getRequestURL(); | ||
String queryString = request.getQueryString(); | ||
|
||
if (queryString == null) { | ||
return requestURL.toString(); | ||
} else { | ||
return requestURL.append('?').append(queryString).toString(); | ||
} | ||
} | ||
|
||
private static final class Entry { | ||
Pattern regex; | ||
String replacement; | ||
|
||
Entry(Pattern regex, String replacement) { | ||
this.regex = regex; | ||
this.replacement = replacement; | ||
} | ||
|
||
Pattern getRegex() { | ||
return regex; | ||
} | ||
|
||
return redirect.toString(); | ||
String getReplacement() { | ||
return replacement; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/test/java/com/bazaarvoice/dropwizard/redirect/PathRedirectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.bazaarvoice.dropwizard.redirect; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Test; | ||
import org.mockito.internal.stubbing.answers.ThrowsException; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class PathRedirectTest { | ||
private final PathRedirect redirect = new PathRedirect(ImmutableMap.<String, String>builder() | ||
.put("/old1", "/new1") | ||
.put("/old2", "/new2") | ||
.build() | ||
); | ||
|
||
@Test | ||
public void testMatchingUri() { | ||
HttpServletRequest request = request("/old1"); | ||
|
||
String uri = redirect.getRedirect(request); | ||
assertEquals("/new1", uri); | ||
} | ||
|
||
@Test | ||
public void testMatchingUriWithKeepParameters() { | ||
HttpServletRequest request = request("/old1?key=value"); | ||
|
||
String uri = redirect.getRedirect(request); | ||
assertEquals("/new1?key=value", uri); | ||
} | ||
|
||
@Test | ||
public void testMatchingUriWithoutKeepParameters() { | ||
PathRedirect redirect = new PathRedirect("/old1", "/new1", false); | ||
HttpServletRequest request = request("/old1?key=value"); | ||
|
||
String uri = redirect.getRedirect(request); | ||
assertEquals("/new1", uri); | ||
} | ||
|
||
@Test | ||
public void testNonMatchingUri() { | ||
HttpServletRequest request = request("/new"); | ||
|
||
String uri = redirect.getRedirect(request); | ||
assertNull(uri); | ||
} | ||
|
||
@Test | ||
public void testSubstringUri() { | ||
HttpServletRequest request = request("/old100"); | ||
|
||
String uri = redirect.getRedirect(request); | ||
assertNull(uri); | ||
} | ||
|
||
private static HttpServletRequest request(String uri) { | ||
int question = uri.indexOf('?'); | ||
String path = (question == -1) ? uri : uri.substring(0, question); | ||
String params = (question == -1) ? null : uri.substring(question + 1); | ||
|
||
// By default have the mock throw an exception when we've forgotten to mock a method that is called. | ||
Exception exception = new RuntimeException("Forgot to mock a method"); | ||
HttpServletRequest request = mock(HttpServletRequest.class, new ThrowsException(exception)); | ||
doReturn(path).when(request).getRequestURI(); | ||
doReturn(params).when(request).getQueryString(); | ||
return request; | ||
} | ||
} |
Oops, something went wrong.