Example usage for org.apache.http.impl.client CloseableHttpClient execute

List of usage examples for org.apache.http.impl.client CloseableHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:org.wuspba.ctams.ws.ITBandRegistrationController.java

protected static void add() throws Exception {
    ITBandController.add();//w ww  .java 2s  .  c  o  m

    CTAMSDocument doc = new CTAMSDocument();
    doc.getBands().add(TestFixture.INSTANCE.skye);
    doc.getBandRegistrations().add(TestFixture.INSTANCE.bandRegistration);

    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        TestFixture.INSTANCE.bandRegistration.setId(doc.getBandRegistrations().get(0).getId());

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:org.wuspba.ctams.ws.ITSoloRegistrationController.java

private static void add() throws Exception {
    ITPersonController.add();/*from w ww.  j  a  v a  2  s.co m*/

    CTAMSDocument doc = new CTAMSDocument();
    doc.getPeople().add(TestFixture.INSTANCE.elaine);
    doc.getSoloRegistrations().add(TestFixture.INSTANCE.soloRegistration);

    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        TestFixture.INSTANCE.soloRegistration.setId(doc.getSoloRegistrations().get(0).getId());

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:org.wuspba.ctams.ws.ITSoloResultController.java

protected static void delete() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH)
            .setParameter("id", TestFixture.INSTANCE.soloResult.getId()).build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {/*from w w w.  ja va 2s. c o  m*/
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITSoloContestController.delete();
}

From source file:com.zxy.commons.httpclient.HttpclientUtils.java

/**
 * Get url/*from  w w w . ja  va2s  .c  o m*/
 * 
 * @param connectTimeoutSec ()
 * @param socketTimeoutSec ??()
 * @param url url?
 * @return post??
 * @throws ClientProtocolException ClientProtocolException
 * @throws IOException IOException
 */
public static String get(int connectTimeoutSec, int socketTimeoutSec, String url)
        throws ClientProtocolException, IOException {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;
    try {
        //            httpClient = HttpClients.createDefault();
        RequestConfig config = RequestConfig.custom().setConnectTimeout(connectTimeoutSec * 1000)
                .setConnectionRequestTimeout(connectTimeoutSec * 1000).setSocketTimeout(socketTimeoutSec * 1000)
                .build();
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();

        HttpGet httpGet = new HttpGet(url);

        httpResponse = httpClient.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        return EntityUtils.toString(entity, Charsets.UTF_8);
    } finally {
        HttpClientUtils.closeQuietly(httpResponse);
        HttpClientUtils.closeQuietly(httpClient);
    }
}

From source file:org.wuspba.ctams.ws.ITBandContestController.java

protected static void delete() throws Exception {
    String id;//from   ww  w  . j a  v a  2  s .co m

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpGet httpGet = new HttpGet(uri);

    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        assertEquals(response.getStatusLine().toString(), IntegrationTestUtils.OK_STRING);

        HttpEntity entity = response.getEntity();

        CTAMSDocument doc = IntegrationTestUtils.convertEntity(entity);

        id = doc.getBandContests().get(0).getId();

        EntityUtils.consume(entity);
    }

    httpclient = HttpClients.createDefault();

    uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).setParameter("id", id)
            .build();

    HttpDelete httpDelete = new HttpDelete(uri);

    CloseableHttpResponse response = null;

    try {
        response = httpclient.execute(httpDelete);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }

    ITVenueController.delete();
    ITHiredJudgeController.delete();
    ITJudgeController.delete();
}

From source file:utils.APIExporter.java

/**
 * This method will get the summary of API documentation
 * @param accessToken access token with scope view
 * @param uuid uuid of the API/*from w ww  .j a  v  a  2s. c  om*/
 * @return String output of documentation summary
 */
private static String getAPIDocumentation(String accessToken, String uuid) {
    ApiImportExportConfiguration config = ApiImportExportConfiguration.getInstance();
    try {
        //REST API call on Get API Documents
        String url = config.getPublisherUrl() + "apis/" + uuid + "/documents";
        CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate());
        //HttpClients.createDefault();
        HttpGet request = new HttpGet(url);
        request.setHeader(HttpHeaders.AUTHORIZATION,
                ImportExportConstants.CONSUMER_KEY_SEGMENT + " " + accessToken);
        CloseableHttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity, ImportExportConstants.CHARSET);
    } catch (IOException e) {
        log.error("Error occurred while getting API documents");
    }
    return null;
}

From source file:utils.ImportExportUtils.java

/**
 * retrieve a access token with requested scope
 *
 * @param scope required token scope//from  www  .  jav a 2 s.  c  om
 * @param  consumerCredentials encoded consumerKey and consumerSecret
 */

