Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2015 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.SocketFactory;
     22 /**
     23  * {@link SocketFactory} which delegates all invocations to the provided delegate
     24  * {@code SocketFactory}.
     25  */
     26 public class DelegatingSocketFactory extends SocketFactory {
     27     private final SocketFactory mDelegate;
     28     public DelegatingSocketFactory(SocketFactory delegate) {
     29         this.mDelegate = delegate;
     30     }
     31     /**
     32      * Invoked after obtaining a socket from the delegate and before returning it to the caller.
     33      *
     34      * <p>The default implementation does nothing.
     35      */
     36     protected Socket configureSocket(Socket socket) throws IOException {
     37         return socket;
     38     }
     39     @Override
     40     public Socket createSocket() throws IOException {
     41         Socket socket = mDelegate.createSocket();
     42         return configureSocket(socket);
     43     }
     44     @Override
     45     public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
     46         Socket socket = mDelegate.createSocket(host, port);
     47         return configureSocket(socket);
     48     }
     49     @Override
     50     public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
     51             throws IOException, UnknownHostException {
     52         Socket socket = mDelegate.createSocket(host, port, localHost, localPort);
     53         return configureSocket(socket);
     54     }
     55     @Override
     56     public Socket createSocket(InetAddress host, int port) throws IOException {
     57         Socket socket = mDelegate.createSocket(host, port);
     58         return configureSocket(socket);
     59     }
     60     @Override
     61     public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
     62             int localPort) throws IOException {
     63         Socket socket = mDelegate.createSocket(address, port, localAddress, localPort);
     64         return configureSocket(socket);
     65     }
     66 }
     67