Example usage for org.apache.http.client.methods CloseableHttpResponse toString

List of usage examples for org.apache.http.client.methods CloseableHttpResponse toString

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:fr.istic.taa.jaxrs.StatusEndpoint.java

@GET
@Path("/swapi")
@Produces(MediaType.APPLICATION_JSON)//from w w  w . j  a va  2 s .c  o  m
public String getSwapi() throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://swapi.co/api/people/1/");
    CloseableHttpResponse response = httpclient.execute(httpget);
    System.out.println(response.toString());
    try {

    } finally {
        response.close();
    }
    return null;

}

From source file:org.apache.hive.service.server.TestHS2HttpServerPam.java

@Test
public void testAuthorizedConnection() throws Exception {
    CloseableHttpClient httpclient = null;
    try {/*from  w w  w  . j  a  v a  2  s  .  c  o m*/
        String username = "user1";
        String password = "1";
        httpclient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("http://" + host + ":" + webUIPort);
        String authB64Code = B64Code.encode(username + ":" + password, StringUtil.__ISO_8859_1);
        httpGet.setHeader(HttpHeader.AUTHORIZATION.asString(), "Basic " + authB64Code);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        Assert.assertTrue(response.toString().contains(Integer.toString(HttpURLConnection.HTTP_OK)));

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:org.apache.hive.service.server.TestHS2HttpServerPam.java

@Test
public void testIncorrectUser() throws Exception {
    CloseableHttpClient httpclient = null;
    try {/*ww  w  .  ja va  2s  . c o m*/
        String username = "nouser";
        String password = "aaaa";
        httpclient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("http://" + host + ":" + webUIPort);
        String authB64Code = B64Code.encode(username + ":" + password, StringUtil.__ISO_8859_1);
        httpGet.setHeader(HttpHeader.AUTHORIZATION.asString(), "Basic " + authB64Code);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        Assert.assertTrue(response.toString().contains(Integer.toString(HttpURLConnection.HTTP_UNAUTHORIZED)));

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:org.apache.hive.service.server.TestHS2HttpServerPam.java

@Test
public void testIncorrectPassword() throws Exception {
    CloseableHttpClient httpclient = null;
    try {/*from  w  w w . j  a  va 2  s  .  c  om*/
        String username = "user1";
        String password = "aaaa";
        httpclient = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet("http://" + host + ":" + webUIPort);
        String authB64Code = B64Code.encode(username + ":" + password, StringUtil.__ISO_8859_1);
        httpGet.setHeader(HttpHeader.AUTHORIZATION.asString(), "Basic " + authB64Code);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        Assert.assertTrue(response.toString().contains(Integer.toString(HttpURLConnection.HTTP_UNAUTHORIZED)));

    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
}

From source file:org.wso2.security.tools.scanner.dependency.js.ticketcreator.JIRARestClient.java

/**
 * Invoke put method to attach file for particular ticket.
 *
 * @param auth credentials info of JIRA.
 * @param url  url to be invoked.//from w w w . j a v a2s  .com
 * @param path path of the file to be attached.
 * @throws TicketCreatorException Exception occurred while attaching the file with ticket.
 */
public void invokePutMethodWithFile(String auth, String url, String path) throws TicketCreatorException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("X-Atlassian-Token", "nocheck");
    httppost.setHeader("Authorization", "Basic " + auth);
    File fileToUpload = new File(path);
    FileBody fileBody = new FileBody(fileToUpload);
    HttpEntity entity = MultipartEntityBuilder.create().addPart("file", fileBody).build();
    httppost.setEntity(entity);
    CloseableHttpResponse response;
    try {
        response = httpclient.execute(httppost);
        log.info("[JS_SEC_DAILY_SCAN] File attached with ticket : " + response.toString());
    } catch (IOException e) {
        throw new TicketCreatorException(
                "File upload failed while attaching the scan report with issue ticket: " + path, e);
    } finally {
        try {
            httpclient.close();
        } catch (IOException e) {
            log.error("Exception occurred while closing the http connection", e);
        }
    }
}

From source file:com.srotya.tau.ui.rules.TenantManager.java

public List<Tenant> getTenants(UserBean ub) throws Exception {
    CloseableHttpClient client = Utils.buildClient(am.getBaseUrl(), am.getConnectTimeout(),
            am.getRequestTimeout());//from   w  w  w .j a va2  s. c  om
    HttpGet get = new HttpGet(am.getBaseUrl() + TENANT_URL);
    if (am.isEnableAuth()) {
        get.addHeader(BapiLoginDAO.X_SUBJECT_TOKEN, ub.getToken());
        get.addHeader(BapiLoginDAO.HMAC, ub.getHmac());
    }
    CloseableHttpResponse resp = client.execute(get);
    if (Utils.validateStatus(resp)) {
        String result = EntityUtils.toString(resp.getEntity());
        Gson gson = new Gson();
        return Arrays.asList(gson.fromJson(result, Tenant[].class));
    } else {
        throw new Exception("Tenant not found:" + resp.toString() + "\t" + am.isEnableAuth() + "\thmac:"
                + ub.getHmac() + "\ttoken:" + ub.getToken());
    }
}

From source file:org.commonjava.indy.httprox.ProxyHttpsWildcardHostCertTest.java

protected String head(String url, boolean withCACert, String user, String pass) throws Exception {
    CloseableHttpClient client;//from   ww  w .  j a v a 2s. c o  m

    if (withCACert) {
        File jks = new File(etcDir, "ssl/ca.jks");
        KeyStore trustStore = getTrustStore(jks);
        SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
        client = proxiedHttp(user, pass, socketFactory);
    } else {
        client = proxiedHttp(user, pass);
    }

    HttpHead req = new HttpHead(url);
    CloseableHttpResponse response = null;

    InputStream stream = null;
    try {
        response = client.execute(req, proxyContext(user, pass));
        /*stream = response.getEntity().getContent();
        final String resulting = IOUtils.toString( stream );
                
        assertThat( resulting, notNullValue() );
        System.out.println( "\n\n>>>>>>>\n\n" + resulting + "\n\n" );*/

        return response.toString();
    } finally {
        IOUtils.closeQuietly(stream);
        HttpResources.cleanupResources(req, response, client);
    }
}

From source file:service.S4ServiceClient.java

/**
 * Sends a test request to the service//www .  j av  a  2  s  . c o m
 * @return true if the service is available
 */
public boolean testEndpoint() {
    HttpGet get = new HttpGet(getEndpointUrl().substring(0, getEndpointUrl().lastIndexOf("/")));
    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(get, ctx);
        StatusLine sl = response.getStatusLine();
        int statusCode = sl.getStatusCode();
        if (statusCode != 200) {
            System.out.println("Error communicating with endpoint.");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            return false;
        } else {
            System.out.println("Endpoint returned status SUCCESS.");
            System.out.println(response.toString());
            System.out.println("Response body: ");
            System.out.println(getContent(response));
            return true;
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
        } catch (IOException e) {
        }
    }
    return false;
}

From source file:jenkins.plugins.logstash.persistence.ElasticSearchDao.java

private String getErrorMessage(CloseableHttpResponse response) {
    ByteArrayOutputStream byteStream = null;
    PrintStream stream = null;//from w ww .j a  v a 2  s . c o m
    try {
        byteStream = new ByteArrayOutputStream();
        stream = new PrintStream(byteStream);

        try {
            stream.print("HTTP error code: ");
            stream.println(response.getStatusLine().getStatusCode());
            stream.print("URI: ");
            stream.println(uri.toString());
            stream.println("RESPONSE: " + response.toString());
            response.getEntity().writeTo(stream);
        } catch (IOException e) {
            stream.println(ExceptionUtils.getStackTrace(e));
        }
        stream.flush();
        return byteStream.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:service.S4ServiceClient.java

/**
 * Serialize a ProcessingRequest and send it to Self Service Semantic Suite Online Processing Service
 * @param pr the processing request to send
 * @param acceptType the type of output we want to produce
 *///from  ww  w  .java 2  s .co m
public String processRequest(String message, String acceptType, String contentType) {
    HttpPost post = new HttpPost(getEndpointUrl());
    post.setHeader("Content-Type", contentType);
    post.setHeader("Accept", acceptType);
    post.setHeader("Accept-Encoding", "gzip");

    System.out.println("POST body is:");
    System.out.println(message);

    post.setEntity(new StringEntity(message, Charset.forName("UTF-8")));

    CloseableHttpResponse response = null;
    try {
        response = httpClient.execute(post, ctx);
        StatusLine sl = response.getStatusLine();
        int statusCode = sl.getStatusCode();
        switch (statusCode) {
        case 200: {
            //Request was processed successfully
            System.out.println("SUCCESS");
            System.out.println(response.toString());
            return getContent(response);
        }
        case 400: {
            //Bad request, there is some problem with user input
            System.out.println("Bad request");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 403: {
            //Problem with user authentication
            System.out.println("Error during authentication");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 404: {
            //Not found
            System.out.println("Not found, check endpoint URL");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 406: {
            //Not Accepted
            System.out.println("The request was not accepted. Check Accept header");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 408: {
            //Processing this request took too long
            System.out.println("Could not process document in time");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 415: {
            //Unsupported media type
            System.out.println("Invalid value in Content-Type header");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        case 500: {
            //Internal server error
            System.out.println("Error during processing");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        default: {
            System.out.println("Could not process request");
            System.out.println(response.toString());
            System.out.println(getContent(response));
            break;
        }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
        } catch (IOException e) {
        }
    }
    return null;
}