use of org.apache.http.impl.client.AbstractHttpClient 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.AbstractHttpClient in project coprhd-controller by CoprHD.
the class BaseHttpClientFactory method createRawHTTPClient.
/**
* Create a HTTPClient using the factories configuration without Credentials
*
* @param useConnectionTimeout - allows override of the the default connectionTimeout
* @param useConnectionReadTimeout - allows override of the default connectionReadTimeout
* @throws AuthenticationException, GeneralSecurityException, RuntimeException
*/
protected AbstractHttpClient createRawHTTPClient(int useConnectionTimeout, int useConnectionReadTimeout) throws AuthenticationException, GeneralSecurityException, RuntimeException {
// select the appropriate connection manager and set options as appropriate
ClientConnectionManager cm = null;
if (threadSafeClients) {
ThreadSafeClientConnManager tscm = new ThreadSafeClientConnManager();
tscm.setMaxTotal(maxConnections);
tscm.setDefaultMaxPerRoute(maxConnectionsPerHost);
cm = tscm;
} else {
cm = new SingleClientConnManager();
}
// construct a client instance with the connection manager embedded
AbstractHttpClient httpClient = new DefaultHttpClient(cm);
if (relaxSSL) {
// !!!WARNING: This effectively turns off the authentication component of SSL, leaving only encryption
// can throw GeneralSecurityException
SSLHelper.configurePermissiveSSL(httpClient);
} else if (isSecureSSL()) {
SSLHelper.configureSSLWithTrustManger(httpClient, coordinator);
}
// see org.apache.http.client.params.AllClientPNames for a collected
// list of the available client parameters
HttpParams clientParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(clientParams, useConnectionTimeout);
HttpConnectionParams.setSoTimeout(clientParams, useConnectionReadTimeout);
// consider turning off the use of the Expect: 100-Continue response if
// your posts/puts tend to be relatively small
HttpProtocolParams.setUseExpectContinue(clientParams, false);
// TODO: reconsider this setting
// by default the client auto-retries on failures - turn that off so we can handle it manually
httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
return httpClient;
}
use of org.apache.http.impl.client.AbstractHttpClient in project ribbon by Netflix.
the class RestClient method apacheHttpClientSpecificInitialization.
protected Client apacheHttpClientSpecificInitialization() {
httpClient4 = NFHttpClientFactory.getNamedNFHttpClient(restClientName, this.ncc, true);
if (httpClient4 instanceof AbstractHttpClient) {
// DONT use our NFHttpClient's default Retry Handler since we have
// retry handling (same server/next server) in RestClient itself
((AbstractHttpClient) httpClient4).setHttpRequestRetryHandler(new NFHttpMethodRetryHandler(restClientName, 0, false, 0));
} else {
logger.warn("Unexpected error: Unable to disable NFHttpClient " + "retry handler, this most likely will not cause an " + "issue but probably should be looked at");
}
HttpParams httpClientParams = httpClient4.getParams();
// initialize Connection Manager cleanup facility
NFHttpClient nfHttpClient = (NFHttpClient) httpClient4;
// should we enable connection cleanup for idle connections?
try {
enableConnectionPoolCleanerTask = ncc.getOrDefault(CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled);
nfHttpClient.getConnPoolCleaner().setEnableConnectionPoolCleanerTask(enableConnectionPoolCleanerTask);
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.ConnectionPoolCleanerTaskEnabled, e1);
}
if (enableConnectionPoolCleanerTask) {
try {
connectionCleanerRepeatInterval = ncc.getOrDefault(CommonClientConfigKey.ConnectionCleanerRepeatInterval);
nfHttpClient.getConnPoolCleaner().setConnectionCleanerRepeatInterval(connectionCleanerRepeatInterval);
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.ConnectionCleanerRepeatInterval, e1);
}
try {
connIdleEvictTimeMilliSeconds = ncc.getDynamicProperty(CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds);
nfHttpClient.setConnIdleEvictTimeMilliSeconds(connIdleEvictTimeMilliSeconds);
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.ConnIdleEvictTimeMilliSeconds, e1);
}
nfHttpClient.initConnectionCleanerTask();
}
try {
maxConnectionsperHost = ncc.getOrDefault(CommonClientConfigKey.MaxHttpConnectionsPerHost);
ClientConnectionManager connMgr = httpClient4.getConnectionManager();
if (connMgr instanceof ThreadSafeClientConnManager) {
((ThreadSafeClientConnManager) connMgr).setDefaultMaxPerRoute(maxConnectionsperHost);
}
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.MaxHttpConnectionsPerHost, e1);
}
try {
maxTotalConnections = ncc.getOrDefault(CommonClientConfigKey.MaxTotalHttpConnections);
ClientConnectionManager connMgr = httpClient4.getConnectionManager();
if (connMgr instanceof ThreadSafeClientConnManager) {
((ThreadSafeClientConnManager) connMgr).setMaxTotal(maxTotalConnections);
}
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.MaxTotalHttpConnections, e1);
}
try {
connectionTimeout = ncc.getOrDefault(CommonClientConfigKey.ConnectTimeout);
HttpConnectionParams.setConnectionTimeout(httpClientParams, connectionTimeout);
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.ConnectTimeout, e1);
}
try {
readTimeout = ncc.getOrDefault(CommonClientConfigKey.ReadTimeout);
HttpConnectionParams.setSoTimeout(httpClientParams, readTimeout);
} catch (Exception e1) {
throwInvalidValue(CommonClientConfigKey.ReadTimeout, e1);
}
// httpclient 4 seems to only have one buffer size controlling both
// send/receive - so let's take the bigger of the two values and use
// it as buffer size
int bufferSize = Integer.MIN_VALUE;
if (ncc.get(CommonClientConfigKey.ReceiveBufferSize) != null) {
try {
bufferSize = ncc.getOrDefault(CommonClientConfigKey.ReceiveBufferSize);
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.ReceiveBufferSize, e);
}
if (ncc.get(CommonClientConfigKey.SendBufferSize) != null) {
try {
int sendBufferSize = ncc.getOrDefault(CommonClientConfigKey.SendBufferSize);
if (sendBufferSize > bufferSize) {
bufferSize = sendBufferSize;
}
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.SendBufferSize, e);
}
}
}
if (bufferSize != Integer.MIN_VALUE) {
HttpConnectionParams.setSocketBufferSize(httpClientParams, bufferSize);
}
if (ncc.get(CommonClientConfigKey.StaleCheckingEnabled) != null) {
try {
HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, ncc.getOrDefault(CommonClientConfigKey.StaleCheckingEnabled));
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.StaleCheckingEnabled, e);
}
}
if (ncc.get(CommonClientConfigKey.Linger) != null) {
try {
HttpConnectionParams.setLinger(httpClientParams, ncc.getOrDefault(CommonClientConfigKey.Linger));
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.Linger, e);
}
}
if (ncc.get(CommonClientConfigKey.ProxyHost) != null) {
try {
proxyHost = (String) ncc.getOrDefault(CommonClientConfigKey.ProxyHost);
proxyPort = ncc.getOrDefault(CommonClientConfigKey.ProxyPort);
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClient4.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.ProxyHost, e);
}
}
if (isSecure) {
final URL trustStoreUrl = getResourceForOptionalProperty(CommonClientConfigKey.TrustStore);
final URL keyStoreUrl = getResourceForOptionalProperty(CommonClientConfigKey.KeyStore);
final ClientConnectionManager currentManager = httpClient4.getConnectionManager();
AbstractSslContextFactory abstractFactory = null;
if (// if client is not is not required, we only need a keystore OR a truststore to warrant configuring
(isClientAuthRequired && (trustStoreUrl != null && keyStoreUrl != null)) || (!isClientAuthRequired && (trustStoreUrl != null || keyStoreUrl != null))) {
try {
abstractFactory = new URLSslContextFactory(trustStoreUrl, ncc.get(CommonClientConfigKey.TrustStorePassword), keyStoreUrl, ncc.get(CommonClientConfigKey.KeyStorePassword));
} catch (ClientSslSocketFactoryException e) {
throw new IllegalArgumentException("Unable to configure custom secure socket factory", e);
}
}
KeyStoreAwareSocketFactory awareSocketFactory;
try {
awareSocketFactory = isHostnameValidationRequired ? new KeyStoreAwareSocketFactory(abstractFactory) : new KeyStoreAwareSocketFactory(abstractFactory, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
currentManager.getSchemeRegistry().register(new Scheme("https", 443, awareSocketFactory));
} catch (Exception e) {
throw new IllegalArgumentException("Unable to configure custom secure socket factory", e);
}
}
// See http://hc.apache.org/httpcomponents-client-ga/tutorial/html/advanced.html
if (ignoreUserToken) {
((DefaultHttpClient) httpClient4).setUserTokenHandler(new UserTokenHandler() {
@Override
public Object getUserToken(HttpContext context) {
return null;
}
});
}
// custom SSL Factory handler
String customSSLFactoryClassName = ncc.get(CommonClientConfigKey.CustomSSLSocketFactoryClassName);
if (customSSLFactoryClassName != null) {
try {
SSLSocketFactory customSocketFactory = (SSLSocketFactory) ClientFactory.instantiateInstanceWithClientConfig(customSSLFactoryClassName, ncc);
httpClient4.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, customSocketFactory));
} catch (Exception e) {
throwInvalidValue(CommonClientConfigKey.CustomSSLSocketFactoryClassName, e);
}
}
ApacheHttpClient4Handler handler = new ApacheHttpClient4Handler(httpClient4, new BasicCookieStore(), false);
return new ApacheHttpClient4(handler, config);
}
use of org.apache.http.impl.client.AbstractHttpClient in project jmeter by apache.
the class HTTPHC4Impl method closeThreadLocalConnections.
/**
*
*/
private void closeThreadLocalConnections() {
// Does not need to be synchronised, as all access is from same thread
Map<HttpClientKey, CloseableHttpClient> mapHttpClientPerHttpClientKey = HTTPCLIENTS_CACHE_PER_THREAD_AND_HTTPCLIENTKEY.get();
if (mapHttpClientPerHttpClientKey != null) {
for (CloseableHttpClient cl : mapHttpClientPerHttpClientKey.values()) {
((AbstractHttpClient) cl).clearRequestInterceptors();
((AbstractHttpClient) cl).clearResponseInterceptors();
JOrphanUtils.closeQuietly(cl);
cl.getConnectionManager().shutdown();
}
mapHttpClientPerHttpClientKey.clear();
}
}
use of org.apache.http.impl.client.AbstractHttpClient in project jmeter by apache.
the class HTTPHC4Impl method setConnectionAuthorization.
/**
* Setup credentials for url AuthScope but keeps Proxy AuthScope credentials
* @param client HttpClient
* @param url URL
* @param authManager {@link AuthManager}
* @param key key
*/
private void setConnectionAuthorization(CloseableHttpClient client, URL url, AuthManager authManager, HttpClientKey key) {
CredentialsProvider credentialsProvider = ((AbstractHttpClient) client).getCredentialsProvider();
if (authManager != null) {
if (authManager.hasAuthForURL(url)) {
authManager.setupCredentials(client, url, credentialsProvider, LOCALHOST);
} else {
credentialsProvider.clear();
}
} else {
Credentials credentials = null;
AuthScope authScope = null;
if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) {
authScope = new AuthScope(key.proxyHost, key.proxyPort);
credentials = credentialsProvider.getCredentials(authScope);
}
credentialsProvider.clear();
if (credentials != null) {
credentialsProvider.setCredentials(authScope, credentials);
}
}
}
Aggregations