Search in sources :

Example 31 with HttpEntity

use of org.apache.http.HttpEntity in project spark by perwendel.

the class SparkTestUtil method doMethod.

public UrlResponse doMethod(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) throws IOException {
    HttpUriRequest httpRequest = getHttpRequest(requestMethod, path, body, secureConnection, acceptType, reqHeaders);
    HttpResponse httpResponse = httpClient.execute(httpRequest);
    UrlResponse urlResponse = new UrlResponse();
    urlResponse.status = httpResponse.getStatusLine().getStatusCode();
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        urlResponse.body = EntityUtils.toString(entity);
    } else {
        urlResponse.body = "";
    }
    Map<String, String> headers = new HashMap<>();
    Header[] allHeaders = httpResponse.getAllHeaders();
    for (Header header : allHeaders) {
        headers.put(header.getName(), header.getValue());
    }
    urlResponse.headers = headers;
    return urlResponse;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse)

Example 32 with HttpEntity

use of org.apache.http.HttpEntity 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 33 with HttpEntity

use of org.apache.http.HttpEntity 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 34 with HttpEntity

use of org.apache.http.HttpEntity in project tdi-studio-se by Talend.

the class paloconnection method logout.

// Logouts the current palo User
public void logout() throws paloexception {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("sid", this.strToken));
    try {
        HttpEntity entity = sendToServer(qparams, "/server/logout");
        CSVReader csv = new CSVReader(entity.getContent(), ';', "UTF-8");
        // CsvReader csv = new CsvReader(sendToServer(qparams, "/server/logout").getContent(),
        // Charset.defaultCharset());
        csv.setQuoteChar('"');
        csv.readNext();
        csv.close();
        entity.consumeContent();
    // paloHttpClient.getConnectionManager().shutdown();
    } catch (Exception e) {
        throw new paloexception(e.getMessage());
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) CSVReader(com.talend.csv.CSVReader) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Example 35 with HttpEntity

use of org.apache.http.HttpEntity in project tdi-studio-se by Talend.

the class paloconnection method getRulefunctions.

public String getRulefunctions() throws paloexception {
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("sid", getPaloToken()));
    try {
        StringBuilder sb = new StringBuilder();
        HttpEntity entity = sendToServer(qparams, "/rule/functions");
        CSVReader csv = new CSVReader(entity.getContent(), ';', "UTF-8");
        csv.setQuoteChar('"');
        while (csv.readNext()) {
            sb.append(csv.get(0));
        }
        csv.close();
        entity.consumeContent();
        return sb.toString();
    } catch (Exception e) {
        throw new paloexception(e.getMessage());
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) CSVReader(com.talend.csv.CSVReader) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList)

Aggregations

HttpEntity (org.apache.http.HttpEntity)1391 HttpResponse (org.apache.http.HttpResponse)509 IOException (java.io.IOException)483 HttpGet (org.apache.http.client.methods.HttpGet)391 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)315 HttpPost (org.apache.http.client.methods.HttpPost)305 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)284 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)248 HashMap (java.util.HashMap)177 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)164 InputStream (java.io.InputStream)163 URI (java.net.URI)153 StatusLine (org.apache.http.StatusLine)149 StringEntity (org.apache.http.entity.StringEntity)148 Header (org.apache.http.Header)136 HttpClient (org.apache.http.client.HttpClient)127 VolleyError (com.android.volley.VolleyError)120 ApiException (io.swagger.client.ApiException)120 Pair (io.swagger.client.Pair)120