Skip to content

Commit 0cc25a8

Browse files
author
duke
committedFeb 27, 2023
Automatic merge of jdk:master into master
2 parents 8516c80 + 306134d commit 0cc25a8

File tree

4 files changed

+113
-76
lines changed

4 files changed

+113
-76
lines changed
 

‎src/java.net.http/share/classes/java/net/http/HttpClient.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -85,7 +85,8 @@
8585
* </ul>
8686
*
8787
* <p><b>Synchronous Example</b>
88-
* <pre>{@code HttpClient client = HttpClient.newBuilder()
88+
* {@snippet :
89+
* HttpClient client = HttpClient.newBuilder()
8990
* .version(Version.HTTP_1_1)
9091
* .followRedirects(Redirect.NORMAL)
9192
* .connectTimeout(Duration.ofSeconds(20))
@@ -94,18 +95,19 @@
9495
* .build();
9596
* HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
9697
* System.out.println(response.statusCode());
97-
* System.out.println(response.body()); }</pre>
98+
* System.out.println(response.body()); }
9899
*
99100
* <p><b>Asynchronous Example</b>
100-
* <pre>{@code HttpRequest request = HttpRequest.newBuilder()
101+
* {@snippet :
102+
* HttpRequest request = HttpRequest.newBuilder()
101103
* .uri(URI.create("https://foo.com/"))
102104
* .timeout(Duration.ofMinutes(2))
103105
* .header("Content-Type", "application/json")
104106
* .POST(BodyPublishers.ofFile(Paths.get("file.json")))
105107
* .build();
106108
* client.sendAsync(request, BodyHandlers.ofString())
107109
* .thenApply(HttpResponse::body)
108-
* .thenAccept(System.out::println); }</pre>
110+
* .thenAccept(System.out::println); }
109111
*
110112
* <p> <a id="securitychecks"><b>Security checks</b></a>
111113
*
@@ -688,20 +690,23 @@ public enum Redirect {
688690
* Creates a new {@code WebSocket} builder (optional operation).
689691
*
690692
* <p> <b>Example</b>
691-
* <pre>{@code HttpClient client = HttpClient.newHttpClient();
693+
* {@snippet :
694+
* HttpClient client = HttpClient.newHttpClient();
692695
* CompletableFuture<WebSocket> ws = client.newWebSocketBuilder()
693-
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }</pre>
696+
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }
694697
*
695698
* <p> Finer control over the WebSocket Opening Handshake can be achieved
696699
* by using a custom {@code HttpClient}.
697700
*
698701
* <p> <b>Example</b>
699-
* <pre>{@code InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80);
702+
* {@snippet :
703+
* InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80);
700704
* HttpClient client = HttpClient.newBuilder()
701705
* .proxy(ProxySelector.of(addr))
702706
* .build();
707+
*
703708
* CompletableFuture<WebSocket> ws = client.newWebSocketBuilder()
704-
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }</pre>
709+
* .buildAsync(URI.create("ws://websocket.example.com"), listener); }
705710
*
706711
* @implSpec The default implementation of this method throws
707712
* {@code UnsupportedOperationException}. Clients obtained through

