Refactor DefaultRedirectStrategy for subclassing. (#164)

- Adds the constructor DefaultRedirectStrategy(String[]) to construct a new instance to redirect the given HTTP methods.
- The default constructor now calls the String[] constructor.
- Reimplement the LaxRedirectStrategy default constructor to call DefaultRedirectStrategy(String[]).

All of this allows for new custom subclasses of DefaultRedirectStrategy to provide their own array of HTTP methods to redirect.
This commit is contained in:
Gary Gregory 2019-08-31 10:26:48 -04:00 committed by GitHub
parent 62f2164b89
commit a91b9ff848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 31 deletions

View File

@ -29,6 +29,7 @@ package org.apache.http.impl.client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -81,16 +82,26 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
/**
* Redirectable methods.
*/
private static final String[] REDIRECT_METHODS = new String[] {
HttpGet.METHOD_NAME,
HttpHead.METHOD_NAME
};
private final String[] redirectMethods;
public DefaultRedirectStrategy() {
this(new String[] {
HttpGet.METHOD_NAME,
HttpHead.METHOD_NAME
});
}
/**
* Constructs a new instance to redirect the given HTTP methods.
*
* @param redirectMethods The methods to redirect.
* @since 4.5.10
*/
public DefaultRedirectStrategy(final String[] redirectMethods) {
super();
final String[] tmp = redirectMethods.clone();
Arrays.sort(tmp);
this.redirectMethods = tmp;
}
@Override
@ -198,12 +209,7 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
* @since 4.2
*/
protected boolean isRedirectable(final String method) {
for (final String m: REDIRECT_METHODS) {
if (m.equalsIgnoreCase(method)) {
return true;
}
}
return false;
return Arrays.binarySearch(redirectMethods, method) >= 0;
}
@Override

View File

@ -47,24 +47,13 @@ public class LaxRedirectStrategy extends DefaultRedirectStrategy {
public static final LaxRedirectStrategy INSTANCE = new LaxRedirectStrategy();
/**
* Redirectable methods.
*/
private static final String[] REDIRECT_METHODS = new String[] {
HttpGet.METHOD_NAME,
HttpPost.METHOD_NAME,
HttpHead.METHOD_NAME,
HttpDelete.METHOD_NAME
};
@Override
protected boolean isRedirectable(final String method) {
for (final String m: REDIRECT_METHODS) {
if (m.equalsIgnoreCase(method)) {
return true;
}
}
return false;
public LaxRedirectStrategy() {
super(new String[] {
HttpGet.METHOD_NAME,
HttpPost.METHOD_NAME,
HttpHead.METHOD_NAME,
HttpDelete.METHOD_NAME
});
}
}