1 package org.metricshub.http;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.IOException;
24 import java.net.InetAddress;
25 import java.net.Socket;
26 import javax.net.ssl.SSLSocket;
27 import javax.net.ssl.SSLSocketFactory;
28
29
30
31
32
33
34
35 public class ProtocolOverridingSSLSocketFactory extends SSLSocketFactory {
36
37 private final SSLSocketFactory underlyingSSLSocketFactory;
38 private final String[] enabledProtocols;
39
40
41
42
43
44
45
46
47 public ProtocolOverridingSSLSocketFactory(final SSLSocketFactory delegate, final String[] enabledProtocols) {
48 this.underlyingSSLSocketFactory = delegate;
49 this.enabledProtocols = enabledProtocols;
50 }
51
52 @Override
53 public String[] getDefaultCipherSuites() {
54 return underlyingSSLSocketFactory.getDefaultCipherSuites();
55 }
56
57 @Override
58 public String[] getSupportedCipherSuites() {
59 return underlyingSSLSocketFactory.getSupportedCipherSuites();
60 }
61
62 @Override
63 public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose)
64 throws IOException {
65 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(socket, host, port, autoClose);
66 return overrideProtocol(underlyingSocket);
67 }
68
69 @Override
70 public Socket createSocket(final String host, final int port) throws IOException {
71 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port);
72 return overrideProtocol(underlyingSocket);
73 }
74
75 @Override
76 public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort)
77 throws IOException {
78 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port, localAddress, localPort);
79 return overrideProtocol(underlyingSocket);
80 }
81
82 @Override
83 public Socket createSocket(final InetAddress host, final int port) throws IOException {
84 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port);
85 return overrideProtocol(underlyingSocket);
86 }
87
88 @Override
89 public Socket createSocket(
90 final InetAddress host,
91 final int port,
92 final InetAddress localAddress,
93 final int localPort
94 ) throws IOException {
95 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port, localAddress, localPort);
96 return overrideProtocol(underlyingSocket);
97 }
98
99
100
101
102
103
104
105
106 private Socket overrideProtocol(final Socket socket) {
107 if (socket instanceof SSLSocket && enabledProtocols != null && enabledProtocols.length > 0) {
108 ((SSLSocket) socket).setEnabledProtocols(enabledProtocols);
109 }
110 return socket;
111 }
112 }