Home | History | Annotate | Download | only in internal
      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 
     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 
     54   public abstract Transport newTransport(Connection connection, HttpEngine httpEngine)
     55       throws IOException;
     56 
     57   public abstract boolean clearOwner(Connection connection);
     58 
     59   public abstract void closeIfOwnedBy(Connection connection, Object owner) throws IOException;
     60 
     61   public abstract int recycleCount(Connection connection);
     62 
     63   public abstract void setProtocol(Connection connection, Protocol protocol);
     64 
     65   public abstract void setOwner(Connection connection, HttpEngine httpEngine);
     66 
     67   public abstract boolean isReadable(Connection pooled);
     68 
     69   public abstract void addLenient(Headers.Builder builder, String line);
     70 
     71   public abstract void addLenient(Headers.Builder builder, String name, String value);
     72 
     73   public abstract void setCache(OkHttpClient client, InternalCache internalCache);
     74 
     75   public abstract InternalCache internalCache(OkHttpClient client);
     76 
     77   public abstract void recycle(ConnectionPool pool, Connection connection);
     78 
     79   public abstract RouteDatabase routeDatabase(OkHttpClient client);
     80 
     81   public abstract Network network(OkHttpClient client);
     82 
     83   public abstract void setNetwork(OkHttpClient client, Network network);
     84 
     85   public abstract void connectAndSetOwner(OkHttpClient client, Connection connection,
     86       HttpEngine owner, Request request) throws RouteException;
     87 
     88   public abstract void apply(ConnectionSpec tlsConfiguration, SSLSocket sslSocket,
     89       boolean isFallback);
     90 
     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.
     95   public abstract void callEnqueue(Call call, Callback responseCallback, boolean forWebSocket);
     96   public abstract void callEngineReleaseConnection(Call call) throws IOException;
     97   public abstract Connection callEngineGetConnection(Call call);
     98   public abstract BufferedSource connectionRawSource(Connection connection);
     99   public abstract BufferedSink connectionRawSink(Connection connection);
    100   public abstract void connectionSetOwner(Connection connection, Object owner);
    101 }
    102