1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.internal; 17 18 import com.squareup.okhttp.Call; 19 import com.squareup.okhttp.Callback; 20 import com.squareup.okhttp.Connection; 21 import com.squareup.okhttp.ConnectionPool; 22 import com.squareup.okhttp.ConnectionSpec; 23 import com.squareup.okhttp.Headers; 24 import com.squareup.okhttp.OkHttpClient; 25 import com.squareup.okhttp.Protocol; 26 import com.squareup.okhttp.Request; 27 import com.squareup.okhttp.internal.http.HttpEngine; 28 import com.squareup.okhttp.internal.http.RouteException; 29 import com.squareup.okhttp.internal.http.Transport; 30 import java.io.IOException; 31 import java.util.logging.Logger; 32 import javax.net.ssl.SSLSocket; 33 import okio.BufferedSink; 34 import okio.BufferedSource; 35 36 /** 37 * Escalate internal APIs in {@code com.squareup.okhttp} so they can be used 38 * from OkHttp's implementation packages. The only implementation of this 39 * interface is in {@link com.squareup.okhttp.OkHttpClient}. 40 */ 41 public abstract class Internal { 42 public static final Logger logger = Logger.getLogger(OkHttpClient.class.getName()); 43 44 public static void initializeInstanceForTests() { 45 // Needed in tests to ensure that the instance is actually pointing to something. 46 new OkHttpClient(); 47 } 48 49 public static Internal instance; 50 51 public abstract Transport newTransport(Connection connection, HttpEngine httpEngine) 52 throws IOException; 53 54 public abstract boolean clearOwner(Connection connection); 55 56 public abstract void closeIfOwnedBy(Connection connection, Object owner) throws IOException; 57 58 public abstract int recycleCount(Connection connection); 59 60 public abstract void setProtocol(Connection connection, Protocol protocol); 61 62 public abstract void setOwner(Connection connection, HttpEngine httpEngine); 63 64 public abstract boolean isReadable(Connection pooled); 65 66 public abstract void addLenient(Headers.Builder builder, String line); 67 68 public abstract void addLenient(Headers.Builder builder, String name, String value); 69 70 public abstract void setCache(OkHttpClient client, InternalCache internalCache); 71 72 public abstract InternalCache internalCache(OkHttpClient client); 73 74 public abstract void recycle(ConnectionPool pool, Connection connection); 75 76 public abstract RouteDatabase routeDatabase(OkHttpClient client); 77 78 public abstract Network network(OkHttpClient client); 79 80 public abstract void setNetwork(OkHttpClient client, Network network); 81 82 public abstract void connectAndSetOwner(OkHttpClient client, Connection connection, 83 HttpEngine owner, Request request) throws RouteException; 84 85 public abstract void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, 86 boolean isFallback); 87 88 // TODO delete the following when web sockets move into the main package. 89 public abstract void callEnqueue(Call call, Callback responseCallback, boolean forWebSocket); 90 public abstract void callEngineReleaseConnection(Call call) throws IOException; 91 public abstract Connection callEngineGetConnection(Call call); 92 public abstract BufferedSource connectionRawSource(Connection connection); 93 public abstract BufferedSink connectionRawSink(Connection connection); 94 public abstract void connectionSetOwner(Connection connection, Object owner); 95 } 96