‎src/java.net.http/share/classes/java/net/http/HttpRequest.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -66,14 +66,17 @@
6666
* <p> The following is an example of a GET request that prints the response
6767
* body as a String:
6868
*
69-
* <pre>{@code HttpClient client = HttpClient.newHttpClient();
69+
* {@snippet :
70+
* HttpClient client = HttpClient.newHttpClient();
71+
*
7072
* HttpRequest request = HttpRequest.newBuilder()
7173
* .uri(URI.create("http://foo.com/"))
7274
* .build();
75+
*
7376
* client.sendAsync(request, BodyHandlers.ofString())
7477
* .thenApply(HttpResponse::body)
7578
* .thenAccept(System.out::println)
76-
* .join(); }</pre>
79+
* .join(); }
7780
*
7881
* <p>The class {@link BodyPublishers BodyPublishers} provides implementations
7982
* of many common publishers. Alternatively, a custom {@code BodyPublisher}
@@ -341,13 +344,16 @@ public static HttpRequest.Builder newBuilder(URI uri) {
341344
* <br><br>
342345
* <ul>
343346
* <li> Retain all headers:
344-
* <pre>{@code HttpRequest.newBuilder(request, (n, v) -> true)}</pre>
347+
* {@snippet :
348+
* HttpRequest.newBuilder(request, (n, v) -> true) }
345349
*
346350
* <li> Remove all headers:
347-
* <pre>{@code HttpRequest.newBuilder(request, (n, v) -> false)}</pre>
351+
* {@snippet :
352+
* HttpRequest.newBuilder(request, (n, v) -> false) }
348353
*
349354
* <li> Remove a particular header (e.g. Foo-Bar):
350-
* <pre>{@code HttpRequest.newBuilder(request, (name, value) -> !name.equalsIgnoreCase("Foo-Bar"))}</pre>
355+
* {@snippet :
356+
* HttpRequest.newBuilder(request, (name, value) -> !name.equalsIgnoreCase("Foo-Bar")) }
351357
* </ul>
352358
*
353359
* @param request the original request
@@ -561,25 +567,28 @@ public interface BodyPublisher extends Flow.Publisher<ByteBuffer> {
561567
* convert common high-level Java objects into a flow of data suitable for
562568
* sending as a request body:
563569
*
564-
* <pre>{@code // Request body from a String
570+
* {@snippet :
571+
* // Request body from a String
565572
* HttpRequest request = HttpRequest.newBuilder()
566573
* .uri(URI.create("https://foo.com/"))
567574
* .header("Content-Type", "text/plain; charset=UTF-8")
568575
* .POST(BodyPublishers.ofString("some body text"))
569-
* .build();
576+
* .build(); }
570577
*
578+
* {@snippet :
571579
* // Request body from a File
572580
* HttpRequest request = HttpRequest.newBuilder()
573581
* .uri(URI.create("https://foo.com/"))
574582
* .header("Content-Type", "application/json")
575583
* .POST(BodyPublishers.ofFile(Paths.get("file.json")))
576-
* .build();
584+
* .build(); }
577585
*
586+
* {@snippet :
578587
* // Request body from a byte array
579588
* HttpRequest request = HttpRequest.newBuilder()
580589
* .uri(URI.create("https://foo.com/"))
581590
* .POST(BodyPublishers.ofByteArray(new byte[] { ... }))
582-
* .build(); }</pre>
591+
* .build(); }
583592
*
584593
* @since 11
585594
*/

‎src/java.net.http/share/classes/java/net/http/HttpResponse.java

+46-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -77,8 +77,9 @@
7777
*
7878
* <p> The following is an example of retrieving a response as a String:
7979
*
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()); }
8283
*
8384
* <p> The class {@link BodyHandlers BodyHandlers} provides implementations
8485
* of many common response handlers. Alternatively, a custom {@code BodyHandler}
@@ -211,28 +212,31 @@ public interface ResponseInfo {
211212
* predefined body handlers} that always process the response body in the
212213
* same way ( streams the response body to a file ).
213214
*
214-
* <pre>{@code HttpRequest request = HttpRequest.newBuilder()
215+
* {@snippet :
216+
* HttpRequest request = HttpRequest.newBuilder()
215217
* .uri(URI.create("http://www.foo.com/"))
216218
* .build();
219+
*
217220
* client.sendAsync(request, BodyHandlers.ofFile(Paths.get("/tmp/f")))
218221
* .thenApply(HttpResponse::body)
219-
* .thenAccept(System.out::println); }</pre>
222+
* .thenAccept(System.out::println); }
220223
*
221224
* Note, that even though the pre-defined handlers do not examine the
222225
* response code, the response code and headers are always retrievable from
223226
* the {@link HttpResponse}, when it is returned.
224227
*
225228
* <p> In the second example, the function returns a different subscriber
226229
* depending on the status code.
227-
* <pre>{@code HttpRequest request = HttpRequest.newBuilder()
230+
* {@snippet :
231+
* HttpRequest request = HttpRequest.newBuilder()
228232
* .uri(URI.create("http://www.foo.com/"))
229233
* .build();
230234
* BodyHandler<Path> bodyHandler = (rspInfo) -> rspInfo.statusCode() == 200
231235
* ? BodySubscribers.ofFile(Paths.get("/tmp/f"))
232236
* : BodySubscribers.replacing(Paths.get("/NULL"));
233237
* client.sendAsync(request, bodyHandler)
234238
* .thenApply(HttpResponse::body)
235-
* .thenAccept(System.out::println); }</pre>
239+
* .thenAccept(System.out::println); }
236240
*
237241
* @param <T> the response body type
238242
* @see BodyHandlers
@@ -272,21 +276,25 @@ public interface BodyHandler<T> {
272276
* <p>The following are examples of using the predefined body handlers to
273277
* convert a flow of response body data into common high-level Java objects:
274278
*
275-
* <pre>{@code // Receives the response body as a String
279+
* {@snippet :
280+
* // Receives the response body as a String
276281
* HttpResponse<String> response = client
277-
* .send(request, BodyHandlers.ofString());
282+
* .send(request, BodyHandlers.ofString()); }
278283
*
284+
* {@snippet :
279285
* // Receives the response body as a file
280286
* HttpResponse<Path> response = client
281-
* .send(request, BodyHandlers.ofFile(Paths.get("example.html")));
287+
* .send(request, BodyHandlers.ofFile(Paths.get("example.html"))); }
282288
*
289+
* {@snippet :
283290
* // Receives the response body as an InputStream
284291
* HttpResponse<InputStream> response = client
285-
* .send(request, BodyHandlers.ofInputStream());
292+
* .send(request, BodyHandlers.ofInputStream()); }
286293
*
294+
* {@snippet :
287295
* // Discards the response body
288296
* HttpResponse<Void> response = client
289-
* .send(request, BodyHandlers.discarding()); }</pre>
297+
* .send(request, BodyHandlers.discarding()); }
290298
*
291299
* @since 11
292300
*/
@@ -310,10 +318,11 @@ private BodyHandlers() { }
310318
* BodySubscriber} and {@code Flow.Subscriber}.
311319
*
312320
* <p> For example:
313-
* <pre> {@code TextSubscriber subscriber = new TextSubscriber();
321+
* {@snippet :
322+
* TextSubscriber subscriber = new TextSubscriber();
314323
* HttpResponse<Void> response = client.sendAsync(request,
315324
* BodyHandlers.fromSubscriber(subscriber)).join();
316-
* System.out.println(response.statusCode()); }</pre>
325+
* System.out.println(response.statusCode()); }
317326
*
318327
* @param subscriber the subscriber
319328
* @return a response body handler
@@ -340,10 +349,11 @@ private BodyHandlers() { }
340349
* BodySubscriber} and {@code Flow.Subscriber}.
341350
*
342351
* <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
344354
* HttpResponse<String> response = client.sendAsync(request,
345355
* BodyHandlers.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join();
346-
* String text = response.body(); }</pre>
356+
* String text = response.body(); }
347357
*
348358
* @param <S> the type of the Subscriber
349359
* @param <T> the type of the response body
@@ -380,7 +390,8 @@ private BodyHandlers() { }
380390
* text line by line.
381391
*
382392
* <p> For example:
383-
* <pre> {@code // A PrintSubscriber that implements Flow.Subscriber<String>
393+
* {@snippet :
394+
* // A PrintSubscriber that implements Flow.Subscriber<String>
384395
* // and print lines received by onNext() on System.out
385396
* PrintSubscriber subscriber = new PrintSubscriber(System.out);
386397
* client.sendAsync(request, BodyHandlers.fromLineSubscriber(subscriber))
@@ -389,7 +400,7 @@ private BodyHandlers() { }
389400
* if (status != 200) {
390401
* System.err.printf("ERROR: %d status received%n", status);
391402
* }
392-
* }); }</pre>
403+
* }); }
393404
*
394405
* @param subscriber the subscriber
395406
* @return a response body handler
@@ -423,15 +434,16 @@ private BodyHandlers() { }
423434
* text line by line.
424435
*
425436
* <p> For example:
426-
* <pre> {@code // A LineParserSubscriber that implements Flow.Subscriber<String>
437+
* {@snippet :
438+
* // A LineParserSubscriber that implements Flow.Subscriber<String>
427439
* // and accumulates lines that match a particular pattern
428440
* Pattern pattern = ...;
429441
* LineParserSubscriber subscriber = new LineParserSubscriber(pattern);
430442
* HttpResponse<List<String>> response = client.send(request,
431443
* BodyHandlers.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n"));
432444
* if (response.statusCode() != 200) {
433445
* System.err.printf("ERROR: %d status received%n", response.statusCode());
434-
* } }</pre>
446+
* } }
435447
*
436448
*
437449
* @param <S> the type of the Subscriber
@@ -904,23 +916,26 @@ public interface BodySubscriber<T>
904916
* to convert a flow of response body data into common high-level Java
905917
* objects:
906918
*
907-
* <pre>{@code // Streams the response body to a File
919+
* {@snippet :
920+
* // Streams the response body to a File
908921
* HttpResponse<Path> response = client
909-
* .send(request, responseInfo -> BodySubscribers.ofFile(Paths.get("example.html"));
922+
* .send(request, responseInfo -> BodySubscribers.ofFile(Paths.get("example.html")); }
910923
*
924+
* {@snippet :
911925
* // Accumulates the response body and returns it as a byte[]
912926
* HttpResponse<byte[]> response = client
913-
* .send(request, responseInfo -> BodySubscribers.ofByteArray());
927+
* .send(request, responseInfo -> BodySubscribers.ofByteArray()); }
914928
*
929+
* {@snippet :
915930
* // Discards the response body
916931
* HttpResponse<Void> response = client
917-
* .send(request, responseInfo -> BodySubscribers.discarding());
932+
* .send(request, responseInfo -> BodySubscribers.discarding()); }
918933
*
934+
* {@snippet :
919935
* // Accumulates the response body as a String then maps it to its bytes
920936
* HttpResponse<byte[]> response = client
921937
* .send(request, responseInfo ->
922-
* BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes));
923-
* }</pre>
938+
* BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes)); }
924939
*
925940
* @since 11
926941
*/
@@ -988,9 +1003,8 @@ private BodySubscribers() { }
9881003
* @apiNote This method can be used as an adapter between {@code
9891004
* BodySubscriber} and {@code Flow.Subscriber}.
9901005
*
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) }
9941008
*
9951009
* @param subscriber the subscriber
9961010
* @return a body subscriber
@@ -1330,7 +1344,8 @@ public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
13301344
* convert an {@code InputStream} into any annotated Java type.
13311345
*
13321346
* <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) {
13341349
* BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream();
13351350
*
13361351
* BodySubscriber<Supplier<W>> downstream = BodySubscribers.mapping(
@@ -1344,7 +1359,7 @@ public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream,
13441359
* }
13451360
* });
13461361
* return downstream;
1347-
* } }</pre>
1362+
* } }
13481363
*
13491364
* @param <T> the upstream body type
13501365
* @param <U> the type of the body subscriber returned

0 commit comments

Comments
 (0)
Failed to load comments.