Search in sources :

Example 61 with ClientConnectionManager

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

the class TrustedHttpClientImplTest method successfullRequestResultsInNoRetries.

@Test
public void successfullRequestResultsInNoRetries() throws ClientProtocolException, IOException {
    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);
    // Setup DefaultHttpClients
    HttpClient securityDefaultHttpClient = createMock("Digest", HttpClient.class);
    expect(securityDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
    expect(securityDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(securityDefaultHttpClient.getConnectionManager()).andReturn(clientConnectionManager);
    replay(securityDefaultHttpClient);
    HttpClient requestDefaultHttpClient = createMock("Request", HttpClient.class);
    expect(requestDefaultHttpClient.getParams()).andReturn(httpParams).anyTimes();
    expect(requestDefaultHttpClient.execute(isA(HttpUriRequest.class))).andReturn(okResponse);
    replay(requestDefaultHttpClient);
    // Setup DefaultHttpClientFactory
    HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
    expect(httpClientFactory.makeHttpClient()).andReturn(requestDefaultHttpClient);
    expect(httpClientFactory.makeHttpClient()).andReturn(securityDefaultHttpClient);
    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) 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) Test(org.junit.Test)

Example 62 with ClientConnectionManager

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

the class TrustedHttpClientImplTest method failsIfNonceReturnAndOneRetries.

@Test
public void failsIfNonceReturnAndOneRetries() 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("1");
    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();
    // Security 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 Security Handshake and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Retry request and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(nonceResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    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(401, 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 63 with ClientConnectionManager

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

the class TrustedHttpClientImplTest method successIfNonceReturnOnceAndOneRetries.

@Test
public void successIfNonceReturnOnceAndOneRetries() 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("1");
    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();
    // Security 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);
    // Nonce retry and close.
    expect(httpClient.execute(isA(HttpUriRequest.class))).andReturn(digestResponse);
    expect(httpClient.getConnectionManager()).andReturn(clientConnectionManager);
    // Final request with success.
    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 64 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project spring-boot-quick by vector4wang.

the class HttpUtil method sslClient.

private static void sslClient(HttpClient httpClient) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException)

Example 65 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project spring-boot-quick by vector4wang.

the class HttpUtils method sslClient.

private static void sslClient(HttpClient httpClient) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(X509Certificate[] xcs, String str) {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String str) {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = httpClient.getConnectionManager();
        SchemeRegistry registry = ccm.getSchemeRegistry();
        registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) X509TrustManager(javax.net.ssl.X509TrustManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) X509Certificate(java.security.cert.X509Certificate) KeyManagementException(java.security.KeyManagementException)

Aggregations

ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)77 Scheme (org.apache.http.conn.scheme.Scheme)54 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)51 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)42 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)38 HttpParams (org.apache.http.params.HttpParams)38 BasicHttpParams (org.apache.http.params.BasicHttpParams)31 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)29 IOException (java.io.IOException)24 CertificateException (java.security.cert.CertificateException)17 HttpClient (org.apache.http.client.HttpClient)15 Test (org.junit.Test)15 KeyManagementException (java.security.KeyManagementException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 SSLContext (javax.net.ssl.SSLContext)14 HttpResponse (org.apache.http.HttpResponse)12 KeyStoreException (java.security.KeyStoreException)11 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)11 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 PoolingClientConnectionManager (org.apache.http.impl.conn.PoolingClientConnectionManager)10