Class DefaultRedirectStrategy

java.lang.Object
com.azure.core.http.policy.DefaultRedirectStrategy
All Implemented Interfaces:
RedirectStrategy

public final class DefaultRedirectStrategy extends Object implements RedirectStrategy
The DefaultRedirectStrategy class is an implementation of the RedirectStrategy interface. This strategy uses the provided maximum retry attempts, header name to look up redirect URL value for, HTTP methods and a known set of redirect status response codes (301, 302, 307, 308) to determine if a request should be redirected.

This class is useful when you need to handle HTTP redirects. It ensures that the requests are redirected correctly based on the response status code and the maximum number of redirect attempts.

Code sample:

In this example, a DefaultRedirectStrategy is created with a maximum of 3 redirect attempts, "Location" as the header name to locate the redirect URL, and GET and HEAD as the allowed methods for performing the redirect. The strategy is then used in a RedirectPolicy which can be added to the pipeline. For a request sent by the pipeline, if the server responds with a redirect status code and provides a "Location" header, the request will be redirected up to 3 times as needed.

 DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(3, "Location",
     EnumSet.of(HttpMethod.GET, HttpMethod.HEAD));
 RedirectPolicy redirectPolicy = new RedirectPolicy(redirectStrategy);
 
See Also:
  • Constructor Details

    • DefaultRedirectStrategy

      public DefaultRedirectStrategy()
      Creates an instance of DefaultRedirectStrategy with a maximum number of redirect attempts 3, header name "Location" to locate the redirect url in the response headers and HttpMethod.GET and HttpMethod.HEAD as allowed methods for performing the redirect.
    • DefaultRedirectStrategy

      public DefaultRedirectStrategy(int maxAttempts)
      Creates an instance of DefaultRedirectStrategy with the provided number of redirect attempts and default header name "Location" to locate the redirect url in the response headers and HttpMethod.GET and HttpMethod.HEAD as allowed methods for performing the redirect.
      Parameters:
      maxAttempts - The max number of redirect attempts that can be made.
      Throws:
      IllegalArgumentException - if maxAttempts is less than 0.
    • DefaultRedirectStrategy

      public DefaultRedirectStrategy(int maxAttempts, String locationHeader, Set<HttpMethod> allowedMethods)
      Creates an instance of DefaultRedirectStrategy.
      Parameters:
      maxAttempts - The max number of redirect attempts that can be made.
      locationHeader - The header name containing the redirect URL.
      allowedMethods - The set of HttpMethod that are allowed to be redirected.
      Throws:
      IllegalArgumentException - if maxAttempts is less than 0.
  • Method Details