Package com.google.api.client.http
Interface HttpResponseInterceptor
-
public interface HttpResponseInterceptor
HTTP response interceptor to intercept the end ofHttpRequest.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.
- Since:
- 1.13
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
interceptResponse(HttpResponse response)
Invoked at the end ofHttpRequest.execute()
before returning a successful response or throwing an exception for an unsuccessful response.
-
-
-
Method Detail
-
interceptResponse
void interceptResponse(HttpResponse response) throws java.io.IOException
Invoked at the end ofHttpRequest.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 fromHttpResponse.getContent()
. If you intend to throw an exception, you should parse the response, or alternatively pass the response as part of the exception.- Parameters:
response
- HTTP response- Throws:
java.io.IOException
-
-