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
33 import org.apache.hc.core5.http.ssl.TLS;
34 import org.apache.hc.core5.ssl.SSLContexts;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class SSLConnectionSocketFactoryBuilder {
63
64 public static SSLConnectionSocketFactoryBuilder create() {
65 return new SSLConnectionSocketFactoryBuilder();
66 }
67
68 private SSLContext sslContext;
69 private String[] tlsVersions;
70 private String[] ciphers;
71 private HostnameVerifier hostnameVerifier;
72 private boolean systemProperties;
73
74
75
76
77 public SSLConnectionSocketFactoryBuilder setSslContext(final SSLContext sslContext) {
78 this.sslContext = sslContext;
79 return this;
80 }
81
82
83
84
85 public final SSLConnectionSocketFactoryBuilder setTlsVersions(final String... tlslVersions) {
86 this.tlsVersions = tlslVersions;
87 return this;
88 }
89
90
91
92
93 public final SSLConnectionSocketFactoryBuilder setTlsVersions(final TLS... tlslVersions) {
94 this.tlsVersions = new String[tlslVersions.length];
95 for (int i = 0; i < tlslVersions.length; i++) {
96 this.tlsVersions[i] = tlslVersions[i].id;
97 }
98 return this;
99 }
100
101
102
103
104 public final SSLConnectionSocketFactoryBuilder setCiphers(final String... ciphers) {
105 this.ciphers = ciphers;
106 return this;
107 }
108
109
110
111
112
113 public SSLConnectionSocketFactoryBuilder setHostnameVerifier(final HostnameVerifier hostnameVerifier) {
114 this.hostnameVerifier = hostnameVerifier;
115 return this;
116 }
117
118
119
120
121
122 public final SSLConnectionSocketFactoryBuilder useSystemProperties() {
123 this.systemProperties = true;
124 return this;
125 }
126
127 public SSLConnectionSocketFactory build() {
128 final javax.net.ssl.SSLSocketFactory socketFactory;
129 if (sslContext != null) {
130 socketFactory = sslContext.getSocketFactory();
131 } else {
132 if (systemProperties) {
133 socketFactory = (javax.net.ssl.SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
134 } else {
135 socketFactory = SSLContexts.createDefault().getSocketFactory();
136 }
137 }
138 final String[] tlsVersionsCopy;
139 if (tlsVersions != null) {
140 tlsVersionsCopy = tlsVersions;
141 } else {
142 tlsVersionsCopy = systemProperties ? HttpsSupport.getSystemProtocols() : null;
143 }
144 final String[] ciphersCopy;
145 if (ciphers != null) {
146 ciphersCopy = ciphers;
147 } else {
148 ciphersCopy = systemProperties ? HttpsSupport.getSystemCipherSuits() : null;
149 }
150 return new SSLConnectionSocketFactory(
151 socketFactory,
152 tlsVersionsCopy,
153 ciphersCopy,
154 hostnameVerifier != null ? hostnameVerifier : HttpsSupport.getDefaultHostnameVerifier());
155 }
156
157 }