httpclient updates
This commit is contained in:
parent
0552363098
commit
c5ddb0b5ac
|
@ -1,42 +0,0 @@
|
|||
package org.baeldung.client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
|
||||
public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
|
||||
HttpHost host;
|
||||
|
||||
public HttpComponentsClientHttpRequestFactoryBasicAuth(final HttpHost host) {
|
||||
super();
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
protected HttpContext createHttpContext(final HttpMethod httpMethod, final URI uri) {
|
||||
return createHttpContext();
|
||||
}
|
||||
|
||||
private HttpContext createHttpContext() {
|
||||
// Create AuthCache instance
|
||||
final AuthCache authCache = new BasicAuthCache();
|
||||
// Generate BASIC scheme object and add it to the local auth cache
|
||||
final BasicScheme basicAuth = new BasicScheme();
|
||||
authCache.put(host, basicAuth);
|
||||
// Add AuthCache to the execution context
|
||||
final BasicHttpContext localcontext = new BasicHttpContext();
|
||||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
|
||||
return localcontext;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +1,15 @@
|
|||
package org.baeldung.client;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.params.ClientPNames;
|
||||
import org.apache.http.params.CoreConnectionPNames;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
@ -38,27 +40,16 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
|
|||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
||||
final HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
|
||||
restTemplate = new RestTemplate(requestFactory);
|
||||
|
||||
final int timeout = 5;
|
||||
final HttpClient httpClient = requestFactory.getHttpClient();
|
||||
// - note: timeout via raw String parameters
|
||||
// httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000);
|
||||
// httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000);
|
||||
|
||||
// httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
|
||||
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
|
||||
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
|
||||
|
||||
// - note: timeout via the API
|
||||
final HttpParams httpParams = httpClient.getParams();
|
||||
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
|
||||
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
|
||||
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
|
||||
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build();
|
||||
|
||||
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
|
||||
restTemplate = new RestTemplate(requestFactory);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,17 +2,16 @@ package org.baeldung.client;
|
|||
|
||||
import static org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.conn.ssl.TrustStrategy;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.baeldung.client.spring.ClientConfig;
|
||||
import org.baeldung.web.dto.Foo;
|
||||
import org.junit.Ignore;
|
||||
|
@ -38,13 +37,14 @@ public class ClientLiveTest {
|
|||
// tests
|
||||
|
||||
@Test
|
||||
public final void whenSecuredRestApiIsConsumed_then200OK() {
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) secureRestTemplate.getRequestFactory();
|
||||
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
|
||||
httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user", "userPass"));
|
||||
public final void whenContextIsBootstrapped_thenNoExceptions() {
|
||||
//
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSecuredRestApiIsConsumed_then200OK() {
|
||||
final ResponseEntity<Foo> responseEntity = secureRestTemplate.exchange("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1", HttpMethod.GET, null, Foo.class);
|
||||
System.out.println(responseEntity.getStatusCode());
|
||||
assertThat(responseEntity.getStatusCode().value(), is(200));
|
||||
}
|
||||
|
||||
@Test(expected = ResourceAccessException.class)
|
||||
|
@ -58,7 +58,7 @@ public class ClientLiveTest {
|
|||
@Ignore("Only to run against a Server with HTTPS enabled (on 8443)")
|
||||
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenException() throws GeneralSecurityException {
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
|
||||
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
|
||||
final CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();
|
||||
|
||||
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
|
||||
@Override
|
||||
|
|
|
@ -35,12 +35,12 @@ public class RawClientLiveTest {
|
|||
public final void whenSecuredRestApiIsConsumed_then200OK() throws ClientProtocolException, IOException {
|
||||
final CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
||||
|
||||
final int timeout = 20; // seconds
|
||||
final int timeout = 30; // seconds
|
||||
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setConnectTimeout(timeout).setSocketTimeout(timeout).build();
|
||||
final HttpGet getMethod = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/bars/1");
|
||||
getMethod.setConfig(requestConfig);
|
||||
|
||||
final int hardTimeout = 5; // seconds
|
||||
final int hardTimeout = 10; // seconds
|
||||
final TimerTask task = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
|
Loading…
Reference in New Issue