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

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

Introduction

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

Prototype

HttpEntity getEntity();

Source Link

Usage

From source file:de.tu_dortmund.ub.data.util.TPUUtil.java

public static String writeResultToFile(final CloseableHttpResponse httpResponse, final Properties config,
        final String exportDataModelID, final String fileEnding) throws IOException, TPUException {

    LOG.info("try to write result to file");

    final String persistInFolderString = config.getProperty(TPUStatics.PERSIST_IN_FOLDER_IDENTIFIER);
    final boolean persistInFolder = Boolean.parseBoolean(persistInFolderString);
    final HttpEntity entity = httpResponse.getEntity();

    final String fileName;

    if (persistInFolder) {

        final InputStream responseStream = entity.getContent();
        final BufferedInputStream bis = new BufferedInputStream(responseStream, 1024);

        final String resultsFolder = config.getProperty(TPUStatics.RESULTS_FOLDER_IDENTIFIER);
        fileName = resultsFolder + File.separatorChar + EXPORT_FILE_NAME_PREFIX + exportDataModelID + DOT
                + fileEnding;/*from w w  w  .ja va2s .c  o m*/

        LOG.info(String.format("start writing result to file '%s'", fileName));

        final FileOutputStream outputStream = new FileOutputStream(fileName);
        final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        IOUtils.copy(bis, bufferedOutputStream);
        bufferedOutputStream.flush();
        outputStream.flush();
        bis.close();
        responseStream.close();
        bufferedOutputStream.close();
        outputStream.close();

        checkResultForError(fileName);
    } else {

        fileName = "[no file name available]";
    }

    EntityUtils.consume(entity);

    return fileName;
}

From source file:org.eclipse.wst.json.core.internal.download.HttpClientProvider.java

private static File download(File file, URL url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    OutputStream out = null;//from  w ww. j  a  va 2 s .c o m
    file.getParentFile().mkdirs();
    try {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
        Builder builder = RequestConfig.custom();
        HttpHost proxy = getProxy(target);
        if (proxy != null) {
            builder = builder.setProxy(proxy);
        }
        RequestConfig config = builder.build();
        HttpGet request = new HttpGet(url.toURI());
        request.setConfig(config);
        response = httpclient.execute(target, request);
        InputStream in = response.getEntity().getContent();
        out = new BufferedOutputStream(new FileOutputStream(file));
        copy(in, out);
        return file;
    } catch (Exception e) {
        logWarning(e);
        ;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
            }
        }
        try {
            httpclient.close();
        } catch (IOException e) {
        }
    }
    return null;
}

From source file:utils.APIExporter.java

/**
 * generate a archive of exporting API//  w  w  w  . j ava 2s. c  o m
 * @param apiName name of the API
 * @param provider provider of the API
 * @param version version of the API
 * @param accessToken valid access token
 */
private static void exportAPI(String destinationLocation, String apiName, String provider, String version,
        String accessToken) throws APIExportException {

    ApiImportExportConfiguration config = ApiImportExportConfiguration.getInstance();

    //creating a directory to hold API details
    String APIFolderPath = destinationLocation.concat(File.separator + apiName + "-" + version);
    ImportExportUtils.createDirectory(APIFolderPath);

    //building the API id
    String apiId = provider + "-" + apiName + "-" + version;
    String responseString;
    try {
        //getting API meta- information
        CloseableHttpClient client = HttpClientGenerator.getHttpClient(config.getCheckSSLCertificate());
        String metaInfoUrl = config.getPublisherUrl() + "apis/" + apiId;
        HttpGet request = new HttpGet(metaInfoUrl);
        request.setHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION,
                ImportExportConstants.CONSUMER_KEY_SEGMENT + " " + accessToken);
        CloseableHttpResponse response = client.execute(request);
        if (response.getStatusLine().getStatusCode() == 404) { ////// TODO: 8/24/16 int value constant 
            String message = "API " + apiId + "does not exist/ not found ";
            log.warn(message);
            return;
        }
        HttpEntity entity = response.getEntity();
        responseString = EntityUtils.toString(entity, ImportExportConstants.CHARSET);
    } catch (IOException e) {
        String errorMsg = "Error occurred while retrieving the API meta information";
        log.error(errorMsg, e);
        throw new APIExportException(errorMsg, e);
    }
    //creating directory to hold meta-information
    String metaInfoFolderPath = APIFolderPath.concat(File.separator + "meta-information");
    ImportExportUtils.createDirectory(metaInfoFolderPath);

    //set API status and scope before exporting
    setJsonValues(responseString, ImportExportConstants.STATUS_CONSTANT, ImportExportConstants.CREATED);
    setJsonValues(responseString, ImportExportConstants.SCOPE_CONSTANT, null);

    try {
        //writing meta-information
        Object json = mapper.readValue(responseString, Object.class);
        String formattedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
        writeFile(metaInfoFolderPath + File.separator + "api.json", formattedJson);
    } catch (IOException e) {
        String error = "Error occurred while formatting the json string";
        log.error(error, e);
        throw new APIExportException(error, e);
    }

    JSONObject jsonObj = null;
    try {
        jsonObj = (JSONObject) parser.parse(responseString);
        String swagger = (String) jsonObj.get(ImportExportConstants.SWAGGER);
        writeFile(metaInfoFolderPath + File.separator + "swagger.json", swagger);
    } catch (ParseException e) {
        log.error("error occurred while getting swagger definision");
    }
    //get API uuid
    String uuid = null;
    if (jsonObj != null) {
        uuid = (String) jsonObj.get(ImportExportConstants.UUID);
    }

    //export api thumbnail
    String thumbnailUrl = null;
    if (jsonObj != null) {
        thumbnailUrl = (String) jsonObj.get(ImportExportConstants.THUMBNAIL);
    }
    if (thumbnailUrl != null) {
        exportAPIThumbnail(uuid, accessToken, APIFolderPath);
    }

    //export api documents
    String documentationSummary = getAPIDocumentation(accessToken, uuid);
    exportAPIDocumentation(uuid, documentationSummary, accessToken, APIFolderPath);
}

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

