View Javadoc
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  
12  import java.io.IOException;
13  import java.net.InetAddress;
14  import java.net.Socket;
15  
16  import javax.net.ssl.SSLSocket;
17  import javax.net.ssl.SSLSocketFactory;
18  
19  /**
20   * An {@link SSLSocketFactory} that delegates to another factory and allows
21   * configuring the created socket via {@link #configure(SSLSocket)} before it is
22   * returned.
23   */
24  public abstract class DelegatingSSLSocketFactory extends SSLSocketFactory {
25  
26  	private final SSLSocketFactory delegate;
27  
28  	/**
29  	 * Creates a new {@link DelegatingSSLSocketFactory} based on the given
30  	 * delegate.
31  	 *
32  	 * @param delegate
33  	 *            {@link SSLSocketFactory} to delegate to
34  	 */
35  	public DelegatingSSLSocketFactory(SSLSocketFactory delegate) {
36  		this.delegate = delegate;
37  	}
38  
39  	@Override
40  	public SSLSocket createSocket() throws IOException {
41  		return prepare(delegate.createSocket());
42  	}
43  
44  	@Override
45  	public SSLSocket createSocket(String host, int port) throws IOException {
46  		return prepare(delegate.createSocket(host, port));
47  	}
48  
49  	@Override
50  	public SSLSocket createSocket(String host, int port,
51  			InetAddress localAddress, int localPort) throws IOException {
52  		return prepare(
53  				delegate.createSocket(host, port, localAddress, localPort));
54  	}
55  
56  	@Override
57  	public SSLSocket createSocket(InetAddress host, int port)
58  			throws IOException {
59  		return prepare(delegate.createSocket(host, port));
60  	}
61  
62  	@Override
63  	public SSLSocket createSocket(InetAddress host, int port,
64  			InetAddress localAddress, int localPort) throws IOException {
65  		return prepare(
66  				delegate.createSocket(host, port, localAddress, localPort));
67  	}
68  
69  	@Override
70  	public SSLSocket createSocket(Socket socket, String host, int port,
71  			boolean autoClose) throws IOException {
72  		return prepare(delegate.createSocket(socket, host, port, autoClose));
73  	}
74  
75  	@Override
76  	public String[] getDefaultCipherSuites() {
77  		return delegate.getDefaultCipherSuites();
78  	}
79  
80  	@Override
81  	public String[] getSupportedCipherSuites() {
82  		return delegate.getSupportedCipherSuites();
83  	}
84  
85  	private SSLSocket prepare(Socket socket) throws IOException {
86  		SSLSocket sslSocket = (SSLSocket) socket;
87  		configure(sslSocket);
88  		return sslSocket;
89  	}
90  
91  	/**
92  	 * Configure the newly created socket.
93  	 *
94  	 * @param socket
95  	 *            to configure
96  	 * @throws IOException
97  	 *             if the socket cannot be configured
98  	 */
99  	protected abstract void configure(SSLSocket socket) throws IOException;
100 
101 }