diff --git a/okhttp/pom.xml b/okhttp/pom.xml deleted file mode 100644 index ba5bcf9725..0000000000 --- a/okhttp/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - 4.0.0 - org.baeldung.okhttp - okhttp - 0.0.1-SNAPSHOT - - - - - - com.squareup.okhttp3 - okhttp - ${com.squareup.okhttp3.version} - - - - - junit - junit - ${junit.version} - test - - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - - - - - - - - 3.4.1 - - - 1.3 - 4.12 - - - - diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java b/okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java deleted file mode 100644 index 829bafe8ef..0000000000 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpMiscTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.baeldung.okhttp; - -import java.io.File; -import java.io.IOException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.junit.Test; - -import okhttp3.Cache; -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -public class OkHttpMiscTest { - - private static final String BASE_URL = "http://localhost:8080/spring-rest"; - - //@Test - public void whenSetRequestTimeoutUsingOkHttp_thenFail() throws IOException { - - OkHttpClient client = new OkHttpClient.Builder() - //.connectTimeout(10, TimeUnit.SECONDS) - //.writeTimeout(10, TimeUnit.SECONDS) - .readTimeout(1, TimeUnit.SECONDS) - .build(); - - Request request = new Request.Builder() - .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. - .build(); - - Call call = client.newCall(request); - Response response = call.execute(); - response.close(); - } - - //@Test - public void whenCancelRequestUsingOkHttp_thenCorrect() throws IOException { - - ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); - - OkHttpClient client = new OkHttpClient(); - - Request request = new Request.Builder() - .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. - .build(); - - final int seconds = 1; - //final int seconds = 3; - - final long startNanos = System.nanoTime(); - final Call call = client.newCall(request); - - // Schedule a job to cancel the call in 1 second. - executor.schedule(new Runnable() { - public void run() { - - System.out.printf("%.2f Canceling call.%n", (System.nanoTime() - startNanos) / 1e9f); - call.cancel(); - System.out.printf("%.2f Canceled call.%n", (System.nanoTime() - startNanos) / 1e9f); - } - }, seconds, TimeUnit.SECONDS); - - try { - - System.out.printf("%.2f Executing call.%n", (System.nanoTime() - startNanos) / 1e9f); - Response response = call.execute(); - System.out.printf("%.2f Call was expected to fail, but completed: %s%n", (System.nanoTime() - startNanos) / 1e9f, response); - - } catch (IOException e) { - - System.out.printf("%.2f Call failed as expected: %s%n", (System.nanoTime() - startNanos) / 1e9f, e); - } - } - - @Test - public void whenSetResponseCacheUsingOkHttp_thenCorrect() throws IOException { - - int cacheSize = 10 * 1024 * 1024; // 10 MiB - File cacheDirectory = new File("src/test/resources/cache"); - Cache cache = new Cache(cacheDirectory, cacheSize); - - OkHttpClient client = new OkHttpClient.Builder() - .cache(cache) - .build(); - - Request request = new Request.Builder() - .url("http://publicobject.com/helloworld.txt") - //.cacheControl(CacheControl.FORCE_NETWORK) - //.cacheControl(CacheControl.FORCE_CACHE) - .build(); - - Response response1 = client.newCall(request).execute(); - - String responseBody1 = response1.body().string(); - - System.out.println("Response 1 response: " + response1); - System.out.println("Response 1 cache response: " + response1.cacheResponse()); - System.out.println("Response 1 network response: " + response1.networkResponse()); - System.out.println("Response 1 responseBody: " + responseBody1); - - Response response2 = client.newCall(request).execute(); - - String responseBody2 = response2.body().string(); - - System.out.println("Response 2 response: " + response2); - System.out.println("Response 2 cache response: " + response2.cacheResponse()); - System.out.println("Response 2 network response: " + response2.networkResponse()); - System.out.println("Response 2 responseBody: " + responseBody2); - } -} diff --git a/pom.xml b/pom.xml index 709c3e4e00..eee9f07ab9 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ apache-cxf autovalue-tutorial - + cdi core-java core-java-8 @@ -68,7 +68,6 @@ rest-assured rest-testing resteasy - okhttp spring-all spring-akka diff --git a/spring-rest/README.md b/spring-rest/README.md index 7d993b38b8..77d12063ed 100644 --- a/spring-rest/README.md +++ b/spring-rest/README.md @@ -5,8 +5,9 @@ ###The Course The "REST With Spring" Classes: http://bit.ly/restwithspring -### Relevant Articles: +### Relevant Articles: - [Spring @RequestMapping](http://www.baeldung.com/spring-requestmapping) - [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest) - [Redirect in Spring](http://www.baeldung.com/spring-redirect-and-forward) - [Returning Custom Status Codes from Spring Controllers](http://www.baeldung.com/spring-mvc-controller-custom-http-status-code) +- [A Guide to OkHttp](http://www.baeldung.com/guide-to-okhttp) diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 18cb1dc72a..69ab4ed361 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -15,7 +15,7 @@ - + org.springframework.boot spring-boot-starter-thymeleaf @@ -71,7 +71,7 @@ com.fasterxml.jackson.core jackson-databind - + com.fasterxml.jackson.dataformat jackson-dataformat-xml @@ -118,6 +118,14 @@ log4j-over-slf4j + + + + com.squareup.okhttp3 + okhttp + ${com.squareup.okhttp3.version} + + @@ -153,14 +161,14 @@ rest-assured ${rest-assured.version} - + com.google.protobuf protobuf-java 2.6.1 - + com.esotericsoftware kryo @@ -198,7 +206,7 @@ maven-surefire-plugin - **/*LiveTest.java + **/*LiveTest.java @@ -285,7 +293,7 @@ - + @@ -320,6 +328,9 @@ 2.19.1 1.6.0 + + 3.4.1 + diff --git a/okhttp/src/main/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java b/spring-rest/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java similarity index 100% rename from okhttp/src/main/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java rename to spring-rest/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java similarity index 89% rename from okhttp/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingTest.java rename to spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java index 32457fc11b..d1cc67b99a 100644 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java @@ -18,12 +18,12 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; -public class OkHttpFileUploadingTest { +public class OkHttpFileUploadingLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; @Test - public void whenUploadFileUsingOkHttp_thenCorrect() throws IOException { + public void whenUploadFile_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -45,7 +45,7 @@ public class OkHttpFileUploadingTest { } @Test - public void whenGetUploadFileProgressUsingOkHttp_thenCorrect() throws IOException { + public void whenGetUploadFileProgress_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpGetTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java similarity index 83% rename from okhttp/src/test/java/org/baeldung/okhttp/OkHttpGetTest.java rename to spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java index e8edff92df..9a49c8d9a2 100644 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpGetTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java @@ -14,12 +14,12 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -public class OkHttpGetTest { +public class OkHttpGetLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; @Test - public void whenGetRequestUsingOkHttp_thenCorrect() throws IOException { + public void whenGetRequest_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -34,7 +34,7 @@ public class OkHttpGetTest { } @Test - public void whenGetRequestWithQueryParameterUsingOkHttp_thenCorrect() throws IOException { + public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -54,7 +54,7 @@ public class OkHttpGetTest { } @Test - public void whenAsynchronousGetRequestUsingOkHttp_thenCorrect() { + public void whenAsynchronousGetRequest_thenCorrect() { OkHttpClient client = new OkHttpClient(); diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpHeaderTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java similarity index 80% rename from okhttp/src/test/java/org/baeldung/okhttp/OkHttpHeaderTest.java rename to spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java index 5b2e89eca8..8eddfcb135 100644 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpHeaderTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java @@ -9,12 +9,12 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -public class OkHttpHeaderTest { +public class OkHttpHeaderLiveTest { private static final String SAMPLE_URL = "http://www.github.com"; @Test - public void whenSetHeaderUsingOkHttp_thenCorrect() throws IOException { + public void whenSetHeader_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -29,7 +29,7 @@ public class OkHttpHeaderTest { } @Test - public void whenSetDefaultHeaderUsingOkHttp_thenCorrect() throws IOException { + public void whenSetDefaultHeader_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new DefaultContentTypeInterceptor("application/json")) diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java new file mode 100644 index 0000000000..c44500f4be --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java @@ -0,0 +1,107 @@ +package org.baeldung.okhttp; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import okhttp3.Cache; +import okhttp3.Call; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class OkHttpMiscLiveTest { + + private static final String BASE_URL = "http://localhost:8080/spring-rest"; + private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class); + + @Test + public void whenSetRequestTimeout_thenFail() throws IOException { + + OkHttpClient client = new OkHttpClient.Builder() + .readTimeout(1, TimeUnit.SECONDS) + .build(); + + Request request = new Request.Builder() + .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. + .build(); + + Call call = client.newCall(request); + Response response = call.execute(); + response.close(); + } + + @Test + public void whenCancelRequest_thenCorrect() throws IOException { + + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + + OkHttpClient client = new OkHttpClient(); + + Request request = new Request.Builder() + .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. + .build(); + + final int seconds = 1; + + final long startNanos = System.nanoTime(); + final Call call = client.newCall(request); + + // Schedule a job to cancel the call in 1 second. + executor.schedule(new Runnable() { + public void run() { + + logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f); + call.cancel(); + logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f); + } + }, seconds, TimeUnit.SECONDS); + + try { + + logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f); + Response response = call.execute(); + logger.debug("Call was expected to fail, but completed: " + (System.nanoTime() - startNanos) / 1e9f, response); + + } catch (IOException e) { + + logger.debug("Call failed as expected: " + (System.nanoTime() - startNanos) / 1e9f, e); + } + } + + @Test + public void whenSetResponseCache_thenCorrect() throws IOException { + + int cacheSize = 10 * 1024 * 1024; // 10 MiB + File cacheDirectory = new File("src/test/resources/cache"); + Cache cache = new Cache(cacheDirectory, cacheSize); + + OkHttpClient client = new OkHttpClient.Builder() + .cache(cache) + .build(); + + Request request = new Request.Builder() + .url("http://publicobject.com/helloworld.txt") + .build(); + + Response response1 = client.newCall(request).execute(); + logResponse(response1); + + Response response2 = client.newCall(request).execute(); + logResponse(response2); + } + + private void logResponse(Response response) throws IOException { + + logger.debug("Response response: " + response); + logger.debug("Response cache response: " + response.cacheResponse()); + logger.debug("Response network response: " + response.networkResponse()); + logger.debug("Response responseBody: " + response.body().string()); + } +} diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java similarity index 85% rename from okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java rename to spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java index 1ba2d517c5..18bd4cdcb3 100644 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpPostingTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java @@ -18,13 +18,13 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; -public class OkHttpPostingTest { +public class OkHttpPostingLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; @Test - public void whenSendPostRequestUsingOkHttp_thenCorrect() throws IOException { + public void whenSendPostRequest_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -45,7 +45,7 @@ public class OkHttpPostingTest { } @Test - public void whenSendPostRequestWithAuthorizationUsingOkHttp_thenCorrect() throws IOException { + public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException { String postBody = "test post"; @@ -64,7 +64,7 @@ public class OkHttpPostingTest { } @Test - public void whenPostJsonUsingOkHttp_thenCorrect() throws IOException { + public void whenPostJson_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); @@ -84,7 +84,7 @@ public class OkHttpPostingTest { } @Test - public void whenSendMultipartRequestUsingOkHttp_thenCorrect() throws IOException { + public void whenSendMultipartRequest_thenCorrect() throws IOException { OkHttpClient client = new OkHttpClient(); diff --git a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpRedirectTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java similarity index 80% rename from okhttp/src/test/java/org/baeldung/okhttp/OkHttpRedirectTest.java rename to spring-rest/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java index 1582a5ff7f..d568a4fdf7 100644 --- a/okhttp/src/test/java/org/baeldung/okhttp/OkHttpRedirectTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java @@ -12,10 +12,10 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -public class OkHttpRedirectTest { +public class OkHttpRedirectLiveTest { @Test - public void whenSetFollowRedirectsUsingOkHttp_thenNotRedirected() throws IOException { + public void whenSetFollowRedirects_thenNotRedirected() throws IOException { OkHttpClient client = new OkHttpClient().newBuilder() .followRedirects(false) diff --git a/okhttp/src/main/java/org/baeldung/okhttp/ProgressRequestWrapper.java b/spring-rest/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java similarity index 89% rename from okhttp/src/main/java/org/baeldung/okhttp/ProgressRequestWrapper.java rename to spring-rest/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java index 6a98f8d20a..255d10b98a 100644 --- a/okhttp/src/main/java/org/baeldung/okhttp/ProgressRequestWrapper.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java @@ -1,11 +1,16 @@ package org.baeldung.okhttp; -import okhttp3.MediaType; import okhttp3.RequestBody; -import okio.*; +import okhttp3.MediaType; import java.io.IOException; +import okio.Buffer; +import okio.BufferedSink; +import okio.ForwardingSink; +import okio.Okio; +import okio.Sink; + public class ProgressRequestWrapper extends RequestBody { protected RequestBody delegate; @@ -61,6 +66,7 @@ public class ProgressRequestWrapper extends RequestBody { } public interface ProgressListener { + void onRequestProgress(long bytesWritten, long contentLength); } diff --git a/okhttp/src/test/resources/test.txt b/spring-rest/src/test/resources/test.txt similarity index 100% rename from okhttp/src/test/resources/test.txt rename to spring-rest/src/test/resources/test.txt