List of usage examples for org.apache.http.impl.conn BasicClientConnectionManager BasicClientConnectionManager
public BasicClientConnectionManager(final SchemeRegistry schreg)
From source file:com.cloud.network.resource.NccHttpCode.java
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/* ww w .j av a 2s . co m*/ 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) { //Successfully created the resource in the NCC, Now get the Job ID and send to the response // 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; }
From source file:org.apache.cloudstack.storage.datastore.util.SolidFireUtil.java
private static DefaultHttpClient getHttpClient(int iPort) { try {// w ww . j a va 2 s .c o m SSLContext sslContext = SSLUtils.getSSLContext(); X509TrustManager tm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; sslContext.init(null, new TrustManager[] { tm }, new SecureRandom()); SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", iPort, socketFactory)); BasicClientConnectionManager mgr = new BasicClientConnectionManager(registry); DefaultHttpClient client = new DefaultHttpClient(); return new DefaultHttpClient(mgr, client.getParams()); } catch (NoSuchAlgorithmException ex) { throw new CloudRuntimeException(ex.getMessage()); } catch (KeyManagementException ex) { throw new CloudRuntimeException(ex.getMessage()); } }
From source file:com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource.java
public static String postHttpRequest(final String jsonCmd, final URI agentUri) { // 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 = null;//from w w w . ja v a 2 s . c o m final TrustStrategy easyStrategy = new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }; try { final SSLSocketFactory sf = new SSLSocketFactory(easyStrategy, new AllowAllHostnameVerifier()); final SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", DEFAULT_AGENT_PORT, sf)); final ClientConnectionManager ccm = new BasicClientConnectionManager(registry); httpClient = new DefaultHttpClient(ccm); } catch (final KeyManagementException e) { s_logger.error("failed to initialize http client " + e.getMessage()); } catch (final UnrecoverableKeyException e) { s_logger.error("failed to initialize http client " + e.getMessage()); } catch (final NoSuchAlgorithmException e) { s_logger.error("failed to initialize http client " + e.getMessage()); } catch (final 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 { final HttpPost request = new HttpPost(agentUri); // JSON encode command // Assumes command sits comfortably in a string, i.e. not used for // large data transfers final StringEntity cmdJson = new StringEntity(jsonCmd); request.addHeader("content-type", "application/json"); request.setEntity(cmdJson); s_logger.debug("Sending cmd to " + agentUri.toString() + " cmd data:" + logMessage); final HttpResponse response = httpClient.execute(request); // Unsupported commands will not route. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { final String errMsg = "Failed to send : HTTP error code : " + response.getStatusLine().getStatusCode(); s_logger.error(errMsg); final String unsupportMsg = "Unsupported command " + agentUri.getPath() + ". Are you sure you got the right type of" + " server?"; final Answer ans = new UnsupportedAnswer(null, unsupportMsg); s_logger.error(ans); result = s_gson.toJson(new Answer[] { ans }); } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final String errMsg = "Failed send to " + agentUri.toString() + " : HTTP error code : " + response.getStatusLine().getStatusCode(); s_logger.error(errMsg); return null; } else { result = EntityUtils.toString(response.getEntity()); final String logResult = cleanPassword(StringEscapeUtils.unescapeJava(result)); s_logger.debug("POST response is " + logResult); } } catch (final ClientProtocolException protocolEx) { // Problem with HTTP message exchange s_logger.error(protocolEx); } catch (final IOException connEx) { // Problem with underlying communications s_logger.error(connEx); } finally { httpClient.getConnectionManager().shutdown(); } return result; }
From source file:org.apache.http.impl.client.AbstractHttpClient.java
protected ClientConnectionManager createClientConnectionManager() { final SchemeRegistry registry = SchemeRegistryFactory.createDefault(); ClientConnectionManager connManager = null; final HttpParams params = getParams(); ClientConnectionManagerFactory factory = null; final String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME); if (className != null) { try {// w w w .ja v a 2 s . c om final Class<?> clazz = Class.forName(className); factory = (ClientConnectionManagerFactory) clazz.newInstance(); } catch (final ClassNotFoundException ex) { throw new IllegalStateException("Invalid class name: " + className); } catch (final IllegalAccessException ex) { throw new IllegalAccessError(ex.getMessage()); } catch (final InstantiationException ex) { throw new InstantiationError(ex.getMessage()); } } if (factory != null) { connManager = factory.newInstance(params, registry); } else { connManager = new BasicClientConnectionManager(registry); } return connManager; }