public interface HttpResponseInterceptor
HttpRequest.execute()
before returning
a successful response or throwing an exception for an unsuccessful response.
For example, this might be used to add a simple timer on requests:
public static class TimerResponseInterceptor implements HttpResponseInterceptor { private final long startTime = System.nanoTime(); public void interceptResponse(HttpResponse response) { long elapsedNanos = System.nanoTime() - startTime; System.out.println("elapsed seconds: " + TimeUnit.NANOSECONDS.toSeconds(elapsedNanos) + "s"); } }
Sample usage with a request factory:
public static HttpRequestFactory createRequestFactory(HttpTransport transport) { return transport.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) { request.setResponseInterceptor(new TimerResponseInterceptor()); } }); }
More complex usage example:
public static HttpRequestFactory createRequestFactory2(HttpTransport transport) { final HttpResponseInterceptor responseInterceptor = new TimerResponseInterceptor(); return transport.createRequestFactory(new HttpRequestInitializer() { public void initialize(HttpRequest request) { request.setResponseInterceptor(new HttpResponseInterceptor() { public void interceptResponse(HttpResponse response) throws IOException { responseInterceptor.interceptResponse(response); } }); } }); }
Implementations should normally be thread-safe.
Modifier and Type | Method and Description |
---|---|
void |
interceptResponse(HttpResponse response)
Invoked at the end of
HttpRequest.execute() before returning a successful response or
throwing an exception for an unsuccessful response. |
void interceptResponse(HttpResponse response) throws IOException
HttpRequest.execute()
before returning a successful response or
throwing an exception for an unsuccessful response.
Do not read from the content stream unless you intend to throw an exception. Otherwise, it
would prevent the caller of HttpRequest.execute()
to be able to read the stream from
HttpResponse.getContent()
. If you intend to throw an exception, you should parse the
response, or alternatively pass the response as part of the exception.
response
- HTTP responseIOException
Copyright © 2011–2024 Google. All rights reserved.