Replaced usage of deprecated "replacement" with "location".
Code cleanup & formatting. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
d6dbf7cae0
commit
eb837293ad
|
@ -20,7 +20,6 @@ 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;
|
||||
|
||||
|
@ -32,9 +31,9 @@ import org.eclipse.jetty.util.annotation.Name;
|
|||
* <p>
|
||||
* The replacement string may use $n" to replace the nth capture group.
|
||||
* <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>
|
||||
* 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
|
||||
{
|
||||
|
@ -43,9 +42,9 @@ public class RedirectRegexRule extends RegexRule
|
|||
|
||||
public RedirectRegexRule()
|
||||
{
|
||||
this(null,null);
|
||||
this(null, null);
|
||||
}
|
||||
|
||||
|
||||
public RedirectRegexRule(@Name("regex") String regex, @Name("location") String location)
|
||||
{
|
||||
super(regex);
|
||||
|
@ -54,65 +53,63 @@ public class RedirectRegexRule extends RegexRule
|
|||
setLocation(location);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param replacement the URI to redirect to
|
||||
* @deprecated use {@link #setLocation(String)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
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)
|
||||
{
|
||||
_location = location;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the redirect status code.
|
||||
*
|
||||
*
|
||||
* @param statusCode the 3xx redirect status code
|
||||
*/
|
||||
public void setStatusCode(int statusCode)
|
||||
{
|
||||
if ((300 <= statusCode) || (statusCode >= 399))
|
||||
{
|
||||
if (statusCode >= 300 && statusCode <= 399)
|
||||
_statusCode = statusCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Invalid redirect status code " + statusCode + " (must be a value between 300 and 399)");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher)
|
||||
throws IOException
|
||||
protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException
|
||||
{
|
||||
target=_location;
|
||||
for (int g=1;g<=matcher.groupCount();g++)
|
||||
target = _location;
|
||||
for (int g = 1; g <= matcher.groupCount(); g++)
|
||||
{
|
||||
String group = matcher.group(g);
|
||||
target=target.replaceAll("\\$"+g,group);
|
||||
target = target.replaceAll("\\$" + g, group);
|
||||
}
|
||||
|
||||
|
||||
target = response.encodeRedirectURL(target);
|
||||
response.setHeader("Location",RedirectUtil.toRedirectURL(request,target));
|
||||
response.setHeader("Location", RedirectUtil.toRedirectURL(request, target));
|
||||
response.setStatus(_statusCode);
|
||||
response.getOutputStream().flush(); // no output / content
|
||||
response.getOutputStream().close();
|
||||
return target;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the redirect status code and replacement.
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append(super.toString());
|
||||
str.append('[').append(_statusCode);
|
||||
str.append('>').append(_location);
|
||||
str.append(']');
|
||||
return str.toString();
|
||||
return String.format("%s[%d>%s]", super.toString(), _statusCode, _location);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,44 +8,44 @@
|
|||
|
||||
<Call name="insertHandler">
|
||||
<Arg>
|
||||
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
|
||||
<Set name="rewriteRequestURI">true</Set>
|
||||
<Set name="rewritePathInfo">false</Set>
|
||||
<Set name="originalPathAttribute">requestedPath</Set>
|
||||
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
|
||||
<Set name="rewriteRequestURI">true</Set>
|
||||
<Set name="rewritePathInfo">false</Set>
|
||||
<Set name="originalPathAttribute">requestedPath</Set>
|
||||
|
||||
<Set name="rules">
|
||||
<Array type="org.eclipse.jetty.rewrite.handler.Rule">
|
||||
<Set name="rules">
|
||||
<Array type="org.eclipse.jetty.rewrite.handler.Rule">
|
||||
|
||||
<!-- add a response rule -->
|
||||
<!--
|
||||
<Item>
|
||||
<New id="response" class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule">
|
||||
<Set name="pattern">/rewrite/session/</Set>
|
||||
<Set name="code">401</Set>
|
||||
<Set name="reason">Setting error code 401</Set>
|
||||
</New>
|
||||
</Item>
|
||||
-->
|
||||
<!-- add a response rule -->
|
||||
<!--
|
||||
<Item>
|
||||
<New id="response" class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule">
|
||||
<Set name="pattern">/rewrite/session/</Set>
|
||||
<Set name="code">401</Set>
|
||||
<Set name="reason">Setting error code 401</Set>
|
||||
</New>
|
||||
</Item>
|
||||
-->
|
||||
|
||||
<!-- add a simple redirect -->
|
||||
<!--
|
||||
<Item>
|
||||
<New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
|
||||
<Set name="pattern">/redirect/*</Set>
|
||||
<Set name="location">/tests/</Set>
|
||||
</New>
|
||||
</Item>
|
||||
-->
|
||||
<!-- add a simple redirect -->
|
||||
<!--
|
||||
<Item>
|
||||
<New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
|
||||
<Set name="pattern">/redirect/*</Set>
|
||||
<Set name="location">/tests/</Set>
|
||||
</New>
|
||||
</Item>
|
||||
-->
|
||||
|
||||
<!-- add a regex rewrite redirect -->
|
||||
<Item>
|
||||
<New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
|
||||
<Set name="regex">/redirect/(.*)</Set>
|
||||
<Set name="replacement">/tests/$1</Set>
|
||||
</New>
|
||||
</Item>
|
||||
</Array>
|
||||
</Set>
|
||||
<!-- add a regex rewrite redirect -->
|
||||
<Item>
|
||||
<New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
|
||||
<Set name="regex">/redirect/(.*)</Set>
|
||||
<Set name="location">/tests/$1</Set>
|
||||
</New>
|
||||
</Item>
|
||||
</Array>
|
||||
</Set>
|
||||
</New>
|
||||
</Arg>
|
||||
</Call>
|
||||
|
|
Loading…
Reference in New Issue