private static void add(Person person) throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getPeople().add(person);// w  ww.java 2 s.co  m
    CloseableHttpClient httpclient = HttpClients.createDefault();
    String xml = XMLUtils.marshal(doc);

    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);

        for (Person p : doc.getPeople()) {
            if (p.getFirstName().equals(TestFixture.INSTANCE.andy.getFirstName())) {
                TestFixture.INSTANCE.andy.setId(p.getId());
            } else if (p.getFirstName().equals(TestFixture.INSTANCE.bob.getFirstName())) {
                TestFixture.INSTANCE.bob.setId(p.getId());
            } else if (p.getFirstName().equals(TestFixture.INSTANCE.elaine.getFirstName())) {
                TestFixture.INSTANCE.elaine.setId(p.getId());
            } else if (p.getFirstName().equals(TestFixture.INSTANCE.eoin.getFirstName())) {
                TestFixture.INSTANCE.eoin.setId(p.getId());
            } else if (p.getFirstName().equals(TestFixture.INSTANCE.jamie.getFirstName())) {
                TestFixture.INSTANCE.jamie.setId(p.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:eu.diacron.crawlservice.app.Util.java

public static String getCrawlStatusById(String crawlid) {

    String status = "";
    System.out.println("get crawlid");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));

    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*ww  w .  jav a2s  . com*/
        //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/crawl/" + crawlid);
        HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL_CRAWL + crawlid);

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            String result = "";

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                result += inputLine;
            }
            in.close();

            // TO-DO should be removed in the future and handle it more gracefully
            result = result.replace("u'", "'");
            result = result.replace("'", "\"");

            JSONObject crawljson = new JSONObject(result);
            System.out.println("myObject " + crawljson.toString());

            status = crawljson.getString("status");

            EntityUtils.consume(response.getEntity());
        } catch (JSONException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return status;
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

private static String assert200Ok(CloseableHttpResponse res, String expectedContentType, String expectedContent)
        throws Exception {

    assertStatusLine(res, "HTTP/1.1 200 OK");

    // Ensure that the 'Last-Modified' header exists and is well-formed.
    final String lastModified;
    assertThat(res.containsHeader(HttpHeaders.LAST_MODIFIED), is(true));
    lastModified = res.getFirstHeader(HttpHeaders.LAST_MODIFIED).getValue();
    HttpHeaderDateFormat.get().parse(lastModified);

    // Ensure the content and its type are correct.
    assertThat(EntityUtils.toString(res.getEntity()), is(expectedContent));

    if (expectedContentType != null) {
        assertThat(res.containsHeader(HttpHeaders.CONTENT_TYPE), is(true));
        assertThat(res.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(), startsWith(expectedContentType));
    } else {//from   ww w .  j  a  v  a2  s  . c  om
        assertThat(res.containsHeader(HttpHeaders.CONTENT_TYPE), is(false));
    }

    return lastModified;
}

From source file:eu.diacron.crawlservice.app.Util.java

public static JSONArray getwarcsByCrawlid(String crawlid) {

    JSONArray warcsArray = null;/*w w  w  . jav a  2  s  . com*/
    System.out.println("get crawlid");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    /*        credsProvider.setCredentials(
     new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
     new UsernamePasswordCredentials("diachron", "7nD9dNGshTtficn"));
     */

    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
            new UsernamePasswordCredentials(Configuration.REMOTE_CRAWLER_USERNAME,
                    Configuration.REMOTE_CRAWLER_PASS));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {

        //HttpGet httpget = new HttpGet("http://diachron.hanzoarchives.com/warcs/" + crawlid);
        HttpGet httpget = new HttpGet(Configuration.REMOTE_CRAWLER_URL + crawlid);

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println("----------------------------------------");

            String result = "";

            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                result += inputLine;
            }
            in.close();

            result = result.replace("u'", "'");
            result = result.replace("'", "\"");

            warcsArray = new JSONArray(result);

            for (int i = 0; i < warcsArray.length(); i++) {

                System.out.println("url to download: " + warcsArray.getString(i));

            }

            EntityUtils.consume(response.getEntity());
        } catch (JSONException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            httpclient.close();
        } catch (IOException ex) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    return warcsArray;
}

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

