All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.larksuite.oapi.okhttp3_14.internal.http.RealInterceptorChain Maven / Gradle / Ivy

Go to download

Larksuite open platform facilitates the integration of enterprise applications and larksuite, making collaboration and management more efficient

There is a newer version: 1.0.18-rc8
Show newest version
/*
 * Copyright (C) 2016 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.larksuite.oapi.okhttp3_14.internal.http;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

import com.larksuite.oapi.okhttp3_14.*;
import com.larksuite.oapi.okhttp3_14.internal.connection.Exchange;
import com.larksuite.oapi.okhttp3_14.internal.connection.Transmitter;

import static com.larksuite.oapi.okhttp3_14.internal.Util.checkDuration;

/**
 * A concrete interceptor chain that carries the entire interceptor chain: all application
 * interceptors, the OkHttp core, all network interceptors, and finally the network caller.
 *
 * 

If the chain is for an application interceptor then {@link #connection} must be null. * Otherwise it is for a network interceptor and {@link #connection} must be non-null. */ public final class RealInterceptorChain implements Interceptor.Chain { private final List interceptors; private final Transmitter transmitter; private final @Nullable Exchange exchange; private final int index; private final Request request; private final Call call; private final int connectTimeout; private final int readTimeout; private final int writeTimeout; private int calls; public RealInterceptorChain(List interceptors, Transmitter transmitter, @Nullable Exchange exchange, int index, Request request, Call call, int connectTimeout, int readTimeout, int writeTimeout) { this.interceptors = interceptors; this.transmitter = transmitter; this.exchange = exchange; this.index = index; this.request = request; this.call = call; this.connectTimeout = connectTimeout; this.readTimeout = readTimeout; this.writeTimeout = writeTimeout; } @Override public @Nullable Connection connection() { return exchange != null ? exchange.connection() : null; } @Override public int connectTimeoutMillis() { return connectTimeout; } @Override public Interceptor.Chain withConnectTimeout(int timeout, TimeUnit unit) { int millis = checkDuration("timeout", timeout, unit); return new RealInterceptorChain(interceptors, transmitter, exchange, index, request, call, millis, readTimeout, writeTimeout); } @Override public int readTimeoutMillis() { return readTimeout; } @Override public Interceptor.Chain withReadTimeout(int timeout, TimeUnit unit) { int millis = checkDuration("timeout", timeout, unit); return new RealInterceptorChain(interceptors, transmitter, exchange, index, request, call, connectTimeout, millis, writeTimeout); } @Override public int writeTimeoutMillis() { return writeTimeout; } @Override public Interceptor.Chain withWriteTimeout(int timeout, TimeUnit unit) { int millis = checkDuration("timeout", timeout, unit); return new RealInterceptorChain(interceptors, transmitter, exchange, index, request, call, connectTimeout, readTimeout, millis); } public Transmitter transmitter() { return transmitter; } public Exchange exchange() { if (exchange == null) throw new IllegalStateException(); return exchange; } @Override public Call call() { return call; } @Override public Request request() { return request; } @Override public Response proceed(Request request) throws IOException { return proceed(request, transmitter, exchange); } public Response proceed(Request request, Transmitter transmitter, @Nullable Exchange exchange) throws IOException { if (index >= interceptors.size()) throw new AssertionError(); calls++; // If we already have a stream, confirm that the incoming request will use it. if (this.exchange != null && !this.exchange.connection().supportsUrl(request.url())) { throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must retain the same host and port"); } // If we already have a stream, confirm that this is the only call to chain.proceed(). if (this.exchange != null && calls > 1) { throw new IllegalStateException("network interceptor " + interceptors.get(index - 1) + " must call proceed() exactly once"); } // Call the next interceptor in the chain. RealInterceptorChain next = new RealInterceptorChain(interceptors, transmitter, exchange, index + 1, request, call, connectTimeout, readTimeout, writeTimeout); Interceptor interceptor = interceptors.get(index); Response response = interceptor.intercept(next); // Confirm that the next interceptor made its required call to chain.proceed(). if (exchange != null && index + 1 < interceptors.size() && next.calls != 1) { throw new IllegalStateException("network interceptor " + interceptor + " must call proceed() exactly once"); } // Confirm that the intercepted response isn't null. if (response == null) { throw new NullPointerException("interceptor " + interceptor + " returned null"); } if (response.body() == null) { throw new IllegalStateException( "interceptor " + interceptor + " returned a response with no body"); } return response; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy