Search in sources :

Example 11 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project jmeter-plugins-manager by undera.

the class JARSourceHTTP method getHTTPClient.

private AbstractHttpClient getHTTPClient() {
    AbstractHttpClient client = new DefaultHttpClient();
    String proxyHost = System.getProperty("https.proxyHost", "");
    if (!proxyHost.isEmpty()) {
        int proxyPort = Integer.parseInt(System.getProperty("https.proxyPort", "-1"));
        log.info("Using proxy " + proxyHost + ":" + proxyPort);
        HttpParams params = client.getParams();
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        String proxyUser = System.getProperty(JMeter.HTTP_PROXY_USER, org.apache.jmeter.util.JMeterUtils.getProperty(JMeter.HTTP_PROXY_USER));
        if (proxyUser != null) {
            log.info("Using authenticated proxy with username: " + proxyUser);
            String proxyPass = System.getProperty(JMeter.HTTP_PROXY_PASS, JMeterUtils.getProperty(JMeter.HTTP_PROXY_PASS));
            String localHost;
            try {
                localHost = InetAddress.getLocalHost().getCanonicalHostName();
            } catch (Throwable e) {
                log.error("Failed to get local host name, defaulting to 'localhost'", e);
                localHost = "localhost";
            }
            AuthScope authscope = new AuthScope(proxyHost, proxyPort);
            String proxyDomain = JMeterUtils.getPropDefault("http.proxyDomain", "");
            NTCredentials credentials = new NTCredentials(proxyUser, proxyPass, localHost, proxyDomain);
            client.getCredentialsProvider().setCredentials(authscope, credentials);
        }
    }
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(RETRY_COUNT, true));
    return client;
}
Also used : AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) HttpParams(org.apache.http.params.HttpParams) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) NTCredentials(org.apache.http.auth.NTCredentials)

Example 12 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project scout.rt by eclipse.

the class ApacheHttpTransportFactory method setConnectionKeepAliveAndRetrySettings.

/**
 * @param builder
 */
protected void setConnectionKeepAliveAndRetrySettings(HttpClientBuilder builder) {
    final boolean keepAliveProp = CONFIG.getPropertyValue(ApacheHttpTransportKeepAliveProperty.class);
    builder.setConnectionReuseStrategy(new ConnectionReuseStrategy() {

        @Override
        public boolean keepAlive(HttpResponse response, HttpContext context) {
            return keepAliveProp;
        }
    });
    // Connections should not be kept open forever, there are numerous reasons a connection could get invalid. Also it does not make much sense to keep a connection open forever
    builder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            long headerKeepAliveDuration = super.getKeepAliveDuration(response, context);
            return headerKeepAliveDuration < 0 ? 5000 : headerKeepAliveDuration;
        }
    });
    final boolean retryPostProp = CONFIG.getPropertyValue(ApacheHttpTransportRetryPostProperty.class);
    builder.setRetryHandler(new DefaultHttpRequestRetryHandler(1, true) {

        @Override
        protected boolean handleAsIdempotent(HttpRequest request) {
            return retryPostProp || super.handleAsIdempotent(request);
        }
    });
}
Also used : HttpRequest(org.apache.http.HttpRequest) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) HttpContext(org.apache.http.protocol.HttpContext) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpResponse(org.apache.http.HttpResponse) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy)

Example 13 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project dropwizard by dropwizard.

the class HttpClientBuilder method createClient.

/**
 * Map the parameters in {@link HttpClientConfiguration} to configuration on a
 * {@link org.apache.http.impl.client.HttpClientBuilder} instance
 *
 * @param builder
 * @param manager
 * @param name
 * @return the configured {@link CloseableHttpClient}
 */