private static void add(Roster roster) throws Exception {
    CTAMSDocument doc = new CTAMSDocument();
    doc.getRosters().add(roster);/*from ww w  .java2s  .c o  m*/
    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);

        roster.setId(doc.getRosters().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:com.zxy.commons.httpclient.HttpclientUtils.java

/**
 * url?/*from  w  w w.j av  a 2  s .  co m*/
 * 
 * @param connectTimeoutSec ()
 * @param socketTimeoutSec ??()
 * @param url url?
 * @param outputFile ?
 * @return ??
 * @throws ClientProtocolException ClientProtocolException
 * @throws IOException IOException
 */
public static boolean download(int connectTimeoutSec, int socketTimeoutSec, String url, String outputFile)
        throws ClientProtocolException, IOException {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse httpResponse = null;

    InputStream in = null;
    OutputStream out = 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();
        in = entity.getContent();

        File file = new File(outputFile);
        out = new FileOutputStream(file);
        IOUtils.copy(in, out);
        return file.exists();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        HttpClientUtils.closeQuietly(httpResponse);
        HttpClientUtils.closeQuietly(httpClient);
    }
}

From source file:nl.knaw.dans.easy.sword2examples.Common.java

static URI trackDeposit(CloseableHttpClient http, URI statUri) throws Exception {
    CloseableHttpResponse response;
    String bodyText;// ww  w . j a v a2 s .c o m
    System.out.println(
            "Start polling Stat-IRI for the current status of the deposit, waiting 10 seconds before every request ...");
    while (true) {
        Thread.sleep(10000);
        System.out.print("Checking deposit status ... ");
        response = http.execute(new HttpGet(statUri));
        if (response.getStatusLine().getStatusCode() != 200) {
            System.out.println("Stat-IRI returned " + response.getStatusLine().getStatusCode());
            System.exit(1);
        }
        bodyText = readEntityAsString(response.getEntity());
        Feed statement = parse(bodyText);
        List<Category> states = statement.getCategories("http://purl.org/net/sword/terms/state");
        if (states.isEmpty()) {
            System.err.println("ERROR: NO STATE FOUND");
            System.exit(1);
        } else if (states.size() > 1) {
            System.err.println("ERROR: FOUND TOO MANY STATES (" + states.size() + "). CAN ONLY HANDLE ONE");
            System.exit(1);
        } else {
            String state = states.get(0).getTerm();
            System.out.println(state);
            if (state.equals("INVALID") || state.equals("REJECTED") || state.equals("FAILED")) {
                System.err.println("FAILURE. Complete statement follows:");
                System.err.println(bodyText);
                System.exit(3);
            } else if (state.equals("ARCHIVED")) {
                List<Entry> entries = statement.getEntries();
                System.out.println("SUCCESS. ");
                if (entries.size() == 1) {
                    System.out.print("Deposit has been archived at: <" + entries.get(0).getId() + ">. ");

                    List<String> dois = getDois(entries.get(0));
                    int numDois = dois.size();
                    switch (numDois) {
                    case 1:
                        System.out.print(" With DOI: [" + dois.get(0) + "]. ");
                        break;
                    case 0:
                        System.out.println("WARNING: No DOI found");
                        break;

                    default:
                        System.out.println("WARNING: More than one DOI found (" + numDois + "): ");
                        boolean first = true;
                        for (String doi : dois) {
                            if (first)
                                first = false;
                            else
                                System.out.print(", ");
                            System.out.print(doi + "");

                        }
                        System.out.println();
                        break;
                    }
                } else {
                    System.out.println(
                            "WARNING: Found (" + entries.size() + ") entry's; should be ONE and only ONE");
                }
                String stateText = states.get(0).getText();
                System.out.println("Dataset landing page will be located at: <" + stateText + ">.");
                System.out.println("Complete statement follows:");
                System.out.println(bodyText);
                return entries.get(0).getId().toURI();
            }
        }
    }
}