Search in sources :

Example 16 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project opencast by opencast.

the class HttpClientImpl method makeHttpClient.

/**
 * Creates a new client that can deal with all kinds of oddities with regards to http/https connections.
 *
 * @return the client
 */
private DefaultHttpClient makeHttpClient() {
    DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
    try {
        logger.debug("Installing forgiving hostname verifier and trust managers");
        X509TrustManager trustManager = createTrustManager();
        X509HostnameVerifier hostNameVerifier = createHostNameVerifier();
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
        SSLSocketFactory ssf = new SSLSocketFactory(sslContext, hostNameVerifier);
        ClientConnectionManager ccm = defaultHttpClient.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error creating context to handle TLS connections: {}", e.getMessage());
    } catch (KeyManagementException e) {
        logger.error("Error creating context to handle TLS connections: {}", e.getMessage());
    }
    return defaultHttpClient;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) X509HostnameVerifier(org.apache.http.conn.ssl.X509HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyManagementException(java.security.KeyManagementException)

Example 17 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project opencast by opencast.

the class TrustedHttpClientImplTest method successIfNonceReturnOnceAndThreeRetries.

@Test
public void successIfNonceReturnOnceAndThreeRetries() throws ClientProtocolException, IOException {
    // Setup bundle context for TrustedHttpClientImpl
    bundleContextMock = createNiceMock(BundleContext.class);
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("3");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_BASE_TIME_KEY)).andReturn("0");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_MAXIMUM_VARIABLE_TIME_KEY)).andReturn("0");
    replay(bundleContextMock);
    componentContextMock = createNiceMock(ComponentContext.class);
    expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
    replay(componentContextMock);
    client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
    client.setServiceRegistry(serviceRegistry);
    client.setSecurityService(securityService);
    client.activate(componentContextMock);
    HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
    HttpParams httpParams = createNiceMock(HttpParams.class);
    expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
    expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
    replay(httpParams);
    ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
    IMocksControl ctrl = EasyMock.createNiceControl();
    ctrl.checkOrder(false);
    HttpClient httpClient = createMock("Request", HttpClient.class);
    expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
    // First Digest handshake and close
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // First request and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Second Digest handshake and close
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // First request retry.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(okResponse);
    replay(httpClient);
    // Setup DefaultHttpClientFactory
    HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
    expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
    replay(httpClientFactory);
    client.setHttpClientFactory(httpClientFactory);
    HttpResponse response = client.execute(httpPost);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : IMocksControl(org.easymock.IMocksControl) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpParams(org.apache.http.params.HttpParams) ComponentContext(org.osgi.service.component.ComponentContext) HttpClient(org.opencastproject.kernel.http.api.HttpClient) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 18 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project opencast by opencast.

the class TrustedHttpClientImplTest method successIfNonceReturnThreeAndThreeRetries.

@Test
public void successIfNonceReturnThreeAndThreeRetries() throws ClientProtocolException, IOException {
    // Setup bundle context for TrustedHttpClientImpl
    bundleContextMock = createNiceMock(BundleContext.class);
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("3");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_BASE_TIME_KEY)).andReturn("0");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_MAXIMUM_VARIABLE_TIME_KEY)).andReturn("0");
    replay(bundleContextMock);
    componentContextMock = createNiceMock(ComponentContext.class);
    expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
    replay(componentContextMock);
    client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
    client.setServiceRegistry(serviceRegistry);
    client.setSecurityService(securityService);
    client.activate(componentContextMock);
    HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
    HttpParams httpParams = createNiceMock(HttpParams.class);
    expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
    expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
    replay(httpParams);
    ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
    HttpClient httpClient = createMock("Request", HttpClient.class);
    expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
    // First Digest handshake and close
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // First request with a nonce timeout and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // First retry getting nonce and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // First retry request and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Second retry getting nonce and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Second retry request and close
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Third retry getting nonce and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Third retry with successful request.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(okResponse);
    replay(httpClient);
    // Setup DefaultHttpClientFactory
    HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
    expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
    replay(httpClientFactory);
    client.setHttpClientFactory(httpClientFactory);
    HttpResponse response = client.execute(httpPost);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpParams(org.apache.http.params.HttpParams) ComponentContext(org.osgi.service.component.ComponentContext) HttpClient(org.opencastproject.kernel.http.api.HttpClient) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 19 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project opencast by opencast.