static String getAccessToken(String scope, String consumerCredentials) {

    ApiImportExportConfiguration config = ApiImportExportConfiguration.getInstance();
    String url = config.getGatewayUrl();
    String responseString;

    //mapping payload to a List
    List<NameValuePair> params = new ArrayList<>(4);
    params.add(new BasicNameValuePair(ImportExportConstants.TOKEN_GRANT_TYPE,
            ImportExportConstants.DEFAULT_GRANT_TYPE));
    params.add(new BasicNameValuePair(ImportExportConstants.USERNAME, config.getUsername()));
    params.add(new BasicNameValuePair(ImportExportConstants.DEFAULT_GRANT_TYPE,
            String.valueOf(config.getPassword())));
    params.add(new BasicNameValuePair(ImportExportConstants.SCOPE_CONSTANT, scope));

    //REST API call for get tokens
    CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate());
    try {
        HttpPost request = new HttpPost(url);
        request.setEntity(new UrlEncodedFormEntity(params, ImportExportConstants.CHARSET));
        request.setHeader(HttpHeaders.AUTHORIZATION,
                ImportExportConstants.AUTHORIZATION_KEY_SEGMENT + " " + consumerCredentials);
        CloseableHttpResponse response;
        response = client.execute(request);
        responseString = EntityUtils.toString(response.getEntity());
        JSONObject jsonObj = (JSONObject) new JSONParser().parse(responseString);
        return ((String) jsonObj.get(ImportExportConstants.ACCESS_TOKEN));
    } catch (ParseException e) {
        log.error("error occurred while getting the access token");
    } catch (UnsupportedEncodingException | ClientProtocolException e) {
        String errormsg = "error occurred while passing the payload for token generation";
        log.error(errormsg, e);
        return null;
    } catch (IOException e) {
        String errormsg = "error occurred while generating tokens";
        log.error(errormsg, e);
        return null;
    } finally {
        IOUtils.closeQuietly(client);
    }
    return null;
}

From source file:org.bedework.util.http.HttpUtil.java

public static CloseableHttpResponse doPost(final CloseableHttpClient cl, final URI uri, final Headers hdrs,
        final String contentType, final String content) throws IOException {
    final Headers headers = ensureHeaders(hdrs);

    if (contentType != null) {
        headers.add("Content-type", contentType);
    }/*from   w ww.j a va  2s . c o  m*/

    final HttpPost httpPost = new HttpPost(uri);

    httpPost.setHeaders(hdrs.asArray());

    if (content != null) {
        final StringEntity entity = new StringEntity(content);
        httpPost.setEntity(entity);
    }

    return cl.execute(httpPost);
}

From source file:org.wuspba.ctams.ws.ITJudgeController.java

private static void add(Judge j) throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getPeople().add(j.getPerson());/*from w ww.  j  a va2  s . c  om*/
    doc.getJudgeQualifications().addAll(j.getQualifications());
    doc.getJudges().add(j);
    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        j.setId(doc.getJudges().get(0).getId());

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}

From source file:org.wuspba.ctams.ws.ITJudgeQualificationController.java

protected static void add() throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getJudgeQualifications().add(TestFixture.INSTANCE.drummingQual);
    doc.getJudgeQualifications().add(TestFixture.INSTANCE.ensembleQual);
    doc.getJudgeQualifications().add(TestFixture.INSTANCE.pipingQual);
    String xml = XMLUtils.marshal(doc);

    CloseableHttpClient httpclient = HttpClients.createDefault();

    URI uri = new URIBuilder().setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(PATH).build();

    HttpPost httpPost = new HttpPost(uri);

    StringEntity xmlEntity = new StringEntity(xml, ContentType.APPLICATION_XML);

    CloseableHttpResponse response = null;

    try {/*w w  w. ja va  2 s  . co m*/
        httpPost.setEntity(xmlEntity);
        response = httpclient.execute(httpPost);

        assertEquals(IntegrationTestUtils.OK_STRING, response.getStatusLine().toString());

        HttpEntity responseEntity = response.getEntity();

        doc = IntegrationTestUtils.convertEntity(responseEntity);

        for (JudgeQualification q : doc.getJudgeQualifications()) {
            if (q.getPanel() == TestFixture.INSTANCE.pipingQual.getPanel()
                    && q.getType() == TestFixture.INSTANCE.pipingQual.getType()) {
                TestFixture.INSTANCE.pipingQual.setId(q.getId());
            } else if (q.getPanel() == TestFixture.INSTANCE.drummingQual.getPanel()
                    && q.getType() == TestFixture.INSTANCE.drummingQual.getType()) {
                TestFixture.INSTANCE.drummingQual.setId(q.getId());
            } else if (q.getPanel() == TestFixture.INSTANCE.ensembleQual.getPanel()
                    && q.getType() == TestFixture.INSTANCE.ensembleQual.getType()) {
                TestFixture.INSTANCE.ensembleQual.setId(q.getId());
            }
        }

        EntityUtils.consume(responseEntity);
    } catch (UnsupportedEncodingException ex) {
        LOG.error("Unsupported coding", ex);
    } catch (IOException ioex) {
        LOG.error("IOException", ioex);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException ex) {
                LOG.error("Could not close response", ex);
            }
        }
    }
}