Search in sources :

Example 71 with ClientConnectionManager

use of org.apache.http.conn.ClientConnectionManager in project musiccabinet by hakko.

the class AbstractWSPostClientTest method getTestWSClient.

/*
	 * Help method to create a base TestWSClient.
	 */
private TestWSPostAuthenticatedClient getTestWSClient(List<NameValuePair> params, String responseURI) throws IOException {
    HttpClient httpClient = Mockito.mock(HttpClient.class);
    ClientConnectionManager connectionManager = Mockito.mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    StatusLine statusLine = Mockito.mock(StatusLine.class);
    when(statusLine.getStatusCode()).thenReturn(200);
    HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
    when(httpEntity.getContent()).thenReturn(new ResourceUtil(responseURI).getInputStream());
    HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(httpResponse.getEntity()).thenReturn(httpEntity);
    when(httpClient.execute(Mockito.any(HttpUriRequest.class))).thenReturn(httpResponse);
    TestWSPostAuthenticatedClient testClient = new TestWSPostAuthenticatedClient(params);
    testClient.setHttpClient(httpClient);
    return testClient;
}
Also used : StatusLine(org.apache.http.StatusLine) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 72 with ClientConnectionManager

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

the class NetScalerControlCenterResource method getHttpClient.

public static HttpClient getHttpClient() {
    HttpClient httpClient = null;
    TrustStrategy easyStrategy = new TrustStrategy() {

        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };
    try {
        SSLSocketFactory sf = new SSLSocketFactory(easyStrategy, new AllowAllHostnameVerifier());
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", DEFAULT_PORT, sf));
        ClientConnectionManager ccm = new BasicClientConnectionManager(registry);
        httpClient = new DefaultHttpClient(ccm);
    } catch (KeyManagementException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (UnrecoverableKeyException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (KeyStoreException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    }
    return httpClient;
}
Also used : TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyManagementException(java.security.KeyManagementException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 73 with ClientConnectionManager

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

the class NetScalerControlCenterResource method postHttpRequest.

public static String postHttpRequest(final String jsonCmd, final URI agentUri, String sessionID) throws ExecutionException {
    // Using Apache's HttpClient for HTTP POST
    // Java-only approach discussed at on StackOverflow concludes with
    // comment to use Apache HttpClient
    // http://stackoverflow.com/a/2793153/939250, but final comment is to
    // use Apache.
    String logMessage = StringEscapeUtils.unescapeJava(jsonCmd);
    logMessage = cleanPassword(logMessage);
    s_logger.debug("POST request to " + agentUri.toString() + " with contents " + logMessage);
    // Create request
    HttpClient httpClient = getHttpClient();
    TrustStrategy easyStrategy = new TrustStrategy() {

        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };
    try {
        SSLSocketFactory sf = new SSLSocketFactory(easyStrategy, new AllowAllHostnameVerifier());
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("https", DEFAULT_PORT, sf));
        ClientConnectionManager ccm = new BasicClientConnectionManager(registry);
        httpClient = new DefaultHttpClient(ccm);
    } catch (KeyManagementException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (UnrecoverableKeyException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    } catch (KeyStoreException e) {
        s_logger.error("failed to initialize http client " + e.getMessage());
    }
    String result = null;
    // TODO: are there timeout settings and worker thread settings to tweak?
    try {
        HttpPost request = new HttpPost(agentUri);
        // JSON encode command
        // Assumes command sits comfortably in a string, i.e. not used for
        // large data transfers
        StringEntity cmdJson = new StringEntity(jsonCmd);
        request.addHeader("content-type", "application/json");
        request.addHeader("Cookie", "SessId=" + sessionID);
        request.setEntity(cmdJson);
        s_logger.debug("Sending cmd to " + agentUri.toString() + " cmd data:" + logMessage + "SEssion id: " + sessionID);
        HttpResponse response = httpClient.execute(request);
        // Unsupported commands will not route.
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            String errMsg = "Failed : HTTP error code : " + response.getStatusLine().getStatusCode();
            throw new ExecutionException(NccHttpCode.NOT_FOUND);
        } else if ((response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) && (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED)) {
            String errMsg = "Command Not Success " + agentUri.toString() + " : HTTP error code : " + response.getStatusLine().getStatusCode();
            s_logger.error(errMsg);
            throw new ExecutionException(NccHttpCode.INTERNAL_ERROR + " " + errMsg);
        } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            // make login request and store new session id
            throw new ExecutionException(NccHttpCode.UNAUTHORIZED);
        } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
            // Successfully created the resource in the NCC, Now get the Job ID and send to the response
            result = response.getFirstHeader(NccHttpCode.JOB_ID).getValue();
        } else {
            result = EntityUtils.toString(response.getEntity());
            String logResult = cleanPassword(StringEscapeUtils.unescapeJava(result));
            s_logger.debug("POST response is " + logResult);
        }
    } catch (ClientProtocolException protocolEx) {
        // Problem with HTTP message exchange
        s_logger.error(protocolEx);
    } catch (IOException connEx) {
        // Problem with underlying communications
        s_logger.error(connEx);
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TrustStrategy(org.apache.http.conn.ssl.TrustStrategy) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpResponse(org.apache.http.HttpResponse) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyManagementException(java.security.KeyManagementException) ClientProtocolException(org.apache.http.client.ClientProtocolException) StringEntity(org.apache.http.entity.StringEntity) UnrecoverableKeyException(java.security.UnrecoverableKeyException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicClientConnectionManager(org.apache.http.impl.conn.BasicClientConnectionManager) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ExecutionException(com.cloud.utils.exception.ExecutionException)

Example 74 with ClientConnectionManager

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

the class SSLConnectionTest method strictHostnameVerifier.

@Test
public void strictHostnameVerifier() {
    assertNotNull(activity.mHttpsClientTest2);
    ClientConnectionManager ccm = activity.mHttpsClientTest2.getConnectionManager();
    Scheme httpsScheme = ccm.getSchemeRegistry().getScheme("https");
    SSLSocketFactory socketFactHttps = (SSLSocketFactory) httpsScheme.getSocketFactory();
    assertEquals(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER, socketFactHttps.getHostnameVerifier());
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) Test(org.junit.Test)

Example 75 with ClientConnectionManager

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

the class SSLConnectionTest method noOptions.

@Test
public void noOptions() {
    assertNotNull(activity.mHttpsClientTest3);
    ClientConnectionManager ccm = activity.mHttpsClientTest3.getConnectionManager();
    assertNotNull(ccm);
}
Also used : ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) Test(org.junit.Test)

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