From fb552a1a814f0ca401f8ee738a215794768d109b Mon Sep 17 00:00:00 2001 From: Rafael Lopez Date: Fri, 1 Oct 2021 14:21:15 -0400 Subject: [PATCH] JAVA-6406: Update 'Exploring the New HTTP Client in Java' article --- .../core-java-9-new-features/README.md | 1 - .../httpclient/HttpClientExample.java | 84 ------- .../com.baeldung.httpclient/module-info.java | 3 - .../httpclient/HttpClientIntegrationTest.java | 218 ------------------ .../HttpRequestIntegrationTest.java | 171 -------------- .../HttpResponseIntegrationTest.java | 55 ----- 6 files changed, 532 deletions(-) delete mode 100644 core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java delete mode 100644 core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java delete mode 100644 core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java diff --git a/core-java-modules/core-java-9-new-features/README.md b/core-java-modules/core-java-9-new-features/README.md index 4045a37d9d..09890bb677 100644 --- a/core-java-modules/core-java-9-new-features/README.md +++ b/core-java-modules/core-java-9-new-features/README.md @@ -6,7 +6,6 @@ This module contains articles about core Java features that have been introduced - [New Features in Java 9](https://www.baeldung.com/new-java-9) - [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles) -- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client) - [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar) - [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation) - [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api) diff --git a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java b/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java deleted file mode 100644 index de08c2164e..0000000000 --- a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.baeldung.httpclient; - -import java.io.File; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpRequest.BodyProcessor; -import jdk.incubator.http.HttpResponse; -import jdk.incubator.http.HttpResponse.BodyHandler; - -/** - * - * @author pkaria - */ -public class HttpClientExample { - - public static void main(String[] args) throws Exception { - httpGetRequest(); - httpPostRequest(); - asynchronousRequest(); - asynchronousMultipleRequests(); - } - - public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException { - HttpClient client = HttpClient.newHttpClient(); - URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); - HttpRequest request = HttpRequest.newBuilder(httpURI).GET() - .headers("Accept-Enconding", "gzip, deflate").build(); - HttpResponse response = client.send(request, HttpResponse.BodyHandler.asString()); - String responseBody = response.body(); - int responseStatusCode = response.statusCode(); - System.out.println(responseBody); - } - - public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException { - HttpClient client = HttpClient - .newBuilder() - .build(); - HttpRequest request = HttpRequest - .newBuilder(new URI("http://jsonplaceholder.typicode.com/posts")) - .POST(BodyProcessor.fromString("Sample Post Request")) - .build(); - HttpResponse response - = client.send(request, HttpResponse.BodyHandler.asString()); - String responseBody = response.body(); - System.out.println(responseBody); - } - - public static void asynchronousRequest() throws URISyntaxException { - HttpClient client = HttpClient.newHttpClient(); - URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1"); - HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); - CompletableFuture> futureResponse = client.sendAsync(request, - HttpResponse.BodyHandler.asString()); - } - - public static void asynchronousMultipleRequests() throws URISyntaxException { - List targets = Arrays.asList(new URI("http://jsonplaceholder.typicode.com/posts/1"), new URI("http://jsonplaceholder.typicode.com/posts/2")); - HttpClient client = HttpClient.newHttpClient(); - List> futures = targets - .stream() - .map(target -> client - .sendAsync( - HttpRequest.newBuilder(target) - .GET() - .build(), - BodyHandler.asFile(Paths.get("base", target.getPath()))) - .thenApply(response -> response.body()) - .thenApply(path -> path.toFile())) - .collect(Collectors.toList()); - } -} diff --git a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java b/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java deleted file mode 100644 index 205c9ea725..0000000000 --- a/core-java-modules/core-java-9-new-features/src/modules/com.baeldung.httpclient/module-info.java +++ /dev/null @@ -1,3 +0,0 @@ -module com.baeldung.httpclient { - requires jdk.incubator.httpclient; -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java deleted file mode 100644 index fc59ae8d8d..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java +++ /dev/null @@ -1,218 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.IOException; -import java.net.*; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.stream.Collectors; - -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.collection.IsEmptyCollection.empty; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpClientIntegrationTest { - - @Test - public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample body")) - .build(); - - HttpResponse response = HttpClient.newBuilder() - .proxy(ProxySelector.getDefault()) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample body")); - } - - @Test - public void shouldNotFollowRedirectWhenSetToDefaultNever() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); - assertThat(response.body(), containsString("")); - } - - @Test - public void shouldFollowRedirectWhenSetToAlways() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.finalRequest() - .uri() - .toString(), equalTo("https://stackoverflow.com/")); - } - - @Test - public void shouldReturnOKStatusForAuthenticatedAccess() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/basic-auth")) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .authenticator(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("postman", "password".toCharArray()); - } - }) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldSendRequestAsync() throws URISyntaxException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample body")) - .build(); - CompletableFuture> response = HttpClient.newBuilder() - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldUseJustTwoThreadWhenProcessingSendAsyncRequest() throws URISyntaxException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - ExecutorService executorService = Executors.newFixedThreadPool(2); - - CompletableFuture> response1 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture> response2 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture> response3 = HttpClient.newBuilder() - .executor(executorService) - .build() - .sendAsync(request, HttpResponse.BodyHandler.asString()); - - CompletableFuture.allOf(response1, response2, response3) - .join(); - - assertThat(response1.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response2.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response3.get() - .statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldNotStoreCookieWhenPolicyAcceptNone() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpClient httpClient = HttpClient.newBuilder() - .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_NONE)) - .build(); - - httpClient.send(request, HttpResponse.BodyHandler.asString()); - - assertThat(httpClient.cookieManager() - .get() - .getCookieStore() - .getCookies(), empty()); - } - - @Test - public void shouldStoreCookieWhenPolicyAcceptAll() throws URISyntaxException, IOException, InterruptedException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpClient httpClient = HttpClient.newBuilder() - .cookieManager(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) - .build(); - - httpClient.send(request, HttpResponse.BodyHandler.asString()); - - assertThat(httpClient.cookieManager() - .get() - .getCookieStore() - .getCookies(), not(empty())); - } - - @Test - public void shouldProcessMultipleRequestViaStream() throws URISyntaxException, ExecutionException, InterruptedException { - List targets = Arrays.asList(new URI("https://postman-echo.com/get?foo1=bar1"), new URI("https://postman-echo.com/get?foo2=bar2")); - - HttpClient client = HttpClient.newHttpClient(); - - List> futures = targets.stream() - .map(target -> client.sendAsync(HttpRequest.newBuilder(target) - .GET() - .build(), HttpResponse.BodyHandler.asString()) - .thenApply(response -> response.body())) - .collect(Collectors.toList()); - - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) - .join(); - - if (futures.get(0) - .get() - .contains("foo1")) { - assertThat(futures.get(0) - .get(), containsString("bar1")); - assertThat(futures.get(1) - .get(), containsString("bar2")); - } else { - assertThat(futures.get(1) - .get(), containsString("bar2")); - assertThat(futures.get(1) - .get(), containsString("bar1")); - } - - } - -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java deleted file mode 100644 index 17af7bd8ba..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Paths; -import java.security.NoSuchAlgorithmException; -import java.time.Duration; - -import static java.time.temporal.ChronoUnit.SECONDS; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpRequestIntegrationTest { - - @Test - public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldUseHttp2WhenWebsiteUsesHttp2() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://stackoverflow.com")) - .version(HttpClient.Version.HTTP_2) - .GET() - .build(); - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.version(), equalTo(HttpClient.Version.HTTP_2)); - } - - @Test - public void shouldFallbackToHttp1_1WhenWebsiteDoesNotUseHttp2() throws IOException, InterruptedException, URISyntaxException, NoSuchAlgorithmException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .version(HttpClient.Version.HTTP_2) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.version(), equalTo(HttpClient.Version.HTTP_1_1)); - } - - @Test - public void shouldReturnStatusOKWhenSendGetRequestWithDummyHeaders() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .headers("key1", "value1", "key2", "value2") - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnStatusOKWhenSendGetRequestTimeoutSet() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .timeout(Duration.of(10, SECONDS)) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnNoContentWhenPostWithNoBody() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .POST(HttpRequest.noBody()) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithBodyText() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromString("Sample request body")) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithInputStream() throws IOException, InterruptedException, URISyntaxException { - byte[] sampleData = "Sample request body".getBytes(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromInputStream(() -> new ByteArrayInputStream(sampleData))) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithByteArrayProcessorStream() throws IOException, InterruptedException, URISyntaxException { - byte[] sampleData = "Sample request body".getBytes(); - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromByteArray(sampleData)) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample request body")); - } - - @Test - public void shouldReturnSampleDataContentWhenPostWithFileProcessorStream() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/post")) - .headers("Content-Type", "text/plain;charset=UTF-8") - .POST(HttpRequest.BodyProcessor.fromFile(Paths.get("src/test/resources/sample.txt"))) - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), containsString("Sample file content")); - } - -} diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java deleted file mode 100644 index 5c6f9c8a52..0000000000 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.java9.httpclient; - -import jdk.incubator.http.HttpClient; -import jdk.incubator.http.HttpRequest; -import jdk.incubator.http.HttpResponse; -import org.junit.Test; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URI; -import java.net.URISyntaxException; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.Matchers.isEmptyString; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; - -/** - * Created by adam. - */ -public class HttpResponseIntegrationTest { - - @Test - public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("https://postman-echo.com/get")) - .GET() - .build(); - - HttpResponse response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_OK)); - assertThat(response.body(), not(isEmptyString())); - } - - @Test - public void shouldResponseURIDifferentThanRequestUIRWhenRedirect() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI("http://stackoverflow.com")) - .version(HttpClient.Version.HTTP_1_1) - .GET() - .build(); - HttpResponse response = HttpClient.newBuilder() - .followRedirects(HttpClient.Redirect.ALWAYS) - .build() - .send(request, HttpResponse.BodyHandler.asString()); - - assertThat(request.uri() - .toString(), equalTo("http://stackoverflow.com")); - assertThat(response.uri() - .toString(), equalTo("https://stackoverflow.com/")); - } - -}