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