Search in sources :

Example 61 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project jmeter by apache.

the class HTTPHC4Impl method sample.

@Override
protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) {
    if (log.isDebugEnabled()) {
        log.debug("Start : sample {} method {} followingRedirect {} depth {}", url, method, areFollowingRedirect, frameDepth);
    }
    JMeterVariables jMeterVariables = JMeterContextService.getContext().getVariables();
    HTTPSampleResult res = createSampleResult(url, method);
    CloseableHttpClient httpClient = null;
    HttpRequestBase httpRequest = null;
    HttpContext localContext = new BasicHttpContext();
    HttpClientContext clientContext = HttpClientContext.adapt(localContext);
    clientContext.setAttribute(CONTEXT_ATTRIBUTE_AUTH_MANAGER, getAuthManager());
    HttpClientKey key = createHttpClientKey(url);
    MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> triple;
    try {
        triple = setupClient(key, jMeterVariables, clientContext);
        httpClient = triple.getLeft();
        URI uri = url.toURI();
        httpRequest = createHttpRequest(uri, method, areFollowingRedirect);
        // can throw IOException
        setupRequest(url, httpRequest, res);
    } catch (Exception e) {
        res.sampleStart();
        res.sampleEnd();
        errorResult(e, res);
        return res;
    }
    setupClientContextBeforeSample(jMeterVariables, localContext);
    res.sampleStart();
    final CacheManager cacheManager = getCacheManager();
    if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method) && cacheManager.inCache(url, httpRequest.getAllHeaders())) {
        return updateSampleResultForResourceInCache(res);
    }
    CloseableHttpResponse httpResponse = null;
    try {
        currentRequest = httpRequest;
        handleMethod(method, res, httpRequest, localContext);
        // store the SampleResult in LocalContext to compute connect time
        localContext.setAttribute(CONTEXT_ATTRIBUTE_SAMPLER_RESULT, res);
        // perform the sample
        httpResponse = executeRequest(httpClient, httpRequest, localContext, url);
        saveProxyAuth(triple, localContext);
        if (log.isDebugEnabled()) {
            log.debug("Headers in request before:{}", Arrays.asList(httpRequest.getAllHeaders()));
        }
        // Needs to be done after execute to pick up all the headers
        final HttpRequest request = (HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
        if (log.isDebugEnabled()) {
            log.debug("Headers in request after:{}, in localContext#request:{}", Arrays.asList(httpRequest.getAllHeaders()), Arrays.asList(request.getAllHeaders()));
        }
        extractClientContextAfterSample(jMeterVariables, localContext);
        // We've finished with the request, so we can add the LocalAddress to it for display
        if (localAddress != null) {
            request.addHeader(HEADER_LOCAL_ADDRESS, localAddress.toString());
        }
        res.setRequestHeaders(getAllHeadersExceptCookie(request));
        Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
        if (contentType != null) {
            String ct = contentType.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            res.setResponseData(readResponse(res, entity.getContent(), entity.getContentLength()));
        }
        // Done with the sampling proper.
        res.sampleEnd();
        currentRequest = null;
        // Now collect the results into the HTTPSampleResult:
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        res.setResponseCode(Integer.toString(statusCode));
        res.setResponseMessage(statusLine.getReasonPhrase());
        res.setSuccessful(isSuccessCode(statusCode));
        res.setResponseHeaders(getResponseHeaders(httpResponse));
        if (res.isRedirect()) {
            final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
            if (headerLocation == null) {
                // HTTP protocol violation, but avoids NPE
                throw new IllegalArgumentException("Missing location header in redirect for " + httpRequest.getRequestLine());
            }
            String redirectLocation = headerLocation.getValue();
            res.setRedirectLocation(redirectLocation);
        }
        // record some sizes to allow HTTPSampleResult.getBytes() with different options
        long headerBytes = // condensed length (without \r)
        (long) res.getResponseHeaders().length() + // Add \r for each header
        (long) httpResponse.getAllHeaders().length + // Add \r for initial header
        1L + // final \r\n before data
        2L;
        HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_ATTRIBUTE_METRICS);
        long totalBytes = metrics.getReceivedBytesCount();
        res.setHeadersSize((int) headerBytes);
        res.setBodySize(totalBytes - headerBytes);
        res.setSentBytes((Long) localContext.getAttribute(CONTEXT_ATTRIBUTE_SENT_BYTES));
        if (log.isDebugEnabled()) {
            long total = res.getHeadersSize() + res.getBodySizeAsLong();
            log.debug("ResponseHeadersSize={} Content-Length={} Total={}", res.getHeadersSize(), res.getBodySizeAsLong(), total);
        }
        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
            HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST);
            HttpHost target = (HttpHost) localContext.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
            URI redirectURI = req.getURI();
            if (redirectURI.isAbsolute()) {
                res.setURL(redirectURI.toURL());
            } else {
                res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
            }
        }
        // Store any cookies received in the cookie manager:
        saveConnectionCookies(httpResponse, res.getURL(), getCookieManager());
        // Save cache information
        if (cacheManager != null) {
            cacheManager.saveDetails(httpResponse, res);
        }
        // Follow redirects and download page resources if appropriate:
        res = resultProcessing(areFollowingRedirect, frameDepth, res);
        if (!isSuccessCode(statusCode)) {
            EntityUtils.consumeQuietly(httpResponse.getEntity());
        }
    } catch (IOException e) {
        log.debug("IOException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        // pick up headers if failed to execute the request
        if (res.getRequestHeaders() != null) {
            log.debug("Overwriting request old headers: {}", res.getRequestHeaders());
        }
        res.setRequestHeaders(getAllHeadersExceptCookie((HttpRequest) localContext.getAttribute(HttpCoreContext.HTTP_REQUEST)));
        errorResult(e, res);
        return res;
    } catch (RuntimeException e) {
        log.debug("RuntimeException", e);
        if (res.getEndTime() == 0) {
            res.sampleEnd();
        }
        errorResult(e, res);
        return res;
    } finally {
        JOrphanUtils.closeQuietly(httpResponse);
        currentRequest = null;
        JMeterContextService.getContext().getSamplerContext().remove(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE);
    }
    return res;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) URI(java.net.URI) URL(java.net.URL) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CacheManager(org.apache.jmeter.protocol.http.control.CacheManager) HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.http.HttpException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) AuthState(org.apache.http.auth.AuthState)

