use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactoryTests method defaultSettingsOfHttpClientMergedOnExecutorCustomization.
@Test
public void defaultSettingsOfHttpClientMergedOnExecutorCustomization() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory(client);
assertSame("Default client configuration is expected", defaultConfig, retrieveRequestConfig(hrf));
hrf.setConnectionRequestTimeout(4567);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertNotNull(requestConfig);
assertEquals(4567, requestConfig.getConnectionRequestTimeout());
// Default connection timeout merged
assertEquals(1234, requestConfig.getConnectTimeout());
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsClientHttpRequestFactoryTests method mergeBasedOnCurrentHttpClient.
@Test
public void mergeBasedOnCurrentHttpClient() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setSocketTimeout(1234).build();
final CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsClientHttpRequestFactory hrf = new HttpComponentsClientHttpRequestFactory() {
@Override
public HttpClient getHttpClient() {
return client;
}
};
hrf.setReadTimeout(5000);
RequestConfig requestConfig = retrieveRequestConfig(hrf);
assertEquals(-1, requestConfig.getConnectTimeout());
assertEquals(-1, requestConfig.getConnectionRequestTimeout());
assertEquals(5000, requestConfig.getSocketTimeout());
// Update the Http client so that it returns an updated config
RequestConfig updatedDefaultConfig = RequestConfig.custom().setConnectTimeout(1234).build();
when(configurable.getConfig()).thenReturn(updatedDefaultConfig);
hrf.setReadTimeout(7000);
RequestConfig requestConfig2 = retrieveRequestConfig(hrf);
assertEquals(1234, requestConfig2.getConnectTimeout());
assertEquals(-1, requestConfig2.getConnectionRequestTimeout());
assertEquals(7000, requestConfig2.getSocketTimeout());
}
use of org.apache.http.client.config.RequestConfig in project openhab1-addons by openhab.
the class Tr064Comm method createTr064HttpClient.
/***
* Creates a apache HTTP Client object, ignoring SSL Exceptions like self signed certificates
* and sets Auth. Scheme to Digest Auth
*
* @param fboxUrl the URL from config file of fbox to connect to
* @return the ready-to-use httpclient for tr064 requests
*/
private CloseableHttpClient createTr064HttpClient(String fboxUrl) {
CloseableHttpClient hc = null;
// Convert URL String from config in easy explotable URI object
URIBuilder uriFbox = null;
try {
uriFbox = new URIBuilder(fboxUrl);
} catch (URISyntaxException e) {
logger.error("Invalid FritzBox URL! {}", e.getMessage());
return null;
}
// Create context of the http client
_httpClientContext = HttpClientContext.create();
CookieStore cookieStore = new BasicCookieStore();
_httpClientContext.setCookieStore(cookieStore);
// SETUP AUTH
// Auth is specific for this target
HttpHost target = new HttpHost(uriFbox.getHost(), uriFbox.getPort(), uriFbox.getScheme());
// Add digest authentication with username/pw from global config
CredentialsProvider credp = new BasicCredentialsProvider();
credp.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(_user, _pw));
// Create AuthCache instance. Manages authentication based on server response
AuthCache authCache = new BasicAuthCache();
// Generate DIGEST scheme object, initialize it and add it to the local auth cache. Digeste is standard for fbox
// auth SOAP
DigestScheme digestAuth = new DigestScheme();
// known from fbox specification
digestAuth.overrideParamter("realm", "HTTPS Access");
// never known at first request
digestAuth.overrideParamter("nonce", "");
authCache.put(target, digestAuth);
// Add AuthCache to the execution context
_httpClientContext.setAuthCache(authCache);
// SETUP SSL TRUST
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
SSLConnectionSocketFactory sslsf = null;
try {
// accept self signed certs
sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
// dont
sslsf = new SSLConnectionSocketFactory(sslContextBuilder.build(), null, null, new NoopHostnameVerifier());
// verify
// hostname
// against
// cert
// CN
} catch (Exception ex) {
logger.error(ex.getMessage());
}
// Set timeout values
RequestConfig rc = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(4000).setConnectTimeout(4000).setConnectionRequestTimeout(4000).build();
// BUILDER
// setup builder with parameters defined before
hc = // set the SSL options which trust every self signed
HttpClientBuilder.create().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(// set auth options using digest
credp).setDefaultRequestConfig(// set the request config specifying timeout
rc).build();
return hc;
}
use of org.apache.http.client.config.RequestConfig in project spring-boot by spring-projects.
the class RestTemplateBuilderTests method readTimeoutCanBeConfiguredOnHttpComponentsRequestFactory.
@Test
public void readTimeoutCanBeConfiguredOnHttpComponentsRequestFactory() {
ClientHttpRequestFactory requestFactory = this.builder.requestFactory(HttpComponentsClientHttpRequestFactory.class).setReadTimeout(1234).build().getRequestFactory();
assertThat(((RequestConfig) ReflectionTestUtils.getField(requestFactory, "requestConfig")).getSocketTimeout()).isEqualTo(1234);
}
use of org.apache.http.client.config.RequestConfig in project spring-framework by spring-projects.
the class HttpComponentsHttpInvokerRequestExecutorTests method localSettingsOverrideClientDefaultSettings.
@Test
public void localSettingsOverrideClientDefaultSettings() throws Exception {
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout(1234).setConnectionRequestTimeout(6789).build();
CloseableHttpClient client = mock(CloseableHttpClient.class, withSettings().extraInterfaces(Configurable.class));
Configurable configurable = (Configurable) client;
when(configurable.getConfig()).thenReturn(defaultConfig);
HttpComponentsHttpInvokerRequestExecutor executor = new HttpComponentsHttpInvokerRequestExecutor(client);
executor.setConnectTimeout(5000);
HttpInvokerClientConfiguration config = mockHttpInvokerClientConfiguration("http://fake-service");
HttpPost httpPost = executor.createHttpPost(config);
RequestConfig requestConfig = httpPost.getConfig();
assertEquals(5000, requestConfig.getConnectTimeout());
assertEquals(6789, requestConfig.getConnectionRequestTimeout());
assertEquals(-1, requestConfig.getSocketTimeout());
}
Aggregations