Replaced usage of deprecated "replacement" with "location".

Code cleanup & formatting.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2019-05-30 14:39:11 +02:00
parent d6dbf7cae0
commit eb837293ad
2 changed files with 61 additions and 64 deletions

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.rewrite.handler;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -32,9 +31,9 @@ import org.eclipse.jetty.util.annotation.Name;
* <p> * <p>
* The replacement string may use $n" to replace the nth capture group. * The replacement string may use $n" to replace the nth capture group.
* <p> * <p>
* All redirects are part of the <a href="http://tools.ietf.org/html/rfc7231#section-6.4"><code>3xx Redirection</code> status code set</a>. * All redirects are part of the <a href="http://tools.ietf.org/html/rfc7231#section-6.4">{@code 3xx Redirection} status code set</a>.
* <p> * <p>
* Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3"><code>302 Found</code></a> * Defaults to <a href="http://tools.ietf.org/html/rfc7231#section-6.4.3">{@code 302 Found}</a>
*/ */
public class RedirectRegexRule extends RegexRule public class RedirectRegexRule extends RegexRule
{ {
@ -43,7 +42,7 @@ public class RedirectRegexRule extends RegexRule
public RedirectRegexRule() public RedirectRegexRule()
{ {
this(null,null); this(null, null);
} }
public RedirectRegexRule(@Name("regex") String regex, @Name("location") String location) public RedirectRegexRule(@Name("regex") String regex, @Name("location") String location)
@ -54,12 +53,21 @@ public class RedirectRegexRule extends RegexRule
setLocation(location); setLocation(location);
} }
/**
* @param replacement the URI to redirect to
* @deprecated use {@link #setLocation(String)} instead.
*/
@Deprecated @Deprecated
public void setReplacement(String replacement) public void setReplacement(String replacement)
{ {
_location = replacement; setLocation(replacement);
} }
/**
* Sets the redirect location.
*
* @param location the URI to redirect to
*/
public void setLocation(String location) public void setLocation(String location)
{ {
_location = location; _location = location;
@ -72,29 +80,24 @@ public class RedirectRegexRule extends RegexRule
*/ */
public void setStatusCode(int statusCode) public void setStatusCode(int statusCode)
{ {
if ((300 <= statusCode) || (statusCode >= 399)) if (statusCode >= 300 && statusCode <= 399)
{
_statusCode = statusCode; _statusCode = statusCode;
}
else else
{
throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)"); throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)");
} }
}
@Override @Override
protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
throws IOException
{ {
target=_location; target = _location;
for (int g=1;g<=matcher.groupCount();g++) for (int g = 1; g <= matcher.groupCount(); g++)
{ {
String group = matcher.group(g); String group = matcher.group(g);
target=target.replaceAll("\\$"+g,group); target = target.replaceAll("\\$" + g, group);
} }
target = response.encodeRedirectURL(target); target = response.encodeRedirectURL(target);
response.setHeader("Location",RedirectUtil.toRedirectURL(request,target)); response.setHeader("Location", RedirectUtil.toRedirectURL(request, target));
response.setStatus(_statusCode); response.setStatus(_statusCode);
response.getOutputStream().flush(); // no output / content response.getOutputStream().flush(); // no output / content
response.getOutputStream().close(); response.getOutputStream().close();
@ -107,12 +110,6 @@ public class RedirectRegexRule extends RegexRule
@Override @Override
public String toString() public String toString()
{ {
StringBuilder str = new StringBuilder(); return String.format("%s[%d>%s]", super.toString(), _statusCode, _location);
str.append(super.toString());
str.append('[').append(_statusCode);
str.append('>').append(_location);
str.append(']');
return str.toString();
} }
} }

View File

@ -41,7 +41,7 @@
<Item> <Item>
<New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule"> <New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
<Set name="regex">/redirect/(.*)</Set> <Set name="regex">/redirect/(.*)</Set>
<Set name="replacement">/tests/$1</Set> <Set name="location">/tests/$1</Set>
</New> </New>
</Item> </Item>
</Array> </Array>