public class HttpRedirector
extends java.lang.Object
Applications can disable redirection via Request.followRedirects(boolean)
and then rely on this class to perform the redirect in a simpler way, for example:
HttpRedirector redirector = new HttpRedirector(httpClient); Request request = httpClient.newRequest("http://host/path").followRedirects(false); ContentResponse response = request.send(); while (redirector.isRedirect(response)) { // Validate the redirect URI if (!validate(redirector.extractRedirectURI(response))) break; Result result = redirector.redirect(request, response); request = result.getRequest(); response = result.getResponse(); }
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
ATTRIBUTE |
private static java.lang.String |
AUTHORITY_REGEXP |
private HttpClient |
client |
private static java.lang.String |
DESTINATION_REGEXP |
private static java.lang.String |
FRAGMENT_REGEXP |
private static Logger |
LOG |
private ResponseNotifier |
notifier |
private static java.lang.String |
PATH_REGEXP |
private static java.lang.String |
QUERY_REGEXP |
private static java.lang.String |
SCHEME_REGEXP |
private static java.util.regex.Pattern |
URI_PATTERN |
Constructor and Description |
---|
HttpRedirector(HttpClient client) |
Modifier and Type | Method and Description |
---|---|
java.net.URI |
extractRedirectURI(Response response)
Extracts and sanitizes (by making it absolute and escaping paths and query parameters)
the redirect URI of the given
response . |
protected void |
fail(Request request,
Response response,
java.lang.Throwable failure) |
boolean |
isRedirect(Response response) |
Result |
redirect(Request request,
Response response)
Redirects the given
response , blocking until the redirect is complete. |
Request |
redirect(Request request,
Response response,
Response.CompleteListener listener)
Redirects the given
response asynchronously. |
private Request |
redirect(Request request,
Response response,
Response.CompleteListener listener,
java.net.URI newURI) |
private Request |
redirect(Request request,
Response response,
Response.CompleteListener listener,
java.net.URI location,
java.lang.String method) |
private java.net.URI |
sanitize(java.lang.String location) |
private Request |
sendRedirect(HttpRequest httpRequest,
Response response,
Response.CompleteListener listener,
java.net.URI location,
java.lang.String method) |
private static final Logger LOG
private static final java.lang.String SCHEME_REGEXP
private static final java.lang.String AUTHORITY_REGEXP
private static final java.lang.String DESTINATION_REGEXP
private static final java.lang.String PATH_REGEXP
private static final java.lang.String QUERY_REGEXP
private static final java.lang.String FRAGMENT_REGEXP
private static final java.util.regex.Pattern URI_PATTERN
private static final java.lang.String ATTRIBUTE
private final HttpClient client
private final ResponseNotifier notifier
public HttpRedirector(HttpClient client)
public boolean isRedirect(Response response)
response
- the response to check for redirectspublic Result redirect(Request request, Response response) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
response
, blocking until the redirect is complete.request
- the original request that triggered the redirectresponse
- the response to the original requestResult
object containing the request to the redirected location and its responsejava.lang.InterruptedException
- if the thread is interrupted while waiting for the redirect to completejava.util.concurrent.ExecutionException
- if the redirect failedredirect(Request, Response, Response.CompleteListener)
public Request redirect(Request request, Response response, Response.CompleteListener listener)
response
asynchronously.request
- the original request that triggered the redirectresponse
- the response to the original requestlistener
- the listener that receives response eventspublic java.net.URI extractRedirectURI(Response response)
response
.response
- the response to extract the redirect URI fromprivate java.net.URI sanitize(java.lang.String location)
private Request redirect(Request request, Response response, Response.CompleteListener listener, java.net.URI newURI)
private Request redirect(Request request, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)
private Request sendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)