author | chegar |
Sat, 24 Feb 2018 11:27:11 +0000 | |
branch | http-client-branch |
changeset 56167 | 96fa4f49a9ff |
parent 56160 | f1b75e394b68 |
child 56203 | be57b0428d84 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56087
diff
changeset
|
26 |
package java.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
28 |
import java.io.BufferedReader; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.io.IOException; |
48083 | 30 |
import java.io.InputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.nio.charset.Charset; |
48083 | 34 |
import java.nio.channels.FileChannel; |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
35 |
import java.nio.charset.StandardCharsets; |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
36 |
import java.nio.file.Files; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.file.OpenOption; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.nio.file.Path; |
48083 | 39 |
import java.util.List; |
40 |
import java.util.Objects; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import java.util.concurrent.CompletionStage; |
56010 | 44 |
import java.util.concurrent.ConcurrentMap; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.util.concurrent.Flow; |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
46 |
import java.util.concurrent.Flow.Subscriber; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.util.function.Consumer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.function.Function; |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
49 |
import java.util.stream.Stream; |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56120
diff
changeset
|
50 |
import javax.net.ssl.SSLSession; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
51 |
import jdk.internal.net.http.BufferingSubscriber; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
52 |
import jdk.internal.net.http.LineSubscriberAdapter; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
53 |
import jdk.internal.net.http.ResponseBodyHandlers.FileDownloadBodyHandler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
54 |
import jdk.internal.net.http.ResponseBodyHandlers.PathBodyHandler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
55 |
import jdk.internal.net.http.ResponseBodyHandlers.PushPromisesHandlerWithMap; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
56 |
import jdk.internal.net.http.ResponseSubscribers; |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
57 |
import jdk.internal.net.http.ResponseSubscribers.PathSubscriber; |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
58 |
import static java.nio.file.StandardOpenOption.*; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
59 |
import static jdk.internal.net.http.common.Utils.charsetFrom; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
/** |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
62 |
* An HTTP response. |
48083 | 63 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
64 |
* <p> An {@code HttpResponse} is not created directly, but rather returned as |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
65 |
* a result of sending an {@link HttpRequest}. An {@code HttpResponse} is |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
66 |
* made available when the response status code and headers have been received, |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
67 |
* and typically after the response body has also been completely received. |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
68 |
* Whether or not the {@code HttpResponse} is made available before the response |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
69 |
* body has been completely received depends on the {@link BodyHandler |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
70 |
* BodyHandler} provided when sending the {@code HttpRequest}. |
48083 | 71 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
72 |
* <p> This class provides methods for accessing the response status code, |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
73 |
* headers, the response body, and the {@code HttpRequest} corresponding |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
74 |
* to this response. |
56157 | 75 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
76 |
* <p> The following is an example of retrieving a response as a String: |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
77 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
78 |
* <pre>{@code HttpResponse<String> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
79 |
* .send(request, BodyHandlers.ofString()); }</pre> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
80 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
81 |
* <p> The class {@link BodyHandlers BodyHandlers} provides implementations |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
82 |
* of many common response handlers. Alternatively, a custom {@code BodyHandler} |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
83 |
* implementation can be used. |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
84 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
85 |
* @param <T> the response body type |
56086 | 86 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
*/ |
56139 | 88 |
public interface HttpResponse<T> { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
* Returns the status code for this response. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
* @return the response code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
*/ |
56139 | 96 |
public int statusCode(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
/** |
48083 | 99 |
* Returns the {@link HttpRequest} corresponding to this response. |
100 |
* |
|
101 |
* <p> This may not be the original request provided by the caller, |
|
102 |
* for example, if that request was redirected. |
|
103 |
* |
|
104 |
* @see #previousResponse() |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
* @return the request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
*/ |
56139 | 108 |
public HttpRequest request(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
/** |
48083 | 111 |
* Returns an {@code Optional} containing the previous intermediate response |
112 |
* if one was received. An intermediate response is one that is received |
|
113 |
* as a result of redirection or authentication. If no previous response |
|
114 |
* was received then an empty {@code Optional} is returned. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
* |
48083 | 116 |
* @return an Optional containing the HttpResponse, if any. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
*/ |
56139 | 118 |
public Optional<HttpResponse<T>> previousResponse(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
* Returns the received response headers. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
* @return the response headers |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
*/ |
56139 | 125 |
public HttpHeaders headers(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
/** |
48083 | 128 |
* Returns the body. Depending on the type of {@code T}, the returned body |
129 |
* may represent the body after it was read (such as {@code byte[]}, or |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
* {@code String}, or {@code Path}) or it may represent an object with |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
* which the body is read, such as an {@link java.io.InputStream}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
* |
48083 | 133 |
* <p> If this {@code HttpResponse} was returned from an invocation of |
134 |
* {@link #previousResponse()} then this method returns {@code null} |
|
135 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
* @return the body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
*/ |
56139 | 138 |
public T body(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
/** |
56131
99f144742013
http-client-branch: minor cleanup and improve testing for response version and session
chegar
parents:
56126
diff
changeset
|
141 |
* Returns an {@link Optional} containing the {@link SSLSession} in effect |
99f144742013
http-client-branch: minor cleanup and improve testing for response version and session
chegar
parents:
56126
diff
changeset
|
142 |
* for this response. Returns an empty {@code Optional} if this is not a |
99f144742013
http-client-branch: minor cleanup and improve testing for response version and session
chegar
parents:
56126
diff
changeset
|
143 |
* <i>HTTPS</i> response. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
* |
56131
99f144742013
http-client-branch: minor cleanup and improve testing for response version and session
chegar
parents:
56126
diff
changeset
|
145 |
* @return an {@code Optional} containing the {@code SSLSession} associated |
99f144742013
http-client-branch: minor cleanup and improve testing for response version and session
chegar
parents:
56126
diff
changeset
|
146 |
* with the response |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
*/ |
56139 | 148 |
public Optional<SSLSession> sslSession(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
* Returns the {@code URI} that the response was received from. This may be |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
* different from the request {@code URI} if redirection occurred. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
* @return the URI of the response |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
*/ |
56139 | 156 |
public URI uri(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
* Returns the HTTP protocol version that was used for this response. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
* @return HTTP protocol version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
*/ |
56139 | 163 |
public HttpClient.Version version(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
|
48083 | 165 |
|
166 |
private static String pathForSecurityCheck(Path path) { |
|
167 |
return path.toFile().getPath(); |
|
168 |
} |
|
169 |
||
170 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
/** |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
172 |
* A handler for response bodies. The class {@link BodyHandlers BodyHandlers} |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
173 |
* provides implementations of many common body handlers. |
48083 | 174 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
175 |
* <p> The {@code BodyHandler} interface allows inspection of the response |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
176 |
* code and headers, before the actual response body is received, and is |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
177 |
* responsible for creating the response {@link BodySubscriber |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
178 |
* BodySubscriber}. The {@code BodySubscriber} consumes the actual response |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
179 |
* body bytes and, typically, converts them into a higher-level Java type. |
48083 | 180 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
181 |
* <p> A {@code BodyHandler} is a function that takes two parameters: the |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
182 |
* response status code and the response headers; and which returns a |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
183 |
* {@code BodySubscriber}. The {@code BodyHandler} is invoked when the |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
184 |
* response status code and headers are available, but before the response |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
185 |
* body bytes are received. |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
186 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
187 |
* <p> The following example uses one of the {@linkplain BodyHandlers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
188 |
* predefined body handlers} that always process the response body in the |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
189 |
* same way ( streams the response body to a file ). |
48083 | 190 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
191 |
* <pre>{@code HttpRequest request = HttpRequest.newBuilder() |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
192 |
* .uri(URI.create("http://www.foo.com/")) |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
193 |
* .build(); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
194 |
* client.sendAsync(request, BodyHandlers.ofFile(Paths.get("/tmp/f"))) |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
195 |
* .thenApply(HttpResponse::body) |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
196 |
* .thenAccept(System.out::println) }</pre> |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
197 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
198 |
* Note, that even though the pre-defined handlers do not examine the |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
199 |
* response code, the response code and headers are always retrievable from |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
200 |
* the {@link HttpResponse}, when it is returned. |
48083 | 201 |
* |
202 |
* <p> In the second example, the function returns a different subscriber |
|
203 |
* depending on the status code. |
|
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
204 |
* <pre>{@code HttpRequest request = HttpRequest.newBuilder() |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
205 |
* .uri(URI.create("http://www.foo.com/")) |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
206 |
* .build(); |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
207 |
* BodyHandler bodyHandler = (status, headers) -> status == 200 |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
208 |
* ? BodySubscribers.ofFile(Paths.get("/tmp/f")) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
209 |
* : BodySubscribers.replacing(Paths.get("/NULL"))); |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
210 |
* client.sendAsync(request, bodyHandler)) |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
211 |
* .thenApply(HttpResponse::body) |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
212 |
* .thenAccept(System.out::println) }</pre> |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
* |
48083 | 214 |
* @param <T> the response body type |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
215 |
* @see BodyHandlers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
216 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
*/ |
56038
e663f24e3020
http-client-branch: restore inadvertantly removed @FunctionalInterface
chegar
parents:
56034
diff
changeset
|
218 |
@FunctionalInterface |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
public interface BodyHandler<T> { |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
220 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
/** |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
222 |
* Returns a {@link BodySubscriber BodySubscriber} considering the |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
223 |
* given response status code and headers. This method is invoked before |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
224 |
* the actual response body bytes are read and its implementation must |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
225 |
* return a {@link BodySubscriber BodySubscriber} to consume the response |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
226 |
* body bytes. |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
227 |
* |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
228 |
* <p> The response body can be discarded using one of {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
229 |
* BodyHandlers#discarding() discarding} or {@link |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
230 |
* BodyHandlers#replacing(Object) replacing}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
* @param statusCode the HTTP status code received |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
* @param responseHeaders the response headers received |
48083 | 234 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
*/ |
48083 | 236 |
public BodySubscriber<T> apply(int statusCode, HttpHeaders responseHeaders); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
237 |
} |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
238 |
|
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
239 |
/** |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
240 |
* Implementations of {@link BodyHandler BodyHandler} that implement various |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
241 |
* useful handlers, such as handling the response body as a String, or |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
242 |
* streaming the response body to a file. |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
243 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
244 |
* <p> These implementations do not examine the status code, meaning the |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
245 |
* body is always accepted. They typically return an equivalently named |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
246 |
* {@code BodySubscriber}. Alternatively, a custom handler can be used to |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
247 |
* examine the status code and headers, and return a different body |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
248 |
* subscriber, of the same type, as appropriate. |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
249 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
250 |
* <p>The following are examples of using the predefined body handlers to |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
251 |
* convert a flow of response body data into common high-level Java objects: |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
252 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
253 |
* <pre>{@code // Receives the response body as a String |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
254 |
* HttpResponse<String> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
255 |
* .send(request, BodyHandlers.ofString()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
256 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
257 |
* // Receives the response body as a file |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
258 |
* HttpResponse<Path> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
259 |
* .send(request, BodyHandlers.ofFile(Paths.get("example.html"))); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
260 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
261 |
* // Receives the response body as an InputStream |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
262 |
* HttpResponse<InputStream> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
263 |
* .send(request, BodyHandlers.ofInputStream()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
264 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
265 |
* // Discards the response body |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
266 |
* HttpResponse<Void> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
267 |
* .send(request, BodyHandlers.discarding()); }</pre> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
268 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
269 |
* @since 11 |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
270 |
*/ |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
271 |
public static class BodyHandlers { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
272 |
|
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
273 |
private BodyHandlers() { } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
276 |
* Returns a response body handler that returns a {@link BodySubscriber |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
277 |
* BodySubscriber}{@code <Void>} obtained from {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
278 |
* BodySubscribers#fromSubscriber(Subscriber)}, with the given |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
279 |
* {@code subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
280 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
281 |
* <p> The response body is not available through this, or the {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
282 |
* HttpResponse} API, but instead all response body is forwarded to the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
283 |
* given {@code subscriber}, which should make it available, if |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
284 |
* appropriate, through some other mechanism, e.g. an entry in a |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
285 |
* database, etc. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
286 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
287 |
* @apiNote This method can be used as an adapter between {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
288 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
289 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
290 |
* <p> For example: |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
291 |
* <pre> {@code TextSubscriber subscriber = new TextSubscriber(); |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
292 |
* HttpResponse<Void> response = client.sendAsync(request, |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
293 |
* BodyHandlers.fromSubscriber(subscriber)).join(); |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
294 |
* System.out.println(response.statusCode()); }</pre> |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
295 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
296 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
297 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
298 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
299 |
public static BodyHandler<Void> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
300 |
fromSubscriber(Subscriber<? super List<ByteBuffer>> subscriber) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
301 |
Objects.requireNonNull(subscriber); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
302 |
return (status, headers) -> BodySubscribers.fromSubscriber(subscriber, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
303 |
s -> null); |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
304 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
305 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
306 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
307 |
* Returns a response body handler that returns a {@link BodySubscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
308 |
* BodySubscriber}{@code <T>} obtained from {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
309 |
* BodySubscribers#fromSubscriber(Subscriber, Function)}, with the |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
310 |
* given {@code subscriber} and {@code finisher} function. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
311 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
312 |
* <p> The given {@code finisher} function is applied after the given |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
313 |
* subscriber's {@code onComplete} has been invoked. The {@code finisher} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
314 |
* function is invoked with the given subscriber, and returns a value |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
315 |
* that is set as the response's body. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
316 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
317 |
* @apiNote This method can be used as an adapter between {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
318 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
319 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
320 |
* <p> For example: |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
321 |
* <pre> {@code TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
322 |
* HttpResponse<String> response = client.sendAsync(request, |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
323 |
* BodyHandlers.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join(); |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
324 |
* String text = response.body(); }</pre> |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
325 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
326 |
* @param <S> the type of the Subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
327 |
* @param <T> the type of the response body |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
328 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
329 |
* @param finisher a function to be applied after the subscriber has completed |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
330 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
331 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
332 |
public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodyHandler<T> |
56160
f1b75e394b68
http-client-branch: type paramater review of generic methods ( missing from first push )
chegar
parents:
56159
diff
changeset
|
333 |
fromSubscriber(S subscriber, Function<? super S,? extends T> finisher) { |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
334 |
Objects.requireNonNull(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
335 |
Objects.requireNonNull(finisher); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
336 |
return (status, headers) -> BodySubscribers.fromSubscriber(subscriber, |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
337 |
finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
338 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
339 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
340 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
341 |
* Returns a response body handler that returns a {@link BodySubscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
342 |
* BodySubscriber}{@code <Void>} obtained from {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
343 |
* BodySubscribers#fromLineSubscriber(Subscriber, Function, Charset, String) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
344 |
* BodySubscribers.fromLineSubscriber(subscriber, s -> null, charset, null)}, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
345 |
* with the given {@code subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
346 |
* The {@link Charset charset} used to decode the response body bytes is |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
347 |
* obtained from the HTTP response headers as specified by {@link #ofString()}, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
348 |
* and lines are delimited in the manner of {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
349 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
350 |
* <p> The response body is not available through this, or the {@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
351 |
* HttpResponse} API, but instead all response body is forwarded to the |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
352 |
* given {@code subscriber}, which should make it available, if |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
353 |
* appropriate, through some other mechanism, e.g. an entry in a |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
354 |
* database, etc. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
355 |
* |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
356 |
* @apiNote This method can be used as an adapter between a {@code |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
357 |
* BodySubscriber} and a text based {@code Flow.Subscriber} that parses |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
358 |
* text line by line. |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
359 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
360 |
* <p> For example: |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
361 |
* <pre> {@code // A PrintSubscriber that implements Flow.Subscriber<String> |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
362 |
* // and print lines received by onNext() on System.out |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
363 |
* PrintSubscriber subscriber = new PrintSubscriber(System.out); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
364 |
* client.sendAsync(request, BodyHandlers.fromLineSubscriber(subscriber)) |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
365 |
* .thenApply(HttpResponse::statusCode) |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
366 |
* .thenAccept((status) -> { |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
367 |
* if (status != 200) { |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
368 |
* System.err.printf("ERROR: %d status received%n", status); |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
369 |
* } |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
370 |
* }); }</pre> |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
371 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
372 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
373 |
* @return a response body handler |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
374 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
375 |
public static BodyHandler<Void> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
376 |
fromLineSubscriber(Subscriber<? super String> subscriber) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
377 |
Objects.requireNonNull(subscriber); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
378 |
return (status, headers) -> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
379 |
BodySubscribers.fromLineSubscriber(subscriber, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
380 |
s -> null, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
381 |
charsetFrom(headers), |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
382 |
null); |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
383 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
384 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
385 |
/** |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
386 |
* Returns a response body handler that returns a {@link BodySubscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
387 |
* BodySubscriber}{@code <T>} obtained from {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
388 |
* BodySubscribers#fromLineSubscriber(Subscriber, Function, Charset, String) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
389 |
* BodySubscribers.fromLineSubscriber(subscriber, finisher, charset, lineSeparator)}, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
390 |
* with the given {@code subscriber}, {@code finisher} function, and line separator. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
391 |
* The {@link Charset charset} used to decode the response body bytes is |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
392 |
* obtained from the HTTP response headers as specified by {@link #ofString()}. |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
393 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
394 |
* <p> The given {@code finisher} function is applied after the given |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
395 |
* subscriber's {@code onComplete} has been invoked. The {@code finisher} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
396 |
* function is invoked with the given subscriber, and returns a value |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
397 |
* that is set as the response's body. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
398 |
* |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
399 |
* @apiNote This method can be used as an adapter between a {@code |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
400 |
* BodySubscriber} and a text based {@code Flow.Subscriber} that parses |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
401 |
* text line by line. |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
402 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
403 |
* <p> For example: |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
404 |
* <pre> {@code // A LineParserSubscriber that implements Flow.Subscriber<String> |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
405 |
* // and accumulates lines that match a particular pattern |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
406 |
* Pattern pattern = ...; |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
407 |
* LineParserSubscriber subscriber = new LineParserSubscriber(pattern); |
56120
5db317238575
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber (tahe II)
dfuchs
parents:
56119
diff
changeset
|
408 |
* HttpResponse<List<String>> response = client.send(request, |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
409 |
* BodyHandlers.fromLineSubscriber(subscriber, s -> s.getMatchingLines(), "\n")); |
56119
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
410 |
* if (response.statusCode() != 200) { |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
411 |
* System.err.printf("ERROR: %d status received%n", response.statusCode()); |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
412 |
* } }</pre> |
33436f5e3b9d
http-client-branch: fix doc examples for BodyHandler::fromLineSubscriber
dfuchs
parents:
56099
diff
changeset
|
413 |
* |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
414 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
415 |
* @param <S> the type of the Subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
416 |
* @param <T> the type of the response body |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
417 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
418 |
* @param finisher a function to be applied after the subscriber has completed |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
419 |
* @param lineSeparator an optional line separator: can be {@code null}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
420 |
* in which case lines will be delimited in the manner of |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
421 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
422 |
* @return a response body handler |
56157 | 423 |
* @throws IllegalArgumentException if the supplied {@code lineSeparator} |
424 |
* is the empty string |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
425 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
426 |
public static <S extends Subscriber<? super String>,T> BodyHandler<T> |
56157 | 427 |
fromLineSubscriber(S subscriber, |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
428 |
Function<? super S,? extends T> finisher, |
56157 | 429 |
String lineSeparator) { |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
430 |
Objects.requireNonNull(subscriber); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
431 |
Objects.requireNonNull(finisher); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
432 |
// implicit null check |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
433 |
if (lineSeparator != null && lineSeparator.isEmpty()) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
434 |
throw new IllegalArgumentException("empty line separator"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
435 |
return (status, headers) -> |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
436 |
BodySubscribers.fromLineSubscriber(subscriber, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
437 |
finisher, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
438 |
charsetFrom(headers), |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
439 |
lineSeparator); |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
440 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
441 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
442 |
/** |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
443 |
* Returns a response body handler that discards the response body. |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
444 |
* |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
445 |
* @return a response body handler |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
446 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
447 |
public static BodyHandler<Void> discarding() { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
448 |
return (status, headers) -> BodySubscribers.discarding(); |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
449 |
} |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
450 |
|
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
451 |
/** |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
452 |
* Returns a response body handler that returns the given replacement |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
453 |
* value, after discarding the response body. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
* @param <U> the response body type |
48083 | 456 |
* @param value the value of U to return as the body, may be {@code null} |
44847 | 457 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
459 |
public static <U> BodyHandler<U> replacing(U value) { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
460 |
return (status, headers) -> BodySubscribers.replacing(value); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
462 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 465 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
466 |
* {@link BodySubscribers#ofString(Charset) BodySubscribers.ofString(Charset)}. |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
467 |
* The body is decoded using the given character set. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
468 |
* |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
469 |
* @param charset the character set to convert the body with |
44847 | 470 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
472 |
public static BodyHandler<String> ofString(Charset charset) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
473 |
Objects.requireNonNull(charset); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
474 |
return (status, headers) -> BodySubscribers.ofString(charset); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
477 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
* Returns a {@code BodyHandler<Path>} that returns a |
48083 | 479 |
* {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
480 |
* {@link BodySubscribers#ofFile(Path, OpenOption...) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
481 |
* BodySubscribers.ofFile(Path,OpenOption...)}. |
48083 | 482 |
* |
483 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
484 |
* been completely written to the file, and {@link #body()} returns a |
|
485 |
* reference to its {@link Path}. |
|
486 |
* |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
487 |
* <p> Security manager permission checks are performed in this factory |
56141 | 488 |
* method, when the {@code BodyHandler} is created. Care must be taken |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
489 |
* that the {@code BodyHandler} is not shared with untrusted code. |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
490 |
* |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
491 |
* @param file the file to store the body in |
48083 | 492 |
* @param openOptions any options to use when opening/creating the file |
493 |
* @return a response body handler |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
494 |
* @throws IllegalArgumentException if an invalid set of open options |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
495 |
* are specified |
48083 | 496 |
* @throws SecurityException If a security manager has been installed |
497 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
498 |
* write access} to the file. |
48083 | 499 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
500 |
public static BodyHandler<Path> ofFile(Path file, OpenOption... openOptions) { |
48083 | 501 |
Objects.requireNonNull(file); |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
502 |
List<OpenOption> opts = List.of(openOptions); |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
503 |
if (opts.contains(DELETE_ON_CLOSE) || opts.contains(READ)) { |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
504 |
// these options make no sense, since the FileChannel is not exposed |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
505 |
throw new IllegalArgumentException("invalid openOptions: " + opts); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
506 |
} |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
507 |
|
48083 | 508 |
SecurityManager sm = System.getSecurityManager(); |
509 |
if (sm != null) { |
|
510 |
String fn = pathForSecurityCheck(file); |
|
511 |
sm.checkWrite(fn); |
|
512 |
} |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
513 |
return new PathBodyHandler(file, opts); |
48083 | 514 |
} |
515 |
||
516 |
/** |
|
517 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
518 |
* {@link BodySubscriber BodySubscriber}{@code <Path>}. |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
519 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
520 |
* <p> Equivalent to: {@code ofFile(file, CREATE, WRITE)} |
48083 | 521 |
* |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
522 |
* <p> Security manager permission checks are performed in this factory |
56141 | 523 |
* method, when the {@code BodyHandler} is created. Care must be taken |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
524 |
* that the {@code BodyHandler} is not shared with untrusted code. |
48083 | 525 |
* |
526 |
* @param file the file to store the body in |
|
527 |
* @return a response body handler |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
528 |
* @throws SecurityException If a security manager has been installed |
48083 | 529 |
* and it denies {@link SecurityManager#checkWrite(String) |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
530 |
* write access} to the file. |
48083 | 531 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
532 |
public static BodyHandler<Path> ofFile(Path file) { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
533 |
return BodyHandlers.ofFile(file, CREATE, WRITE); |
48083 | 534 |
} |
535 |
||
536 |
/** |
|
537 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
538 |
* {@link BodySubscriber BodySubscriber}<{@link Path}> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
539 |
* where the download directory is specified, but the filename is |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
* obtained from the {@code Content-Disposition} response header. The |
48083 | 541 |
* {@code Content-Disposition} header must specify the <i>attachment</i> |
542 |
* type and must also contain a <i>filename</i> parameter. If the |
|
543 |
* filename specifies multiple path components only the final component |
|
544 |
* is used as the filename (with the given directory name). |
|
545 |
* |
|
546 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
547 |
* been completely written to the file and {@link #body()} returns a |
|
548 |
* {@code Path} object for the file. The returned {@code Path} is the |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
* combination of the supplied directory name and the file name supplied |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
550 |
* by the server. If the destination directory does not exist or cannot |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
551 |
* be written to, then the response will fail with an {@link IOException}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
552 |
* |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
553 |
* <p> Security manager permission checks are performed in this factory |
56141 | 554 |
* method, when the {@code BodyHandler} is created. Care must be taken |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
555 |
* that the {@code BodyHandler} is not shared with untrusted code. |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
556 |
* |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
557 |
* @param directory the directory to store the file in |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
558 |
* @param openOptions open options used when opening the file |
44847 | 559 |
* @return a response body handler |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
560 |
* @throws IllegalArgumentException if the given path does not exist, |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
561 |
* is not a directory, is not writable, or if an invalid set |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
562 |
* of open options are specified |
48083 | 563 |
* @throws SecurityException If a security manager has been installed |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
564 |
* and it denies {@linkplain SecurityManager#checkRead(String) |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
565 |
* read access} or {@linkplain SecurityManager#checkWrite(String) |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
566 |
* write access} to the directory. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
568 |
public static BodyHandler<Path> ofFileDownload(Path directory, |
48083 | 569 |
OpenOption... openOptions) { |
570 |
Objects.requireNonNull(directory); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
571 |
List<OpenOption> opts = List.of(openOptions); |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
572 |
if (opts.contains(DELETE_ON_CLOSE)) { |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
573 |
throw new IllegalArgumentException("invalid option: " + DELETE_ON_CLOSE); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
574 |
} |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
575 |
|
48083 | 576 |
SecurityManager sm = System.getSecurityManager(); |
577 |
if (sm != null) { |
|
578 |
String fn = pathForSecurityCheck(directory); |
|
579 |
sm.checkWrite(fn); |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
580 |
sm.checkRead(fn); |
48083 | 581 |
} |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
582 |
|
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
583 |
if (Files.notExists(directory)) |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
584 |
throw new IllegalArgumentException("non-existent directory: " + directory); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
585 |
if (!Files.isDirectory(directory)) |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
586 |
throw new IllegalArgumentException("not a directory: " + directory); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
587 |
if (!Files.isWritable(directory)) |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
588 |
throw new IllegalArgumentException("non-writable directory: " + directory); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
589 |
|
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
590 |
return new FileDownloadBodyHandler(directory, opts); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
591 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
592 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
593 |
/** |
48083 | 594 |
* Returns a {@code BodyHandler<InputStream>} that returns a |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
595 |
* {@link BodySubscriber BodySubscriber}{@code <InputStream>} obtained from |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
596 |
* {@link BodySubscribers#ofInputStream() BodySubscribers.ofInputStream}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
597 |
* |
48083 | 598 |
* <p> When the {@code HttpResponse} object is returned, the response |
599 |
* headers will have been completely read, but the body may not have |
|
600 |
* been fully received yet. The {@link #body()} method returns an |
|
601 |
* {@link InputStream} from which the body can be read as it is received. |
|
602 |
* |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
603 |
* @apiNote See {@link BodySubscribers#ofInputStream()} for more |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
604 |
* information. |
48083 | 605 |
* |
44847 | 606 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
607 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
608 |
public static BodyHandler<InputStream> ofInputStream() { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
609 |
return (status, headers) -> BodySubscribers.ofInputStream(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
610 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
611 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
612 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
613 |
* Returns a {@code BodyHandler<Stream<String>>} that returns a |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
614 |
* {@link BodySubscriber BodySubscriber}{@code <Stream<String>>} obtained |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
615 |
* from {@link BodySubscribers#ofLines(Charset) BodySubscribers.ofLines(charset)}. |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
616 |
* The {@link Charset charset} used to decode the response body bytes is |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
617 |
* obtained from the HTTP response headers as specified by {@link #ofString()}, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
618 |
* and lines are delimited in the manner of {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
619 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
620 |
* <p> When the {@code HttpResponse} object is returned, the body may |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
621 |
* not have been completely received. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
622 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
623 |
* @return a response body handler |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
624 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
625 |
public static BodyHandler<Stream<String>> ofLines() { |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
626 |
return (status, headers) -> |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
627 |
BodySubscribers.ofLines(charsetFrom(headers)); |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
628 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
629 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
630 |
/** |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
631 |
* Returns a {@code BodyHandler<Void>} that returns a |
48083 | 632 |
* {@link BodySubscriber BodySubscriber}{@code <Void>} obtained from |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
633 |
* {@link BodySubscribers#ofByteArrayConsumer(Consumer) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
634 |
* BodySubscribers.ofByteArrayConsumer(Consumer)}. |
48083 | 635 |
* |
636 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
637 |
* been completely written to the consumer. |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
638 |
* |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
639 |
* @apiNote |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
640 |
* The subscriber returned by this handler is not flow controlled. |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
641 |
* Therefore, the supplied consumer must be able to process whatever |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
642 |
* amount of data is delivered in a timely fashion. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
643 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
644 |
* @param consumer a Consumer to accept the response body |
44847 | 645 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
646 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
647 |
public static BodyHandler<Void> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
648 |
ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
649 |
Objects.requireNonNull(consumer); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
650 |
return (status, headers) -> BodySubscribers.ofByteArrayConsumer(consumer); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
651 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
652 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
653 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
654 |
* Returns a {@code BodyHandler<byte[]>} that returns a |
48083 | 655 |
* {@link BodySubscriber BodySubscriber}<{@code byte[]}> obtained |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
656 |
* from {@link BodySubscribers#ofByteArray() BodySubscribers.ofByteArray()}. |
48083 | 657 |
* |
658 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
659 |
* been completely written to the byte array. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
660 |
* |
44847 | 661 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
662 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
663 |
public static BodyHandler<byte[]> ofByteArray() { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
664 |
return (status, headers) -> BodySubscribers.ofByteArray(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
665 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
666 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
667 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
668 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 669 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
670 |
* {@link BodySubscribers#ofString(Charset) BodySubscribers.ofString(Charset)}. |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
671 |
* The body is decoded using the character set specified in |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
672 |
* the {@code Content-Type} response header. If there is no such |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
673 |
* header, or the character set is not supported, then |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
674 |
* {@link StandardCharsets#UTF_8 UTF_8} is used. |
48083 | 675 |
* |
676 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
677 |
* been completely written to the string. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
678 |
* |
44847 | 679 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
680 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
681 |
public static BodyHandler<String> ofString() { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
682 |
return (status, headers) -> BodySubscribers.ofString(charsetFrom(headers)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
683 |
} |
48083 | 684 |
|
685 |
/** |
|
686 |
* Returns a {@code BodyHandler} which, when invoked, returns a {@linkplain |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
687 |
* BodySubscribers#buffering(BodySubscriber,int) buffering BodySubscriber} |
48083 | 688 |
* that buffers data before delivering it to the downstream subscriber. |
689 |
* These {@code BodySubscriber} instances are created by calling |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
690 |
* {@link BodySubscribers#buffering(BodySubscriber,int) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
691 |
* BodySubscribers.buffering} with a subscriber obtained from the given |
48083 | 692 |
* downstream handler and the {@code bufferSize} parameter. |
693 |
* |
|
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
694 |
* @param <T> the response body type |
48083 | 695 |
* @param downstreamHandler the downstream handler |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
696 |
* @param bufferSize the buffer size parameter passed to {@link |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
697 |
* BodySubscribers#buffering(BodySubscriber,int) BodySubscribers.buffering} |
48083 | 698 |
* @return a body handler |
699 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
700 |
*/ |
|
701 |
public static <T> BodyHandler<T> buffering(BodyHandler<T> downstreamHandler, |
|
702 |
int bufferSize) { |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
703 |
Objects.requireNonNull(downstreamHandler); |
48083 | 704 |
if (bufferSize <= 0) |
705 |
throw new IllegalArgumentException("must be greater than 0"); |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
706 |
return (status, headers) -> BodySubscribers |
48083 | 707 |
.buffering(downstreamHandler.apply(status, headers), |
708 |
bufferSize); |
|
709 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
710 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
711 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
712 |
/** |
56025 | 713 |
* A handler for push promises. |
714 |
* |
|
715 |
* <p> A <i>push promise</i> is a synthetic request sent by an HTTP/2 server |
|
716 |
* when retrieving an initiating client-sent request. The server has |
|
717 |
* determined, possibly through inspection of the initiating request, that |
|
718 |
* the client will likely need the promised resource, and hence pushes a |
|
719 |
* synthetic push request, in the form of a push promise, to the client. The |
|
720 |
* client can choose to accept or reject the push promise request. |
|
721 |
* |
|
722 |
* <p> A push promise request may be received up to the point where the |
|
723 |
* response body of the initiating client-sent request has been fully |
|
724 |
* received. The delivery of a push promise response, however, is not |
|
725 |
* coordinated with the delivery of the response to the initiating |
|
726 |
* client-sent request. |
|
727 |
* |
|
728 |
* @param <T> the push promise response body type |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
729 |
* @since 11 |
56010 | 730 |
*/ |
731 |
public interface PushPromiseHandler<T> { |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
732 |
|
56010 | 733 |
/** |
56025 | 734 |
* Notification of an incoming push promise. |
735 |
* |
|
736 |
* <p> This method is invoked once for each push promise received, up |
|
737 |
* to the point where the response body of the initiating client-sent |
|
738 |
* request has been fully received. |
|
739 |
* |
|
740 |
* <p> A push promise is accepted by invoking the given {@code acceptor} |
|
741 |
* function. The {@code acceptor} function must be passed a non-null |
|
742 |
* {@code BodyHandler}, that is to be used to handle the promise's |
|
743 |
* response body. The acceptor function will return a {@code |
|
744 |
* CompletableFuture} that completes with the promise's response. |
|
745 |
* |
|
746 |
* <p> If the {@code acceptor} function is not successfully invoked, |
|
747 |
* then the push promise is rejected. The {@code acceptor} function will |
|
748 |
* throw an {@code IllegalStateException} if invoked more than once. |
|
749 |
* |
|
750 |
* @param initiatingRequest the initiating client-send request |
|
751 |
* @param pushPromiseRequest the synthetic push request |
|
752 |
* @param acceptor the acceptor function that must be successfully |
|
753 |
* invoked to accept the push promise |
|
56010 | 754 |
*/ |
755 |
public void applyPushPromise( |
|
56025 | 756 |
HttpRequest initiatingRequest, |
757 |
HttpRequest pushPromiseRequest, |
|
56010 | 758 |
Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor |
759 |
); |
|
760 |
||
761 |
||
762 |
/** |
|
763 |
* Returns a push promise handler that accumulates push promises, and |
|
764 |
* their responses, into the given map. |
|
765 |
* |
|
56025 | 766 |
* <p> Entries are added to the given map for each push promise accepted. |
767 |
* The entry's key is the push request, and the entry's value is a |
|
768 |
* {@code CompletableFuture} that completes with the response |
|
769 |
* corresponding to the key's push request. A push request is rejected / |
|
770 |
* cancelled if there is already an entry in the map whose key is |
|
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
771 |
* {@link HttpRequest#equals equal} to it. A push request is |
56025 | 772 |
* rejected / cancelled if it does not have the same origin as its |
773 |
* initiating request. |
|
56010 | 774 |
* |
775 |
* <p> Entries are added to the given map as soon as practically |
|
776 |
* possible when a push promise is received and accepted. That way code, |
|
777 |
* using such a map like a cache, can determine if a push promise has |
|
778 |
* been issued by the server and avoid making, possibly, unnecessary |
|
779 |
* requests. |
|
780 |
* |
|
56025 | 781 |
* <p> The delivery of a push promise response is not coordinated with |
782 |
* the delivery of the response to the initiating client-sent request. |
|
783 |
* However, when the response body for the initiating client-sent |
|
784 |
* request has been fully received, the map is guaranteed to be fully |
|
785 |
* populated, that is, no more entries will be added. The individual |
|
786 |
* {@code CompletableFutures} contained in the map may or may not |
|
787 |
* already be completed at this point. |
|
56010 | 788 |
* |
56025 | 789 |
* @param <T> the push promise response body type |
56010 | 790 |
* @param pushPromiseHandler t he body handler to use for push promises |
791 |
* @param pushPromisesMap a map to accumulate push promises into |
|
56025 | 792 |
* @return a push promise handler |
56010 | 793 |
*/ |
794 |
public static <T> PushPromiseHandler<T> |
|
56034
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56031
diff
changeset
|
795 |
of(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler, |
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56031
diff
changeset
|
796 |
ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) { |
56010 | 797 |
return new PushPromisesHandlerWithMap<>(pushPromiseHandler, pushPromisesMap); |
798 |
} |
|
799 |
} |
|
800 |
||
801 |
/** |
|
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
802 |
* A {@code BodySubscriber} consumes response body bytes and converts them |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
803 |
* into a higher-level Java type. The class {@link BodySubscribers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
804 |
* BodySubscriber} provides implementations of many common body subscribers. |
48083 | 805 |
* |
806 |
* <p> The object acts as a {@link Flow.Subscriber}<{@link List}<{@link |
|
807 |
* ByteBuffer}>> to the HTTP client implementation, which publishes |
|
56071 | 808 |
* unmodifiable lists of read-only ByteBuffers containing the response body. |
809 |
* The Flow of data, as well as the order of ByteBuffers in the Flow lists, |
|
810 |
* is a strictly ordered representation of the response body. Both the Lists |
|
811 |
* and the ByteBuffers, once passed to the subscriber, are no longer used by |
|
812 |
* the HTTP client. The subscriber converts the incoming buffers of data to |
|
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
813 |
* some higher-level Java type {@code T}. |
48083 | 814 |
* |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
815 |
* <p> The {@link #getBody()} method returns a |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
816 |
* {@link CompletionStage}<{@code T}> that provides the response body |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
817 |
* object. The {@code CompletionStage} must be obtainable at any time. When |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
818 |
* it completes depends on the nature of type {@code T}. In many cases, |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
819 |
* when {@code T} represents the entire body after being consumed then |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
820 |
* the {@code CompletionStage} completes after the body has been consumed. |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
821 |
* If {@code T} is a streaming type, such as {@link java.io.InputStream |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
822 |
* InputStream}, then it completes before the body has been read, because |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
823 |
* the calling code uses the {@code InputStream} to consume the data. |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
824 |
* |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
825 |
* @apiNote To ensure that all resources associated with the corresponding |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
826 |
* HTTP exchange are properly released, an implementation of {@code |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
827 |
* BodySubscriber} should ensure to {@link Flow.Subscription#request |
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
828 |
* request} more data until one of {@link #onComplete() onComplete} or |
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
829 |
* {@link #onError(Throwable) onError} are signalled, or {@link |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
830 |
* Flow.Subscription#request cancel} its {@linkplain |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
831 |
* #onSubscribe(Flow.Subscription) subscription} if unable or unwilling to |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
832 |
* do so. Calling {@code cancel} before exhausting the response body data |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
833 |
* may cause the underlying HTTP connection to be closed and prevent it |
48083 | 834 |
* from being reused for subsequent operations. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
835 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
836 |
* @param <T> the response body type |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
837 |
* @see BodySubscribers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
838 |
* @since 11 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
839 |
*/ |
48083 | 840 |
public interface BodySubscriber<T> |
841 |
extends Flow.Subscriber<List<ByteBuffer>> { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
842 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
843 |
/** |
48083 | 844 |
* Returns a {@code CompletionStage} which when completed will return |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
845 |
* the response body object. This method can be called at any time |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
846 |
* relative to the other {@link Flow.Subscriber} methods and is invoked |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
847 |
* using the client's {@link HttpClient#executor() executor}. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
848 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
849 |
* @return a CompletionStage for the response body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
850 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
851 |
public CompletionStage<T> getBody(); |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
852 |
} |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
853 |
|
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
854 |
/** |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
855 |
* Implementations of {@link BodySubscriber BodySubscriber} that implement |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
856 |
* various useful subscribers, such as converting the response body bytes |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
857 |
* into a String, or streaming the bytes to a file. |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
858 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
859 |
* <p>The following are examples of using the predefined body subscribers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
860 |
* to convert a flow of response body data into common high-level Java |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
861 |
* objects: |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
862 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
863 |
* <pre>{@code // Streams the response body to a File |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
864 |
* HttpResponse<byte[]> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
865 |
* .send(request, (statusCode, responseHeaders) -> BodySubscribers.ofByteArray()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
866 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
867 |
* // Accumulates the response body and returns it as a byte[] |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
868 |
* HttpResponse<byte[]> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
869 |
* .send(request, (statusCode, responseHeaders) -> BodySubscribers.ofByteArray()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
870 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
871 |
* // Discards the response body |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
872 |
* HttpResponse<Void> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
873 |
* .send(request, (statusCode, responseHeaders) -> BodySubscribers.discarding()); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
874 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
875 |
* // Accumulates the response body as a String then maps it to its bytes |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
876 |
* HttpResponse<byte[]> response = client |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
877 |
* .send(request, (sc, hdrs) -> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
878 |
* BodySubscribers.mapping(BodySubscribers.ofString(), String::getBytes)); |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
879 |
* }</pre> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
880 |
* |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
881 |
* @since 11 |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
882 |
*/ |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
883 |
public static class BodySubscribers { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
884 |
|
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
885 |
private BodySubscribers() { } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
886 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
887 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
888 |
* Returns a body subscriber that forwards all response body to the |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
889 |
* given {@code Flow.Subscriber}. The {@linkplain BodySubscriber#getBody() |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
890 |
* completion stage} of the returned body subscriber completes after one |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
891 |
* of the given subscribers {@code onComplete} or {@code onError} has |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
892 |
* been invoked. |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
893 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
894 |
* @apiNote This method can be used as an adapter between {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
895 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
896 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
897 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
898 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
899 |
*/ |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
900 |
public static BodySubscriber<Void> |
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
901 |
fromSubscriber(Subscriber<? super List<ByteBuffer>> subscriber) { |
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
902 |
return new ResponseSubscribers.SubscriberAdapter<>(subscriber, s -> null); |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
903 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
904 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
905 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
906 |
* Returns a body subscriber that forwards all response body to the |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
907 |
* given {@code Flow.Subscriber}. The {@linkplain BodySubscriber#getBody() |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
908 |
* completion stage} of the returned body subscriber completes after one |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
909 |
* of the given subscribers {@code onComplete} or {@code onError} has |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
910 |
* been invoked. |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
911 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
912 |
* <p> The given {@code finisher} function is applied after the given |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
913 |
* subscriber's {@code onComplete} has been invoked. The {@code finisher} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
914 |
* function is invoked with the given subscriber, and returns a value |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
915 |
* that is set as the response's body. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
916 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
917 |
* @apiNote This method can be used as an adapter between {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
918 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
919 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
920 |
* @param <S> the type of the Subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
921 |
* @param <T> the type of the response body |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
922 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
923 |
* @param finisher a function to be applied after the subscriber has |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
924 |
* completed |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
925 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
926 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
927 |
public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodySubscriber<T> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
928 |
fromSubscriber(S subscriber, |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
929 |
Function<? super S,? extends T> finisher) { |
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
930 |
return new ResponseSubscribers.SubscriberAdapter<>(subscriber, finisher); |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
931 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
932 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
933 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
934 |
* Returns a body subscriber that forwards all response body to the |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
935 |
* given {@code Flow.Subscriber}, line by line. |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
936 |
* The {@linkplain BodySubscriber#getBody() completion |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
937 |
* stage} of the returned body subscriber completes after one of the |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
938 |
* given subscribers {@code onComplete} or {@code onError} has been |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
939 |
* invoked. |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
940 |
* Bytes are decoded using the {@link StandardCharsets#UTF_8 |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
941 |
* UTF-8} charset, and lines are delimited in the manner of |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
942 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
943 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
944 |
* @apiNote This method can be used as an adapter between {@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
945 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
946 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
947 |
* @implNote This is equivalent to calling <pre>{@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
948 |
* fromLineSubscriber(subscriber, s -> null, StandardCharsets.UTF_8, null) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
949 |
* }</pre> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
950 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
951 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
952 |
* @return a body subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
953 |
*/ |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
954 |
public static BodySubscriber<Void> |
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
955 |
fromLineSubscriber(Subscriber<? super String> subscriber) { |
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
956 |
return fromLineSubscriber(subscriber, s -> null, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
957 |
StandardCharsets.UTF_8, null); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
958 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
959 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
960 |
/** |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
961 |
* Returns a body subscriber that forwards all response body to the |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
962 |
* given {@code Flow.Subscriber}, line by line. The {@linkplain |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
963 |
* BodySubscriber#getBody() completion stage} of the returned body |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
964 |
* subscriber completes after one of the given subscribers |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
965 |
* {@code onComplete} or {@code onError} has been invoked. |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
966 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
967 |
* <p> The given {@code finisher} function is applied after the given |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
968 |
* subscriber's {@code onComplete} has been invoked. The {@code finisher} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
969 |
* function is invoked with the given subscriber, and returns a value |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
970 |
* that is set as the response's body. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
971 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
972 |
* @apiNote This method can be used as an adapter between {@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
973 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
974 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
975 |
* @param <S> the type of the Subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
976 |
* @param <T> the type of the response body |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
977 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
978 |
* @param finisher a function to be applied after the subscriber has |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
979 |
* completed |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
980 |
* @param charset a {@link Charset} to decode the bytes |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
981 |
* @param lineSeparator an optional line separator: can be {@code null}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
982 |
* in which case lines will be delimited in the manner of |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
983 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
984 |
* @return a body subscriber |
56157 | 985 |
* @throws IllegalArgumentException if the supplied {@code lineSeparator} |
986 |
* is the empty string |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
987 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
988 |
public static <S extends Subscriber<? super String>,T> BodySubscriber<T> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
989 |
fromLineSubscriber(S subscriber, |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
990 |
Function<? super S,? extends T> finisher, |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
991 |
Charset charset, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
992 |
String lineSeparator) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
993 |
return LineSubscriberAdapter.create(subscriber, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
994 |
finisher, charset, lineSeparator); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
995 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
996 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
997 |
/** |
48083 | 998 |
* Returns a body subscriber which stores the response body as a {@code |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
999 |
* String} converted using the given {@code Charset}. |
48083 | 1000 |
* |
1001 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1002 |
* the entire response has been read. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1003 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1004 |
* @param charset the character set to convert the String with |
48083 | 1005 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1006 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1007 |
public static BodySubscriber<String> ofString(Charset charset) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
1008 |
Objects.requireNonNull(charset); |
48083 | 1009 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1010 |
bytes -> new String(bytes, charset) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1011 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1012 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1013 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1014 |
/** |
48083 | 1015 |
* Returns a {@code BodySubscriber} which stores the response body as a |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1016 |
* byte array. |
48083 | 1017 |
* |
1018 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1019 |
* the entire response has been read. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1020 |
* |
48083 | 1021 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1022 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1023 |
public static BodySubscriber<byte[]> ofByteArray() { |
48083 | 1024 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1025 |
Function.identity() // no conversion |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1026 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1027 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1028 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
/** |
48083 | 1030 |
* Returns a {@code BodySubscriber} which stores the response body in a |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1031 |
* file opened with the given options and name. The file will be opened |
48083 | 1032 |
* with the given options using {@link FileChannel#open(Path,OpenOption...) |
1033 |
* FileChannel.open} just before the body is read. Any exception thrown |
|
1034 |
* will be returned or thrown from {@link HttpClient#send(HttpRequest, |
|
1035 |
* BodyHandler) HttpClient::send} or {@link HttpClient#sendAsync(HttpRequest, |
|
1036 |
* BodyHandler) HttpClient::sendAsync} as appropriate. |
|
1037 |
* |
|
1038 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1039 |
* the entire response has been read. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1040 |
* |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1041 |
* <p> Security manager permission checks are performed in this factory |
56141 | 1042 |
* method, when the {@code BodySubscriber} is created. Care must be taken |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1043 |
* that the {@code BodyHandler} is not shared with untrusted code. |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1044 |
* |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1045 |
* @param file the file to store the body in |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1046 |
* @param openOptions the list of options to open the file with |
48083 | 1047 |
* @return a body subscriber |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1048 |
* @throws IllegalArgumentException if an invalid set of open options |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1049 |
* are specified |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1050 |
* @throws SecurityException if a security manager has been installed |
48083 | 1051 |
* and it denies {@link SecurityManager#checkWrite(String) |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1052 |
* write access} to the file |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1053 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1054 |
public static BodySubscriber<Path> ofFile(Path file, OpenOption... openOptions) { |
48083 | 1055 |
Objects.requireNonNull(file); |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
1056 |
List<OpenOption> opts = List.of(openOptions); |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1057 |
if (opts.contains(DELETE_ON_CLOSE) || opts.contains(READ)) { |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1058 |
// these options make no sense, since the FileChannel is not exposed |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1059 |
throw new IllegalArgumentException("invalid openOptions: " + opts); |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1060 |
} |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1061 |
|
48083 | 1062 |
SecurityManager sm = System.getSecurityManager(); |
1063 |
if (sm != null) { |
|
1064 |
String fn = pathForSecurityCheck(file); |
|
1065 |
sm.checkWrite(fn); |
|
1066 |
} |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1067 |
return new PathSubscriber(file, opts); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1068 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1069 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1070 |
/** |
48083 | 1071 |
* Returns a {@code BodySubscriber} which stores the response body in a |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1072 |
* file opened with the given name. |
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1073 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1074 |
* <p> Equivalent to: {@code ofFile(file, CREATE, WRITE)} |
48083 | 1075 |
* |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1076 |
* <p> Security manager permission checks are performed in this factory |
56141 | 1077 |
* method, when the {@code BodySubscriber} is created. Care must be taken |
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56131
diff
changeset
|
1078 |
* that the {@code BodyHandler} is not shared with untrusted code. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1079 |
* |
48083 | 1080 |
* @param file the file to store the body in |
1081 |
* @return a body subscriber |
|
1082 |
* @throws SecurityException if a security manager has been installed |
|
1083 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
1084 |
* write access} to the file |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1085 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1086 |
public static BodySubscriber<Path> ofFile(Path file) { |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1087 |
return ofFile(file, CREATE, WRITE); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1088 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1089 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1090 |
/** |
48083 | 1091 |
* Returns a {@code BodySubscriber} which provides the incoming body |
1092 |
* data to the provided Consumer of {@code Optional<byte[]>}. Each |
|
1093 |
* call to {@link Consumer#accept(java.lang.Object) Consumer.accept()} |
|
1094 |
* will contain a non empty {@code Optional}, except for the final |
|
1095 |
* invocation after all body data has been read, when the {@code |
|
1096 |
* Optional} will be empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1097 |
* |
48083 | 1098 |
* <p> The {@link HttpResponse} using this subscriber is available after |
1099 |
* the entire response has been read. |
|
1100 |
* |
|
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1101 |
* @apiNote |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1102 |
* This subscriber is not flow controlled. |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1103 |
* Therefore, the supplied consumer must be able to process whatever |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1104 |
* amount of data is delivered in a timely fashion. |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1105 |
* |
48083 | 1106 |
* @param consumer a Consumer of byte arrays |
1107 |
* @return a BodySubscriber |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1108 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1109 |
public static BodySubscriber<Void> |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1110 |
ofByteArrayConsumer(Consumer<Optional<byte[]>> consumer) { |
48083 | 1111 |
return new ResponseSubscribers.ConsumerSubscriber(consumer); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1112 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1113 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1114 |
/** |
48083 | 1115 |
* Returns a {@code BodySubscriber} which streams the response body as |
1116 |
* an {@link InputStream}. |
|
1117 |
* |
|
1118 |
* <p> The {@link HttpResponse} using this subscriber is available |
|
1119 |
* immediately after the response headers have been read, without |
|
1120 |
* requiring to wait for the entire body to be processed. The response |
|
1121 |
* body can then be read directly from the {@link InputStream}. |
|
1122 |
* |
|
1123 |
* @apiNote To ensure that all resources associated with the |
|
1124 |
* corresponding exchange are properly released the caller must |
|
1125 |
* ensure to either read all bytes until EOF is reached, or call |
|
1126 |
* {@link InputStream#close} if it is unable or unwilling to do so. |
|
1127 |
* Calling {@code close} before exhausting the stream may cause |
|
1128 |
* the underlying HTTP connection to be closed and prevent it |
|
1129 |
* from being reused for subsequent operations. |
|
1130 |
* |
|
1131 |
* @return a body subscriber that streams the response body as an |
|
1132 |
* {@link InputStream}. |
|
1133 |
*/ |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1134 |
public static BodySubscriber<InputStream> ofInputStream() { |
48083 | 1135 |
return new ResponseSubscribers.HttpResponseInputStream(); |
1136 |
} |
|
1137 |
||
1138 |
/** |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1139 |
* Returns a {@code BodySubscriber} which streams the response body as |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1140 |
* a {@link Stream Stream<String>}, where each string in the stream |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1141 |
* corresponds to a line as defined by {@link BufferedReader#lines()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1142 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1143 |
* <p> The {@link HttpResponse} using this subscriber is available |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1144 |
* immediately after the response headers have been read, without |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1145 |
* requiring to wait for the entire body to be processed. The response |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1146 |
* body can then be read directly from the {@link Stream}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1147 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1148 |
* @apiNote To ensure that all resources associated with the |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1149 |
* corresponding exchange are properly released the caller must |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1150 |
* ensure to either read all lines until the stream is exhausted, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1151 |
* or call {@link Stream#close} if it is unable or unwilling to do so. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1152 |
* Calling {@code close} before exhausting the stream may cause |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1153 |
* the underlying HTTP connection to be closed and prevent it |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1154 |
* from being reused for subsequent operations. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1155 |
* |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1156 |
* @param charset the character set to use when converting bytes to characters |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1157 |
* @return a body subscriber that streams the response body as a |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1158 |
* {@link Stream Stream<String>}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1159 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1160 |
* @see BufferedReader#lines() |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1161 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1162 |
public static BodySubscriber<Stream<String>> ofLines(Charset charset) { |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1163 |
return ResponseSubscribers.createLineStream(charset); |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1164 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1165 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1166 |
/** |
48083 | 1167 |
* Returns a response subscriber which discards the response body. The |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1168 |
* supplied value is the value that will be returned from |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1169 |
* {@link HttpResponse#body()}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1170 |
* |
56157 | 1171 |
* @param <U> the type of the response body |
48083 | 1172 |
* @param value the value to return from HttpResponse.body(), may be {@code null} |
1173 |
* @return a {@code BodySubscriber} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1174 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1175 |
public static <U> BodySubscriber<U> replacing(U value) { |
48083 | 1176 |
return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(value)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1177 |
} |
48083 | 1178 |
|
1179 |
/** |
|
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1180 |
* Returns a response subscriber which discards the response body. |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1181 |
* |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1182 |
* @return a response body subscriber |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1183 |
*/ |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1184 |
public static BodySubscriber<Void> discarding() { |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1185 |
return new ResponseSubscribers.NullSubscriber<>(Optional.ofNullable(null)); |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1186 |
} |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1187 |
|
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1188 |
/** |
48083 | 1189 |
* Returns a {@code BodySubscriber} which buffers data before delivering |
1190 |
* it to the given downstream subscriber. The subscriber guarantees to |
|
1191 |
* deliver {@code buffersize} bytes of data to each invocation of the |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1192 |
* downstream's {@link BodySubscriber#onNext(Object) onNext} method, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1193 |
* except for the final invocation, just before |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1194 |
* {@link BodySubscriber#onComplete() onComplete} is invoked. The final |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1195 |
* invocation of {@code onNext} may contain fewer than {@code bufferSize} |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1196 |
* bytes. |
48083 | 1197 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1198 |
* <p> The returned subscriber delegates its {@link BodySubscriber#getBody() |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1199 |
* getBody()} method to the downstream subscriber. |
48083 | 1200 |
* |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
1201 |
* @param <T> the type of the response body |
48083 | 1202 |
* @param downstream the downstream subscriber |
1203 |
* @param bufferSize the buffer size |
|
1204 |
* @return a buffering body subscriber |
|
1205 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
1206 |
*/ |
|
1207 |
public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream, |
|
1208 |
int bufferSize) { |
|
1209 |
if (bufferSize <= 0) |
|
1210 |
throw new IllegalArgumentException("must be greater than 0"); |
|
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
1211 |
return new BufferingSubscriber<>(downstream, bufferSize); |
48083 | 1212 |
} |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1213 |
|
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1214 |
/** |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1215 |
* Returns a {@code BodySubscriber} whose response body value is that of |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1216 |
* the result of applying the given function to the body object of the |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1217 |
* given {@code upstream} {@code BodySubscriber}. |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1218 |
* |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1219 |
* <p> The mapping function is executed using the client's {@linkplain |
56099
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
1220 |
* HttpClient#executor() executor}, and can therefore be used to map any |
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
1221 |
* response body type, including blocking {@link InputStream}, as shown |
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
1222 |
* in the following example which uses a well-known JSON parser to |
41ba54ac9403
http-client-branch: revert overzealous use of @linkplain
chegar
parents:
56097
diff
changeset
|
1223 |
* convert an {@code InputStream} into any annotated Java type. |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1224 |
* |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1225 |
* <p>For example: |
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1226 |
* <pre> {@code public static <W> BodySubscriber<W> asJSON(Class<W> targetType) { |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1227 |
* BodySubscriber<InputStream> upstream = BodySubscribers.ofInputStream(); |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1228 |
* |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56160
diff
changeset
|
1229 |
* BodySubscriber<W> downstream = BodySubscribers.mapping( |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1230 |
* upstream, |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1231 |
* (InputStream is) -> { |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1232 |
* try (InputStream stream = is) { |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1233 |
* ObjectMapper objectMapper = new ObjectMapper(); |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1234 |
* return objectMapper.readValue(stream, targetType); |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1235 |
* } catch (IOException e) { |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1236 |
* throw new UncheckedIOException(e); |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1237 |
* } |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1238 |
* }); |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1239 |
* return downstream; |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1240 |
* } }</pre> |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1241 |
* |
56157 | 1242 |
* @param <T> the upstream body type |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1243 |
* @param <U> the type of the body subscriber returned |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1244 |
* @param upstream the body subscriber to be mapped |
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1245 |
* @param mapper the mapping function |
56157 | 1246 |
* @return a mapping body subscriber |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1247 |
*/ |
56097
15dc43936d39
http-client-branch: javadoc refresh and minor fixes
chegar
parents:
56092
diff
changeset
|
1248 |
public static <T,U> BodySubscriber<U> mapping(BodySubscriber<T> upstream, |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
1249 |
Function<? super T, ? extends U> mapper) |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1250 |
{ |
56159
039ab5a71a5c
http-client-branch: type paramater review of generic methods
chegar
parents:
56157
diff
changeset
|
1251 |
return new ResponseSubscribers.MappingSubscriber<>(upstream, mapper); |
56082
1da51fab3032
http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
56079
diff
changeset
|
1252 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1253 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1254 |
} |