Search in sources :

Example 1 with RequestConfig

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

the class WelcomeController method checkForUpdates.

private void checkForUpdates() {
    checkForUpdatesStatus.setText(localization.getString("welcome.checkForUpdates.label.currentlyChecking"));
    checkForUpdatesIndicator.setVisible(true);
    asyncTaskService.asyncTaskOf(() -> {
        RequestConfig requestConfig = //
        RequestConfig.custom().setConnectTimeout(//
        5000).setConnectionRequestTimeout(//
        5000).setSocketTimeout(//
        5000).build();
        HttpClientBuilder httpClientBuilder = //
        HttpClients.custom().disableCookieManagement().setDefaultRequestConfig(//
        requestConfig).setUserAgent("Cryptomator VersionChecker/" + ApplicationVersion.orElse("SNAPSHOT"));
        LOG.debug("Checking for updates...");
        try (CloseableHttpClient client = httpClientBuilder.build()) {
            HttpGet request = new HttpGet("https://cryptomator.org/downloads/latestVersion.json");
            try (CloseableHttpResponse response = client.execute(request)) {
                if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) {
                    try (InputStream in = response.getEntity().getContent()) {
                        Gson gson = new GsonBuilder().setLenient().create();
                        Reader utf8Reader = new InputStreamReader(in, StandardCharsets.UTF_8);
                        Map<String, String> map = gson.fromJson(utf8Reader, new TypeToken<Map<String, String>>() {
                        }.getType());
                        if (map != null) {
                            this.compareVersions(map);
                        }
                    }
                }
            }
        }
    }).andFinally(() -> {
        checkForUpdatesStatus.setText("");
        checkForUpdatesIndicator.setVisible(false);
    }).run();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 2 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project elasticsearch-analysis-ik by medcl.

the class Dictionary method getRemoteWords.

/**
	 * 从远程服务器上下载自定义词条
	 */
private static List<String> getRemoteWords(String location) {
    List<String> buffer = new ArrayList<String>();
    RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build();
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response;
    BufferedReader in;
    HttpGet get = new HttpGet(location);
    get.setConfig(rc);
    try {
        response = httpclient.execute(get);
        if (response.getStatusLine().getStatusCode() == 200) {
            String charset = "UTF-8";
            // 获取编码,默认为utf-8
            if (response.getEntity().getContentType().getValue().contains("charset=")) {
                String contentType = response.getEntity().getContentType().getValue();
                charset = contentType.substring(contentType.lastIndexOf("=") + 1);
            }
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
            String line;
            while ((line = in.readLine()) != null) {
                buffer.add(line);
            }
            in.close();
            response.close();
            return buffer;
        }
        response.close();
    } catch (ClientProtocolException e) {
        logger.error("getRemoteWords {} error", e, location);
    } catch (IllegalStateException e) {
        logger.error("getRemoteWords {} error", e, location);
    } catch (IOException e) {
        logger.error("getRemoteWords {} error", e, location);
    }
    return buffer;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 3 with RequestConfig

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

the class ApacheConnector method getUriHttpRequest.

private HttpUriRequest getUriHttpRequest(final ClientRequest clientRequest) {
    final RequestConfig.Builder requestConfigBuilder = RequestConfig.copy(requestConfig);
    final int connectTimeout = clientRequest.resolveProperty(ClientProperties.CONNECT_TIMEOUT, -1);
    final int socketTimeout = clientRequest.resolveProperty(ClientProperties.READ_TIMEOUT, -1);
    if (connectTimeout >= 0) {
        requestConfigBuilder.setConnectTimeout(connectTimeout);
    }
    if (socketTimeout >= 0) {
        requestConfigBuilder.setSocketTimeout(socketTimeout);
    }
    final Boolean redirectsEnabled = clientRequest.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, requestConfig.isRedirectsEnabled());
    requestConfigBuilder.setRedirectsEnabled(redirectsEnabled);
    final Boolean bufferingEnabled = clientRequest.resolveProperty(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class) == RequestEntityProcessing.BUFFERED;
    final HttpEntity entity = getHttpEntity(clientRequest, bufferingEnabled);
    return RequestBuilder.create(clientRequest.getMethod()).setUri(clientRequest.getUri()).setConfig(requestConfigBuilder.build()).setEntity(entity).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) RequestEntityProcessing(org.glassfish.jersey.client.RequestEntityProcessing)

Example 4 with RequestConfig

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

the class DisableContentEncodingTest method testEnabledByRequestConfig.

@Test
public void testEnabledByRequestConfig() {
    ClientConfig cc = new ClientConfig(GZipEncoder.class);
    final RequestConfig requestConfig = RequestConfig.custom().setContentCompressionEnabled(true).build();
    cc.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig);
    cc.connectorProvider(new ApacheConnectorProvider());
    Client client = ClientBuilder.newClient(cc);
    WebTarget r = client.target(getBaseUri());
    String enc = r.request().get().readEntity(String.class);
    assertEquals("gzip,deflate", enc);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 5 with RequestConfig

use of org.apache.http.client.config.RequestConfig in project DataX by alibaba.

the class RetryUtilTest method testRetryAsync3.

//@Test
@Ignore
public void testRetryAsync3() throws Exception {
    final int TIME_OUT = 30000;
    ThreadPoolExecutor executor = RetryUtil.createThreadPoolExecutor();
    String res = RetryUtil.asyncExecuteWithRetry(new Callable<String>() {

        @Override
        public String call() throws Exception {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).setConnectionRequestTimeout(TIME_OUT).setStaleConnectionCheckEnabled(true).build();
            HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(10).setMaxConnPerRoute(10).setDefaultRequestConfig(requestConfig).build();
            HttpGet httpGet = new HttpGet();
            httpGet.setURI(new URI("http://0.0.0.0:8080/test"));
            httpClient.execute(httpGet);
            return OK;
        }
    }, 3, 1000L, false, 6000L, executor);
    Assert.assertEquals(res, OK);
//        Assert.assertEquals(RetryUtil.EXECUTOR.getActiveCount(), 0);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) URI(java.net.URI) ExpectedException(org.junit.rules.ExpectedException)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)331 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)142 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)96 HttpGet (org.apache.http.client.methods.HttpGet)93 IOException (java.io.IOException)75 HttpEntity (org.apache.http.HttpEntity)67 HttpPost (org.apache.http.client.methods.HttpPost)63 HttpResponse (org.apache.http.HttpResponse)59 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)53 URI (java.net.URI)45 StringEntity (org.apache.http.entity.StringEntity)42 Map (java.util.Map)41 Test (org.junit.Test)38 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)32 HttpHost (org.apache.http.HttpHost)31 HttpClient (org.apache.http.client.HttpClient)31 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)30 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)26 URISyntaxException (java.net.URISyntaxException)23 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)23