Search in sources :

Example 6 with HttpClientBuilder

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

use of org.apache.http.impl.client.HttpClientBuilder in project camel by apache.

the class Olingo2Component method createOlingo2App.

private Olingo2AppWrapper createOlingo2App(Olingo2Configuration configuration) {
    Object clientBuilder = configuration.getHttpAsyncClientBuilder();
    if (clientBuilder == null) {
        HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClientBuilder.create();
        // apply simple configuration properties
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        requestConfigBuilder.setConnectTimeout(configuration.getConnectTimeout());
        requestConfigBuilder.setSocketTimeout(configuration.getSocketTimeout());
        final HttpHost proxy = configuration.getProxy();
        if (proxy != null) {
            requestConfigBuilder.setProxy(proxy);
        }
        // set default request config
        asyncClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        SSLContextParameters sslContextParameters = configuration.getSslContextParameters();
        if (sslContextParameters == null) {
            // use defaults if not specified
            sslContextParameters = new SSLContextParameters();
        }
        try {
            asyncClientBuilder.setSSLContext(sslContextParameters.createSSLContext(getCamelContext()));
        } catch (GeneralSecurityException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        } catch (IOException e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    Olingo2AppImpl olingo2App;
    if (clientBuilder == null || clientBuilder instanceof HttpAsyncClientBuilder) {
        olingo2App = new Olingo2AppImpl(configuration.getServiceUri(), (HttpAsyncClientBuilder) clientBuilder);
    } else {
        olingo2App = new Olingo2AppImpl(configuration.getServiceUri(), (HttpClientBuilder) clientBuilder);
    }
    apiProxy = new Olingo2AppWrapper(olingo2App);
    apiProxy.getOlingo2App().setContentType(configuration.getContentType());
    apiProxy.getOlingo2App().setHttpHeaders(configuration.getHttpHeaders());
    return apiProxy;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) Olingo2AppImpl(org.apache.camel.component.olingo2.api.impl.Olingo2AppImpl) HttpHost(org.apache.http.HttpHost) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) SSLContextParameters(org.apache.camel.util.jsse.SSLContextParameters)

Example 8 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project google-analytics-java by brsanthu.

the class GoogleAnalyticsThreadFactory method createHttpClient.

protected CloseableHttpClient createHttpClient(GoogleAnalyticsConfig config) {
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connManager);
    if (isNotEmpty(config.getUserAgent())) {
        builder.setUserAgent(config.getUserAgent());
    }
    if (isNotEmpty(config.getProxyHost())) {
        builder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort()));
        if (isNotEmpty(config.getProxyUserName())) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
    }
    return builder.build();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 9 with HttpClientBuilder

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

the class HttpClientWrapper method getClient.

public CloseableHttpClient getClient() {
    if (m_httpClient == null) {
        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        if (!m_reuseConnections) {
            httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
        }
        if (m_usePreemptiveAuth) {
            enablePreemptiveAuth(httpClientBuilder);
        }
        if (m_useSystemProxySettings) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        }
        if (!isEmpty(m_cookieSpec)) {
            requestConfigBuilder.setCookieSpec(m_cookieSpec);
        }
        if (m_cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(m_cookieStore);
        }
        if (m_username != null) {
            setCredentials(httpClientBuilder, m_username, m_password);
        }
        if (m_socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(m_socketTimeout);
        }
        if (m_connectionTimeout != null) {
            requestConfigBuilder.setConnectTimeout(m_connectionTimeout);
        }
        if (m_retries != null) {
            httpClientBuilder.setRetryHandler(new HttpRequestRetryOnExceptionHandler(m_retries, false));
        }
        if (m_sslContext.size() != 0) {
            configureSSLContext(httpClientBuilder);
        }
        for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        if (m_useLaxRedirect) {
            httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
        }
        httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        m_httpClient = httpClientBuilder.build();
    }
    return m_httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 10 with HttpClientBuilder

use of org.apache.http.impl.client.HttpClientBuilder in project Xponents by OpenSextant.

the class SharepointClient method getClient.

/**
     * Sharepoint requires NTLM. This client requires a non-null user/passwd/domain.
     *
     */
@Override
public HttpClient getClient() {
    if (currentConn != null) {
        return currentConn;
    }
    HttpClientBuilder clientHelper = HttpClientBuilder.create();
    if (proxyHost != null) {
        clientHelper.setProxy(proxyHost);
    }
    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();
    CredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(AuthScope.ANY, new NTCredentials(user, passwd, server, domain));
    clientHelper.setDefaultCredentialsProvider(creds);
    HttpClient httpClient = clientHelper.setDefaultRequestConfig(globalConfig).build();
    return httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) NTCredentials(org.apache.http.auth.NTCredentials)

Aggregations

HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 RequestConfig (org.apache.http.client.config.RequestConfig)17 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)12 HttpHost (org.apache.http.HttpHost)10 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)10 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)10 SSLContext (javax.net.ssl.SSLContext)9 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)9 CredentialsProvider (org.apache.http.client.CredentialsProvider)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 HttpClient (org.apache.http.client.HttpClient)8 IOException (java.io.IOException)7 HttpResponse (org.apache.http.HttpResponse)7 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)7 AuthScope (org.apache.http.auth.AuthScope)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpPost (org.apache.http.client.methods.HttpPost)5 URI (java.net.URI)4 HttpEntity (org.apache.http.HttpEntity)4