Search in sources :

Example 16 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project cuba by cuba-platform.

the class FileLoaderClientImpl method saveStreamWithServlet.

protected void saveStreamWithServlet(FileDescriptor fd, Supplier<InputStream> inputStreamSupplier, @Nullable StreamingProgressListener streamingListener) throws FileStorageException, InterruptedException {
    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
    }
    ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
    String fileUploadContext = clientConfig.getFileUploadContext();
    while (true) {
        String url = selectedUrl + fileUploadContext + "?s=" + userSessionSource.getUserSession().getId() + "&f=" + fd.toUrlParam();
        try (InputStream inputStream = inputStreamSupplier.get()) {
            InputStreamProgressEntity.UploadProgressListener progressListener = null;
            if (streamingListener != null) {
                progressListener = streamingListener::onStreamingProgressChanged;
            }
            HttpPost method = new HttpPost(url);
            method.setEntity(new InputStreamProgressEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM, progressListener));
            HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
            HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
            try {
                HttpResponse response = client.execute(method);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    break;
                } else {
                    log.debug("Unable to upload file to {}\n{}", url, response.getStatusLine());
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null) {
                        throw new FileStorageException(FileStorageException.Type.fromHttpStatus(statusCode), fd.getName());
                    }
                }
            } catch (InterruptedIOException e) {
                log.trace("Uploading has been interrupted");
                throw new InterruptedException("File uploading is interrupted");
            } catch (IOException e) {
                log.debug("Unable to upload file to {}\n{}", url, e);
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null) {
                    throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
                }
            } finally {
                connectionManager.shutdown();
            }
        } catch (IOException | RetryUnsupportedException e) {
            throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InterruptedIOException(java.io.InterruptedIOException) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamProgressEntity(com.haulmont.cuba.client.sys.fileupload.InputStreamProgressEntity) HttpClient(org.apache.http.client.HttpClient) ClientConfig(com.haulmont.cuba.client.ClientConfig) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 17 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project cuba by cuba-platform.

the class FileLoaderClientImpl method openStreamWithServlet.

protected InputStream openStreamWithServlet(FileDescriptor fd) throws FileStorageException {
    ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
    String fileDownloadContext = clientConfig.getFileDownloadContext();
    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
    }
    while (true) {
        String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&f=" + fd.getId().toString();
        HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int httpStatus = httpResponse.getStatusLine().getStatusCode();
            if (httpStatus == HttpStatus.SC_OK) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    return httpEntity.getContent();
                } else {
                    log.debug("Unable to download file from {}\nHttpEntity is null", url);
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null) {
                        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
                    }
                }
            } else {
                log.debug("Unable to download file from {}\n{}", url, httpResponse.getStatusLine());
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null) {
                    throw new FileStorageException(FileStorageException.Type.fromHttpStatus(httpStatus), fd.getName());
                }
            }
        } catch (InterruptedIOException e) {
            log.trace("Downloading has been interrupted");
            throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
        } catch (IOException ex) {
            log.debug("Unable to download file from {}\n{}", url, ex);
            selectedUrl = failAndGetNextUrl(context);
            if (selectedUrl == null) {
                throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), ex);
            }
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpClient(org.apache.http.client.HttpClient) ClientConfig(com.haulmont.cuba.client.ClientConfig) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 18 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project cuba by cuba-platform.

the class SimpleFileDataProvider method provide.

@Override
public InputStream provide() {
    if (filePath == null)
        throw new IllegalStateException("File path is null");
    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new RuntimeException(String.format("Unable to download file '%s': no available server URLs", filePath));
    }
    while (true) {
        String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&p=" + URLEncodeUtils.encodeUtf8(filePath);
        HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
        HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            int httpStatus = httpResponse.getStatusLine().getStatusCode();
            if (httpStatus == HttpStatus.SC_OK) {
                HttpEntity httpEntity = httpResponse.getEntity();
                if (httpEntity != null) {
                    inputStream = httpEntity.getContent();
                    break;
                } else {
                    log.debug("Unable to download file from " + url + "\nHttpEntity is null");
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null)
                        throw new RuntimeException(String.format("Unable to download file '%s': HttpEntity is null", filePath));
                }
            } else {
                log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null)
                    throw new RuntimeException(String.format("Unable to download file '%s': HTTP status is %d", filePath, httpStatus));
            }
        } catch (IOException ex) {
            log.debug("Unable to download file from " + url + "\n" + ex);
            selectedUrl = failAndGetNextUrl(context);
            if (selectedUrl == null)
                throw new RuntimeException(String.format("Unable to download file '%s'", filePath), ex);
        }
    }
    return inputStream;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 19 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project openremote by openremote.

the class ExtensibleResteasyClientBuilder method initDefaultEngine43.

