Search in sources :

Example 71 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project goci by EBISPOT.

the class SolrSearchController method dispatchDownloadSearch.

private void dispatchDownloadSearch(String searchString, OutputStream outputStream, boolean efo, String facet, boolean ancestry) throws IOException {
    getLog().trace(searchString);
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(searchString);
    if (System.getProperty("http.proxyHost") != null) {
        HttpHost proxy;
        if (System.getProperty("http.proxyPort") != null) {
            proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
        } else {
            proxy = new HttpHost(System.getProperty("http.proxyHost"));
        }
        httpGet.setConfig(RequestConfig.custom().setProxy(proxy).build());
    }
    String file = null;
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        getLog().debug("Received HTTP response: " + response.getStatusLine().toString());
        HttpEntity entity = response.getEntity();
        BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
        String output;
        while ((output = br.readLine()) != null) {
            JsonProcessingService jsonProcessor = new JsonProcessingService(output, efo, facet, ancestry);
            file = jsonProcessor.processJson();
        }
        EntityUtils.consume(entity);
    }
    if (file == null) {
        //TO DO throw exception here and add error handler
        file = "Some error occurred during your request. Please try again or contact the GWAS Catalog team for assistance";
    }
    PrintWriter outputWriter = new PrintWriter(outputStream);
    outputWriter.write(file);
    outputWriter.flush();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpHost(org.apache.http.HttpHost) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) JsonProcessingService(uk.ac.ebi.spot.goci.ui.service.JsonProcessingService) PrintWriter(java.io.PrintWriter)

Example 72 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project mastering-java by Kingminghuang.

the class HttpClientCrawler method download.

public static HttpEntity download(String url) throws IOException {
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
    CloseableHttpResponse response = client.execute(httpGet);
    return response.getEntity();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 73 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project mastering-java by Kingminghuang.

the class HttpClientCrawler method testStatusCode.

private static void testStatusCode() throws IOException {
    RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
    config(requestConfig);
    HttpGet httpGet = new HttpGet(EXAMPLE_URL);
    CloseableHttpResponse response = client.execute(httpGet);
    int statusCode = response.getStatusLine().getStatusCode();
    System.out.println(statusCode == HttpStatus.SC_OK);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 74 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project libresonic by Libresonic.

the class ProxyController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
    HttpGet method = new HttpGet(url);
    method.setConfig(requestConfig);
    InputStream in = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        try (CloseableHttpResponse resp = client.execute(method)) {
            int statusCode = resp.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                response.sendError(statusCode);
            } else {
                in = resp.getEntity().getContent();
                IOUtils.copy(in, response.getOutputStream());
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
    return null;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 75 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project libresonic by Libresonic.

the class PodcastService method downloadImage.

private void downloadImage(PodcastChannel channel) {
    InputStream in = null;
    OutputStream out = null;
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        String imageUrl = channel.getImageUrl();
        if (imageUrl == null) {
            return;
        }
        File dir = getChannelDirectory(channel);
        MediaFile channelMediaFile = mediaFileService.getMediaFile(dir);
        File existingCoverArt = mediaFileService.getCoverArt(channelMediaFile);
        boolean imageFileExists = existingCoverArt != null && mediaFileService.getMediaFile(existingCoverArt) == null;
        if (imageFileExists) {
            return;
        }
        HttpGet method = new HttpGet(imageUrl);
        try (CloseableHttpResponse response = client.execute(method)) {
            in = response.getEntity().getContent();
            out = new FileOutputStream(new File(dir, "cover." + getCoverArtSuffix(response)));
            IOUtils.copy(in, out);
            mediaFileService.refreshMediaFile(channelMediaFile);
        }
    } catch (Exception x) {
        LOG.warn("Failed to download cover art for podcast channel '" + channel.getTitle() + "': " + x, x);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}
Also used : MediaFile(org.libresonic.player.domain.MediaFile) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpGet(org.apache.http.client.methods.HttpGet) FileOutputStream(java.io.FileOutputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MediaFile(org.libresonic.player.domain.MediaFile) File(java.io.File)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)536 HttpGet (org.apache.http.client.methods.HttpGet)242 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)171 StringEntity (org.apache.http.entity.StringEntity)127 JsonNode (com.fasterxml.jackson.databind.JsonNode)125 Test (org.junit.Test)125 HttpPost (org.apache.http.client.methods.HttpPost)107 IOException (java.io.IOException)105 HttpEntity (org.apache.http.HttpEntity)103 Deployment (org.activiti.engine.test.Deployment)87 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)67 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)57 InputStream (java.io.InputStream)53 HttpPut (org.apache.http.client.methods.HttpPut)50 Task (org.activiti.engine.task.Task)41 URI (java.net.URI)36 StatusLine (org.apache.http.StatusLine)34 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)34 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)31 BufferedReader (java.io.BufferedReader)30