List of usage examples for org.apache.http.impl.client DefaultHttpClient getConnectionManager
public synchronized final ClientConnectionManager getConnectionManager()
From source file:com.brightcove.com.uploader.helper.MediaAPIHelper.java
/** * Executes HTTP get or post request and returns json response from api * @param httpCall Get or Post method/* w w w . j a va 2 s . c om*/ * @return json response from api * @throws MediaAPIError */ protected JsonNode executeCall(HttpRequestBase httpCall) throws MediaAPIError { JsonNode jsonObj; try { HttpResponse response = null; DefaultHttpClient httpAgent = new DefaultHttpClient(); response = httpAgent.execute(httpCall); // Make sure the HTTP communication was OK (not the same as an error in the Media API reponse) Integer statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpAgent.getConnectionManager().shutdown(); throw new MediaAPIError("Response code from HTTP server: '" + statusCode + "'"); } // Parse the response HttpEntity entity = response.getEntity(); jsonObj = getJSONFromEntity(entity); httpAgent.getConnectionManager().shutdown(); } catch (IllegalStateException ise) { throw new MediaAPIError("Exception: '" + ise + "'"); } catch (ClientProtocolException cpe) { throw new MediaAPIError("Exception: '" + cpe + "'"); } catch (IOException ioe) { throw new MediaAPIError("Exception: '" + ioe + "'"); } return jsonObj; }
From source file:com.asakusafw.yaess.jobqueue.client.HttpJobClient.java
private DefaultHttpClient createClient() { try {/*from w w w . ja va2s . co m*/ DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager()); SSLSocketFactory socketFactory = TrustedSSLSocketFactory.create(); Scheme sch = new Scheme("https", 443, socketFactory); client.getConnectionManager().getSchemeRegistry().register(sch); return client; } catch (GeneralSecurityException e) { throw new IllegalStateException( MessageFormat.format("Failed to initialize SSL socket factory: {0}", baseUri), e); } }
From source file:com.terremark.exception.LeakTest.java
/** * TODO//from ww w. j a v a 2 s . co m */ @Test public void testConnectionTimeouts() { httpServer.setDelayResponse(10000); final DefaultHttpClient httpClient = getHttpClient(); final RestClient client = new RestClient(new ApacheHttpClientConfig(httpClient)); try { for (int i = 0; i < 10; i++) { try { Resource resource = client.resource("http://localhost:" + httpServer.getServerPort()); resource.get(Object.class); } catch (ClientRuntimeException ex) { // Ignore } } } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:org.opencastproject.remotetest.server.DigestAuthenticationTest.java
@Test public void testDigestAuthenticatedGet() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("matterhorn_system_account", "CHANGE_ME"); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet get = new HttpGet(BASE_URL + "/welcome.html"); get.addHeader("X-Requested-Auth", "Digest"); try {//from w w w . j a v a2 s. co m httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); HttpResponse response = httpclient.execute(get); String content = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); Assert.assertTrue(content.contains("Opencast Matterhorn")); } finally { httpclient.getConnectionManager().shutdown(); } }
From source file:com.brightcove.zartan.common.helper.MediaAPIHelper.java
/** * Executes HTTP get or post request and returns json response from api * //from w ww .ja v a2 s . c om * @param httpCall Get or Post method * @return json response from api * @throws MediaAPIException */ protected JsonNode executeCall(HttpRequestBase httpCall) throws MediaAPIException { JsonNode jsonObj; try { HttpResponse response = null; DefaultHttpClient httpAgent = new DefaultHttpClient(); response = httpAgent.execute(httpCall); /* Make sure the HTTP communication was OK (not the same as an * error in the Media API response */ Integer statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { httpAgent.getConnectionManager().shutdown(); throw new MediaAPIException("Response code from HTTP server: '" + statusCode + "'"); } // Parse the response HttpEntity entity = response.getEntity(); jsonObj = getJSONFromEntity(entity); httpAgent.getConnectionManager().shutdown(); } catch (IllegalStateException ise) { throw new MediaAPIException("Exception: '" + ise + "'"); } catch (ClientProtocolException cpe) { throw new MediaAPIException("Exception: '" + cpe + "'"); } catch (IOException ioe) { throw new MediaAPIException("Exception: '" + ioe + "'"); } return jsonObj; }
From source file:com.terremark.exception.LeakTest.java
/** * TODO/*from www .j a va 2 s. c om*/ */ @Test public void test503Errors() { httpServer.setDelayResponse(0); final DefaultHttpClient httpClient = getHttpClient(); final RestClient client = new RestClient(new ApacheHttpClientConfig(httpClient)); try { for (int i = 0; i < 10; i++) { try { Resource resource = client.resource("http://localhost:" + httpServer.getServerPort()); resource.get(Object.class); } catch (ClientWebException ex) { ex.getResponse().consumeContent(); } } } finally { httpClient.getConnectionManager().shutdown(); } }
From source file:org.picketbox.http.test.config.ProtectedResourceManagerUnitTestCase.java
@Test public void testUnprotectedResource() throws Exception { URL url = new URL(this.urlStr + "notProtected"); DefaultHttpClient httpclient = null; try {//from w ww. j ava 2s.co m httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); assertEquals(404, response.getStatusLine().getStatusCode()); } finally { // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:org.picketbox.http.test.config.ProtectedResourceManagerUnitTestCase.java
@Test public void testProtectedResource() throws Exception { URL url = new URL(this.urlStr + "onlyManagers"); DefaultHttpClient httpclient = null; try {// w ww. j a va 2s. c o m httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); assertEquals(401, response.getStatusLine().getStatusCode()); } finally { // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.surya.HttpClient.java
public final static String getContent(String url) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.addRequestInterceptor(new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); }/*w ww .ja v a 2 s . c om*/ } }); httpclient.addResponseInterceptor(new HttpResponseInterceptor() { public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { HttpEntity entity = response.getEntity(); Header ceheader = entity.getContentEncoding(); if (ceheader != null) { HeaderElement[] codecs = ceheader.getElements(); for (int i = 0; i < codecs.length; i++) { if (codecs[i].getName().equalsIgnoreCase("gzip")) { response.setEntity(new GzipDecompressingEntity(response.getEntity())); return; } } } } }); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); String content = null; if (entity != null) { content = EntityUtils.toString(entity); } httpclient.getConnectionManager().shutdown(); return content; }