the class TrustedHttpClientImplTest method failsIfNonceReturnAndNoRetries.

@Test
public void failsIfNonceReturnAndNoRetries() throws ClientProtocolException, IOException {
    // Setup bundle context for TrustedHttpClientImpl
    bundleContextMock = createNiceMock(BundleContext.class);
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_USER_KEY)).andReturn("matterhorn_system_account");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.DIGEST_AUTH_PASS_KEY)).andReturn("CHANGE_ME");
    expect(bundleContextMock.getProperty(TrustedHttpClientImpl.NONCE_TIMEOUT_RETRY_KEY)).andReturn("0");
    replay(bundleContextMock);
    componentContextMock = createNiceMock(ComponentContext.class);
    expect(componentContextMock.getBundleContext()).andReturn(bundleContextMock).anyTimes();
    replay(componentContextMock);
    client = new TrustedHttpClientImpl("matterhorn_system_account", "CHANGE_ME");
    client.setServiceRegistry(serviceRegistry);
    client.setSecurityService(securityService);
    client.activate(componentContextMock);
    HttpPost httpPost = new HttpPost("http://localhost:8080/fake");
    HttpParams httpParams = createNiceMock(HttpParams.class);
    expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
    expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
    replay(httpParams);
    ClientConnectionManager clientConnectionManager = createMock(ClientConnectionManager.class);
    HttpClient requestDefaultHttpClient = createMock("Request", HttpClient.class);
    expect(requestDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
    // Digest authentication and close
    expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(requestDefaultHttpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Try request and close.
    expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    replay(requestDefaultHttpClient);
    // Setup DefaultHttpClientFactory
    HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
    expect(httpClientFactory.makeHttpClient()).andReturn(requestDefaultHttpClient).atLeastOnce();
    replay(httpClientFactory);
    client.setHttpClientFactory(httpClientFactory);
    HttpResponse response = client.execute(httpPost);
    Assert.assertEquals(401, response.getStatusLine().getStatusCode());
    verify(requestDefaultHttpClient);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpParams(org.apache.http.params.HttpParams) ComponentContext(org.osgi.service.component.ComponentContext) HttpClient(org.opencastproject.kernel.http.api.HttpClient) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) HttpClientFactory(org.opencastproject.kernel.http.impl.HttpClientFactory) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 20 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project docker-java-api by amihaiemil.

the class UnixHttpClientTestCase method getsClientConnectionManager.

/**
 * UnixHttpClient returns its ClientConnectionManager.
 */
@Test
public void getsClientConnectionManager() {
    final ClientConnectionManager cmngr = Mockito.mock(ClientConnectionManager.class);
    final HttpClient decorated = Mockito.mock(HttpClient.class);
    Mockito.when(decorated.getConnectionManager()).thenReturn(cmngr);
    final HttpClient unix = new UnixHttpClient(decorated);
    MatcherAssert.assertThat(unix.getConnectionManager(), Matchers.is(cmngr));
    Mockito.verify(decorated, Mockito.times(1)).getConnectionManager();
}
Also used : HttpClient(org.apache.http.client.HttpClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) Test(org.junit.Test)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)74 Scheme (org.apache.http.conn.scheme.Scheme)51 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)48 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)39 HttpParams (org.apache.http.params.HttpParams)37 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)35 BasicHttpParams (org.apache.http.params.BasicHttpParams)28 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)26 IOException (java.io.IOException)21 Test (org.junit.Test)15 CertificateException (java.security.cert.CertificateException)14 HttpClient (org.apache.http.client.HttpClient)14 KeyManagementException (java.security.KeyManagementException)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)13 SSLContext (javax.net.ssl.SSLContext)12 HttpResponse (org.apache.http.HttpResponse)11 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)11 KeyStoreException (java.security.KeyStoreException)10 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)9