use of org.apache.http.client.config.RequestConfig 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
private CloseableHttpClient buildHttpClient() {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.readTimeout).setConnectTimeout((int) this.connectionTimeout).setConnectionRequestTimeout((int) this.connectionTimeout).setCircularRedirectsAllowed(this.circularRedirectsAllowed).setRedirectsEnabled(this.redirectsEnabled).setAuthenticationEnabled(this.authenticationEnabled).build();
final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connMgmr).setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslsf).setSSLHostnameVerifier(this.hostnameVerifier).setRedirectStrategy(this.redirectionStrategy).setDefaultCredentialsProvider(this.credentialsProvider).setDefaultCookieStore(this.cookieStore).setConnectionReuseStrategy(this.connectionReuseStrategy).setConnectionBackoffStrategy(this.connectionBackoffStrategy).setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy).setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy).setDefaultHeaders(this.defaultHeaders).useSystemProperties();
return builder.build();
}
use of org.apache.http.client.config.RequestConfig in project scheduling by ow2-proactive.
the class ProActiveVersionUtility method getProActiveServerVersion.
protected static String getProActiveServerVersion(ApplicationContext currentContext) {
int timeout = (int) TimeUnit.SECONDS.toMillis(2);
RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
try (CloseableHttpClient httpClient = new HttpClientBuilder().setDefaultRequestConfig(config).useSystemProperties().build()) {
HttpGet getMethod = new HttpGet(currentContext.getResourceUrl("version"));
HttpResponse response = httpClient.execute(getMethod);
String jsonObject = handleResponse(response);
if (jsonObject != null)
return jsonObject;
} catch (IOException ignore) {
// ignore exception, default value will be used
}
return VERSION_UNDEFINED;
}
use of org.apache.http.client.config.RequestConfig in project camel by apache.
the class HttpPollingConsumer method doReceive.
protected Exchange doReceive(int timeout) {
Exchange exchange = endpoint.createExchange();
HttpRequestBase method = createMethod(exchange);
HttpClientContext httpClientContext = new HttpClientContext();
// set optional timeout in millis
if (timeout > 0) {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).build();
httpClientContext.setRequestConfig(requestConfig);
}
HttpEntity responeEntity = null;
try {
// execute request
HttpResponse response = httpClient.execute(method, httpClientContext);
int responseCode = response.getStatusLine().getStatusCode();
responeEntity = response.getEntity();
Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);
// lets store the result in the output message.
Message message = exchange.getOut();
message.setBody(body);
// lets set the headers
Header[] headers = response.getAllHeaders();
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
for (Header header : headers) {
String name = header.getName();
// mapping the content-type
if (name.toLowerCase().equals("content-type")) {
name = Exchange.CONTENT_TYPE;
}
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
message.setHeader(name, value);
}
}
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
if (response.getStatusLine() != null) {
message.setHeader(Exchange.HTTP_RESPONSE_TEXT, response.getStatusLine().getReasonPhrase());
}
return exchange;
} catch (IOException e) {
throw new RuntimeCamelException(e);
} finally {
if (responeEntity != null) {
try {
EntityUtils.consume(responeEntity);
} catch (IOException e) {
// nothing what we can do
}
}
}
}
use of org.apache.http.client.config.RequestConfig in project dubbo by alibaba.
the class RestProtocol method doRefer.
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).build();
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext 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")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
}).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 : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.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);
}
use of org.apache.http.client.config.RequestConfig in project jersey by jersey.
the class DisableContentEncodingTest method testDisabledByRequestConfig.
@Test
public void testDisabledByRequestConfig() {
ClientConfig cc = new ClientConfig(GZipEncoder.class);
final RequestConfig requestConfig = RequestConfig.custom().setContentCompressionEnabled(false).build();
cc.property(ApacheClientProperties.REQUEST_CONFIG, requestConfig);
cc.connectorProvider(new ApacheConnectorProvider());
Client client = ClientBuilder.newClient(cc);
WebTarget r = client.target(getBaseUri());
String enc = r.request().get().readEntity(String.class);
assertEquals("", enc);
}
Aggregations