Package org.eclipse.jetty.client
Class HttpRedirector
java.lang.Object
org.eclipse.jetty.client.HttpRedirector
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
FieldsModifier and TypeFieldDescriptionprivate static final String
private static final String
private final HttpClient
private static final String
private static final String
private static final Logger
private final ResponseNotifier
private static final String
private static final String
private static final String
private static final Pattern
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionextractRedirectURI
(Response response) Extracts and sanitizes (by making it absolute and escaping paths and query parameters) the redirect URI of the givenresponse
.protected void
boolean
isRedirect
(Response response) Redirects the givenresponse
, blocking until the redirect is complete.redirect
(Request request, Response response, Response.CompleteListener listener) Redirects the givenresponse
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
private Request
sendRedirect
(HttpRequest httpRequest, Response response, Response.CompleteListener listener, URI location, String method)
-
Field Details
-
LOG
-
SCHEME_REGEXP
- See Also:
-
AUTHORITY_REGEXP
- See Also:
-
DESTINATION_REGEXP
- See Also:
-
PATH_REGEXP
- See Also:
-
QUERY_REGEXP
- See Also:
-
FRAGMENT_REGEXP
- See Also:
-
URI_PATTERN
-
ATTRIBUTE
-
client
-
notifier
-
-
Constructor Details
-
HttpRedirector
-
-
Method Details
-
isRedirect
- Parameters:
response
- the response to check for redirects- Returns:
- whether the response code is an HTTP redirect code
-
redirect
public Result redirect(Request request, Response response) throws InterruptedException, ExecutionException Redirects the givenresponse
, blocking until the redirect is complete.- Parameters:
request
- the original request that triggered the redirectresponse
- the response to the original request- Returns:
- a
Result
object containing the request to the redirected location and its response - Throws:
InterruptedException
- if the thread is interrupted while waiting for the redirect to completeExecutionException
- if the redirect failed- See Also:
-
redirect
Redirects the givenresponse
asynchronously.- Parameters:
request
- the original request that triggered the redirectresponse
- the response to the original requestlistener
- the listener that receives response events- Returns:
- the request to the redirected location
-
extractRedirectURI
Extracts and sanitizes (by making it absolute and escaping paths and query parameters) the redirect URI of the givenresponse
.- Parameters:
response
- the response to extract the redirect URI from- Returns:
- the absolute redirect URI, or null if the response does not contain a valid redirect location
-
sanitize
-
redirect
private Request redirect(Request request, Response response, Response.CompleteListener listener, URI newURI) -
redirect
-
sendRedirect
private Request sendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, URI location, String method) -
fail
-