1 package org.sentrysoftware.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
27 import javax.net.ssl.SSLSocket;
28 import javax.net.ssl.SSLSocketFactory;
29
30
31
32
33
34
35
36 public class ProtocolOverridingSSLSocketFactory extends SSLSocketFactory {
37
38 private final SSLSocketFactory underlyingSSLSocketFactory;
39 private final String[] enabledProtocols;
40
41
42
43
44
45
46
47
48 public ProtocolOverridingSSLSocketFactory(final SSLSocketFactory delegate, final String[] enabledProtocols) {
49 this.underlyingSSLSocketFactory = delegate;
50 this.enabledProtocols = enabledProtocols;
51 }
52
53 @Override
54 public String[] getDefaultCipherSuites() {
55 return underlyingSSLSocketFactory.getDefaultCipherSuites();
56 }
57
58 @Override
59 public String[] getSupportedCipherSuites() {
60 return underlyingSSLSocketFactory.getSupportedCipherSuites();
61 }
62
63 @Override
64 public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) 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) throws IOException {
77 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port, localAddress, localPort);
78 return overrideProtocol(underlyingSocket);
79 }
80
81 @Override
82 public Socket createSocket(final InetAddress host, final int port) throws IOException {
83 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port);
84 return overrideProtocol(underlyingSocket);
85 }
86
87 @Override
88 public Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress, final int localPort) throws IOException {
89 Socket underlyingSocket = underlyingSSLSocketFactory.createSocket(host, port, localAddress, localPort);
90 return overrideProtocol(underlyingSocket);
91 }
92
93
94
95
96
97
98
99
100 private Socket overrideProtocol(final Socket socket) {
101 if (socket instanceof SSLSocket && enabledProtocols != null && enabledProtocols.length > 0) {
102 ((SSLSocket)socket).setEnabledProtocols(enabledProtocols);
103 }
104 return socket;
105 }
106 }