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
34 import org.apache.hc.core5.function.Factory;
35 import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
36 import org.apache.hc.core5.http.ssl.TLS;
37 import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
38 import org.apache.hc.core5.reactor.ssl.TlsDetails;
39 import org.apache.hc.core5.ssl.SSLContexts;
40
41
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 public class ClientTlsStrategyBuilder {
68
69 public static ClientTlsStrategyBuilder create() {
70 return new ClientTlsStrategyBuilder();
71 }
72
73 private SSLContext sslContext;
74 private String[] tlsVersions;
75 private String[] ciphers;
76 private SSLBufferMode sslBufferMode;
77 private HostnameVerifier hostnameVerifier;
78
79
80
81 @Deprecated
82 private Factory<SSLEngine, TlsDetails> tlsDetailsFactory;
83 private boolean systemProperties;
84
85
86
87
88 public ClientTlsStrategyBuilder setSslContext(final SSLContext sslContext) {
89 this.sslContext = sslContext;
90 return this;
91 }
92
93
94
95
96 public final ClientTlsStrategyBuilder setTlsVersions(final String... tlslVersions) {
97 this.tlsVersions = tlslVersions;
98 return this;
99 }
100
101
102
103
104 public final ClientTlsStrategyBuilder setTlsVersions(final TLS... tlslVersions) {
105 this.tlsVersions = new String[tlslVersions.length];
106 for (int i = 0; i < tlslVersions.length; i++) {
107 this.tlsVersions[i] = tlslVersions[i].id;
108 }
109 return this;
110 }
111
112
113
114
115 public final ClientTlsStrategyBuilder setCiphers(final String... ciphers) {
116 this.ciphers = ciphers;
117 return this;
118 }
119
120
121
122
123 public ClientTlsStrategyBuilder setSslBufferMode(final SSLBufferMode sslBufferMode) {
124 this.sslBufferMode = sslBufferMode;
125 return this;
126 }
127
128
129
130
131 public ClientTlsStrategyBuilder setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
132 this.hostnameVerifier = hostnameVerifier;
133 return this;
134 }
135
136
137
138
139
140
141 @Deprecated
142 public ClientTlsStrategyBuilder setTlsDetailsFactory(final Factory<SSLEngine, TlsDetails> tlsDetailsFactory) {
143 this.tlsDetailsFactory = tlsDetailsFactory;
144 return this;
145 }
146
147
148
149
150
151 public final ClientTlsStrategyBuilder useSystemProperties() {
152 this.systemProperties = true;
153 return this;
154 }
155
156 @SuppressWarnings("deprecation")
157 public TlsStrategy build() {
158 final SSLContext sslContextCopy;
159 if (sslContext != null) {
160 sslContextCopy = sslContext;
161 } else {
162 sslContextCopy = systemProperties ? SSLContexts.createSystemDefault() : SSLContexts.createDefault();
163 }
164 final String[] tlsVersionsCopy;
165 if (tlsVersions != null) {
166 tlsVersionsCopy = tlsVersions;
167 } else {
168 tlsVersionsCopy = systemProperties ? HttpsSupport.getSystemProtocols() : null;
169 }
170 final String[] ciphersCopy;
171 if (ciphers != null) {
172 ciphersCopy = ciphers;
173 } else {
174 ciphersCopy = systemProperties ? HttpsSupport.getSystemCipherSuits() : null;
175 }
176 return new DefaultClientTlsStrategy(
177 sslContextCopy,
178 tlsVersionsCopy,
179 ciphersCopy,
180 sslBufferMode != null ? sslBufferMode : SSLBufferMode.STATIC,
181 hostnameVerifier != null ? hostnameVerifier : HttpsSupport.getDefaultHostnameVerifier(),
182 tlsDetailsFactory);
183 }
184
185 }