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.ServerSocket; 21 import javax.net.ServerSocketFactory; 22 23 /** 24 * A {@link ServerSocketFactory} that delegates calls. Sockets can be configured after creation by 25 * overriding {@link #configureServerSocket(java.net.ServerSocket)}. 26 */ 27 public class DelegatingServerSocketFactory extends ServerSocketFactory { 28 29 private final ServerSocketFactory delegate; 30 DelegatingServerSocketFactory(ServerSocketFactory delegate)31 public DelegatingServerSocketFactory(ServerSocketFactory delegate) { 32 this.delegate = delegate; 33 } 34 35 @Override createServerSocket()36 public ServerSocket createServerSocket() throws IOException { 37 ServerSocket serverSocket = delegate.createServerSocket(); 38 return configureServerSocket(serverSocket); 39 } 40 41 @Override createServerSocket(int port)42 public ServerSocket createServerSocket(int port) throws IOException { 43 ServerSocket serverSocket = delegate.createServerSocket(port); 44 return configureServerSocket(serverSocket); 45 } 46 47 @Override createServerSocket(int port, int backlog)48 public ServerSocket createServerSocket(int port, int backlog) throws IOException { 49 ServerSocket serverSocket = delegate.createServerSocket(port, backlog); 50 return configureServerSocket(serverSocket); 51 } 52 53 @Override createServerSocket(int port, int backlog, InetAddress ifAddress)54 public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) 55 throws IOException { 56 ServerSocket serverSocket = delegate.createServerSocket(port, backlog, ifAddress); 57 return configureServerSocket(serverSocket); 58 } 59 configureServerSocket(ServerSocket serverSocket)60 protected ServerSocket configureServerSocket(ServerSocket serverSocket) throws IOException { 61 // No-op by default. 62 return serverSocket; 63 } 64 } 65