Package org.eclipse.jetty.client
Class HttpRedirector
- java.lang.Object
-
- org.eclipse.jetty.client.HttpRedirector
-
public class HttpRedirector extends java.lang.ObjectUtility 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
Fields Modifier and Type Field Description private static java.lang.StringATTRIBUTEprivate static java.lang.StringAUTHORITY_REGEXPprivate HttpClientclientprivate static java.lang.StringDESTINATION_REGEXPprivate static java.lang.StringFRAGMENT_REGEXPprivate static LoggerLOGprivate ResponseNotifiernotifierprivate static java.lang.StringPATH_REGEXPprivate static java.lang.StringQUERY_REGEXPprivate static java.lang.StringSCHEME_REGEXPprivate static java.util.regex.PatternURI_PATTERN
-
Constructor Summary
Constructors Constructor Description HttpRedirector(HttpClient client)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.net.URIextractRedirectURI(Response response)Extracts and sanitizes (by making it absolute and escaping paths and query parameters) the redirect URI of the givenresponse.protected voidfail(Request request, java.lang.Throwable failure, Response response)private voidfail(Request request, java.lang.Throwable requestFailure, Response response, java.lang.Throwable responseFailure)protected voidfail(Request request, Response response, java.lang.Throwable failure)booleanisRedirect(Response response)Resultredirect(Request request, Response response)Redirects the givenresponse, blocking until the redirect is complete.Requestredirect(Request request, Response response, Response.CompleteListener listener)Redirects the givenresponseasynchronously.private Requestredirect(Request request, Response response, Response.CompleteListener listener, java.net.URI newURI)private Requestredirect(Request request, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)private java.net.URIsanitize(java.lang.String location)private RequestsendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)
-
-
-
Field Detail
-
LOG
private static final Logger LOG
-
SCHEME_REGEXP
private static final java.lang.String SCHEME_REGEXP
- See Also:
- Constant Field Values
-
AUTHORITY_REGEXP
private static final java.lang.String AUTHORITY_REGEXP
- See Also:
- Constant Field Values
-
DESTINATION_REGEXP
private static final java.lang.String DESTINATION_REGEXP
- See Also:
- Constant Field Values
-
PATH_REGEXP
private static final java.lang.String PATH_REGEXP
- See Also:
- Constant Field Values
-
QUERY_REGEXP
private static final java.lang.String QUERY_REGEXP
- See Also:
- Constant Field Values
-
FRAGMENT_REGEXP
private static final java.lang.String FRAGMENT_REGEXP
- See Also:
- Constant Field Values
-
URI_PATTERN
private static final java.util.regex.Pattern URI_PATTERN
-
ATTRIBUTE
private static final java.lang.String ATTRIBUTE
-
client
private final HttpClient client
-
notifier
private final ResponseNotifier notifier
-
-
Constructor Detail
-
HttpRedirector
public HttpRedirector(HttpClient client)
-
-
Method Detail
-
isRedirect
public boolean isRedirect(Response response)
- 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 java.lang.InterruptedException, java.util.concurrent.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
Resultobject containing the request to the redirected location and its response - Throws:
java.lang.InterruptedException- if the thread is interrupted while waiting for the redirect to completejava.util.concurrent.ExecutionException- if the redirect failed- See Also:
redirect(Request, Response, Response.CompleteListener)
-
redirect
public Request redirect(Request request, Response response, Response.CompleteListener listener)
Redirects the givenresponseasynchronously.- 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
public java.net.URI extractRedirectURI(Response response)
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
private java.net.URI sanitize(java.lang.String location)
-
redirect
private Request redirect(Request request, Response response, Response.CompleteListener listener, java.net.URI newURI)
-
redirect
private Request redirect(Request request, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)
-
sendRedirect
private Request sendRedirect(HttpRequest httpRequest, Response response, Response.CompleteListener listener, java.net.URI location, java.lang.String method)
-
-