1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.ssl;
29
30 import javax.net.ssl.HostnameVerifier;
31 import javax.net.ssl.SSLContext;
32 import javax.net.ssl.SSLEngine;
33 import javax.net.ssl.SSLSession;
34
35 import org.apache.hc.core5.function.Factory;
36 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
37 import org.apache.hc.core5.http.ssl.TLS;
38 import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
39 import org.apache.hc.core5.reactor.ssl.TlsDetails;
40 import org.apache.hc.core5.ssl.SSLContexts;
41 import org.apache.hc.core5.util.ReflectionUtils;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class ClientTlsStrategyBuilder {
70
71 public static ClientTlsStrategyBuilder create() {
72 return new ClientTlsStrategyBuilder();
73 }
74
75 private SSLContext sslContext;
76 private String[] tlsVersions;
77 private String[] ciphers;
78 private SSLBufferMode sslBufferMode;
79 private HostnameVerifier hostnameVerifier;
80 private Factory<SSLEngine, TlsDetails> tlsDetailsFactory;
81 private boolean systemProperties;
82
83
84
85
86 public ClientTlsStrategyBuilder setSslContext(final SSLContext sslContext) {
87 this.sslContext = sslContext;
88 return this;
89 }
90
91
92
93
94 public final ClientTlsStrategyBuilder setTlsVersions(final String... tlslVersions) {
95 this.tlsVersions = tlslVersions;
96 return this;
97 }
98
99
100
101
102 public final ClientTlsStrategyBuilder setTlsVersions(final TLS... tlslVersions) {
103 this.tlsVersions = new String[tlslVersions.length];
104 for (int i = 0; i < tlslVersions.length; i++) {
105 this.tlsVersions[i] = tlslVersions[i].id;
106 }
107 return this;
108 }
109
110
111
112
113 public final ClientTlsStrategyBuilder setCiphers(final String... ciphers) {
114 this.ciphers = ciphers;
115 return this;
116 }
117
118
119
120
121 public ClientTlsStrategyBuilder setSslBufferMode(final SSLBufferMode sslBufferMode) {
122 this.sslBufferMode = sslBufferMode;
123 return this;
124 }
125
126
127
128
129 public ClientTlsStrategyBuilder setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
130 this.hostnameVerifier = hostnameVerifier;
131 return this;
132 }
133
134
135
136
137 public ClientTlsStrategyBuilder setTlsDetailsFactory(final Factory<SSLEngine, TlsDetails> tlsDetailsFactory) {
138 this.tlsDetailsFactory = tlsDetailsFactory;
139 return this;
140 }
141
142
143
144
145
146 public final ClientTlsStrategyBuilder useSystemProperties() {
147 this.systemProperties = true;
148 return this;
149 }
150
151 public TlsStrategy build() {
152 final SSLContext sslContextCopy;
153 if (sslContext != null) {
154 sslContextCopy = sslContext;
155 } else {
156 sslContextCopy = systemProperties ? SSLContexts.createSystemDefault() : SSLContexts.createDefault();
157 }
158 final String[] tlsVersionsCopy;
159 if (tlsVersions != null) {
160 tlsVersionsCopy = tlsVersions;
161 } else {
162 tlsVersionsCopy = systemProperties ? HttpsSupport.getSystemProtocols() : null;
163 }
164 final String[] ciphersCopy;
165 if (ciphers != null) {
166 ciphersCopy = ciphers;
167 } else {
168 ciphersCopy = systemProperties ? HttpsSupport.getSystemCipherSuits() : null;
169 }
170 final Factory<SSLEngine, TlsDetails> tlsDetailsFactoryCopy;
171 if (tlsDetailsFactory != null) {
172 tlsDetailsFactoryCopy = tlsDetailsFactory;
173 } else {
174 tlsDetailsFactoryCopy = new Factory<SSLEngine, TlsDetails>() {
175 @Override
176 public TlsDetails create(final SSLEngine sslEngine) {
177 final SSLSession sslSession = sslEngine.getSession();
178 final String applicationProtocol = ReflectionUtils.callGetter(sslEngine,
179 "ApplicationProtocol", String.class);
180 return new TlsDetails(sslSession, applicationProtocol);
181 }
182 };
183 }
184 return new DefaultClientTlsStrategy(
185 sslContextCopy,
186 tlsVersionsCopy,
187 ciphersCopy,
188 sslBufferMode != null ? sslBufferMode : SSLBufferMode.STATIC,
189 hostnameVerifier != null ? hostnameVerifier : HttpsSupport.getDefaultHostnameVerifier(),
190 tlsDetailsFactoryCopy);
191 }
192
193 }