public void initialize(HttpRequest request) throws IOException { if (wrapped != null) { wrapped.initialize(request); } request.setLoggingEnabled(true); request.setCurlLoggingEnabled(false); request.setContentLoggingLimit(Integer.MAX_VALUE); request.setResponseInterceptor( new HttpResponseInterceptor() { private HttpResponseInterceptor wrapped = null; public void interceptResponse(HttpResponse response) throws IOException { if (wrapped != null) { wrapped.interceptResponse(response); } response.setLoggingEnabled(true); response.setContentLoggingLimit(Integer.MAX_VALUE); } public HttpResponseInterceptor setWrapped(HttpResponseInterceptor toWrap) { this.wrapped = toWrap; return this; } }.setWrapped(request.getResponseInterceptor())); }
/** * Initializes this {@link com.google.api.client.auth.oauth2.Credential} subclass. Adds an * additional interceptor that properly sets the default encoding of the "application/json" * media type to UTF-8 as required by <a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>. * * @param request The {@link HttpRequest} to be initialized. */ @Override public void initialize(HttpRequest request) throws IOException { // Initially, pass the initialization on to super. super.initialize(request); // Set the timeouts. request.setConnectTimeout(3 * 600000); // 10 minutes connect timeout request.setReadTimeout(3 * 600000); // 10 minutes read timeout // Add an additional interceptor. request.setResponseInterceptor(new HttpResponseInterceptor() { @Override public void interceptResponse(HttpResponse response) throws IOException { // Decode JSON as UTF-8 if no charset is specified. if (response.getContentType().equals("application/json") && response.getMediaType() != null && response.getMediaType().getCharsetParameter() == null) { response.getMediaType().setCharsetParameter(StandardCharsets.UTF_8); } } }); // Add a custom handler for unsuccessful responses. request.setUnsuccessfulResponseHandler( new UnsuccessfulResponseHandler()); }
/** * @param additionalIgnoredResponseCodes a list of HTTP status codes that should not be logged. * @param responseInterceptor HttpResponseInterceptor to be applied on all requests. May be null. */ public RetryHttpRequestInitializer( Collection<Integer> additionalIgnoredResponseCodes, @Nullable HttpResponseInterceptor responseInterceptor) { this(NanoClock.SYSTEM, Sleeper.DEFAULT, additionalIgnoredResponseCodes, responseInterceptor); }
/** * Visible for testing. * * @param nanoClock used as a timing source for knowing how much time has elapsed. * @param sleeper used to sleep between retries. * @param additionalIgnoredResponseCodes a list of HTTP status codes that should not be logged. */ RetryHttpRequestInitializer( NanoClock nanoClock, Sleeper sleeper, Collection<Integer> additionalIgnoredResponseCodes, HttpResponseInterceptor responseInterceptor) { this.nanoClock = nanoClock; this.sleeper = sleeper; this.ignoredResponseCodes.addAll(additionalIgnoredResponseCodes); this.responseInterceptor = responseInterceptor; }
@Override public void initialize(HttpRequest request) throws IOException { List<HttpIOExceptionHandler> ioExceptionHandlers = new ArrayList<>(); List<HttpUnsuccessfulResponseHandler> unsuccessfulResponseHandlers = new ArrayList<>(); List<HttpExecuteInterceptor> interceptors = new ArrayList<>(); List<HttpResponseInterceptor> responseInterceptors = new ArrayList<>(); for (HttpRequestInitializer initializer : initializers) { initializer.initialize(request); if (request.getIOExceptionHandler() != null) { ioExceptionHandlers.add(request.getIOExceptionHandler()); request.setIOExceptionHandler(null); } if (request.getUnsuccessfulResponseHandler() != null) { unsuccessfulResponseHandlers.add(request.getUnsuccessfulResponseHandler()); request.setUnsuccessfulResponseHandler(null); } if (request.getInterceptor() != null) { interceptors.add(request.getInterceptor()); request.setInterceptor(null); } if (request.getResponseInterceptor() != null) { responseInterceptors.add(request.getResponseInterceptor()); request.setResponseInterceptor(null); } } request.setIOExceptionHandler( makeIoExceptionHandler(ioExceptionHandlers)); request.setUnsuccessfulResponseHandler( makeUnsuccessfulResponseHandler(unsuccessfulResponseHandlers)); request.setInterceptor( makeInterceptor(interceptors)); request.setResponseInterceptor( makeResponseInterceptor(responseInterceptors)); }
private HttpResponseInterceptor makeResponseInterceptor( final Iterable<HttpResponseInterceptor> responseInterceptors) { return new HttpResponseInterceptor() { @Override public void interceptResponse(HttpResponse response) throws IOException { for (HttpResponseInterceptor interceptor : responseInterceptors) { interceptor.interceptResponse(response); } } }; }
@VisibleForTesting void setInterceptor(HttpResponseInterceptor interceptor) { this.interceptor = interceptor; }