From 1cdfa2e2d0817d65bdb58e8591149abb9f2a1d2a Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 21 Apr 2019 20:29:58 +0530 Subject: [PATCH 1/2] [BAEL-13598] - Updated HttpClient Timeout article. moved code --- httpclient-simple/README.md | 3 ++- .../httpclient/HttpClientTimeoutLiveTest.java | 14 +++++++------- spring-security-rest-basic-auth/README.md | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) rename {httpclient => httpclient-simple}/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java (96%) diff --git a/httpclient-simple/README.md b/httpclient-simple/README.md index d1224359e0..b4d8d61993 100644 --- a/httpclient-simple/README.md +++ b/httpclient-simple/README.md @@ -8,4 +8,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring ### Relevant Articles: - [HttpClient 4 – Get the Status Code](http://www.baeldung.com/httpclient-status-code) -- [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl) \ No newline at end of file +- [HttpClient with SSL](http://www.baeldung.com/httpclient-ssl) +- [HttpClient Timeout](http://www.baeldung.com/httpclient-timeout) \ No newline at end of file diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java similarity index 96% rename from httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java rename to httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java index 74255e416c..9d14a20e22 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java +++ b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java @@ -1,20 +1,20 @@ package org.baeldung.httpclient; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + 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.config.SocketConfig; -import org.apache.http.conn.HttpHostConnectException; +import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.junit.After; import org.junit.Test; -import java.io.IOException; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - public class HttpClientTimeoutLiveTest { private CloseableHttpResponse response; @@ -71,7 +71,7 @@ public class HttpClientTimeoutLiveTest { /** * This simulates a timeout against a domain with multiple routes/IPs to it (not a single raw IP) */ - @Test(expected = HttpHostConnectException.class) + @Test(expected = ConnectTimeoutException.class) public final void givenTimeoutIsConfigured_whenTimingOut_thenTimeoutException() throws IOException { final int timeout = 3; diff --git a/spring-security-rest-basic-auth/README.md b/spring-security-rest-basic-auth/README.md index 0a48a747c2..b905824161 100644 --- a/spring-security-rest-basic-auth/README.md +++ b/spring-security-rest-basic-auth/README.md @@ -7,6 +7,5 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: - [Basic Authentication with the RestTemplate](http://www.baeldung.com/how-to-use-resttemplate-with-basic-authentication-in-spring) -- [HttpClient Timeout](http://www.baeldung.com/httpclient-timeout) - [A Custom Filter in the Spring Security Filter Chain](http://www.baeldung.com/spring-security-custom-filter) - [Spring Security Basic Authentication](http://www.baeldung.com/spring-security-basic-authentication) From e3a40972fe8c267a676d62ffdec5096c57bf59fc Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 28 Apr 2019 16:31:11 +0530 Subject: [PATCH 2/2] [BAEL-13598] - Added old API, moved TimerTask Junit to httpclient-simple --- .../httpclient/HttpClientTimeoutLiveTest.java | 46 ++++++++++++++++++- .../baeldung/client/RawClientLiveTest.java | 22 +-------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java index 9d14a20e22..8041080b3d 100644 --- a/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java +++ b/httpclient-simple/src/test/java/org/baeldung/httpclient/HttpClientTimeoutLiveTest.java @@ -4,14 +4,21 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; +import org.apache.http.HttpResponse; 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.client.params.ClientPNames; import org.apache.http.config.SocketConfig; import org.apache.http.conn.ConnectTimeoutException; 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.HttpParams; import org.junit.After; import org.junit.Test; @@ -25,6 +32,20 @@ public class HttpClientTimeoutLiveTest { } // tests + @Test + public final void givenUsingOldApi_whenSettingTimeoutViaParameter_thenCorrect() throws IOException { + + DefaultHttpClient httpClient = new DefaultHttpClient(); + int timeout = 5; // seconds + 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 HttpGet request = new HttpGet("http://www.github.com"); + HttpResponse execute = httpClient.execute(request); + assertThat(execute.getStatusLine().getStatusCode(), equalTo(200)); + } @Test public final void givenUsingNewApi_whenSettingTimeoutViaRequestConfig_thenCorrect() throws IOException { @@ -33,8 +54,6 @@ public class HttpClientTimeoutLiveTest { final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); final HttpGet request = new HttpGet("http://www.github.com"); - // 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)); @@ -81,5 +100,28 @@ public class HttpClientTimeoutLiveTest { final HttpGet request = new HttpGet("http://www.google.com:81"); client.execute(request); } + + @Test + public void whenSecuredRestApiIsConsumed_then200OK() throws IOException { + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + int timeout = 20; // seconds + RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000) + .setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); + HttpGet getMethod = new HttpGet("http://localhost:8082/httpclient-simple/api/bars/1"); + getMethod.setConfig(requestConfig); + + int hardTimeout = 5; // seconds + TimerTask task = new TimerTask() { + @Override + public void run() { + getMethod.abort(); + } + }; + new Timer(true).schedule(task, hardTimeout * 1000); + + HttpResponse response = httpClient.execute(getMethod); + System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); + } + } diff --git a/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java b/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java index 90c2a29968..e8dcf82ebc 100644 --- a/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java +++ b/spring-security-mvc-digest-auth/src/test/java/org/baeldung/client/RawClientLiveTest.java @@ -1,7 +1,8 @@ package org.baeldung.client; +import java.io.IOException; + import org.apache.http.HttpResponse; -import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -12,10 +13,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; -import java.io.IOException; -import java.util.Timer; -import java.util.TimerTask; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {ClientConfig.class}, loader = AnnotationConfigContextLoader.class) public class RawClientLiveTest { @@ -25,22 +22,7 @@ public class RawClientLiveTest { @Test public void whenSecuredRestApiIsConsumed_then200OK() throws IOException { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); - - int timeout = 20; // seconds - RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout) - .setConnectTimeout(timeout).setSocketTimeout(timeout).build(); HttpGet getMethod = new HttpGet("http://localhost:8082/spring-security-rest-basic-auth/api/bars/1"); - getMethod.setConfig(requestConfig); - - int hardTimeout = 5; // seconds - TimerTask task = new TimerTask() { - @Override - public void run() { - getMethod.abort(); - } - }; - new Timer(true).schedule(task, hardTimeout * 1000); - HttpResponse response = httpClient.execute(getMethod); System.out.println("HTTP Status of response: " + response.getStatusLine().getStatusCode()); }