private SSLContext getSSLContext() { if (sslContext == null) { sslContext = createSSLContext(); } return sslContext; }
public GetMethodWithBody(String uri) { super(uri); // not supported out of the box for EntityEnclosingMethods setFollowRedirects(false); }
public Object clone() { return new ProtocolAwareHostConfiguration(this); }
@Test public void testProtocolReplacement() throws Exception { final ProtocolSocketFactory socketFactory = getSocketFactory(); CommonsHttpTransport.replaceProtocol(socketFactory, "https", 443); Protocol protocol = Protocol.getProtocol("https"); assertThat(protocol, instanceOf(DelegatedProtocol.class)); DelegatedProtocol delegatedProtocol = (DelegatedProtocol) protocol; assertThat(delegatedProtocol.getSocketFactory(), sameInstance(socketFactory)); assertThat(delegatedProtocol.getOriginal(), sameInstance(original)); // ensure we do not re-wrap a delegated protocol CommonsHttpTransport.replaceProtocol(socketFactory, "https", 443); protocol = Protocol.getProtocol("https"); assertThat(protocol, instanceOf(DelegatedProtocol.class)); delegatedProtocol = (DelegatedProtocol) protocol; assertThat(delegatedProtocol.getSocketFactory(), sameInstance(socketFactory)); assertThat(delegatedProtocol.getOriginal(), sameInstance(original)); }
private HostConfiguration setupSSLIfNeeded(Settings settings, SecureSettings secureSettings, HostConfiguration hostConfig) { if (!sslEnabled) { return hostConfig; } // we actually have a socks proxy, let's start the setup if (log.isDebugEnabled()) { log.debug("SSL Connection enabled"); } // // switch protocol // due to how HttpCommons work internally this dance is best to be kept as is // String schema = "https"; int port = 443; SecureProtocolSocketFactory sslFactory = new SSLSocketFactory(settings, secureSettings); replaceProtocol(sslFactory, schema, port); return hostConfig; }
static void replaceProtocol(ProtocolSocketFactory socketFactory, String schema, int defaultPort) { // // switch protocol // due to how HttpCommons work internally this dance is best to be kept as is // Protocol directHttp = Protocol.getProtocol(schema); if (directHttp instanceof DelegatedProtocol) { // unwrap the original directHttp = ((DelegatedProtocol)directHttp).getOriginal(); assert directHttp instanceof DelegatedProtocol == false; } Protocol proxiedHttp = new DelegatedProtocol(socketFactory, directHttp, schema, defaultPort); // NB: register the new protocol since when using absolute URIs, HttpClient#executeMethod will override the configuration (#387) // NB: hence why the original/direct http protocol is saved - as otherwise the connection is not closed since it is considered different // NB: (as the protocol identities don't match) // this is not really needed since it's being replaced later on // hostConfig.setHost(proxyHost, proxyPort, proxiedHttp); Protocol.registerProtocol(schema, proxiedHttp); // end dance }
public synchronized void setHost(String host, int port, String scheme) { setHost(new HttpHost(host, port, keepProtocol(host, port, scheme))); }
@Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose); }
@Override public Transport create(Settings settings, SecureSettings secureSettings, String hostInfo) { if (log.isDebugEnabled()) { log.debug("Creating new CommonsHttpTransport"); } return new CommonsHttpTransport(settings, secureSettings, hostInfo); } }
public void checkServerTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { if (!this.trustStrategy.isTrusted(chain, authType)) { this.trustManager.checkServerTrusted(chain, authType); } }
public ResponseInputStream(HttpMethod http) throws IOException { super(http.getResponseBodyAsStream()); this.method = http; reusable = (delegate() instanceof ByteArrayInputStream); }
public void close() { if (httpConnection != null) { if (httpConnection.isOpen()) { releaseConnection(httpConnection); } httpConnection.close(); } httpConnection = null; conn = null; } }
protected Protocol keepProtocol(String host, int port, String scheme) { final Protocol oldProtocol = getProtocol(); if (oldProtocol != null) { final String oldScheme = oldProtocol.getScheme(); if (oldScheme == scheme || (oldScheme != null && oldScheme.equalsIgnoreCase(scheme))) { return oldProtocol; } } return Protocol.getProtocol(scheme); } }
@Override public void close() throws IOException { if (!isNull()) { try { super.close(); } catch (IOException e) { // silently ignore } } method.releaseConnection(); } }
SSLSocketFactory(Settings settings, SecureSettings secureSettings) { sslProtocol = settings.getNetworkSSLProtocol(); keyStoreLocation = settings.getNetworkSSLKeyStoreLocation(); keyStorePass = secureSettings.getSecureProperty(ConfigurationOptions.ES_NET_SSL_KEYSTORE_PASS); keyStoreType = settings.getNetworkSSLKeyStoreType(); trustStoreLocation = settings.getNetworkSSLTrustStoreLocation(); trustStorePass = secureSettings.getSecureProperty(ConfigurationOptions.ES_NET_SSL_TRUST_STORE_PASS); trust = (settings.getNetworkSSLAcceptSelfSignedCert() ? new SelfSignedStrategy() : null); }
public NetworkClient(Settings settings) { this(settings, (!SettingsUtils.hasJobTransportPoolingKey(settings) ? new CommonsHttpTransportFactory() : PooledTransportManager.getTransportFactory(settings))); }
@Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { return this.createSocket(host, port, localAddress, localPort); }
@Override public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, localAddress, localPort); }
public GetMethodWithBody() { // not supported out of the box for EntityEnclosingMethods setFollowRedirects(false); }
@Override public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port); }