diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 8047d56cd4..d3ef5e6d69 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -1,36 +1,40 @@ - - 4.0.0 - org.baeldung - httpclient - 0.1-SNAPSHOT + + 4.0.0 + org.baeldung + httpclient + 0.1-SNAPSHOT - httpclient + httpclient - + - + - - com.google.guava - guava - ${guava.version} - + + com.google.guava + guava + ${guava.version} + - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + - + + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + + + org.apache.httpcomponents + httpcore + ${httpcore.version} + - - org.apache.httpcomponents - httpclient - ${httpclient.version} - - @@ -55,106 +59,106 @@ log4j-over-slf4j ${org.slf4j.version} - - - - junit - junit-dep - ${junit.version} - test - + - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - + + junit + junit-dep + ${junit.version} + test + - - org.mockito - mockito-core - ${mockito.version} - test - + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + - + + org.mockito + mockito-core + ${mockito.version} + test + - - httpclient - - - src/main/resources - true - - + - + + httpclient + + + src/main/resources + true + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - + - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + - + - - - 4.0.0.RELEASE - 3.2.0.RELEASE + - - 4.3.0.Final - 5.1.27 + + + 4.0.0.RELEASE + 3.2.0.RELEASE - - 1.7.5 - 1.0.11 + + 4.3.0.Final + 5.1.27 - - 5.0.1.Final + + 1.7.5 + 1.0.11 - - 15.0 - 3.1 + + 5.0.1.Final - - 1.3 - 4.11 - 1.9.5 + + 15.0 + 3.1 - 4.3 - 4.3.1 + + 1.3 + 4.11 + 1.9.5 - 2.1.0 - - - 3.1 - 2.4 - 2.16 - 2.6 - 1.4.5 + 4.3.1 + 4.3.1 - + 2.1.0 + + + 3.1 + 2.4 + 2.16 + 2.6 + 1.4.5 + + \ No newline at end of file diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java new file mode 100644 index 0000000000..d603d3414b --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java @@ -0,0 +1,95 @@ +package org.baeldung.httpclient; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.params.CoreConnectionPNames; +import org.apache.http.params.HttpConnectionParams; +import org.apache.http.params.HttpParams; +import org.junit.After; +import org.junit.Test; + +public class HttpClientTimeoutLiveTest { + + private CloseableHttpResponse response; + + @After + public final void after() throws IllegalStateException, IOException { + if (response == null) { + return; + } + + try { + final HttpEntity entity = response.getEntity(); + if (entity != null) { + final InputStream instream = entity.getContent(); + instream.close(); + } + } finally { + response.close(); + } + } + + // tests + + @SuppressWarnings("deprecation") + @Test + public final void givenUsingDeprecatedApi_whenSettingTimeoutViaRawParams_thenCorrect() throws ClientProtocolException, IOException { + final int timeout = 2; + final DefaultHttpClient client = new DefaultHttpClient(); + final HttpGet request = new HttpGet("http://www.github.com"); + + final HttpParams httpParams = client.getParams(); + httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000); + httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000); + // httpParams.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000)); // https://issues.apache.org/jira/browse/HTTPCLIENT-1418 + + response = client.execute(request); + + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + client.close(); + } + + @SuppressWarnings("deprecation") + @Test + public final void givenUsingDeprecatedApi_whenSettingTimeoutViaHigherLevelApi_thenCorrect() throws ClientProtocolException, IOException { + final int timeout = 2; + final DefaultHttpClient client = new DefaultHttpClient(); + final HttpGet request = new HttpGet("http://www.github.com"); + + final HttpParams httpParams = client.getParams(); + HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout + HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout + + response = client.execute(request); + + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + client.close(); + } + + @Test + public final void givenUsingNewApi_whenSettingTimeoutViaHighLevelApi_thenCorrect() throws ClientProtocolException, IOException { + final int timeout = 5; + + final RequestConfig.Builder requestBuilder = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000); + final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build()).build(); + + final HttpGet request = new HttpGet("http://www.github.com"); + + response = client.execute(request); + + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + } + +} diff --git a/spring-security-rest-basic-auth/pom.xml b/spring-security-rest-basic-auth/pom.xml index 08038ba23e..27e3404cff 100644 --- a/spring-security-rest-basic-auth/pom.xml +++ b/spring-security-rest-basic-auth/pom.xml @@ -294,7 +294,7 @@ 5.1.27 - 4.3 + 4.3.1 4.3.1 diff --git a/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java index e8df87524c..3eaa9e64ec 100644 --- a/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java +++ b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java @@ -2,6 +2,8 @@ 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.springframework.beans.factory.FactoryBean; @@ -51,6 +53,10 @@ public class RestTemplateFactory implements FactoryBean, Initializ // - 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)); + HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout }