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.apache.camel.itest.osgi.cxf.blueprint.CxfRsBlueprintRouterTest.java

@Test
public void testGetCustomer() throws Exception {
    HttpGet get = new HttpGet("http://localhost:9000/route/customerservice/customers/123");
    get.addHeader("Accept", "application/json");
    HttpClient httpclient = new DefaultHttpClient();

    try {/*  w w w . ja  va2 s .co  m*/
        HttpResponse response = httpclient.execute(get);
        assertEquals(200, response.getStatusLine().getStatusCode());

        // should either by John or Mary depending on PUT test executed first
        String s = EntityUtils.toString(response.getEntity());
        boolean isJohn = "{\"Customer\":{\"id\":123,\"name\":\"John\"}}".equals(s);
        boolean isMary = "{\"Customer\":{\"id\":123,\"name\":\"Mary\"}}".equals(s);
        assertTrue("Should be John or Mary", isJohn || isMary);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.cm.podd.report.util.RequestDataUtil.java

public static ResponseObject registerDeviceId(String deviceId, String token) {
    JSONObject jsonObj = null;//from   w ww .  j a va 2 s  . c om
    int statusCode = 0;

    //SharedPreferences settings = PoddApplication.getAppContext().getSharedPreferences("PoddPrefsFile", 0);
    String serverUrl = settings.getString("serverUrl", BuildConfig.SERVER_URL);

    String reqUrl = serverUrl + "/gcm/";
    Log.i(TAG, "submit url=" + reqUrl);

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient client = new DefaultHttpClient(params);

    try {
        HttpPost post = new HttpPost(reqUrl);
        post.setHeader("Content-Type", "application/json");

        if (token != null) {
            post.setHeader("Authorization", "Token " + token);
        }

        JSONObject data = new JSONObject();
        try {
            data.put("gcmRegId", deviceId);
        } catch (JSONException e) {
            Log.e(TAG, "Error while create json object", e);
        }

        post.setEntity(new StringEntity(data.toString(), HTTP.UTF_8));

        HttpResponse response;
        response = client.execute(post);
        HttpEntity entity = response.getEntity();

        // Detect server complaints
        statusCode = response.getStatusLine().getStatusCode();
        Log.v(TAG, "status code=" + statusCode);

        if (statusCode == HttpURLConnection.HTTP_OK || statusCode == HttpURLConnection.HTTP_CREATED) {
            InputStream in = entity.getContent();
            String resp = FileUtil.convertInputStreamToString(in);
            Log.d(TAG, "Register device id : Response text= " + resp);
            jsonObj = new JSONObject();
            entity.consumeContent();
        }

    } catch (ClientProtocolException e) {
        Log.e(TAG, "error post data", e);
    } catch (IOException e) {
        Log.e(TAG, "Can't connect server", e);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return new ResponseObject(statusCode, jsonObj);
}

From source file:org.sociotech.communitymashup.framework.java.apiwrapper.CommunityMashupApi.java

/**
 * Processes a get request against the given url.
 * //w ww . jav  a 2s .c om
 * @param url
 *            Url for the get request.
 * @return The response as string
 * @throws MashupConnectionException
 *             If connection was not successful
 */
private String doGet(String url) throws MashupConnectionException {
    // System.out.println("calling: " + url);

    String result = null;

    HttpClient httpClient = new DefaultHttpClient();

    HttpGet get = new HttpGet(url);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    try {
        result = httpClient.execute(get, responseHandler);
    } catch (Exception e) {
        throw new MashupConnectionException(e, url);
    } finally {
        // client is no longer needed
        httpClient.getConnectionManager().shutdown();
    }

    // System.out.println("got: " + result);

    return result;
}

From source file:org.apache.juddi.v3.tck.UDDI_160_RESTIntergrationTest.java

@Test
public void InquiryREST_GET_TModel() throws Exception {
    Assume.assumeTrue(TckPublisher.isEnabled());
    Assume.assumeTrue(TckPublisher.isInquiryRestEnabled());
    FindTModel fb = new FindTModel();
    fb.setMaxRows(1);//ww  w  .  j  a v  a  2s.  c om
    fb.setName(new Name(UDDIConstants.WILDCARD, null));
    fb.setFindQualifiers(new FindQualifiers());
    fb.getFindQualifiers().getFindQualifier().add(UDDIConstants.APPROXIMATE_MATCH);
    TModelList findTModel = inquiry.findTModel(fb);
    Assume.assumeTrue(findTModel != null);
    Assume.assumeTrue(findTModel.getTModelInfos() != null);
    Assume.assumeTrue(!findTModel.getTModelInfos().getTModelInfo().isEmpty());

    String url = manager.getClientConfig().getHomeNode().getInquiry_REST_Url();

    Assume.assumeNotNull(url);

    HttpClient client = new DefaultHttpClient();

    HttpGet httpGet = new HttpGet(
            url + "?tModelKey=" + findTModel.getTModelInfos().getTModelInfo().get(0).getTModelKey());
    logger.info("Fetching " + httpGet.getURI());
    HttpResponse response = client.execute(httpGet);

    Assert.assertTrue(response.getStatusLine().getStatusCode() == 200);
    logger.info("Response content: " + response.getEntity().getContent());
    TModel unmarshal = JAXB.unmarshal(response.getEntity().getContent(), TModel.class);
    client.getConnectionManager().shutdown();
    Assert.assertNotNull(unmarshal);
    Assert.assertEquals(unmarshal.getTModelKey(),
            findTModel.getTModelInfos().getTModelInfo().get(0).getTModelKey());

}

From source file:org.semantictools.jsonld.impl.AppspotContextPublisher.java

@Override
public void publish(LdAsset asset) throws LdPublishException {

    String uri = asset.getURI();/*from   w ww.j  av a 2  s  .c o m*/

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(SERVLET_URL);
    post.setHeader("CONTENT-TYPE", "multipart/form-data; boundary=xxxBOUNDARYxxx");
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "xxxBOUNDARYxxx",
            Charset.forName("UTF-8"));

    try {

        String content = asset.loadContent();

        entity.addPart(URI, new StringBody(uri));
        entity.addPart(FILE_UPLOAD, new StringBody(content));
        post.setEntity(entity);

        logger.debug("uploading... " + uri);

        HttpResponse response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();

        client.getConnectionManager().shutdown();
        if (status != HttpURLConnection.HTTP_OK) {
            throw new LdPublishException(uri, null);
        }

    } catch (Exception e) {
        throw new LdPublishException(uri, e);
    }

}

From source file:com.couchbase.capi.TestCouchbase.java

public void testPool() throws Exception {
    HttpClient client = getClient();

    // first access the pool with its uuid
    HttpUriRequest request = new HttpGet(
            String.format("http://localhost:%d/pools/default?uuid=00000000000000000000000000000000", port));
    HttpResponse response = client.execute(request);
    validateSuccessfulPoolResponse(response);

    // now access it with the wrong uuid
    request = new HttpGet(
            String.format("http://localhost:%d/pools/default?uuid=00000000000000000000000000000001", port));
    response = client.execute(request);//w  w  w.j  a  v a  2s  .  c o m
    validateMissingPoolResponse(response);

    client.getConnectionManager().shutdown();
}

From source file:net.paissad.waqtsalat.service.utils.HttpUtils.java

private static InputStream sendRequest(final String url, final RequestType requestType) throws WSException {

    InputStream in = null;//  w  w w. ja  va2 s  .c om
    HttpClient client = null;
    HttpRequestBase request = null;
    boolean errorOccured = false;

    try {
        request = getNewHttpRequest(url, requestType, null);
        client = getNewHttpClient();
        HttpResponse resp = client.execute(request);

        int statusCode = resp.getStatusLine().getStatusCode();

        if (statusCode == HttpStatus.SC_OK) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(resp.getEntity().getContent(), baos);
            baos.flush();
            in = new ByteArrayInputStream(baos.toByteArray());

        } else {
            // The response code is not OK.
            StringBuilder errMsg = new StringBuilder();
            errMsg.append("HTTP ").append(requestType).append(" failed => ").append(resp.getStatusLine());
            if (request != null) {
                errMsg.append(" : ").append(request.getURI());
            }
            throw new WSException(errMsg.toString());
        }

        return in;

    } catch (Exception e) {
        errorOccured = true;
        throw new WSException("Error during file HTTP request.", e);

    } finally {
        if (errorOccured) {
            if (request != null) {
                request.abort();
            }
        }
        if (client != null) {
            client.getConnectionManager().shutdown();
        }
    }
}