diff --git a/src/java.net.http/share/classes/java/net/http/HttpClient.java b/src/java.net.http/share/classes/java/net/http/HttpClient.java index 2cb2a5d21de..dadadc6cc79 100644 --- a/src/java.net.http/share/classes/java/net/http/HttpClient.java +++ b/src/java.net.http/share/classes/java/net/http/HttpClient.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -85,7 +85,8 @@ import jdk.internal.net.http.HttpClientBuilderImpl; * * *
Synchronous Example - *
{@code HttpClient client = HttpClient.newBuilder()
+ * {@snippet :
+ * HttpClient client = HttpClient.newBuilder()
* .version(Version.HTTP_1_1)
* .followRedirects(Redirect.NORMAL)
* .connectTimeout(Duration.ofSeconds(20))
@@ -94,10 +95,11 @@ import jdk.internal.net.http.HttpClientBuilderImpl;
* .build();
* HttpResponse response = client.send(request, BodyHandlers.ofString());
* System.out.println(response.statusCode());
- * System.out.println(response.body()); }
+ * System.out.println(response.body()); }
*
* Asynchronous Example - *
{@code HttpRequest request = HttpRequest.newBuilder()
+ * {@snippet :
+ * HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("https://foo.com/"))
* .timeout(Duration.ofMinutes(2))
* .header("Content-Type", "application/json")
@@ -105,7 +107,7 @@ import jdk.internal.net.http.HttpClientBuilderImpl;
* .build();
* client.sendAsync(request, BodyHandlers.ofString())
* .thenApply(HttpResponse::body)
- * .thenAccept(System.out::println); }
+ * .thenAccept(System.out::println); }
*
* Security checks * @@ -688,20 +690,23 @@ public abstract class HttpClient { * Creates a new {@code WebSocket} builder (optional operation). * *
Example - *
{@code HttpClient client = HttpClient.newHttpClient();
+ * {@snippet :
+ * HttpClient client = HttpClient.newHttpClient();
* CompletableFuture ws = client.newWebSocketBuilder()
- * .buildAsync(URI.create("ws://websocket.example.com"), listener); }
+ * .buildAsync(URI.create("ws://websocket.example.com"), listener); }
*
* Finer control over the WebSocket Opening Handshake can be achieved * by using a custom {@code HttpClient}. * *
Example - *
{@code InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80);
+ * {@snippet :
+ * InetSocketAddress addr = new InetSocketAddress("proxy.example.com", 80);
* HttpClient client = HttpClient.newBuilder()
* .proxy(ProxySelector.of(addr))
* .build();
+ *
* CompletableFuture ws = client.newWebSocketBuilder()
- * .buildAsync(URI.create("ws://websocket.example.com"), listener); }
+ * .buildAsync(URI.create("ws://websocket.example.com"), listener); }
*
* @implSpec The default implementation of this method throws
* {@code UnsupportedOperationException}. Clients obtained through
diff --git a/src/java.net.http/share/classes/java/net/http/HttpRequest.java b/src/java.net.http/share/classes/java/net/http/HttpRequest.java
index 974ea54717e..a91cb935dc8 100644
--- a/src/java.net.http/share/classes/java/net/http/HttpRequest.java
+++ b/src/java.net.http/share/classes/java/net/http/HttpRequest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -66,14 +66,17 @@ import static java.nio.charset.StandardCharsets.UTF_8;
* The following is an example of a GET request that prints the response * body as a String: * - *
{@code HttpClient client = HttpClient.newHttpClient();
+ * {@snippet :
+ * HttpClient client = HttpClient.newHttpClient();
+ *
* HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("http://foo.com/"))
* .build();
+ *
* client.sendAsync(request, BodyHandlers.ofString())
* .thenApply(HttpResponse::body)
* .thenAccept(System.out::println)
- * .join(); }
+ * .join(); }
*
* The class {@link BodyPublishers BodyPublishers} provides implementations
* of many common publishers. Alternatively, a custom {@code BodyPublisher}
@@ -341,13 +344,16 @@ public abstract class HttpRequest {
*
*
{@code HttpRequest.newBuilder(request, (n, v) -> true)}
+ * {@snippet :
+ * HttpRequest.newBuilder(request, (n, v) -> true) }
*
* {@code HttpRequest.newBuilder(request, (n, v) -> false)}
+ * {@snippet :
+ * HttpRequest.newBuilder(request, (n, v) -> false) }
*
* {@code HttpRequest.newBuilder(request, (name, value) -> !name.equalsIgnoreCase("Foo-Bar"))}
+ * {@snippet :
+ * HttpRequest.newBuilder(request, (name, value) -> !name.equalsIgnoreCase("Foo-Bar")) }
* {@code // Request body from a String
+ * {@snippet :
+ * // Request body from a String
* HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("https://foo.com/"))
* .header("Content-Type", "text/plain; charset=UTF-8")
* .POST(BodyPublishers.ofString("some body text"))
- * .build();
+ * .build(); }
*
+ * {@snippet :
* // Request body from a File
* HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("https://foo.com/"))
* .header("Content-Type", "application/json")
* .POST(BodyPublishers.ofFile(Paths.get("file.json")))
- * .build();
+ * .build(); }
*
+ * {@snippet :
* // Request body from a byte array
* HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("https://foo.com/"))
* .POST(BodyPublishers.ofByteArray(new byte[] { ... }))
- * .build(); }
+ * .build(); }
*
* @since 11
*/
diff --git a/src/java.net.http/share/classes/java/net/http/HttpResponse.java b/src/java.net.http/share/classes/java/net/http/HttpResponse.java
index f518d86eae1..47f7da46d76 100644
--- a/src/java.net.http/share/classes/java/net/http/HttpResponse.java
+++ b/src/java.net.http/share/classes/java/net/http/HttpResponse.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -77,8 +77,9 @@ import static jdk.internal.net.http.common.Utils.charsetFrom;
*
* The following is an example of retrieving a response as a String: * - *
{@code HttpResponse response = client
- * .send(request, BodyHandlers.ofString()); }
+ * {@snippet :
+ * HttpResponse The class {@link BodyHandlers BodyHandlers} provides implementations
* of many common response handlers. Alternatively, a custom {@code BodyHandler}
@@ -211,12 +212,14 @@ public interface HttpResponse In the second example, the function returns a different subscriber
* depending on the status code.
- * The following are examples of using the predefined body handlers to
* convert a flow of response body data into common high-level Java objects:
*
- * For example:
- * For example:
- * For example:
- * For example:
- * For example:
- * To specify a custom closure code or reason code the
* {@code sendClose} method may be invoked from inside the
* {@code onClose} invocation:
- * Here is an example of a listener that requests invocations, one at a
* time, until a complete message has been accumulated, and then processes
* the result:
- * {@code HttpRequest request = HttpRequest.newBuilder()
+ * {@snippet :
+ * HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("http://www.foo.com/"))
* .build();
+ *
* client.sendAsync(request, BodyHandlers.ofFile(Paths.get("/tmp/f")))
* .thenApply(HttpResponse::body)
- * .thenAccept(System.out::println); }
+ * .thenAccept(System.out::println); }
*
* Note, that even though the pre-defined handlers do not examine the
* response code, the response code and headers are always retrievable from
@@ -224,7 +227,8 @@ public interface HttpResponse{@code HttpRequest request = HttpRequest.newBuilder()
+ * {@snippet :
+ * HttpRequest request = HttpRequest.newBuilder()
* .uri(URI.create("http://www.foo.com/"))
* .build();
* BodyHandler
+ * .thenAccept(System.out::println); }
*
* @param {@code // Receives the response body as a String
+ * {@snippet :
+ * // Receives the response body as a String
* HttpResponse
+ * .send(request, BodyHandlers.discarding()); }
*
* @since 11
*/
@@ -310,10 +318,11 @@ public interface HttpResponse {@code TextSubscriber subscriber = new TextSubscriber();
+ * {@snippet :
+ * TextSubscriber subscriber = new TextSubscriber();
* HttpResponse
+ * System.out.println(response.statusCode()); }
*
* @param subscriber the subscriber
* @return a response body handler
@@ -340,10 +349,11 @@ public interface HttpResponse {@code TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String
+ * {@snippet :
+ * TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String
* HttpResponse
+ * String text = response.body(); }
*
* @param the type of the Subscriber
* @param {@code // A PrintSubscriber that implements Flow.Subscriber
+ * }); }
*
* @param subscriber the subscriber
* @return a response body handler
@@ -423,7 +434,8 @@ public interface HttpResponse {@code // A LineParserSubscriber that implements Flow.Subscriber
+ * } }
*
*
* @param the type of the Subscriber
@@ -904,23 +916,26 @@ public interface HttpResponse{@code // Streams the response body to a File
+ * {@snippet :
+ * // Streams the response body to a File
* HttpResponse
+ * BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), String::getBytes)); }
*
* @since 11
*/
@@ -988,9 +1003,8 @@ public interface HttpResponse{@code
- * fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null)
- * }
+ * @implNote This is equivalent to calling {@snippet :
+ * fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null) }
*
* @param subscriber the subscriber
* @return a body subscriber
@@ -1330,7 +1344,8 @@ public interface HttpResponse {@code public static
+ * } }
*
* @param {@code WebSocket.Listener listener = new WebSocket.Listener() {
+ * {@snippet :
+ * WebSocket.Listener listener = new WebSocket.Listener() {
*
* List
+ * }; }
*
* @since 11
*/
@@ -292,7 +292,8 @@ public interface WebSocket {
* typically used to make a request for more invocations.
*
* @implSpec The default implementation is equivalent to:
- * {@code webSocket.request(1); }
+ * {@snippet :
+ * webSocket.request(1); }
*
* @param webSocket
* the WebSocket that has been connected
@@ -308,8 +309,9 @@ public interface WebSocket {
* this {@code CompletionStage} has completed.
*
* @implSpec The default implementation is equivalent to:
- * {@code webSocket.request(1);
- * return null; }
+ * {@snippet :
+ * webSocket.request(1);
+ * return null; }
*
* @implNote The {@code data} is always a legal UTF-16 sequence.
*
@@ -343,8 +345,9 @@ public interface WebSocket {
* this {@code CompletionStage} has completed.
*
* @implSpec The default implementation is equivalent to:
- * {@code webSocket.request(1);
- * return null; }
+ * {@snippet :
+ * webSocket.request(1);
+ * return null; }
*
* @param webSocket
* the WebSocket on which the data has been received
@@ -381,8 +384,9 @@ public interface WebSocket {
* this {@code CompletionStage} has completed.
*
* @implSpec The default implementation is equivalent to:
- * {@code webSocket.request(1);
- * return null; }
+ * {@snippet :
+ * webSocket.request(1);
+ * return null; }
*
* @param webSocket
* the WebSocket on which the message has been received
@@ -412,8 +416,9 @@ public interface WebSocket {
* this {@code CompletionStage} has completed.
*
* @implSpec The default implementation is equivalent to:
- * {@code webSocket.request(1);
- * return null; }
+ * {@snippet :
+ * webSocket.request(1);
+ * return null; }
*
* @param webSocket
* the WebSocket on which the message has been received
@@ -456,12 +461,13 @@ public interface WebSocket {
* {@code public CompletionStage> onClose(WebSocket webSocket,
- * int statusCode,
- * String reason) {
+ * {@snippet :
+ * public CompletionStage> onClose(WebSocket webSocket,
+ * int statusCode,
+ * String reason) {
* webSocket.sendClose(CUSTOM_STATUS_CODE, CUSTOM_REASON);
* return new CompletableFuture
+ * } }
*
* @implSpec The default implementation of this method returns
* {@code null}, indicating that the output should be closed
@@ -654,11 +660,13 @@ public interface WebSocket {
*
* @apiNote Use the provided integer constant {@link #NORMAL_CLOSURE} as a
* status code and an empty string as a reason in a typical case:
- * {@code CompletableFuture
+ * .join();
+ * }
*
* The {@code sendClose} method does not close this WebSocket's input. It
* merely closes this WebSocket's output by sending a Close message. To
@@ -666,7 +674,8 @@ public interface WebSocket {
* example of an application that sends a Close message, and then starts a
* timer. Once no data has been received within the specified timeout, the
* timer goes off and the alarm aborts {@code WebSocket}:
- * {@code MyAlarm alarm = new MyAlarm(webSocket::abort);
+ * {@snippet :
+ * MyAlarm alarm = new MyAlarm(webSocket::abort);
* WebSocket.Listener listener = new WebSocket.Listener() {
*
* public CompletionStage> onText(WebSocket webSocket,
@@ -682,8 +691,7 @@ public interface WebSocket {
* MyTimer idleTimer = new MyTimer();
* idleTimer.add(alarm, 30, TimeUnit.SECONDS);
* };
- * webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok").thenRun(startTimer);
- * }
+ * webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok").thenRun(startTimer); }
*
* @param statusCode
* the status code
@@ -717,7 +725,8 @@ public interface WebSocket {
* {@code WebSocket.Listener listener = new WebSocket.Listener() {
+ * {@snippet :
+ * WebSocket.Listener listener = new WebSocket.Listener() {
*
* StringBuilder text = new StringBuilder();
*
@@ -732,8 +741,7 @@ public interface WebSocket {
* webSocket.request(1);
* return null;
* }
- * ...
- * } }
+ * }; }
*
* @param n
* the number of invocations