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.HttpUrl;
25 import com.squareup.okhttp.OkHttpClient;
26 import com.squareup.okhttp.Protocol;
27 import com.squareup.okhttp.Request;
28 import com.squareup.okhttp.internal.http.HttpEngine;
29 import com.squareup.okhttp.internal.http.RouteException;
30 import com.squareup.okhttp.internal.http.Transport;
31 import java.io.IOException;
32 import java.net.MalformedURLException;
33 import java.net.UnknownHostException;
34 import java.util.logging.Logger;
35 import javax.net.ssl.SSLSocket;
36 import okio.BufferedSink;
37 import okio.BufferedSource;
38 
39 /**
40  * Escalate internal APIs in {@code com.squareup.okhttp} so they can be used
41  * from OkHttp's implementation packages. The only implementation of this
42  * interface is in {@link com.squareup.okhttp.OkHttpClient}.
43  */
44 public abstract class Internal {
45   public static final Logger logger = Logger.getLogger(OkHttpClient.class.getName());
46 
initializeInstanceForTests()47   public static void initializeInstanceForTests() {
48     // Needed in tests to ensure that the instance is actually pointing to something.
49     new OkHttpClient();
50   }
51 
52   public static Internal instance;
53 
newTransport(Connection connection, HttpEngine httpEngine)54   public abstract Transport newTransport(Connection connection, HttpEngine httpEngine)
55       throws IOException;
56 
clearOwner(Connection connection)57   public abstract boolean clearOwner(Connection connection);
58 
closeIfOwnedBy(Connection connection, Object owner)59   public abstract void closeIfOwnedBy(Connection connection, Object owner) throws IOException;
60 
recycleCount(Connection connection)61   public abstract int recycleCount(Connection connection);
62 
setProtocol(Connection connection, Protocol protocol)63   public abstract void setProtocol(Connection connection, Protocol protocol);
64 
setOwner(Connection connection, HttpEngine httpEngine)65   public abstract void setOwner(Connection connection, HttpEngine httpEngine);
66 
isReadable(Connection pooled)67   public abstract boolean isReadable(Connection pooled);
68 
addLenient(Headers.Builder builder, String line)69   public abstract void addLenient(Headers.Builder builder, String line);
70 
addLenient(Headers.Builder builder, String name, String value)71   public abstract void addLenient(Headers.Builder builder, String name, String value);
72 
setCache(OkHttpClient client, InternalCache internalCache)73   public abstract void setCache(OkHttpClient client, InternalCache internalCache);
74 
internalCache(OkHttpClient client)75   public abstract InternalCache internalCache(OkHttpClient client);
76 
recycle(ConnectionPool pool, Connection connection)77   public abstract void recycle(ConnectionPool pool, Connection connection);
78 
routeDatabase(OkHttpClient client)79   public abstract RouteDatabase routeDatabase(OkHttpClient client);
80 
network(OkHttpClient client)81   public abstract Network network(OkHttpClient client);
82 
setNetwork(OkHttpClient client, Network network)83   public abstract void setNetwork(OkHttpClient client, Network network);
84 
connectAndSetOwner(OkHttpClient client, Connection connection, HttpEngine owner, Request request)85   public abstract void connectAndSetOwner(OkHttpClient client, Connection connection,
86       HttpEngine owner, Request request) throws RouteException;
87 
apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket, boolean isFallback)88   public abstract void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket,
89       boolean isFallback);
90 
getHttpUrlChecked(String url)91   public abstract HttpUrl getHttpUrlChecked(String url)
92       throws MalformedURLException, UnknownHostException;
93 
94   // TODO delete the following when web sockets move into the main package.
callEnqueue(Call call, Callback responseCallback, boolean forWebSocket)95   public abstract void callEnqueue(Call call, Callback responseCallback, boolean forWebSocket);
callEngineReleaseConnection(Call call)96   public abstract void callEngineReleaseConnection(Call call) throws IOException;
callEngineGetConnection(Call call)97   public abstract Connection callEngineGetConnection(Call call);
connectionRawSource(Connection connection)98   public abstract BufferedSource connectionRawSource(Connection connection);
connectionRawSink(Connection connection)99   public abstract BufferedSink connectionRawSink(Connection connection);
connectionSetOwner(Connection connection, Object owner)100   public abstract void connectionSetOwner(Connection connection, Object owner);
101 }
102