Search in sources :

Example 86 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testCompression.

@Test
public void testCompression() throws Exception {
    SolrQuery q = new SolrQuery("*:*");
    final String clientUrl = jetty.getBaseUrl().toString() + "/debug/foo";
    try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
        // verify request header gets set
        DebugServlet.clear();
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNull(DebugServlet.headers.toString(), DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, true)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
        assertNotNull(DebugServlet.headers.get("Accept-Encoding"));
    }
    try (HttpSolrClient client = getHttpSolrClient(clientUrl, null, null, false)) {
        try {
            client.query(q);
        } catch (ParseException ignored) {
        }
    }
    assertNull(DebugServlet.headers.get("Accept-Encoding"));
    // verify server compresses output
    HttpGet get = new HttpGet(jetty.getBaseUrl().toString() + "/collection1" + "/select?q=foo&wt=xml");
    get.setHeader("Accept-Encoding", "gzip");
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION, true);
    RequestConfig config = RequestConfig.custom().setDecompressionEnabled(false).build();
    get.setConfig(config);
    CloseableHttpClient httpclient = HttpClientUtil.createClient(params);
    HttpEntity entity = null;
    try {
        HttpResponse response = httpclient.execute(get, HttpClientUtil.createNewHttpClientRequestContext());
        entity = response.getEntity();
        Header ceheader = entity.getContentEncoding();
        assertNotNull(Arrays.asList(response.getAllHeaders()).toString(), ceheader);
        assertEquals("gzip", ceheader.getValue());
    } finally {
        if (entity != null) {
            entity.getContent().close();
        }
        HttpClientUtil.close(httpclient);
    }
    // verify compressed response can be handled
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/collection1")) {
        q = new SolrQuery("foo");
        QueryResponse response = client.query(q);
        assertEquals(0, response.getStatus());
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) ParseException(org.apache.http.ParseException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 87 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project algoliasearch-client-java by algolia.

the class APIClient method _requestByHost.

private JSONObject _requestByHost(HttpRequestBase req, String host, String url, String json, List<AlgoliaInnerException> errors, boolean searchTimeout) throws AlgoliaException {
    req.reset();
    // set URL
    try {
        req.setURI(new URI("https://" + host + url));
    } catch (URISyntaxException e) {
        // never reached
        throw new IllegalStateException(e);
    }
    // set auth headers
    req.setHeader("Accept-Encoding", "gzip");
    req.setHeader("X-Algolia-Application-Id", this.applicationID);
    if (forwardAdminAPIKey == null) {
        req.setHeader("X-Algolia-API-Key", this.apiKey);
    } else {
        req.setHeader("X-Algolia-API-Key", this.forwardAdminAPIKey);
        req.setHeader("X-Forwarded-For", this.forwardEndUserIP);
        req.setHeader("X-Forwarded-API-Key", this.forwardRateLimitAPIKey);
    }
    for (Entry<String, String> entry : headers.entrySet()) {
        req.setHeader(entry.getKey(), entry.getValue());
    }
    // set user agent
    req.setHeader("User-Agent", userAgent);
    // set JSON entity
    if (json != null) {
        if (!(req instanceof HttpEntityEnclosingRequestBase)) {
            throw new IllegalArgumentException("Method " + req.getMethod() + " cannot enclose entity");
        }
        req.setHeader("Content-type", "application/json");
        try {
            StringEntity se = new StringEntity(json, "UTF-8");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            ((HttpEntityEnclosingRequestBase) req).setEntity(se);
        } catch (Exception e) {
            // $COVERAGE-IGNORE$
            throw new AlgoliaException("Invalid JSON Object: " + json);
        }
    }
    RequestConfig config = RequestConfig.custom().setSocketTimeout(searchTimeout ? httpSearchTimeoutMS : httpSocketTimeoutMS).setConnectTimeout(httpConnectTimeoutMS).setConnectionRequestTimeout(httpConnectTimeoutMS).build();
    req.setConfig(config);
    HttpResponse response;
    try {
        response = httpClient.execute(req);
    } catch (IOException e) {
        // on error continue on the next host
        if (verbose) {
            System.out.println(String.format("%s: %s=%s", host, e.getClass().getName(), e.getMessage()));
        }
        errors.add(new AlgoliaInnerException(host, e));
        return null;
    }
    try {
        int code = response.getStatusLine().getStatusCode();
        if (code / 100 == 4) {
            String message = "";
            try {
                message = EntityUtils.toString(response.getEntity());
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (code == 400) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Bad request");
            } else if (code == 403) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Invalid Application-ID or API-Key");
            } else if (code == 404) {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Resource does not exist");
            } else {
                throw new AlgoliaException(code, message.length() > 0 ? message : "Error");
            }
        }
        if (code / 100 != 2) {
            try {
                if (verbose) {
                    System.out.println(String.format("%s: %s", host, EntityUtils.toString(response.getEntity())));
                }
                errors.add(new AlgoliaInnerException(host, EntityUtils.toString(response.getEntity())));
            } catch (IOException e) {
                if (verbose) {
                    System.out.println(String.format("%s: %s", host, String.valueOf(code)));
                }
                errors.add(new AlgoliaInnerException(host, e));
            }
            // KO, continue
            return null;
        }
        try {
            InputStream istream = response.getEntity().getContent();
            String encoding = response.getEntity().getContentEncoding() != null ? response.getEntity().getContentEncoding().getValue() : null;
            if (encoding != null && encoding.contains("gzip")) {
                istream = new GZIPInputStream(istream);
            }
            InputStreamReader is = new InputStreamReader(istream, "UTF-8");
            StringBuilder jsonRaw = new StringBuilder();
            char[] buffer = new char[4096];
            int read;
            while ((read = is.read(buffer)) > 0) {
                jsonRaw.append(buffer, 0, read);
            }
            is.close();
            return new JSONObject(jsonRaw.toString());
        } catch (IOException e) {
            if (verbose) {
                System.out.println(String.format("%s: %s=%s", host, e.getClass().getName(), e.getMessage()));
            }
            errors.add(new AlgoliaInnerException(host, e));
            return null;
        } catch (JSONException e) {
            throw new AlgoliaException("JSON decode error:" + e.getMessage());
        }
    } finally {
        req.releaseConnection();
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) GZIPInputStream(java.util.zip.GZIPInputStream) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ParseException(org.apache.http.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) GZIPInputStream(java.util.zip.GZIPInputStream) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) ParseException(org.apache.http.ParseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 88 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project local-data-aragopedia by aragonopendata.

the class Utils method processURLGetApache.

public static void processURLGetApache() {
    CookieStore cookieStore = new BasicCookieStore();
    RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setExpectContinueEnabled(true).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)).setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    try {
        HttpGet httpget = new HttpGet("http://bi.aragon.es/analytics/saw.dll?Go&path=/shared/IAEST-PUBLICA/Estadistica%20Local/03/030018TP&Action=Download&Options=df&NQUser=granpublico&NQPassword=granpublico");
        httpget.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
        httpget.addHeader("Cookie", "sawU=granpublico; ORA_BIPS_LBINFO=153c4c924b8; ORA_BIPS_NQID=k8vgekohfuquhdg71on5hjvqbcorcupbmh4h3lu25iepaq5izOr07UFe9WiFvM3; __utma=263932892.849551431.1443517596.1457200753.1458759706.17; __utmc=263932892; __utmz=263932892.1456825145.15.6.utmcsr=alzir.dia.fi.upm.es|utmccn=(referral)|utmcmd=referral|utmcct=/kos/iaest/clase-vivienda-agregado");
        httpget.addHeader("content-type", "text/csv; charset=utf-8");
        RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).setConnectTimeout(5000).setConnectionRequestTimeout(5000).setCookieSpec(CookieSpecs.DEFAULT).build();
        httpget.setConfig(requestConfig);
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        System.out.println("executing request " + httpget.getURI());
        CloseableHttpResponse response = httpclient.execute(httpget, context);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("----------------------------------------");
            context.getRequest();
            context.getHttpRoute();
            context.getTargetAuthState();
            context.getTargetAuthState();
            context.getCookieOrigin();
            context.getCookieSpec();
            context.getUserToken();
        } finally {
            response.close();
        }
        response = httpclient.execute(httpget, context);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
            System.out.println("----------------------------------------");
            context.getRequest();
            context.getHttpRoute();
            context.getTargetAuthState();
            context.getTargetAuthState();
            context.getCookieOrigin();
            context.getCookieSpec();
            context.getUserToken();
        } finally {
            response.close();
        }
        httpclient.close();
    } catch (ClientProtocolException e) {
        log.error("Error en el método processURLGetApache", e);
    } catch (IOException e) {
        log.error("Error en el método processURLGetApache", e);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 89 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project bayou by capergroup.

the class ApiSynthesizerRemoteTensorFlowAsts method sendGenerateAstRequest.

private JSONObject sendGenerateAstRequest(String evidence) throws IOException {
    _logger.debug("entering");
    /*
         * Check parameters.
         */
    if (evidence == null) {
        _logger.debug("exiting");
        throw new NullPointerException("evidence");
    }
    String astServerHttpResponseBody;
    {
        RequestConfig config = RequestConfig.custom().setConnectTimeout(_maxNetworkWaitTimeMs.AsInt).setSocketTimeout(_maxNetworkWaitTimeMs.AsInt).build();
        try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
            JSONObject requestObj = new JSONObject();
            requestObj.put("request type", "generate asts");
            requestObj.put("evidence", evidence);
            HttpPost httppost = new HttpPost("http://" + _tensorFlowHost + ":" + _tensorFlowPort);
            httppost.setEntity(new StringEntity(requestObj.toString(2)));
            HttpResponse response = client.execute(httppost);
            int responseStatusCode = response.getStatusLine().getStatusCode();
            if (responseStatusCode != 200)
                throw new IOException("Unexpected http response code: " + responseStatusCode);
            HttpEntity entity = response.getEntity();
            if (entity == null)
                throw new IOException("Expected response body.");
            astServerHttpResponseBody = IOUtils.toString(entity.getContent(), "UTF-8");
        }
    }
    JSONObject responseObject = new JSONObject(astServerHttpResponseBody);
    _logger.debug("exiting");
    return responseObject;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.JSONObject) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) ContentString(edu.rice.cs.caper.programming.ContentString)

Example 90 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project scheduling by ow2-proactive.

the class HttpClientBuilderTest method testSetDefaultRequestConfig.

@Test
public void testSetDefaultRequestConfig() throws Exception {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(42).build();
    httpClientBuilder.setDefaultRequestConfig(config);
    httpClientBuilder.build();
    Mockito.verify(internalHttpClientBuilder).build();
    Mockito.verify(internalHttpClientBuilder).setDefaultRequestConfig(config);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)91 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)33 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)31 HttpGet (org.apache.http.client.methods.HttpGet)29 HttpPost (org.apache.http.client.methods.HttpPost)19 Test (org.junit.Test)16 IOException (java.io.IOException)14 StringEntity (org.apache.http.entity.StringEntity)14 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 HttpResponse (org.apache.http.HttpResponse)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 HttpEntity (org.apache.http.HttpEntity)11 URI (java.net.URI)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 InputStream (java.io.InputStream)9 HttpHost (org.apache.http.HttpHost)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 Header (org.apache.http.Header)6