1
1
/*
2
- * Copyright (c) 2015, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
77
77
*
78
78
* <p> The following is an example of retrieving a response as a String:
79
79
*
80
- * <pre>{@code HttpResponse<String> response = client
81
- * .send(request, BodyHandlers.ofString()); }</pre>
80
+ * {@snippet :
81
+ * HttpResponse<String> response = client
82
+ * .send(request, BodyHandlers.ofString()); }
82
83
*
83
84
* <p> The class {@link BodyHandlers BodyHandlers} provides implementations
84
85
* of many common response handlers. Alternatively, a custom {@code BodyHandler}
@@ -211,28 +212,31 @@ public interface ResponseInfo {
211
212
* predefined body handlers} that always process the response body in the
212
213
* same way ( streams the response body to a file ).
213
214
*
214
- * <pre>{@code HttpRequest request = HttpRequest.newBuilder()
215
+ * {@snippet :
216
+ * HttpRequest request = HttpRequest.newBuilder()
215
217
* .uri(URI.create("http://www.foo.com/"))
216
218
* .build();
219
+ *
217
220
* client.sendAsync(request, BodyHandlers.ofFile(Paths.get("/tmp/f")))
218
221
* .thenApply(HttpResponse::body)
219
- * .thenAccept(System.out::println); }</pre>
222
+ * .thenAccept(System.out::println); }
220
223
*
221
224
* Note, that even though the pre-defined handlers do not examine the
222
225
* response code, the response code and headers are always retrievable from
223
226
* the {@link HttpResponse}, when it is returned.
224
227
*
225
228
* <p> In the second example, the function returns a different subscriber
226
229
* depending on the status code.
227
- * <pre>{@code HttpRequest request = HttpRequest.newBuilder()
230
+ * {@snippet :
231
+ * HttpRequest request = HttpRequest.newBuilder()
228
232
* .uri(URI.create("http://www.foo.com/"))
229
233
* .build();
230
234
* BodyHandler<Path> bodyHandler = (rspInfo) -> rspInfo.statusCode() == 200
231
235
* ? BodySubscribers.ofFile(Paths.get("/tmp/f"))
232
236
* : BodySubscribers.replacing(Paths.get("/NULL"));
233
237
* client.sendAsync(request, bodyHandler)
234
238
* .thenApply(HttpResponse::body)
235
- * .thenAccept(System.out::println); }</pre>
239
+ * .thenAccept(System.out::println); }
236
240
*
237
241
* @param <T> the response body type
238
242
* @see BodyHandlers
@@ -272,21 +276,25 @@ public interface BodyHandler<T> {
272
276
* <p>The following are examples of using the predefined body handlers to
273
277
* convert a flow of response body data into common high-level Java objects:
274
278
*
275
- * <pre>{@code // Receives the response body as a String
279
+ * {@snippet :
280
+ * // Receives the response body as a String
276
281
* HttpResponse<String> response = client
277
- * .send(request, BodyHandlers.ofString());
282
+ * .send(request, BodyHandlers.ofString()); }
278
283
*
284
+ * {@snippet :
279
285
* // Receives the response body as a file
280
286
* HttpResponse<Path> response = client
281
- * .send(request, BodyHandlers.ofFile(Paths.get("example.html")));
287
+ * .send(request, BodyHandlers.ofFile(Paths.get("example.html"))); }
282
288
*
289
+ * {@snippet :
283
290
* // Receives the response body as an InputStream
284
291
* HttpResponse<InputStream> response = client
285
- * .send(request, BodyHandlers.ofInputStream());
292
+ * .send(request, BodyHandlers.ofInputStream()); }
286
293
*
294
+ * {@snippet :
287
295
* // Discards the response body
288
296
* HttpResponse<Void> response = client
289
- * .send(request, BodyHandlers.discarding()); }</pre>
297
+ * .send(request, BodyHandlers.discarding()); }
290
298
*
291
299
* @since 11
292
300
*/
@@ -310,10 +318,11 @@ private BodyHandlers() { }
310
318
* BodySubscriber} and {@code Flow.Subscriber}.
311
319
*
312
320
* <p> For example:
313
- * <pre> {@code TextSubscriber subscriber = new TextSubscriber();
321
+ * {@snippet :
322
+ * TextSubscriber subscriber = new TextSubscriber();
314
323
* HttpResponse<Void> response = client.sendAsync(request,
315
324
* BodyHandlers.fromSubscriber(subscriber)).join();
316
- * System.out.println(response.statusCode()); }</pre>
325
+ * System.out.println(response.statusCode()); }
317
326
*
318
327
* @param subscriber the subscriber
319
328
* @return a response body handler
@@ -340,10 +349,11 @@ private BodyHandlers() { }
340
349
* BodySubscriber} and {@code Flow.Subscriber}.
341
350
*
342
351
* <p> For example:
343
- * <pre> {@code TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String
352
+ * {@snippet :
353
+ * TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String
344
354
* HttpResponse<String> response = client.sendAsync(request,
345
355
* BodyHandlers.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join();
346
- * String text = response.body(); }</pre>
356
+ * String text = response.body(); }
347
357
*
348
358
* @param <S> the type of the Subscriber
349
359
* @param <T> the type of the response body
@@ -380,7 +390,8 @@ private BodyHandlers() { }
380
390
* text line by line.
381
391
*
382
392
* <p> For example:
383
- * <pre> {@code // A PrintSubscriber that implements Flow.Subscriber<String>
393
+ * {@snippet :
394
+ * // A PrintSubscriber that implements Flow.Subscriber<String>
384
395
* // and print lines received by onNext() on System.out
385
396
* PrintSubscriber subscriber = new PrintSubscriber(System.out);
386
397
* client.sendAsync(request, BodyHandlers.fromLineSubscriber(subscriber))
@@ -389,7 +400,7 @@ private BodyHandlers() { }
389
400
* if (status != 200) {
390
401
* System.err.printf("ERROR: %d status received%n", status);
391
402
* }
392
- * }); }</pre>
403
+ * }); }
393
404
*
394
405
* @param subscriber the subscriber
395
406
* @return a response body handler
@@ -423,15 +434,16 @@ private BodyHandlers() { }
423
434
* text line by line.
424
435
*
425
436
* <p> For example:
426
- * <pre> {@code // A LineParserSubscriber that implements Flow.Subscriber<String>
437
+ * {@snippet :
438
+ * // A LineParserSubscriber that implements Flow.Subscriber<String>
427
439
* // and accumulates lines that match a particular pattern
428
440
* Pattern pattern = ...;
429
441
* LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
430
442
* HttpResponse<List<String>> response = client.send(request,
431
443
* BodyHandlers.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
432
444
* if (response.statusCode() != 200) {
433
445
* System.err.printf("ERROR: %d status received%n", response.statusCode());
434
- * } }</pre>
446
+ * } }
435
447
*
436
448
*
437
449
* @param <S> the type of the Subscriber
@@ -904,23 +916,26 @@ public interface BodySubscriber<T>
904
916
* to convert a flow of response body data into common high-level Java
905
917
* objects:
906
918
*
907
- * <pre>{@code // Streams the response body to a File
919
+ * {@snippet :
920
+ * // Streams the response body to a File
908
921
* HttpResponse<Path> response = client
909
- * .send(request, responseInfo -> BodySubscribers.ofFile(Paths.get("example.html"));
922
+ * .send(request, responseInfo -> BodySubscribers.ofFile(Paths.get("example.html")); }
910
923
*
924
+ * {@snippet :
911
925
* // Accumulates the response body and returns it as a byte[]
912
926
* HttpResponse<byte[]> response = client
913
- * .send(request, responseInfo -> BodySubscribers.ofByteArray());
927
+ * .send(request, responseInfo -> BodySubscribers.ofByteArray()); }
914
928
*
929
+ * {@snippet :
915
930
* // Discards the response body
916
931
* HttpResponse<Void> response = client
917
- * .send(request, responseInfo -> BodySubscribers.discarding());
932
+ * .send(request, responseInfo -> BodySubscribers.discarding()); }
918
933
*
934
+ * {@snippet :
919
935
* // Accumulates the response body as a String then maps it to its bytes
920
936
* HttpResponse<byte[]> response = client
921
937
* .send(request, responseInfo ->
922
- * BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes));
923
- * }</pre>
938
+ * BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes)); }
924
939
*
925
940
* @since 11
926
941
*/
@@ -988,9 +1003,8 @@ private BodySubscribers() { }
988
1003
* @apiNote This method can be used as an adapter between {@code
989
1004
* BodySubscriber} and {@code Flow.Subscriber}.
990
1005
*
991
- * @implNote This is equivalent to calling <pre>{@code
992
- * fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null)
993
- * }</pre>
1006
+ * @implNote This is equivalent to calling {@snippet :
1007
+ * fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null) }
994
1008
*
995
1009
* @param subscriber the subscriber
996
1010
* @return a body subscriber
@@ -1330,7 +1344,8 @@ public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
1330
1344
* convert an {@code InputStream} into any annotated Java type.
1331
1345
*
1332
1346
* <p>For example:
1333
- * <pre> {@code public static <W> BodySubscriber<Supplier<W>> asJSON(Class<W> targetType) {
1347
+ * {@snippet :
1348
+ * public static <W> BodySubscriber<Supplier<W>> asJSON(Class<W> targetType) {
1334
1349
* BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
1335
1350
*
1336
1351
* BodySubscriber<Supplier<W>> downstream = BodySubscribers.mapping(
@@ -1344,7 +1359,7 @@ public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
1344
1359
* }
1345
1360
* });
1346
1361
* return downstream;
1347
- * } }</pre>
1362
+ * } }
1348
1363
*
1349
1364
* @param <T> the upstream body type
1350
1365
* @param <U> the type of the body subscriber returned
0 commit comments