Example usage for org.apache.http.client HttpClient getConnectionManager

List of usage examples for org.apache.http.client HttpClient getConnectionManager

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getConnectionManager.

Prototype

@Deprecated
ClientConnectionManager getConnectionManager();

Source Link

Document

Obtains the connection manager used by this client.

Usage

From source file:org.neo4j.server.rest.ConfigureBaseUriDocIT.java

@Test
public void shouldForwardHttpsAndHost() throws Exception {
    URI rootUri = functionalTestHelper.baseUri();

    HttpClient httpclient = new DefaultHttpClient();
    try {/*from ww w.j ava  2s.c  o m*/
        HttpGet httpget = new HttpGet(rootUri);

        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("X-Forwarded-Host", "foobar.com");
        httpget.setHeader("X-Forwarded-Proto", "https");

        HttpResponse response = httpclient.execute(httpget);

        String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
        byte[] data = new byte[Integer.valueOf(length)];
        response.getEntity().getContent().read(data);

        String responseEntityBody = new String(data);

        assertTrue(responseEntityBody.contains("https://foobar.com"));
        assertFalse(responseEntityBody.contains("localhost"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.urbanstew.soundcloudapi.SoundCloudAPI.java

private void initialize() {
    HttpClient client = new DefaultHttpClient();

    httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(client.getParams(),
            client.getConnectionManager().getSchemeRegistry()), client.getParams());

}

From source file:org.neo4j.server.rest.ConfigureBaseUriDocIT.java

@Test
public void shouldForwardHttpAndHostOnDifferentPort() throws Exception {

    URI rootUri = functionalTestHelper.baseUri();

    HttpClient httpclient = new DefaultHttpClient();
    try {//  w w  w.  j a  v a  2  s .  c  o  m
        HttpGet httpget = new HttpGet(rootUri);

        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("X-Forwarded-Host", "foobar.com:9999");
        httpget.setHeader("X-Forwarded-Proto", "http");

        HttpResponse response = httpclient.execute(httpget);

        String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
        byte[] data = new byte[Integer.valueOf(length)];
        response.getEntity().getContent().read(data);

        String responseEntityBody = new String(data);

        assertTrue(responseEntityBody.contains("http://foobar.com:9999"));
        assertFalse(responseEntityBody.contains("localhost"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.neo4j.server.rest.ConfigureBaseUriDocIT.java

@Test
public void shouldForwardHttpsAndHostOnDifferentPort() throws Exception {
    URI rootUri = functionalTestHelper.baseUri();

    HttpClient httpclient = new DefaultHttpClient();
    try {//from   ww  w  .  j a va 2 s. c o  m
        HttpGet httpget = new HttpGet(rootUri);

        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("X-Forwarded-Host", "foobar.com:9999");
        httpget.setHeader("X-Forwarded-Proto", "https");

        HttpResponse response = httpclient.execute(httpget);

        String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
        byte[] data = new byte[Integer.valueOf(length)];
        response.getEntity().getContent().read(data);

        String responseEntityBody = new String(data);

        assertTrue(responseEntityBody.contains("https://foobar.com:9999"));
        assertFalse(responseEntityBody.contains("localhost"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.neo4j.server.rest.ConfigureBaseUriDocIT.java

@Test
public void shouldForwardHttpAndFirstHost() throws Exception {
    URI rootUri = functionalTestHelper.baseUri();

    HttpClient httpclient = new DefaultHttpClient();
    try {/* www  .  j a  v  a  2  s  . c  o m*/
        HttpGet httpget = new HttpGet(rootUri);

        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("X-Forwarded-Host", "foobar.com, bazbar.com");
        httpget.setHeader("X-Forwarded-Proto", "http");

        HttpResponse response = httpclient.execute(httpget);

        String length = response.getHeaders("CONTENT-LENGTH")[0].getValue();
        byte[] data = new byte[Integer.valueOf(length)];
        response.getEntity().getContent().read(data);

        String responseEntityBody = new String(data);

        assertTrue(responseEntityBody.contains("http://foobar.com"));
        assertFalse(responseEntityBody.contains("localhost"));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

}

From source file:org.neo4j.server.rest.RetrieveRelationshipsFromNodeDocIT.java

@Test
public void shouldParameteriseUrisInRelationshipRepresentationWithHostHeaderValue() throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    try {/*from w  ww .  ja  v a2s  .  c  o m*/
        HttpGet httpget = new HttpGet("http://localhost:7474/db/data/relationship/" + likes);
        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Host", "dummy.neo4j.org");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        String entityBody = IOUtils.toString(entity.getContent(), "UTF-8");

        System.out.println(entityBody);

        assertThat(entityBody, containsString("http://dummy.neo4j.org/db/data/relationship/" + likes));
        assertThat(entityBody, not(containsString("localhost:7474")));
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:de.unidue.inf.is.ezdl.dlservices.library.manager.referencesystems.bibsonomy.BibsonomyUtils.java

/** send DELETE REQUEST */
String sendDeleteRequest(String url) throws Exception {
    String sresponse;//  w w w  . j a v a  2  s.c  o m

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpDelete httpdelete = new HttpDelete(url);

        // add authorization header
        httpdelete.addHeader("Authorization", authHeader);

        HttpResponse response = httpclient.execute(httpdelete);

        // Check status code
        if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK
                || response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_NO_CONTENT) {
            if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_NO_CONTENT) {
                sresponse = "";
            } else {
                HttpEntity entity = response.getEntity();

                sresponse = readResponseStream(entity.getContent());
            }
            httpclient.getConnectionManager().shutdown();
        } else {
            HttpEntity entity = response.getEntity();
            sresponse = readResponseStream(entity.getContent());
            String httpcode = Integer.toString(response.getStatusLine().getStatusCode());
            httpclient.getConnectionManager().shutdown();
            throw new ReferenceSystemException(httpcode, "Bibsonomy Error", JSONUtils.parseError(sresponse));
        }
    } catch (UnknownHostException e) {
        throw new ReferenceSystemException("", "Unknow Host Exception", e.toString());
    }
    return sresponse;
}

From source file:ca.sqlpower.wabit.enterprise.client.WabitClientSession.java

public void deleteServerWorkspace() throws URISyntaxException, ClientProtocolException, IOException {
    SPServerInfo serviceInfo = workspaceLocation.getServiceInfo();
    HttpClient httpClient = createHttpClient(serviceInfo);
    try {//from  w w  w .j a v  a  2s  .  c  o  m
        HttpUriRequest request = new HttpDelete(
                getServerURI(serviceInfo, "workspaces/" + getWorkspace().getUUID()));
        httpClient.execute(request, new HttpResponseHandler());
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
}

From source file:org.bigmouth.nvwa.pay.service.prepay.AbstractPrepay.java

@Override
public PrepayResponse prepay(PrepayRequest argument) {
    Preconditions.checkNotNull(argument, "argument");
    argument.validate();/*from  w w w .  j  av a 2  s  .  co  m*/
    Map<String, PayConfig> configs = configService.getConfigs();
    if (MapUtils.isEmpty(configs)) {
        throw new PayConfigException("pay configs is empty!");
    }
    String appId = argument.getAppId();
    PayConfig config = configs.get(appId);
    config.validate();
    File pkcs12 = config.getPkcs12();
    String appSecret = config.getAppSecret();
    String url = config.getUrlPrepay();

    String xml = getPayRequest(argument, config).toXML();

    HttpClient https = HttpClientHelper.https(pkcs12, appSecret.toCharArray());

    try {
        HttpPost post = HttpClientHelper.post(url);
        HttpClientHelper.addByteArrayEntity(post, StringHelper.convert(xml), PayDefaults.APPLICATION_XML);
        HttpResponse httpResponse = https.execute(post);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Received response statusCode {} from {}", statusCode, url);
        }
        byte[] array = HttpClientHelper.getResponse(httpResponse);
        if (ArrayUtils.isNotEmpty(array)) {
            return convert(array);
        } else {
            LOGGER.warn("Empty response content! from {}", url);
        }
    } catch (Exception e) {
        LOGGER.error("Access " + url + " occur exception!", e);
    } finally {
        https.getConnectionManager().shutdown();
    }
    return null;
}

From source file:org.ckan.Connection.java

/**
* Makes a POST request/*from   w w w  .ja  va2 s  . co m*/
*
* Submits a POST HTTP request to the CKAN instance configured within
* the constructor, returning the entire contents of the response.
*
* @param  path The URL path to make the POST request to
* @param  data The data to be posted to the URL
* @returns The String contents of the response
* @throws A CKANException if the request fails
*/
protected String post(String path, String data) throws CKANException {
    URL url = null;

    try {
        url = new URL(this.m_host + ":" + this.m_port + path);
    } catch (MalformedURLException mue) {
        System.err.println(mue);
        return null;
    }

    String body = "";

    BasicClientConnectionManager bccm = null;
    ClientConnectionManager cm = null;
    try {
        /***********************************************************************/
        SSLContext sslContext = SSLContext.getInstance("SSL");
        // set up a TrustManager that trusts everything
        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());
        SSLSocketFactory sf = new SSLSocketFactory(sslContext);
        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);
        //bccm = new BasicClientConnectionManager(schemeRegistry);
        // apache HttpClient version >4.2 should use BasicClientConnectionManager
        cm = new SingleClientConnManager(schemeRegistry);
        /***********************************************************************/
    } catch (KeyManagementException kme) {
        System.out.println("Con ex: " + kme.getMessage());
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println("Con ex: " + nsae.getMessage());
    }

    //HttpClient httpclient = new DefaultHttpClient(cm);
    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpPost postRequest = new HttpPost(url.toString());
        postRequest.setHeader("X-CKAN-API-Key", this._apikey);

        StringEntity input = new StringEntity(data);
        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpclient.execute(postRequest);
        int statusCode = response.getStatusLine().getStatusCode();

        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

        String line = "";
        while ((line = br.readLine()) != null) {
            body += line;
        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    return body;
}