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 32 public DelegatingSocketFactory(SocketFactory delegate) { 33 this.delegate = delegate; 34 } 35 36 @Override 37 public Socket createSocket() throws IOException { 38 Socket socket = delegate.createSocket(); 39 configureSocket(socket); 40 return socket; 41 } 42 43 @Override 44 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 45 Socket socket = delegate.createSocket(host, port); 46 configureSocket(socket); 47 return socket; 48 } 49 50 @Override 51 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) 52 throws IOException, UnknownHostException { 53 Socket socket = delegate.createSocket(host, port, localAddress, localPort); 54 configureSocket(socket); 55 return socket; 56 } 57 58 @Override 59 public Socket createSocket(InetAddress host, int port) throws IOException { 60 Socket socket = delegate.createSocket(host, port); 61 configureSocket(socket); 62 return socket; 63 } 64 65 @Override 66 public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort) 67 throws IOException { 68 Socket socket = delegate.createSocket(host, port, localAddress, localPort); 69 configureSocket(socket); 70 return socket; 71 } 72 73 protected void configureSocket(Socket socket) throws IOException { 74 // No-op by default. 75 } 76 } 77