use of org.apache.http.impl.client.StandardHttpRequestRetryHandler in project jmeter by apache.
the class HTTPHC4Impl method setupClient.
private CloseableHttpClient setupClient(URL url) {
Map<HttpClientKey, CloseableHttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
final String host = url.getHost();
String proxyHost = getProxyHost();
int proxyPort = getProxyPortInt();
String proxyPass = getProxyPass();
String proxyUser = getProxyUser();
// static proxy is the globally define proxy eg command line or properties
boolean useStaticProxy = isStaticProxy(host);
// dynamic proxy is the proxy defined for this sampler
boolean useDynamicProxy = isDynamicProxy(proxyHost, proxyPort);
boolean useProxy = useStaticProxy || useDynamicProxy;
// if both dynamic and static are used, the dynamic proxy has priority over static
if (!useDynamicProxy) {
proxyHost = PROXY_HOST;
proxyPort = PROXY_PORT;
proxyUser = PROXY_USER;
proxyPass = PROXY_PASS;
}
// Lookup key - must agree with all the values used to create the HttpClient.
HttpClientKey key = new HttpClientKey(url, useProxy, proxyHost, proxyPort, proxyUser, proxyPass);
CloseableHttpClient httpClient = null;
boolean concurrentDwn = this.testElement.isConcurrentDwn();
if (concurrentDwn) {
httpClient = (CloseableHttpClient) JMeterContextService.getContext().getSamplerContext().get(HTTPCLIENT_TOKEN);
}
if (httpClient == null) {
httpClient = mapHttpClientPerHttpClientKey.get(key);
}
if (httpClient != null && resetSSLContext && HTTPConstants.PROTOCOL_HTTPS.equalsIgnoreCase(url.getProtocol())) {
((AbstractHttpClient) httpClient).clearRequestInterceptors();
((AbstractHttpClient) httpClient).clearResponseInterceptors();
httpClient.getConnectionManager().closeIdleConnections(1L, TimeUnit.MICROSECONDS);
httpClient = null;
JsseSSLManager sslMgr = (JsseSSLManager) SSLManager.getInstance();
sslMgr.resetContext();
resetSSLContext = false;
}
if (httpClient == null) {
// One-time init for this client
HttpParams clientParams = new DefaultedHttpParams(new BasicHttpParams(), DEFAULT_HTTP_PARAMS);
DnsResolver resolver = this.testElement.getDNSResolver();
if (resolver == null) {
resolver = SystemDefaultDnsResolver.INSTANCE;
}
MeasuringConnectionManager connManager = new MeasuringConnectionManager(createSchemeRegistry(), resolver, TIME_TO_LIVE, VALIDITY_AFTER_INACTIVITY_TIMEOUT);
// to be realistic JMeter must set an higher value to DefaultMaxPerRoute
if (concurrentDwn) {
try {
int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
connManager.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, connManager.getDefaultMaxPerRoute()));
} catch (NumberFormatException nfe) {
// no need to log -> will be done by the sampler
}
}
httpClient = new DefaultHttpClient(connManager, clientParams) {
@Override
protected HttpRequestRetryHandler createHttpRequestRetryHandler() {
return new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED);
}
};
if (IDLE_TIMEOUT > 0) {
((AbstractHttpClient) httpClient).setKeepAliveStrategy(IDLE_STRATEGY);
}
// see https://issues.apache.org/jira/browse/HTTPCORE-397
((AbstractHttpClient) httpClient).setReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE);
((AbstractHttpClient) httpClient).addResponseInterceptor(RESPONSE_CONTENT_ENCODING);
// HACK
((AbstractHttpClient) httpClient).addResponseInterceptor(METRICS_SAVER);
((AbstractHttpClient) httpClient).addRequestInterceptor(METRICS_RESETTER);
// Override the default schemes as necessary
SchemeRegistry schemeRegistry = httpClient.getConnectionManager().getSchemeRegistry();
if (SLOW_HTTP != null) {
schemeRegistry.register(SLOW_HTTP);
}
// Set up proxy details
if (useProxy) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
clientParams.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
if (proxyUser.length() > 0) {
((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUser, proxyPass, LOCALHOST, PROXY_DOMAIN));
}
}
// Bug 52126 - we do our own cookie handling
clientParams.setParameter(ClientPNames.COOKIE_POLICY, CookieSpecs.IGNORE_COOKIES);
if (log.isDebugEnabled()) {
log.debug("Created new HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
}
// save the agent for next time round
mapHttpClientPerHttpClientKey.put(key, httpClient);
} else {
if (log.isDebugEnabled()) {
log.debug("Reusing the HttpClient: @" + System.identityHashCode(httpClient) + " " + key.toString());
}
}
if (concurrentDwn) {
JMeterContextService.getContext().getSamplerContext().put(HTTPCLIENT_TOKEN, httpClient);
}
// TODO - should this be done when the client is created?
// If so, then the details need to be added as part of HttpClientKey
setConnectionAuthorization(httpClient, url, getAuthManager(), key);
return httpClient;
}
use of org.apache.http.impl.client.StandardHttpRequestRetryHandler in project ovirt-engine by oVirt.
the class HttpClientBuilder method build.
public CloseableHttpClient build() throws IOException, GeneralSecurityException {
// Prepare the default configuration for all requests:
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout != null ? connectTimeout : 0).setSocketTimeout(readTimeout != null ? readTimeout : 0).build();
// Configure the trust manager:
TrustManager[] trustManager = null;
if (verifyChain) {
if (trustStore != null) {
try (InputStream is = new FileInputStream(trustStore)) {
KeyStore ks = KeyStore.getInstance(trustStoreType);
ks.load(is, StringUtils.isEmpty(trustStorePassword) ? null : trustStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm);
tmf.init(ks);
trustManager = tmf.getTrustManagers();
}
}
} else {
trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
}
// Create the SSL context:
SSLContext sslContext = SSLContext.getInstance(tlsProtocol);
sslContext.init(null, trustManager, null);
// Create the SSL host name verifier:
HostnameVerifier sslHostnameVerifier = null;
if (!verifyHost) {
sslHostnameVerifier = (hostname, session) -> true;
}
// Create the socket factory for HTTP:
ConnectionSocketFactory httpSocketFactory = new PlainConnectionSocketFactory();
// Create the socket factory for HTTPS:
ConnectionSocketFactory httpsSocketFactory = new SSLConnectionSocketFactory(sslContext, sslHostnameVerifier);
// Create the socket factory registry:
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpSocketFactory).register("https", httpsSocketFactory).build();
// Create the connection manager:
HttpClientConnectionManager connectionManager;
if (poolSize != null) {
PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolManager.setDefaultMaxPerRoute(poolSize);
poolManager.setMaxTotal(poolSize);
poolManager.setValidateAfterInactivity(validateAfterInactivity == null ? 100 : validateAfterInactivity);
connectionManager = poolManager;
} else {
connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
}
// Create the client:
return org.apache.http.impl.client.HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setSSLHostnameVerifier(sslHostnameVerifier).setConnectionManager(connectionManager).setRetryHandler(new StandardHttpRequestRetryHandler(retryCount == null ? 1 : retryCount, true)).build();
}
use of org.apache.http.impl.client.StandardHttpRequestRetryHandler 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();
}
use of org.apache.http.impl.client.StandardHttpRequestRetryHandler in project cosmic by MissionCriticalCloud.
the class HttpClientHelper method createHttpClient.
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
s_logger.info("Creating new HTTP connection pool and client");
final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
final BasicCookieStore cookieStore = new BasicCookieStore();
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE);
connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS);
final RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).setSocketTimeout(DEFAULT_SOCKET_TIMEOUT).setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT).setConnectTimeout(DEFAULT_CONNECT_TIMEOUT).build();
return HttpClientBuilder.create().setConnectionManager(connManager).setRedirectStrategy(new LaxRedirectStrategy()).setDefaultRequestConfig(requestConfig).setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}
use of org.apache.http.impl.client.StandardHttpRequestRetryHandler in project jmeter by apache.
the class HTTPHC4Impl method setupClient.
private MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> setupClient(HttpClientKey key, JMeterVariables jMeterVariables, HttpClientContext clientContext) throws GeneralSecurityException {
Map<HttpClientKey, MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
clientContext.setAttribute(CONTEXT_ATTRIBUTE_CLIENT_KEY, key);
CloseableHttpClient httpClient = null;
MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager> triple = null;
boolean concurrentDwn = this.testElement.isConcurrentDwn();
Map<String, Object> samplerContext = JMeterContextService.getContext().getSamplerContext();
if (concurrentDwn) {
triple = (MutableTriple<CloseableHttpClient, AuthState, PoolingHttpClientConnectionManager>) samplerContext.get(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE);
}
if (triple == null) {
triple = mapHttpClientPerHttpClientKey.get(key);
}
if (triple != null) {
httpClient = triple.getLeft();
}
setupProxyAuthState(triple, clientContext);
resetStateIfNeeded(triple, jMeterVariables, clientContext, mapHttpClientPerHttpClientKey);
if (httpClient == null) {
// One-time init for this client
DnsResolver resolver = this.testElement.getDNSResolver();
if (resolver == null) {
resolver = SystemDefaultDnsResolver.INSTANCE;
}
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", new LazyLayeredConnectionSocketFactory()).register("http", CONNECTION_SOCKET_FACTORY).build();
// Modern browsers use more connections per host than the current httpclient default (2)
// when using parallel download the httpclient and connection manager are shared by the downloads threads
// to be realistic JMeter must set an higher value to DefaultMaxPerRoute
PoolingHttpClientConnectionManager pHCCM = new PoolingHttpClientConnectionManager(new JMeterDefaultHttpClientConnectionOperator(registry, null, resolver), null, TIME_TO_LIVE, TimeUnit.MILLISECONDS);
pHCCM.setValidateAfterInactivity(VALIDITY_AFTER_INACTIVITY_TIMEOUT);
if (concurrentDwn) {
try {
int maxConcurrentDownloads = Integer.parseInt(this.testElement.getConcurrentPool());
pHCCM.setDefaultMaxPerRoute(Math.max(maxConcurrentDownloads, pHCCM.getDefaultMaxPerRoute()));
} catch (NumberFormatException nfe) {
// no need to log -> will be done by the sampler
}
}
CookieSpecProvider cookieSpecProvider = new IgnoreSpecProvider();
Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create().register(CookieSpecs.IGNORE_COOKIES, cookieSpecProvider).build();
HttpClientBuilder builder = HttpClients.custom().setConnectionManager(pHCCM).setSchemePortResolver(new DefaultSchemePortResolver()).setDnsResolver(resolver).setRequestExecutor(REQUEST_EXECUTOR).setSSLSocketFactory(new LazyLayeredConnectionSocketFactory()).setDefaultCookieSpecRegistry(cookieSpecRegistry).setDefaultSocketConfig(SocketConfig.DEFAULT).setRedirectStrategy(new LaxRedirectStrategy()).setConnectionTimeToLive(TIME_TO_LIVE, TimeUnit.MILLISECONDS).setRetryHandler(new StandardHttpRequestRetryHandler(RETRY_COUNT, REQUEST_SENT_RETRY_ENABLED)).setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE).setProxyAuthenticationStrategy(getProxyAuthStrategy());
if (DISABLE_DEFAULT_UA) {
builder.disableDefaultUserAgent();
}
Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.BASIC, new BasicSchemeFactory()).register(AuthSchemes.DIGEST, new DigestSchemeFactory()).register(AuthSchemes.NTLM, new NTLMSchemeFactory()).register(AuthSchemes.SPNEGO, new DynamicSPNegoSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).register(AuthSchemes.KERBEROS, new DynamicKerberosSchemeFactory(AuthManager.STRIP_PORT, AuthManager.USE_CANONICAL_HOST_NAME)).build();
builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
if (IDLE_TIMEOUT > 0) {
builder.setKeepAliveStrategy(IDLE_STRATEGY);
}
// Set up proxy details
AuthScope proxyAuthScope = null;
NTCredentials proxyCredentials = null;
if (key.hasProxy) {
HttpHost proxy = new HttpHost(key.proxyHost, key.proxyPort, key.proxyScheme);
builder.setProxy(proxy);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (!key.proxyUser.isEmpty()) {
proxyAuthScope = new AuthScope(key.proxyHost, key.proxyPort);
proxyCredentials = new NTCredentials(key.proxyUser, key.proxyPass, LOCALHOST, PROXY_DOMAIN);
credsProvider.setCredentials(proxyAuthScope, proxyCredentials);
}
builder.setDefaultCredentialsProvider(credsProvider);
}
builder.disableContentCompression().addInterceptorLast(RESPONSE_CONTENT_ENCODING);
if (BASIC_AUTH_PREEMPTIVE) {
builder.addInterceptorFirst(PREEMPTIVE_AUTH_INTERCEPTOR);
} else {
builder.setDefaultCredentialsProvider(new ManagedCredentialsProvider(getAuthManager(), proxyAuthScope, proxyCredentials));
}
httpClient = builder.build();
if (log.isDebugEnabled()) {
log.debug("Created new HttpClient: @{} {}", System.identityHashCode(httpClient), key);
}
triple = MutableTriple.of(httpClient, null, pHCCM);
// save the agent for next time round
mapHttpClientPerHttpClientKey.put(key, triple);
} else {
if (log.isDebugEnabled()) {
log.debug("Reusing the HttpClient: @{} {}", System.identityHashCode(httpClient), key);
}
}
if (concurrentDwn) {
samplerContext.put(CONTEXT_ATTRIBUTE_PARENT_SAMPLE_CLIENT_STATE, triple);
}
return triple;
}
Aggregations