Top Description Fields Constructors Methods
org.eclipse.jetty.client

public Class HttpRedirector

extends Object
Class Inheritance
Imports
java.net.URI, .URISyntaxException, java.util.concurrent.CountDownLatch, .ExecutionException, .TimeUnit, .TimeoutException, java.util.concurrent.atomic.AtomicReference, java.util.regex.Matcher, .Pattern, org.eclipse.jetty.client.internal.HttpContentResponse, org.eclipse.jetty.client.transport.HttpConversation, .HttpRequest, org.eclipse.jetty.http.HttpHeader, .HttpMethod, .HttpStatus, org.eclipse.jetty.util.NanoTime, org.slf4j.Logger, .LoggerFactory

Utility class that handles HTTP redirects.

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();
}

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private final HttpClient
private static final String
private static final String
private static final Logger
private static final String
private static final String
private static final String
private static final Pattern

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public URI

Returns:

the absolute redirect URI, or null if the response does not contain a valid redirect location
extractRedirectURI
(Response
the response to extract the redirect URI from
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, Throwable failure)

protected void
fail(Request request, Throwable failure, Response response)

private void
fail(Request request, Throwable requestFailure, Response response, Throwable responseFailure)

public boolean

Returns:

whether the response code is an HTTP redirect code
isRedirect
(Response
the response to check for redirects
response
)

public Result

Returns:

a Result object containing the request to the redirected location and its response
redirect
(Request
the original request that triggered the redirect
request
,
Response
the response to the original request
response
)

Redirects the given response, blocking until the redirect is complete.

public Request

Returns:

the request to the redirected location
redirect
(Request
the original request that triggered the redirect
request
,
Response
the response to the original request
response
,
Response.CompleteListener
the listener that receives response events
listener
)

Redirects the given response asynchronously.

private Request
redirect(Request request, Response response, Response.CompleteListener listener, URI newURI)

private Request
redirect(Request request, Response response, Response.CompleteListener listener, URI location, String method)

private URI
sanitize(String location)

private Request
sendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, URI location, String method)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ATTRIBUTEback to summary
private static final String ATTRIBUTE
AUTHORITY_REGEXPback to summary
private static final String AUTHORITY_REGEXP
clientback to summary
private final HttpClient client
DESTINATION_REGEXPback to summary
private static final String DESTINATION_REGEXP
FRAGMENT_REGEXPback to summary
private static final String FRAGMENT_REGEXP
LOGback to summary
private static final Logger LOG
PATH_REGEXPback to summary
private static final String PATH_REGEXP
QUERY_REGEXPback to summary
private static final String QUERY_REGEXP
SCHEME_REGEXPback to summary
private static final String SCHEME_REGEXP
URI_PATTERNback to summary
private static final Pattern URI_PATTERN

Constructor Detail

HttpRedirectorback to summary
public HttpRedirector(HttpClient client)

Method Detail

extractRedirectURIback to summary
public URI extractRedirectURI(Response response)

Extracts and sanitizes (by making it absolute and escaping paths and query parameters) the redirect URI of the given response.

Parameters
response:Response

the response to extract the redirect URI from

Returns:URI

the absolute redirect URI, or null if the response does not contain a valid redirect location

failback to summary
protected void fail(Request request, Response response, Throwable failure)
failback to summary
protected void fail(Request request, Throwable failure, Response response)
failback to summary
private void fail(Request request, Throwable requestFailure, Response response, Throwable responseFailure)
isRedirectback to summary
public boolean isRedirect(Response response)
Parameters
response:Response

the response to check for redirects

Returns:boolean

whether the response code is an HTTP redirect code

redirectback to summary
public Result redirect(Request request, Response response) throws InterruptedException, ExecutionException

Redirects the given response, blocking until the redirect is complete.

Parameters
request:Request

the original request that triggered the redirect

response:Response

the response to the original request

Returns:Result

a Result object containing the request to the redirected location and its response

Exceptions
InterruptedException:
if the thread is interrupted while waiting for the redirect to complete
ExecutionException:
if the redirect failed
See Also
redirect(Request, Response, Response.CompleteListener)
redirectback to summary
public Request redirect(Request request, Response response, Response.CompleteListener listener)

Redirects the given response asynchronously.

Parameters
request:Request

the original request that triggered the redirect

response:Response

the response to the original request

listener:Response.CompleteListener

the listener that receives response events

Returns:Request

the request to the redirected location

redirectback to summary
private Request redirect(Request request, Response response, Response.CompleteListener listener, URI newURI)
redirectback to summary
private Request redirect(Request request, Response response, Response.CompleteListener listener, URI location, String method)
sanitizeback to summary
private URI sanitize(String location)
sendRedirectback to summary
private Request sendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, URI location, String method)