Home | History | Annotate | Download | only in okhttp
      1 /*
      2  * Copyright (C) 2012 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;
     17 
     18 import com.squareup.okhttp.internal.http.HttpURLConnectionImpl;
     19 import com.squareup.okhttp.internal.http.HttpsURLConnectionImpl;
     20 import java.net.CookieHandler;
     21 import java.net.HttpURLConnection;
     22 import java.net.Proxy;
     23 import java.net.ProxySelector;
     24 import java.net.ResponseCache;
     25 import java.net.URL;
     26 import javax.net.ssl.HostnameVerifier;
     27 import javax.net.ssl.HttpsURLConnection;
     28 import javax.net.ssl.SSLSocketFactory;
     29 
     30 /** Configures and creates HTTP connections. */
     31 public final class OkHttpClient {
     32   private Proxy proxy;
     33   private ProxySelector proxySelector;
     34   private CookieHandler cookieHandler;
     35   private ResponseCache responseCache;
     36   private SSLSocketFactory sslSocketFactory;
     37   private HostnameVerifier hostnameVerifier;
     38   private ConnectionPool connectionPool;
     39   private boolean followProtocolRedirects = true;
     40 
     41   /**
     42    * Sets the HTTP proxy that will be used by connections created by this
     43    * client. This takes precedence over {@link #setProxySelector}, which is
     44    * only honored when this proxy is null (which it is by default). To disable
     45    * proxy use completely, call {@code setProxy(Proxy.NO_PROXY)}.
     46    */
     47   public OkHttpClient setProxy(Proxy proxy) {
     48     this.proxy = proxy;
     49     return this;
     50   }
     51 
     52   public Proxy getProxy() {
     53     return proxy;
     54   }
     55 
     56   /**
     57    * Sets the proxy selection policy to be used if no {@link #setProxy proxy}
     58    * is specified explicitly. The proxy selector may return multiple proxies;
     59    * in that case they will be tried in sequence until a successful connection
     60    * is established.
     61    *
     62    * <p>If unset, the {@link ProxySelector#getDefault() system-wide default}
     63    * proxy selector will be used.
     64    */
     65   public OkHttpClient setProxySelector(ProxySelector proxySelector) {
     66     this.proxySelector = proxySelector;
     67     return this;
     68   }
     69 
     70   public ProxySelector getProxySelector() {
     71     return proxySelector;
     72   }
     73 
     74   /**
     75    * Sets the cookie handler to be used to read outgoing cookies and write
     76    * incoming cookies.
     77    *
     78    * <p>If unset, the {@link CookieHandler#getDefault() system-wide default}
     79    * cookie handler will be used.
     80    */
     81   public OkHttpClient setCookieHandler(CookieHandler cookieHandler) {
     82     this.cookieHandler = cookieHandler;
     83     return this;
     84   }
     85 
     86   public CookieHandler getCookieHandler() {
     87     return cookieHandler;
     88   }
     89 
     90   /**
     91    * Sets the response cache to be used to read and write cached responses.
     92    *
     93    * <p>If unset, the {@link ResponseCache#getDefault() system-wide default}
     94    * response cache will be used.
     95    */
     96   public OkHttpClient setResponseCache(ResponseCache responseCache) {
     97     this.responseCache = responseCache;
     98     return this;
     99   }
    100 
    101   public ResponseCache getResponseCache() {
    102     return responseCache;
    103   }
    104 
    105   /**
    106    * Sets the socket factory used to secure HTTPS connections.
    107    *
    108    * <p>If unset, the {@link HttpsURLConnection#getDefaultSSLSocketFactory()
    109    * system-wide default} SSL socket factory will be used.
    110    */
    111   public OkHttpClient setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
    112     this.sslSocketFactory = sslSocketFactory;
    113     return this;
    114   }
    115 
    116   public SSLSocketFactory getSslSocketFactory() {
    117     return sslSocketFactory;
    118   }
    119 
    120   /**
    121    * Sets the verifier used to confirm that response certificates apply to
    122    * requested hostnames for HTTPS connections.
    123    *
    124    * <p>If unset, the {@link HttpsURLConnection#getDefaultHostnameVerifier()
    125    * system-wide default} hostname verifier will be used.
    126    */
    127   public OkHttpClient setHostnameVerifier(HostnameVerifier hostnameVerifier) {
    128     this.hostnameVerifier = hostnameVerifier;
    129     return this;
    130   }
    131 
    132   public HostnameVerifier getHostnameVerifier() {
    133     return hostnameVerifier;
    134   }
    135 
    136   /**
    137    * Sets the connection pool used to recycle HTTP and HTTPS connections.
    138    *
    139    * <p>If unset, the {@link ConnectionPool#getDefault() system-wide
    140    * default} connection pool will be used.
    141    */
    142   public OkHttpClient setConnectionPool(ConnectionPool connectionPool) {
    143     this.connectionPool = connectionPool;
    144     return this;
    145   }
    146 
    147   public ConnectionPool getConnectionPool() {
    148     return connectionPool;
    149   }
    150 
    151   /**
    152    * Configure this client to follow redirects from HTTPS to HTTP and from HTTP
    153    * to HTTPS.
    154    *
    155    * <p>If unset, protocol redirects will be followed. This is different than
    156    * the built-in {@code HttpURLConnection}'s default.
    157    */
    158   public OkHttpClient setFollowProtocolRedirects(boolean followProtocolRedirects) {
    159     this.followProtocolRedirects = followProtocolRedirects;
    160     return this;
    161   }
    162 
    163   public boolean getFollowProtocolRedirects() {
    164     return followProtocolRedirects;
    165   }
    166 
    167   public HttpURLConnection open(URL url) {
    168     String protocol = url.getProtocol();
    169     if (protocol.equals("http")) {
    170       return new HttpURLConnectionImpl(url, copyWithDefaults());
    171     } else if (protocol.equals("https")) {
    172       return new HttpsURLConnectionImpl(url, copyWithDefaults());
    173     } else {
    174       throw new IllegalArgumentException("Unexpected protocol: " + protocol);
    175     }
    176   }
    177 
    178   /**
    179    * Returns a copy of this OkHttpClient that uses the system-wide default for
    180    * each field that hasn't been explicitly configured.
    181    */
    182   private OkHttpClient copyWithDefaults() {
    183     OkHttpClient result = new OkHttpClient();
    184     result.proxy = proxy;
    185     result.proxySelector = proxySelector != null ? proxySelector : ProxySelector.getDefault();
    186     result.cookieHandler = cookieHandler != null ? cookieHandler : CookieHandler.getDefault();
    187     result.responseCache = responseCache != null ? responseCache : ResponseCache.getDefault();
    188     result.sslSocketFactory = sslSocketFactory != null
    189         ? sslSocketFactory
    190         : HttpsURLConnection.getDefaultSSLSocketFactory();
    191     result.hostnameVerifier = hostnameVerifier != null
    192         ? hostnameVerifier
    193         : HttpsURLConnection.getDefaultHostnameVerifier();
    194     result.connectionPool = connectionPool != null ? connectionPool : ConnectionPool.getDefault();
    195     result.followProtocolRedirects = followProtocolRedirects;
    196     return result;
    197   }
    198 }
    199