DelegatingSSLSocketFactory.java revision 26f2557b26ea23326178f029e07a8adbfc27d0bf
1/* 2 * Copyright (C) 2014 The Android Open Source Project 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 17package tests.net; 18 19import java.io.IOException; 20import java.net.InetAddress; 21import java.net.Socket; 22import java.net.UnknownHostException; 23 24import javax.net.ssl.SSLSocket; 25import javax.net.ssl.SSLSocketFactory; 26 27/** 28 * {@link SSLSocketFactory} which delegates all invocations to the provided delegate 29 * {@code SSLSocketFactory}. 30 */ 31public class DelegatingSSLSocketFactory extends SSLSocketFactory { 32 33 private final SSLSocketFactory mDelegate; 34 35 public DelegatingSSLSocketFactory(SSLSocketFactory delegate) { 36 this.mDelegate = delegate; 37 } 38 39 /** 40 * Invoked after obtaining a socket from the delegate and before returning it to the caller. 41 * 42 * <p>The default implementation does nothing. 43 */ 44 protected SSLSocket configureSocket(SSLSocket socket) throws IOException { 45 return socket; 46 } 47 48 @Override 49 public String[] getDefaultCipherSuites() { 50 return mDelegate.getDefaultCipherSuites(); 51 } 52 53 @Override 54 public String[] getSupportedCipherSuites() { 55 return mDelegate.getSupportedCipherSuites(); 56 } 57 58 @Override 59 public Socket createSocket() throws IOException { 60 SSLSocket socket = (SSLSocket) mDelegate.createSocket(); 61 return configureSocket(socket); 62 } 63 64 @Override 65 public Socket createSocket(Socket s, String host, int port, boolean autoClose) 66 throws IOException { 67 SSLSocket socket = (SSLSocket) mDelegate.createSocket(s, host, port, autoClose); 68 return configureSocket(socket); 69 } 70 71 @Override 72 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 73 SSLSocket socket = (SSLSocket) mDelegate.createSocket(host, port); 74 return configureSocket(socket); 75 } 76 77 @Override 78 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) 79 throws IOException, UnknownHostException { 80 SSLSocket socket = (SSLSocket) mDelegate.createSocket(host, port, localHost, localPort); 81 return configureSocket(socket); 82 } 83 84 @Override 85 public Socket createSocket(InetAddress host, int port) throws IOException { 86 SSLSocket socket = (SSLSocket) mDelegate.createSocket(host, port); 87 return configureSocket(socket); 88 } 89 90 @Override 91 public Socket createSocket(InetAddress address, int port, InetAddress localAddress, 92 int localPort) throws IOException { 93 SSLSocket socket = (SSLSocket) mDelegate.createSocket(address, port, localAddress, localPort); 94 return configureSocket(socket); 95 } 96} 97