Example 62 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project cloudstack by apache.

the class HttpClientHelper method createHttpClient.

public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    return HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)).setRedirectStrategy(new LaxRedirectStrategy()).setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build()).setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler)

Example 63 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project c4sg-services by Code4SocialGood.

the class SlackUtils method createHttpClient.

public static CloseableHttpClient createHttpClient(int timeout) {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
    return httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 64 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project dubbo by alibaba.

the class RestProtocol method doRefer.

@Override
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
    // TODO more configs to add
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    // 20 is the default maxTotal of current PoolingClientConnectionManager
    connectionManager.setMaxTotal(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL));
    connectionManager.setDefaultMaxPerRoute(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE));
    if (connectionMonitor == null) {
        connectionMonitor = new ConnectionMonitor();
        connectionMonitor.start();
    }
    connectionMonitor.addConnectionManager(connectionManager);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(url.getParameter(CONNECT_TIMEOUT_KEY, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT)).build();
    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
    CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setKeepAliveStrategy((response, context) -> {
        HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
        while (it.hasNext()) {
            HeaderElement he = it.nextElement();
            String param = he.getName();
            String value = he.getValue();
            if (value != null && param.equalsIgnoreCase(TIMEOUT_KEY)) {
                return Long.parseLong(value) * 1000;
            }
        }
        return HTTPCLIENT_KEEPALIVEDURATION;
    }).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build();
    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
    ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
    clients.add(client);
    client.register(RpcContextFilter.class);
    for (String clazz : COMMA_SPLIT_PATTERN.split(url.getParameter(EXTENSION_KEY, ""))) {
        if (!StringUtils.isEmpty(clazz)) {
            try {
                client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
            } catch (ClassNotFoundException e) {
                throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
            }
        }
    }
    // TODO protocol
    ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
    return target.proxy(serviceType);
}
Also used : TIMEOUT_KEY(org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY) DEFAULT_CONNECT_TIMEOUT(org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT) AbstractProxyProtocol(org.apache.dubbo.rpc.protocol.AbstractProxyProtocol) INTERFACE_KEY(org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) SERVER_KEY(org.apache.dubbo.remoting.Constants.SERVER_KEY) SocketConfig(org.apache.http.config.SocketConfig) RequestConfig(org.apache.http.client.config.RequestConfig) COMMA_SPLIT_PATTERN(org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN) BootstrapListener(org.apache.dubbo.remoting.http.servlet.BootstrapListener) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) StringUtils(org.apache.dubbo.common.utils.StringUtils) URL(org.apache.dubbo.common.URL) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) GetRestful(org.jboss.resteasy.util.GetRestful) LinkedList(java.util.LinkedList) HttpBinder(org.apache.dubbo.remoting.http.HttpBinder) HTTP(org.apache.http.protocol.HTTP) HeaderElementIterator(org.apache.http.HeaderElementIterator) DEFAULT_TIMEOUT(org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) EXTENSION_KEY(org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY) ApplicationModel(org.apache.dubbo.rpc.model.ApplicationModel) CONNECTIONS_KEY(org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY) RpcException(org.apache.dubbo.rpc.RpcException) HeaderElement(org.apache.http.HeaderElement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CONNECT_TIMEOUT_KEY(org.apache.dubbo.remoting.Constants.CONNECT_TIMEOUT_KEY) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ServletManager(org.apache.dubbo.remoting.http.servlet.ServletManager) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ServletContext(javax.servlet.ServletContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Collections(java.util.Collections) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) SocketConfig(org.apache.http.config.SocketConfig) HeaderElement(org.apache.http.HeaderElement) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) RpcException(org.apache.dubbo.rpc.RpcException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 65 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project cas by apereo.

the class SimpleHttpClientFactoryBean method buildHttpClient.

/**
 * Build a HTTP client based on the current properties.
 *
 * @return the built HTTP client
 */
@SneakyThrows
@SuppressWarnings("java:S2095")
private CloseableHttpClient buildHttpClient() {
    val plainSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
    val registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainSocketFactory).register("https", this.sslSocketFactory).build();
    val connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(this.maxPooledConnections);
    connectionManager.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
    connectionManager.setValidateAfterInactivity(DEFAULT_TIMEOUT);
    val httpHost = new HttpHost(InetAddress.getLocalHost());
    val httpRoute = new HttpRoute(httpHost);
    connectionManager.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
    val requestConfig = RequestConfig.custom().setSocketTimeout(this.readTimeout).setConnectTimeout((int) this.connectionTimeout).setConnectionRequestTimeout((int) this.connectionTimeout).setCircularRedirectsAllowed(this.circularRedirectsAllowed).setRedirectsEnabled(this.redirectsEnabled).setAuthenticationEnabled(this.authenticationEnabled).build();
    val builder = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).setSSLSocketFactory(this.sslSocketFactory).setSSLHostnameVerifier(this.hostnameVerifier).setRedirectStrategy(this.redirectionStrategy).setDefaultCredentialsProvider(this.credentialsProvider).setDefaultCookieStore(this.cookieStore).setConnectionReuseStrategy(this.connectionReuseStrategy).setKeepAliveStrategy(this.connectionKeepAliveStrategy).setConnectionBackoffStrategy(this.connectionBackoffStrategy).setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy).setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy).setDefaultHeaders(this.defaultHeaders).setProxy(this.proxy).setRetryHandler(this.httpRequestRetryHandler).useSystemProperties();
    return builder.build();
}
Also used : lombok.val(lombok.val) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpHost(org.apache.http.HttpHost) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) SneakyThrows(lombok.SneakyThrows)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12