下面列出了怎么用com.google.api.client.http.HttpResponseInterceptor的API类实例代码及写法,或者点击链接到github查看源代码。
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()));
}
InstanceIdClientImpl(
HttpRequestFactory requestFactory,
JsonFactory jsonFactory,
@Nullable HttpResponseInterceptor responseInterceptor) {
this.requestFactory = checkNotNull(requestFactory);
this.jsonFactory = checkNotNull(jsonFactory);
this.responseInterceptor = responseInterceptor;
}
private FirebaseMessagingClientImpl initMessagingClient(
MockLowLevelHttpResponse mockResponse, HttpResponseInterceptor interceptor) {
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(mockResponse)
.build();
return FirebaseMessagingClientImpl.builder()
.setProjectId("test-project")
.setJsonFactory(Utils.getDefaultJsonFactory())
.setRequestFactory(transport.createRequestFactory())
.setChildRequestFactory(Utils.getDefaultTransport().createRequestFactory())
.setResponseInterceptor(interceptor)
.build();
}
private static InstanceIdClientImpl initInstanceIdClient(
final MockLowLevelHttpResponse mockResponse,
final HttpResponseInterceptor interceptor) {
MockHttpTransport transport = new MockHttpTransport.Builder()
.setLowLevelHttpResponse(mockResponse)
.build();
return new InstanceIdClientImpl(
transport.createRequestFactory(),
Utils.getDefaultJsonFactory(),
interceptor);
}
/**
* 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;
this.writeTimeout = 0;
}
@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);
}
}
};
}
/** Create a request suitable for use against this service. */
private HttpRequest buildHttpRequest(boolean usingHead) throws IOException {
Preconditions.checkArgument(uploader == null);
Preconditions.checkArgument(!usingHead || requestMethod.equals(HttpMethods.GET));
String requestMethodToUse = usingHead ? HttpMethods.HEAD : requestMethod;
final HttpRequest httpRequest = getAbstractGoogleClient()
.getRequestFactory().buildRequest(requestMethodToUse, buildHttpRequestUrl(), httpContent);
new MethodOverride().intercept(httpRequest);
httpRequest.setParser(getAbstractGoogleClient().getObjectParser());
// custom methods may use POST with no content but require a Content-Length header
if (httpContent == null && (requestMethod.equals(HttpMethods.POST)
|| requestMethod.equals(HttpMethods.PUT) || requestMethod.equals(HttpMethods.PATCH))) {
httpRequest.setContent(new EmptyContent());
}
httpRequest.getHeaders().putAll(requestHeaders);
if (!disableGZipContent) {
httpRequest.setEncoding(new GZipEncoding());
}
httpRequest.setResponseReturnRawInputStream(returnRawInputStream);
final HttpResponseInterceptor responseInterceptor = httpRequest.getResponseInterceptor();
httpRequest.setResponseInterceptor(new HttpResponseInterceptor() {
public void interceptResponse(HttpResponse response) throws IOException {
if (responseInterceptor != null) {
responseInterceptor.interceptResponse(response);
}
if (!response.isSuccessStatusCode() && httpRequest.getThrowExceptionOnExecuteError()) {
throw newExceptionOnError(response);
}
}
});
return httpRequest;
}
@VisibleForTesting
void setInterceptor(HttpResponseInterceptor interceptor) {
httpHelper.setInterceptor(interceptor);
}
void setInterceptor(HttpResponseInterceptor interceptor) {
this.interceptor = interceptor;
}
void setInterceptor(HttpResponseInterceptor interceptor) {
this.interceptor = interceptor;
}
@VisibleForTesting
void setInterceptor(HttpResponseInterceptor interceptor) {
this.interceptor = interceptor;
}
@VisibleForTesting
void setInterceptor(HttpResponseInterceptor interceptor) {
this.interceptor = interceptor;
}
Builder setResponseInterceptor(HttpResponseInterceptor responseInterceptor) {
this.responseInterceptor = responseInterceptor;
return this;
}
/**
* @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);
}