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 24 /** 25 * A {@link SocketFactory} that delegates calls. Sockets can be configured after creation by 26 * overriding {@link #configureSocket(java.net.Socket)}. 27 */ 28 public class DelegatingSocketFactory extends SocketFactory { 29 30 private final SocketFactory delegate; 31 DelegatingSocketFactory(SocketFactory delegate)32 public DelegatingSocketFactory(SocketFactory delegate) { 33 this.delegate = delegate; 34 } 35 36 @Override createSocket()37 public Socket createSocket() throws IOException { 38 Socket socket = delegate.createSocket(); 39 return configureSocket(socket); 40 } 41 42 @Override createSocket(String host, int port)43 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 44 Socket socket = delegate.createSocket(host, port); 45 return configureSocket(socket); 46 } 47 48 @Override createSocket(String host, int port, InetAddress localAddress, int localPort)49 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) 50 throws IOException, UnknownHostException { 51 Socket socket = delegate.createSocket(host, port, localAddress, localPort); 52 return configureSocket(socket); 53 } 54 55 @Override createSocket(InetAddress host, int port)56 public Socket createSocket(InetAddress host, int port) throws IOException { 57 Socket socket = delegate.createSocket(host, port); 58 return configureSocket(socket); 59 } 60 61 @Override createSocket(InetAddress host, int port, InetAddress localAddress, int localPort)62 public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort) 63 throws IOException { 64 Socket socket = delegate.createSocket(host, port, localAddress, localPort); 65 return configureSocket(socket); 66 } 67 configureSocket(Socket socket)68 protected Socket configureSocket(Socket socket) throws IOException { 69 // No-op by default. 70 return socket; 71 } 72 } 73