use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project htmlunit by HtmlUnit.
the class HttpWebConnection method createConnectionManager.
/**
* Has the exact logic in {@link HttpClientBuilder#build()} which sets the {@code connManager} part,
* but with the ability to configure {@code socketFactory}.
*/
private static PoolingHttpClientConnectionManager createConnectionManager(final HttpClientBuilder builder) {
try {
PublicSuffixMatcher publicSuffixMatcher = getField(builder, "publicSuffixMatcher");
if (publicSuffixMatcher == null) {
publicSuffixMatcher = PublicSuffixMatcherLoader.getDefault();
}
LayeredConnectionSocketFactory sslSocketFactory = getField(builder, "sslSocketFactory");
final SocketConfig defaultSocketConfig = getField(builder, "defaultSocketConfig");
final ConnectionConfig defaultConnectionConfig = getField(builder, "defaultConnectionConfig");
final boolean systemProperties = getField(builder, "systemProperties");
final int maxConnTotal = getField(builder, "maxConnTotal");
final int maxConnPerRoute = getField(builder, "maxConnPerRoute");
HostnameVerifier hostnameVerifier = getField(builder, "hostnameVerifier");
final SSLContext sslcontext = getField(builder, "sslContext");
final DnsResolver dnsResolver = getField(builder, "dnsResolver");
final long connTimeToLive = getField(builder, "connTimeToLive");
final TimeUnit connTimeToLiveTimeUnit = getField(builder, "connTimeToLiveTimeUnit");
if (sslSocketFactory == null) {
final String[] supportedProtocols = systemProperties ? split(System.getProperty("https.protocols")) : null;
final String[] supportedCipherSuites = systemProperties ? split(System.getProperty("https.cipherSuites")) : null;
if (hostnameVerifier == null) {
hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
}
if (sslcontext == null) {
if (systemProperties) {
sslSocketFactory = new SSLConnectionSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault(), supportedProtocols, supportedCipherSuites, hostnameVerifier);
} else {
sslSocketFactory = new SSLConnectionSocketFactory(SSLContexts.createDefault(), hostnameVerifier);
}
} else {
sslSocketFactory = new SSLConnectionSocketFactory(sslcontext, supportedProtocols, supportedCipherSuites, hostnameVerifier);
}
}
final PoolingHttpClientConnectionManager poolingmgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", new SocksConnectionSocketFactory()).register("https", sslSocketFactory).build(), null, null, dnsResolver, connTimeToLive, connTimeToLiveTimeUnit != null ? connTimeToLiveTimeUnit : TimeUnit.MILLISECONDS);
if (defaultSocketConfig != null) {
poolingmgr.setDefaultSocketConfig(defaultSocketConfig);
}
if (defaultConnectionConfig != null) {
poolingmgr.setDefaultConnectionConfig(defaultConnectionConfig);
}
if (systemProperties) {
String s = System.getProperty("http.keepAlive", "true");
if ("true".equalsIgnoreCase(s)) {
s = System.getProperty("http.maxConnections", "5");
final int max = Integer.parseInt(s);
poolingmgr.setDefaultMaxPerRoute(max);
poolingmgr.setMaxTotal(2 * max);
}
}
if (maxConnTotal > 0) {
poolingmgr.setMaxTotal(maxConnTotal);
}
if (maxConnPerRoute > 0) {
poolingmgr.setDefaultMaxPerRoute(maxConnPerRoute);
}
return poolingmgr;
} catch (final IllegalAccessException e) {
throw new RuntimeException(e);
}
}
use of org.apache.http.conn.socket.LayeredConnectionSocketFactory in project lavaplayer by WearifulCupid0.
the class ExtendedConnectionOperator method upgrade.
@Override
public void upgrade(ManagedHttpClientConnection connection, HttpHost host, HttpContext context) throws IOException {
ConnectionSocketFactory socketFactory = getSocketFactory(host, HttpClientContext.adapt(context));
if (!(socketFactory instanceof LayeredConnectionSocketFactory)) {
throw new UnsupportedSchemeException(host.getSchemeName() + " protocol does not support connection upgrade");
}
LayeredConnectionSocketFactory layeredFactory = (LayeredConnectionSocketFactory) socketFactory;
Socket socket = connection.getSocket();
int port = this.schemePortResolver.resolve(host);
socket = layeredFactory.createLayeredSocket(socket, host.getHostName(), port, context);
connection.bind(socket);
}
Aggregations