Class HttpRedirector


  • public class HttpRedirector
    extends java.lang.Object
    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();
     }
     
    • 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 given response, blocking until the redirect is complete.
        Parameters:
        request - the original request that triggered the redirect
        response - the response to the original request
        Returns:
        a Result object 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 complete
        java.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 given response asynchronously.
        Parameters:
        request - the original request that triggered the redirect
        response - the response to the original request
        listener - 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 given response.
        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
      • fail

        protected void fail​(Request request,
                            Response response,
                            java.lang.Throwable failure)
      • fail

        protected void fail​(Request request,
                            java.lang.Throwable failure,
                            Response response)