// The rest is copy/paste pretty much
public static ApacheHttpClient43Engine initDefaultEngine43(ExtensibleResteasyClientBuilder that) {
    HttpClient httpClient = null;
    HostnameVerifier verifier = null;
    if (that.verifier != null) {
        verifier = new ExtensibleResteasyClientBuilder.VerifierWrapper(that.verifier);
    } else {
        switch(that.policy) {
            case ANY:
                verifier = new NoopHostnameVerifier();
                break;
            case WILDCARD:
                verifier = new DefaultHostnameVerifier();
                break;
            case STRICT:
                verifier = new DefaultHostnameVerifier();
                break;
        }
    }
    try {
        SSLConnectionSocketFactory sslsf = null;
        SSLContext theContext = that.sslContext;
        if (that.disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new NoopHostnameVerifier();
            sslsf = new SSLConnectionSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new SSLConnectionSocketFactory(theContext, verifier) {

                @Override
                protected void prepareSocket(SSLSocket socket) throws IOException {
                    that.prepareSocketForSni(socket);
                }
            };
        } else if (that.clientKeyStore != null || that.truststore != null) {
            SSLContext ctx = SSLContexts.custom().setProtocol(SSLConnectionSocketFactory.TLS).setSecureRandom(null).loadKeyMaterial(that.clientKeyStore, that.clientPrivateKeyPassword != null ? that.clientPrivateKeyPassword.toCharArray() : null).loadTrustMaterial(that.truststore, TrustSelfSignedStrategy.INSTANCE).build();
            sslsf = new SSLConnectionSocketFactory(ctx, verifier) {

                @Override
                protected void prepareSocket(SSLSocket socket) throws IOException {
                    that.prepareSocketForSni(socket);
                }
            };
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new SSLConnectionSocketFactory(tlsContext, verifier);
        }
        final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
        HttpClientConnectionManager cm = null;
        if (that.connectionPoolSize > 0) {
            PoolingHttpClientConnectionManager tcm = new PoolingHttpClientConnectionManager(registry, null, null, null, that.connectionTTL, that.connectionTTLUnit);
            tcm.setMaxTotal(that.connectionPoolSize);
            if (that.maxPooledPerRoute == 0) {
                that.maxPooledPerRoute = that.connectionPoolSize;
            }
            tcm.setDefaultMaxPerRoute(that.maxPooledPerRoute);
            cm = tcm;
        } else {
            cm = new BasicHttpClientConnectionManager(registry);
        }
        RequestConfig.Builder rcBuilder = RequestConfig.custom();
        if (that.socketTimeout > -1) {
            rcBuilder.setSocketTimeout((int) that.socketTimeoutUnits.toMillis(that.socketTimeout));
        }
        if (that.establishConnectionTimeout > -1) {
            rcBuilder.setConnectTimeout((int) that.establishConnectionTimeoutUnits.toMillis(that.establishConnectionTimeout));
        }
        if (that.connectionCheckoutTimeoutMs > -1) {
            rcBuilder.setConnectionRequestTimeout(that.connectionCheckoutTimeoutMs);
        }
        // The magic configure()
        httpClient = that.configure(HttpClientBuilder.create().setConnectionManager(cm).setDefaultRequestConfig(rcBuilder.build()).setProxy(that.defaultProxy).disableContentCompression()).build();
        ApacheHttpClient43Engine engine = (ApacheHttpClient43Engine) ApacheHttpClient4EngineFactory.create(httpClient, true);
        engine.setResponseBufferSize(that.responseBufferSize);
        engine.setHostnameVerifier(verifier);
        // this may be null.  We can't really support this with Apache Client.
        engine.setSslContext(theContext);
        return engine;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) PassthroughTrustManager(org.jboss.resteasy.client.jaxrs.engines.PassthroughTrustManager) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ApacheHttpClient43Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine) IOException(java.io.IOException) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 20 with HttpClientConnectionManager

use of org.apache.http.conn.HttpClientConnectionManager in project coprhd-controller by CoprHD.

the class SSLHelper method getHttpClientBuilder.

/**
 * Generate a {@link HttpClientBuilder} instance to be used to create a httpclient instance.
 * @param threadSafeClients boolean if true a thread-safe HTTP client instance is created.
 * @param maxConnections for thread-safe clients, the maximum concurrent connections
 * @param maxConnectionsPerHost for thread-safe clients, the maximum concurrent connections allows to a specific host
 * @param useConnectionTimeout socket connection timeout in milliseconds
 * @param useConnectionReadTimeout connection read timeout in milliseconds
 * @param sslContext {@link SSLContext} initialized SSLContext instance
 * @return {@link HttpClientBuilder} instance
 */
private static HttpClientBuilder getHttpClientBuilder(boolean threadSafeClients, int maxConnections, int maxConnectionsPerHost, int useConnectionTimeout, int useConnectionReadTimeout, SSLContext sslContext) {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(HTTPS, socketFactory).build();
    HttpClientConnectionManager cm = null;
    if (threadSafeClients) {
        cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        if (maxConnections > 0) {
            ((PoolingHttpClientConnectionManager) cm).setMaxTotal(maxConnections);
        }
        if (maxConnectionsPerHost > 0) {
            ((PoolingHttpClientConnectionManager) cm).setDefaultMaxPerRoute(maxConnectionsPerHost);
        }
    } else {
        cm = new BasicHttpClientConnectionManager(socketFactoryRegistry);
    }
    // Build the request config identifying the socket connection parameters.
    RequestConfig.Builder requestConfigBulder = RequestConfig.custom();
    if (useConnectionReadTimeout > 0) {
        requestConfigBulder.setSocketTimeout(useConnectionReadTimeout);
    }
    if (useConnectionTimeout > 0) {
        requestConfigBulder.setConnectTimeout(useConnectionTimeout);
    }
    RequestConfig requestConfig = requestConfigBulder.setExpectContinueEnabled(true).build();
    // construct a client instance with the connection manager embedded
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    httpClientBuilder.setConnectionManager(cm);
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    return httpClientBuilder;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

HttpClientConnectionManager (org.apache.http.conn.HttpClientConnectionManager)35 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)23 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)19 IOException (java.io.IOException)16 HttpResponse (org.apache.http.HttpResponse)14 HttpClient (org.apache.http.client.HttpClient)11 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)11 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 SSLContext (javax.net.ssl.SSLContext)8 RequestConfig (org.apache.http.client.config.RequestConfig)8 CertificateException (java.security.cert.CertificateException)7 HttpGet (org.apache.http.client.methods.HttpGet)6 HttpPost (org.apache.http.client.methods.HttpPost)6 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 KeyManagementException (java.security.KeyManagementException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)4 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)4