author | chegar |
Tue, 16 Jan 2018 21:08:16 +0000 | |
branch | http-client-tutorial |
changeset 56018 | 40e766eb611b |
parent 56017 | fc030486080e |
permissions | -rw-r--r-- |
56013 | 1 |
/* |
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
package openjdk.http.tutorial.exercise1; |
|
25 |
||
26 |
import java.io.IOException; |
|
27 |
import java.io.UncheckedIOException; |
|
28 |
import java.net.URI; |
|
29 |
import java.nio.file.Files; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.nio.file.Paths; |
|
32 |
import java.nio.file.StandardOpenOption; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Map; |
|
35 |
import java.util.concurrent.CompletableFuture; |
|
36 |
import java.util.function.Function; |
|
37 |
||
38 |
import com.fasterxml.jackson.core.type.TypeReference; |
|
39 |
import com.fasterxml.jackson.databind.ObjectMapper; |
|
40 |
import jdk.incubator.http.HttpClient; |
|
41 |
import jdk.incubator.http.HttpRequest; |
|
42 |
import jdk.incubator.http.HttpResponse; |
|
43 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
|
44 |
||
45 |
import static java.lang.System.out; |
|
46 |
import static java.util.stream.Collectors.joining; |
|
47 |
import static jdk.incubator.http.HttpClient.Version.*; |
|
48 |
import static jdk.incubator.http.HttpResponse.BodyHandler.*; |
|
49 |
||
50 |
/** |
|
56017 | 51 |
* The JDK HTTP Client tutorial. |
52 |
* |
|
53 |
* Consists of a set of exercises that must be completed. Each exercise is |
|
54 |
* represented as a method, whose method body must be written successfully to |
|
55 |
* complete the exercise. Each method-level comment describes the particular |
|
56 |
* exercise, what must be done, and may provide a hint to help complete it. |
|
57 |
* |
|
58 |
* The test source tree contains `Exercise1Test` that contains a set of unit |
|
59 |
* tests, similarly named to the to the methods in this class, that, when |
|
60 |
* executed, verify that the exercises have been completed successfully ( by |
|
61 |
* checking the value returned by each exercise ). These can be run through the |
|
62 |
* Maven Project's `test` target. |
|
63 |
* |
|
64 |
* |
|
56013 | 65 |
* @author Chris Hegarty |
66 |
*/ |
|
67 |
public class Retrievals { |
|
68 |
||
69 |
/** |
|
70 |
* Exercise 1. |
|
71 |
* |
|
72 |
* Retrieve the response status code from a request to the given |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
73 |
* URI. Return the response code, which is an integer. |
56013 | 74 |
* |
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
75 |
* Hint: use the {@link BodyHandler#discard(Object)BodyHandler} since |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
76 |
* the response body is not interesting. |
56013 | 77 |
* |
78 |
* Hint: static imports reduce boilerplate when using BodyHandlers |
|
79 |
* and BodyProcessors, e.g. import static |
|
80 |
* jdk.incubator.http.HttpResponse.BodyHandler.discard |
|
81 |
*/ |
|
82 |
public static int retrieveTheStatusCode(URI uri) |
|
83 |
throws IOException, InterruptedException |
|
84 |
{ |
|
85 |
HttpClient client = HttpClient.newBuilder().version(HTTP_1_1).build(); |
|
86 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
87 |
HttpResponse<?> response = client.send(request, discard(null)); |
|
88 |
||
89 |
return response.statusCode(); |
|
90 |
} |
|
91 |
||
92 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
93 |
* Exercise 2. |
56013 | 94 |
* |
95 |
* Retrieve the response body from a given URI. Return the response |
|
96 |
* body as a String. |
|
97 |
* |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
98 |
* Hint: use the {@link BodyHandler#asString()} BodyHandler to convert |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
99 |
* the HTTP response body to a String. |
56013 | 100 |
* |
101 |
* Hint: static imports reduce boilerplate when using BodyHandlers |
|
102 |
* and BodyProcessors, e.g. import static |
|
103 |
* jdk.incubator.http.HttpResponse.BodyHandler.asString |
|
104 |
*/ |
|
105 |
public static String retrieveResourceAsString(URI uri) |
|
106 |
throws IOException, InterruptedException |
|
107 |
{ |
|
108 |
HttpClient client = HttpClient.newBuilder().version(HTTP_1_1).build(); |
|
109 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
|
110 |
HttpResponse<String> response = client.send(request, asString()); |
|
111 |
||
112 |
return response.body(); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
116 |
* Exercise 3. |
56013 | 117 |
* |
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
118 |
* Retrieve the response body from a given URI, streaming the body |
56013 | 119 |
* out to a file. Return the file's Path. |
120 |
* |
|
121 |
* Hint: use {@linkplain BodyHandler#asFile} to stream the HTTP |
|
122 |
* response body to a file. |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
123 |
* |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
124 |
* Hint: if a file already exists from a previous test run, either |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
125 |
* remove it or use the {@link java.nio.file.StandardOpenOption#CREATE} |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
126 |
* along with the {@link java.nio.file.StandardOpenOption#TRUNCATE_EXISTING} |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
127 |
* to create or truncate as needed. |
56013 | 128 |
*/ |
129 |
public static Path retrieveResourceAsFile(URI uri) |
|
130 |
throws IOException, InterruptedException |
|
131 |
{ |
|
132 |
HttpClient client = HttpClient.newBuilder().build(); |
|
133 |
HttpRequest request = HttpRequest.newBuilder() |
|
134 |
.uri(uri) |
|
135 |
.version(HTTP_1_1) |
|
136 |
.GET() |
|
137 |
.build(); |
|
138 |
HttpResponse<Path> response = client.send(request, |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
139 |
asFile(Paths.get("retrieveResourceAsFile.txt"), |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
140 |
StandardOpenOption.CREATE, |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
141 |
StandardOpenOption.TRUNCATE_EXISTING, |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
142 |
StandardOpenOption.WRITE)); |
56013 | 143 |
return response.body(); |
144 |
} |
|
145 |
||
146 |
||
147 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
148 |
* A helper method, NOT an exercise. |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
149 |
* |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
150 |
* Asserts that the response code is 200 ( OK ). Throws IOException if |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
151 |
* the response code is not 200. |
56013 | 152 |
* |
153 |
* Can be used in CompletableFuture pipelines when checking the |
|
154 |
* response of an {@linkplain HttpClient#sendAsync} call. For |
|
155 |
* example: |
|
156 |
* client.sendAsync(request, bodyHandler) |
|
157 |
* .thenApply(Retrievals::require200StatusCode) |
|
158 |
* .thenApply(...) |
|
159 |
* |
|
160 |
*/ |
|
161 |
public static <T> HttpResponse<T> require200StatusCode(HttpResponse<T> response) { |
|
162 |
int sc = response.statusCode(); |
|
163 |
if (sc != 200) { |
|
164 |
IOException e = new IOException("Expected 200, got: " + sc); |
|
165 |
throw new UncheckedIOException(e); |
|
166 |
} |
|
167 |
return response; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
171 |
* Exercise 4. |
56013 | 172 |
* |
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
173 |
* Retrieve the response body from a given URI, using thea synchronous |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
174 |
* send API, {@link HttpClient#sendAsync(HttpRequest, BodyHandler)}. |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
175 |
* Return a CompletableFuture that completes with the response body |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
176 |
* as a String. |
56013 | 177 |
* |
178 |
* Hint: The {@linkplain CompletableFuture#thenApply(Function)} |
|
179 |
* method can be used to map the HttpResponse to a String. |
|
180 |
*/ |
|
181 |
public static CompletableFuture<String> retrieveResourceAsStringUsingAsyncAPI(URI uri) { |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
182 |
// TODO: why version needed? |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
183 |
|
56013 | 184 |
return HttpClient.newHttpClient() |
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
185 |
.sendAsync(HttpRequest.newBuilder(uri).version(HTTP_1_1).build(), asString()) |
56013 | 186 |
.thenApply(Retrievals::require200StatusCode) |
187 |
.thenApply(HttpResponse::body); |
|
188 |
} |
|
189 |
||
190 |
||
191 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
192 |
* A helper method, NOT an exercise. |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
193 |
* |
56013 | 194 |
* Wrapper around Jackson's ObjectMapper that provides an unchecked |
195 |
* {@code readValue}, what can be used to help solve the next |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
196 |
* exercise, 5. |
56013 | 197 |
*/ |
198 |
public static class UncheckedObjectMapper extends ObjectMapper { |
|
199 |
||
200 |
/** Parses the given JSON string into a Map. */ |
|
201 |
Map<String,String> readValue(String content) { |
|
202 |
try { |
|
203 |
return this.readValue(content, new TypeReference<>(){}); |
|
204 |
} catch (IOException ioe) { |
|
205 |
throw new UncheckedIOException(ioe); |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
211 |
* Exercise 5. |
56013 | 212 |
* |
213 |
* Retrieve the response body from a given URI. The response body |
|
214 |
* will be in the JSON format. The Jackson based UncheckedObjectMapper |
|
215 |
* ( above ) can be used to parse the String response body into a |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
216 |
* Map. Return the response body as a Map. |
56013 | 217 |
* |
218 |
* Hint: The asynchronous send API will allow construction of a |
|
219 |
* pipeline of CompletableFutures. |
|
220 |
* |
|
221 |
* Hint: The {@linkplain CompletableFuture#thenApply(Function)} |
|
222 |
* method can be used to map the HttpResponse to a String, and then |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
223 |
* again from a String ( of JSON ) to a Map ( via the object mapper ). |
56013 | 224 |
*/ |
225 |
public CompletableFuture<Map<String,String>> JSONBodyAsMap(URI uri) { |
|
226 |
UncheckedObjectMapper objectMapper = new UncheckedObjectMapper(); |
|
227 |
return HttpClient.newHttpClient() |
|
228 |
.sendAsync(HttpRequest.newBuilder(uri).version(HTTP_1_1).build(), asString()) |
|
229 |
.thenApply(HttpResponse::body) |
|
230 |
.thenApply(objectMapper::readValue); |
|
231 |
} |
|
232 |
||
233 |
||
234 |
||
235 |
/** |
|
56018
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
236 |
* Exercise 6. |
40e766eb611b
http-client-tutorial: additional informational comments and fixes
chegar
parents:
56017
diff
changeset
|
237 |
* |
56013 | 238 |
* Post the given {@code data}, and receive the same data in |
239 |
* response. Return the response body data as a String. |
|
240 |
*/ |
|
241 |
public static String postData(URI uri, String data) |
|
242 |
throws IOException, InterruptedException |
|
243 |
{ |
|
244 |
HttpClient client = HttpClient.newBuilder().build(); |
|
245 |
HttpRequest request = HttpRequest.newBuilder() |
|
246 |
.uri(uri) |
|
247 |
.version(HTTP_1_1) |
|
248 |
.POST(HttpRequest.BodyProcessor.fromString(data)) |
|
249 |
.build(); |
|
250 |
HttpResponse<String> response = client.send(request, asString()); |
|
251 |
||
252 |
return response.body(); |
|
253 |
} |
|
254 |
||
255 |
} |