DelegatingSSLSocketFactory.java

  1. /*
  2.  * Copyright (c) 2020 Thomas Wolf <thomas.wolf@paranor.ch>
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.internal.transport.http;

  11. import java.io.IOException;
  12. import java.net.InetAddress;
  13. import java.net.Socket;

  14. import javax.net.ssl.SSLSocket;
  15. import javax.net.ssl.SSLSocketFactory;

  16. /**
  17.  * An {@link SSLSocketFactory} that delegates to another factory and allows
  18.  * configuring the created socket via {@link #configure(SSLSocket)} before it is
  19.  * returned.
  20.  */
  21. public abstract class DelegatingSSLSocketFactory extends SSLSocketFactory {

  22.     private final SSLSocketFactory delegate;

  23.     /**
  24.      * Creates a new {@link DelegatingSSLSocketFactory} based on the given
  25.      * delegate.
  26.      *
  27.      * @param delegate
  28.      *            {@link SSLSocketFactory} to delegate to
  29.      */
  30.     public DelegatingSSLSocketFactory(SSLSocketFactory delegate) {
  31.         this.delegate = delegate;
  32.     }

  33.     @Override
  34.     public SSLSocket createSocket() throws IOException {
  35.         return prepare(delegate.createSocket());
  36.     }

  37.     @Override
  38.     public SSLSocket createSocket(String host, int port) throws IOException {
  39.         return prepare(delegate.createSocket(host, port));
  40.     }

  41.     @Override
  42.     public SSLSocket createSocket(String host, int port,
  43.             InetAddress localAddress, int localPort) throws IOException {
  44.         return prepare(
  45.                 delegate.createSocket(host, port, localAddress, localPort));
  46.     }

  47.     @Override
  48.     public SSLSocket createSocket(InetAddress host, int port)
  49.             throws IOException {
  50.         return prepare(delegate.createSocket(host, port));
  51.     }

  52.     @Override
  53.     public SSLSocket createSocket(InetAddress host, int port,
  54.             InetAddress localAddress, int localPort) throws IOException {
  55.         return prepare(
  56.                 delegate.createSocket(host, port, localAddress, localPort));
  57.     }

  58.     @Override
  59.     public SSLSocket createSocket(Socket socket, String host, int port,
  60.             boolean autoClose) throws IOException {
  61.         return prepare(delegate.createSocket(socket, host, port, autoClose));
  62.     }

  63.     @Override
  64.     public String[] getDefaultCipherSuites() {
  65.         return delegate.getDefaultCipherSuites();
  66.     }

  67.     @Override
  68.     public String[] getSupportedCipherSuites() {
  69.         return delegate.getSupportedCipherSuites();
  70.     }

  71.     private SSLSocket prepare(Socket socket) throws IOException {
  72.         SSLSocket sslSocket = (SSLSocket) socket;
  73.         configure(sslSocket);
  74.         return sslSocket;
  75.     }

  76.     /**
  77.      * Configure the newly created socket.
  78.      *
  79.      * @param socket
  80.      *            to configure
  81.      * @throws IOException
  82.      *             if the socket cannot be configured
  83.      */
  84.     protected abstract void configure(SSLSocket socket) throws IOException;

  85. }