protected ConfiguredCloseableHttpClient createClient(final org.apache.http.impl.client.HttpClientBuilder builder, final InstrumentedHttpClientConnectionManager manager, final String name) {
    final String cookiePolicy = configuration.isCookiesEnabled() ? CookieSpecs.DEFAULT : CookieSpecs.IGNORE_COOKIES;
    final Integer timeout = (int) configuration.getTimeout().toMilliseconds();
    final Integer connectionTimeout = (int) configuration.getConnectionTimeout().toMilliseconds();
    final Integer connectionRequestTimeout = (int) configuration.getConnectionRequestTimeout().toMilliseconds();
    final boolean normalizeUri = configuration.isNormalizeUriEnabled();
    final long keepAlive = configuration.getKeepAlive().toMilliseconds();
    final ConnectionReuseStrategy reuseStrategy = keepAlive == 0 ? new NoConnectionReuseStrategy() : new DefaultConnectionReuseStrategy();
    final HttpRequestRetryHandler retryHandler = configuration.getRetries() == 0 ? NO_RETRIES : (httpRequestRetryHandler == null ? new DefaultHttpRequestRetryHandler(configuration.getRetries(), false) : httpRequestRetryHandler);
    final RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(cookiePolicy).setSocketTimeout(timeout).setConnectTimeout(connectionTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setNormalizeUri(normalizeUri).build();
    final SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoTimeout(timeout).build();
    builder.setRequestExecutor(createRequestExecutor(name)).setConnectionManager(manager).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).setConnectionReuseStrategy(reuseStrategy).setRetryHandler(retryHandler).setUserAgent(createUserAgent(name));
    if (keepAlive != 0) {
        // either keep alive based on response header Keep-Alive,
        // or if the server can keep a persistent connection (-1), then override based on client's configuration
        builder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {

            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                final long duration = super.getKeepAliveDuration(response, context);
                return (duration == -1) ? keepAlive : duration;
            }
        });
    }
    // create a tunnel through a proxy host if it's specified in the config
    final ProxyConfiguration proxy = configuration.getProxyConfiguration();
    if (proxy != null) {
        final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts()));
        // if the proxy host requires authentication then add the host credentials to the credentials provider
        final AuthConfiguration auth = proxy.getAuth();
        if (auth != null) {
            if (credentialsProvider == null) {
                credentialsProvider = new BasicCredentialsProvider();
            }
            // set the AuthScope
            AuthScope authScope = new AuthScope(httpHost, auth.getRealm(), auth.getAuthScheme());
            // set the credentials type
            Credentials credentials = configureCredentials(auth);
            credentialsProvider.setCredentials(authScope, credentials);
        }
    }
    if (credentialsProvider != null) {
        builder.setDefaultCredentialsProvider(credentialsProvider);
    }
    if (routePlanner != null) {
        builder.setRoutePlanner(routePlanner);
    }
    if (disableContentCompression) {
        builder.disableContentCompression();
    }
    if (redirectStrategy != null) {
        builder.setRedirectStrategy(redirectStrategy);
    }
    if (defaultHeaders != null) {
        builder.setDefaultHeaders(defaultHeaders);
    }
    if (verifier != null) {
        builder.setSSLHostnameVerifier(verifier);
    }
    if (httpProcessor != null) {
        builder.setHttpProcessor(httpProcessor);
    }
    if (serviceUnavailableRetryStrategy != null) {
        builder.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy);
    }
    customizeBuilder(builder);
    return new ConfiguredCloseableHttpClient(builder.build(), requestConfig);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) NonProxyListProxyRoutePlanner(io.dropwizard.client.proxy.NonProxyListProxyRoutePlanner) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) NTCredentials(org.apache.http.auth.NTCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 14 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project pinpoint by naver.

the class HttpClient4PluginTest method addDefaultHttpRequestRetryHandlerClass.

@Test
public void addDefaultHttpRequestRetryHandlerClass() {
    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
    IOException iOException = new IOException();
    HttpContext context = new BasicHttpContext();
    assertTrue(retryHandler.retryRequest(iOException, 1, context));
    assertTrue(retryHandler.retryRequest(iOException, 2, context));
}
Also used : BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with DefaultHttpRequestRetryHandler

use of org.apache.http.impl.client.DefaultHttpRequestRetryHandler in project pinpoint by naver.

the class DefaultHttpRequestRetryHandlerModifierIT method test.

@Test
public void test() throws Exception {
    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
    IOException iOException = new IOException();
    HttpContext context = new BasicHttpContext();
    assertTrue(retryHandler.retryRequest(iOException, 1, context));
    assertTrue(retryHandler.retryRequest(iOException, 2, context));
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 1"), annotation("RETURN_DATA", true)));
    verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", DefaultHttpRequestRetryHandler.class.getMethod("retryRequest", IOException.class, int.class, HttpContext.class), annotation("http.internal.display", IOException.class.getName() + ", 2"), annotation("RETURN_DATA", true)));
    verifier.verifyTraceCount(0);
}
Also used : BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DefaultHttpRequestRetryHandler(org.apache.http.impl.client.DefaultHttpRequestRetryHandler) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) IOException(java.io.IOException) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Aggregations

DefaultHttpRequestRetryHandler (org.apache.http.impl.client.DefaultHttpRequestRetryHandler)16 IOException (java.io.IOException)6 HttpContext (org.apache.http.protocol.HttpContext)5 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)4 HttpParams (org.apache.http.params.HttpParams)4 RequestConfig (org.apache.http.client.config.RequestConfig)3 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)3 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)2 HttpHost (org.apache.http.HttpHost)2 HttpResponse (org.apache.http.HttpResponse)2 AuthScope (org.apache.http.auth.AuthScope)2 NTCredentials (org.apache.http.auth.NTCredentials)2 HttpClient (org.apache.http.client.HttpClient)2 SocketConfig (org.apache.http.config.SocketConfig)2 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)2 AbstractHttpClient (org.apache.http.impl.client.AbstractHttpClient)2 DefaultConnectionKeepAliveStrategy (org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy)2