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