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