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; 17 18 import java.io.IOException; 19 import java.net.InetAddress; 20 import java.net.Socket; 21 import java.net.UnknownHostException; 22 import javax.net.SocketFactory; 23 import javax.net.ssl.SSLSocket; 24 import javax.net.ssl.SSLSocketFactory; 25 26 /** 27 * A {@link SSLSocketFactory} that delegates calls. Sockets can be configured after 28 * creation by overriding {@link #configureSocket(javax.net.ssl.SSLSocket)}. 29 */ 30 public class DelegatingSSLSocketFactory extends SSLSocketFactory { 31 32 private final SSLSocketFactory delegate; 33 DelegatingSSLSocketFactory(SSLSocketFactory delegate)34 public DelegatingSSLSocketFactory(SSLSocketFactory delegate) { 35 this.delegate = delegate; 36 } 37 createSocket()38 @Override public SSLSocket createSocket() throws IOException { 39 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(); 40 return configureSocket(sslSocket); 41 } 42 createSocket(String host, int port)43 @Override public SSLSocket createSocket(String host, int port) throws IOException { 44 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port); 45 return configureSocket(sslSocket); 46 } 47 createSocket( String host, int port, InetAddress localAddress, int localPort)48 @Override public SSLSocket createSocket( 49 String host, int port, InetAddress localAddress, int localPort) throws IOException { 50 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port, localAddress, localPort); 51 return configureSocket(sslSocket); 52 } 53 createSocket(InetAddress host, int port)54 @Override public SSLSocket createSocket(InetAddress host, int port) throws IOException { 55 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port); 56 return configureSocket(sslSocket); 57 } 58 createSocket( InetAddress host, int port, InetAddress localAddress, int localPort)59 @Override public SSLSocket createSocket( 60 InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException { 61 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(host, port, localAddress, localPort); 62 return configureSocket(sslSocket); 63 } 64 getDefaultCipherSuites()65 @Override public String[] getDefaultCipherSuites() { 66 return delegate.getDefaultCipherSuites(); 67 } 68 getSupportedCipherSuites()69 @Override public String[] getSupportedCipherSuites() { 70 return delegate.getSupportedCipherSuites(); 71 } 72 createSocket( Socket socket, String host, int port, boolean autoClose)73 @Override public SSLSocket createSocket( 74 Socket socket, String host, int port, boolean autoClose) throws IOException { 75 SSLSocket sslSocket = (SSLSocket) delegate.createSocket(socket, host, port, autoClose); 76 return configureSocket(sslSocket); 77 } 78 configureSocket(SSLSocket sslSocket)79 protected SSLSocket configureSocket(SSLSocket sslSocket) throws IOException { 80 // No-op by default. 81 return sslSocket; 82 } 83 } 84