Search in sources :

Example 46 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient 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 47 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project opennms by OpenNMS.

the class MetadataUtils method fetchGeodata.

public static Map<String, String> fetchGeodata() {
    final Map<String, String> ret = new HashMap<>();
    final String url = "http://freegeoip.net/xml/";
    final CloseableHttpClient httpclient = HttpClients.createDefault();
    final HttpGet get = new HttpGet(url);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(get);
        final HttpEntity entity = response.getEntity();
        final String xml = EntityUtils.toString(entity);
        System.err.println("xml = " + xml);
        final GeodataResponse geoResponse = JaxbUtils.unmarshal(GeodataResponse.class, xml);
        ret.put("external-ip-address", InetAddressUtils.str(geoResponse.getIp()));
        ret.put("country-code", geoResponse.getCountryCode());
        ret.put("region-code", geoResponse.getRegionCode());
        ret.put("city", geoResponse.getCity());
        ret.put("zip-code", geoResponse.getZipCode());
        ret.put("time-zone", geoResponse.getTimeZone());
        ret.put("latitude", geoResponse.getLatitude() == null ? null : geoResponse.getLatitude().toString());
        ret.put("longitude", geoResponse.getLongitude() == null ? null : geoResponse.getLongitude().toString());
        EntityUtils.consumeQuietly(entity);
    } catch (final Exception e) {
        LOG.debug("Failed to get GeoIP data from " + url, e);
    } finally {
        IOUtils.closeQuietly(response);
    }
    return ret;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) GeodataResponse(org.opennms.netmgt.poller.remote.support.GeodataResponse) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 48 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project api-snippets by TwilioDevEd.

the class Example method main.

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("lookups.twilio.com", 80), new UsernamePasswordCredentials("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "your_auth_token"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {
        HttpGet httpget = new HttpGet("https://lookups.twilio.com/v1/PhoneNumbers/+16502530000/?AddOns=payfone_tcpa_compliance&AddOns.payfone_tcpa_compliance.RightPartyContactedDate=20160101");
        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 49 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project voltdb by VoltDB.

the class HTTPBenchmark method runBenchmark.

/**
     * Core benchmark code. Connect. Initialize. Run the loop. Cleanup. Print Results.
     *
     * @throws Exception if anything unexpected happens.
     */
public void runBenchmark() throws Exception {
    System.out.print(HORIZONTAL_RULE);
    System.out.println(" Setup & Initialization");
    System.out.println(HORIZONTAL_RULE);
    // connect to one or more servers, loop until success
    connect(config.servers);
    // preload keys if requested
    System.out.println();
    if (config.preload) {
        System.out.println("Preloading data store...");
        for (int i = 0; i < config.poolsize; i++) {
            client.callProcedure(new NullCallback(), "Put", String.format(processor.KeyFormat, i), processor.generateForStore().getStoreValue());
        }
        client.drain();
        System.out.println("Preloading complete.\n");
    }
    System.out.print(HORIZONTAL_RULE);
    System.out.println(" Starting Benchmark");
    System.out.println(HORIZONTAL_RULE);
    // setup the HTTP connection pool that will be used by the threads
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(config.threads * 2);
    cm.setDefaultMaxPerRoute(config.threads);
    CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
    String[] servers = config.servers.split(",");
    // create/start the requested number of threads
    Thread[] kvThreads = new Thread[config.threads];
    for (int i = 0; i < config.threads; ++i) {
        HttpPost httppost = new HttpPost("http://" + servers[i % servers.length] + ":8080/api/1.0/");
        kvThreads[i] = new Thread(new KVThread(httpclient, httppost));
        kvThreads[i].start();
    }
    // Run the benchmark loop for the requested warmup time
    System.out.println("Warming up...");
    Thread.sleep(1000l * config.warmup);
    // signal to threads to end the warmup phase
    warmupComplete.set(true);
    // Run the benchmark loop for the requested warmup time
    System.out.println("\nRunning benchmark...");
    Thread.sleep(1000l * config.duration);
    // stop the threads
    benchmarkComplete.set(true);
    // cancel periodic stats printing
    // timer.cancel();
    // block until all outstanding txns return
    client.drain();
    // join on the threads
    for (Thread t : kvThreads) {
        t.join();
    }
    // print the summary results
    printResults();
    // close down the client connections
    client.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) NullCallback(org.voltdb.client.NullCallback) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 50 with CloseableHttpClient

use of org.apache.http.impl.client.CloseableHttpClient in project selenium-tests by Wikia.

the class GraphApi method deleteTestUser.

private HttpResponse deleteTestUser(String userId) throws IOException, URISyntaxException {
    URL url = new URL(getURLdeleteUser(userId));
    CloseableHttpClient httpClient = HttpClientBuilder.create().disableAutomaticRetries().build();
    HttpDelete httpDelete = getHttpDelete(url);
    return httpClient.execute(httpDelete);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpDelete(org.apache.http.client.methods.HttpDelete) URL(java.net.URL)

Aggregations

CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)430 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)238 HttpGet (org.apache.http.client.methods.HttpGet)212 Test (org.junit.Test)206 HttpResponse (org.apache.http.HttpResponse)108 HttpEntity (org.apache.http.HttpEntity)92 IOException (java.io.IOException)90 HttpPost (org.apache.http.client.methods.HttpPost)85 StringEntity (org.apache.http.entity.StringEntity)67 InputStream (java.io.InputStream)57 StatusLine (org.apache.http.StatusLine)41 HttpHost (org.apache.http.HttpHost)36 URI (java.net.URI)35 RequestConfig (org.apache.http.client.config.RequestConfig)32 Header (org.apache.http.Header)24 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)24 File (java.io.File)22 HttpPut (org.apache.http.client.methods.HttpPut)22 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19