For example, this might be used to refresh an OAuth 2 token:
public static class RefreshTokenHandler implements HttpUnsuccessfulResponseHandler { public boolean handleResponse( HttpRequest request, HttpResponse response, boolean retrySupported) throws IOException { if (response.statusCode == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) { refreshToken(); } return false; } }
Sample usage with a request factory:
public static HttpRequestFactory createRequestFactory(HttpTransport transport) { final RefreshTokenHandler handler = new RefreshTokenHandler(); return transport.createRequestFactory(new HttpRequestInitializer() { public void initialize(HttpRequest request) { request.unsuccessfulResponseHandler = handler; } }); }
If you have a custom unsuccessful response handler, use this more complex example:
public static HttpRequestFactory createRequestFactory(HttpTransport transport) { final RefreshTokenHandler handler = new RefreshTokenHandler(); return transport.createRequestFactory(new HttpRequestInitializer() { public void initialize(HttpRequest request) { request.unsuccessfulResponseHandler = new HttpUnsuccessfulResponseHandler() { public boolean handleResponse( HttpRequest request, HttpResponse response, boolean retrySupported) throws IOException { return handler.handleResponse(request, response, retrySupported); } }; } }); }
Implementations should normally be thread-safe.
@author moshenko@google.com (Jacob Moshenko) @since 1.4
|
|
|
|