author | chegar |
Tue, 06 Feb 2018 11:15:40 +0000 | |
branch | http-client-branch |
changeset 56077 | 3f6b75adcdc0 |
parent 56075 | c76699d2e572 |
child 56078 | 6c11b48a0695 |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http; |
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; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.nio.file.OpenOption; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.file.Path; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.nio.file.Paths; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.nio.file.StandardOpenOption; |
48083 | 40 |
import java.security.AccessControlContext; |
41 |
import java.util.List; |
|
42 |
import java.util.Objects; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.util.concurrent.CompletionStage; |
56010 | 46 |
import java.util.concurrent.ConcurrentMap; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
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
|
48 |
import java.util.concurrent.Flow.Subscriber; |
56010 | 49 |
import java.util.function.BiFunction; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.function.Consumer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
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
|
52 |
import java.util.stream.Stream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
import javax.net.ssl.SSLParameters; |
56075
c76699d2e572
http-client-branch: move BufferingSubscriber to internal
chegar
parents:
56071
diff
changeset
|
54 |
import jdk.incubator.http.internal.BufferingSubscriber; |
56077
3f6b75adcdc0
http-client-branch: move ResponseSubscribers to internal
chegar
parents:
56075
diff
changeset
|
55 |
import jdk.incubator.http.internal.ResponseSubscribers; |
56075
c76699d2e572
http-client-branch: move BufferingSubscriber to internal
chegar
parents:
56071
diff
changeset
|
56 |
import static jdk.incubator.http.internal.common.Utils.unchecked; |
c76699d2e572
http-client-branch: move BufferingSubscriber to internal
chegar
parents:
56071
diff
changeset
|
57 |
import static jdk.incubator.http.internal.common.Utils.charsetFrom; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
/** |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
60 |
* Represents a response to a {@link HttpRequest}. |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
61 |
* {@Incubating} |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
62 |
* |
48083 | 63 |
* <p> A {@code HttpResponse} is available when the response status code and |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
64 |
* headers have been received, and typically after the response body has also |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
65 |
* been received. This depends on the response body handler provided when |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
66 |
* sending the request. In all cases, the response body handler is invoked |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
67 |
* before the body is read. This gives applications an opportunity to decide |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
68 |
* how to handle the body. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
* <p> Methods are provided in this class for accessing the response headers, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
* and response body. |
48083 | 72 |
* |
73 |
* <p><b>Response handlers and subscribers</b> |
|
74 |
* |
|
75 |
* <p> Response bodies are handled at two levels. Application code supplies a |
|
76 |
* response handler ({@link BodyHandler}) which may examine the response status |
|
77 |
* code and headers, and which then returns a {@link BodySubscriber} to actually |
|
78 |
* read (or discard) the body and convert it into some useful Java object type. |
|
79 |
* The handler can return one of the pre-defined subscriber types, or a custom |
|
80 |
* subscriber, or if the body is to be discarded it can call {@link |
|
81 |
* BodySubscriber#discard(Object) discard} and return a subscriber which |
|
82 |
* discards the response body. Static implementations of both handlers and |
|
83 |
* subscribers are provided in {@linkplain BodyHandler BodyHandler} and |
|
84 |
* {@linkplain BodySubscriber BodySubscriber} respectively. In all cases, the |
|
85 |
* handler functions provided are convenience implementations which ignore the |
|
86 |
* supplied status code and headers and return the relevant pre-defined {@code |
|
87 |
* BodySubscriber}. |
|
88 |
* |
|
89 |
* <p> See {@link BodyHandler} for example usage. |
|
42460
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 |
* @param <T> the response body type |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
* @since 9 |
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 |
public abstract class HttpResponse<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
* Creates an HttpResponse. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
protected HttpResponse() { } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
* 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
|
103 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
* @return the response code |
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 |
public abstract int statusCode(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
/** |
48083 | 109 |
* Returns the {@link HttpRequest} corresponding to this response. |
110 |
* |
|
111 |
* <p> This may not be the original request provided by the caller, |
|
112 |
* for example, if that request was redirected. |
|
113 |
* |
|
114 |
* @see #previousResponse() |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
* @return the request |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
public abstract HttpRequest request(); |
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 |
/** |
48083 | 121 |
* Returns an {@code Optional} containing the previous intermediate response |
122 |
* if one was received. An intermediate response is one that is received |
|
123 |
* as a result of redirection or authentication. If no previous response |
|
124 |
* 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
|
125 |
* |
48083 | 126 |
* @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
|
127 |
*/ |
48083 | 128 |
public abstract Optional<HttpResponse<T>> previousResponse(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
* Returns the received response headers. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
* @return the response headers |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
public abstract HttpHeaders headers(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
/** |
48083 | 138 |
* Returns the body. Depending on the type of {@code T}, the returned body |
139 |
* 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
|
140 |
* {@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
|
141 |
* 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
|
142 |
* |
48083 | 143 |
* <p> If this {@code HttpResponse} was returned from an invocation of |
144 |
* {@link #previousResponse()} then this method returns {@code null} |
|
145 |
* |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
* @return the body |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
public abstract T body(); |
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 {@link javax.net.ssl.SSLParameters} in effect for this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
* response. Returns {@code null} if this is not a HTTPS response. |
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 SSLParameters associated with the response |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
156 |
public abstract SSLParameters sslParameters(); |
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 {@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
|
160 |
* 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
|
161 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
* @return the URI of the response |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
163 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
public abstract URI uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
* 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
|
168 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
* @return HTTP protocol version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
public abstract HttpClient.Version version(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
|
48083 | 173 |
|
174 |
private static String pathForSecurityCheck(Path path) { |
|
175 |
return path.toFile().getPath(); |
|
176 |
} |
|
177 |
||
178 |
/** A body handler that is further restricted by a given ACC. */ |
|
179 |
interface UntrustedBodyHandler<T> extends BodyHandler<T> { |
|
180 |
void setAccessControlContext(AccessControlContext acc); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* A Path body handler. |
|
185 |
* |
|
186 |
* Note: Exists mainly too allow setting of the senders ACC post creation of |
|
187 |
* the handler. |
|
188 |
*/ |
|
189 |
static class PathBodyHandler implements UntrustedBodyHandler<Path> { |
|
190 |
private final Path file; |
|
191 |
private final OpenOption[]openOptions; |
|
192 |
private volatile AccessControlContext acc; |
|
193 |
||
194 |
PathBodyHandler(Path file, OpenOption... openOptions) { |
|
195 |
this.file = file; |
|
196 |
this.openOptions = openOptions; |
|
197 |
} |
|
198 |
||
199 |
@Override |
|
200 |
public void setAccessControlContext(AccessControlContext acc) { |
|
201 |
this.acc = acc; |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
205 |
public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) { |
|
206 |
ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber) |
|
207 |
BodySubscriber.asFileImpl(file, openOptions); |
|
208 |
bs.setAccessControlContext(acc); |
|
209 |
return bs; |
|
210 |
} |
|
211 |
} |
|
212 |
||
56031
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
213 |
/* package-private with push promise Map implementation */ |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
214 |
static class PushPromisesHandlerWithMap<T> implements PushPromiseHandler<T> { |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
215 |
|
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
216 |
private final ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
217 |
private final Function<HttpRequest,BodyHandler<T>> pushPromiseHandler; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
218 |
|
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
219 |
PushPromisesHandlerWithMap(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler, |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
220 |
ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) { |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
221 |
this.pushPromiseHandler = pushPromiseHandler; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
222 |
this.pushPromisesMap = pushPromisesMap; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
223 |
} |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
224 |
|
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
225 |
@Override |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
226 |
public void applyPushPromise( |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
227 |
HttpRequest initiatingRequest, HttpRequest pushRequest, |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
228 |
Function<BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor) |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
229 |
{ |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
230 |
URI initiatingURI = initiatingRequest.uri(); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
231 |
URI pushRequestURI = pushRequest.uri(); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
232 |
if (!initiatingURI.getHost().equalsIgnoreCase(pushRequestURI.getHost())) |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
233 |
return; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
234 |
|
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
235 |
int initiatingPort = initiatingURI.getPort(); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
236 |
if (initiatingPort == -1 ) { |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
237 |
if ("https".equalsIgnoreCase(initiatingURI.getScheme())) |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
238 |
initiatingPort = 443; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
239 |
else |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
240 |
initiatingPort = 80; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
241 |
} |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
242 |
int pushPort = pushRequestURI.getPort(); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
243 |
if (pushPort == -1 ) { |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
244 |
if ("https".equalsIgnoreCase(pushRequestURI.getScheme())) |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
245 |
pushPort = 443; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
246 |
else |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
247 |
pushPort = 80; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
248 |
} |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
249 |
if (initiatingPort != pushPort) |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
250 |
return; |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
251 |
|
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
252 |
CompletableFuture<HttpResponse<T>> cf = |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
253 |
acceptor.apply(pushPromiseHandler.apply(pushRequest)); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
254 |
pushPromisesMap.put(pushRequest, cf); |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
255 |
} |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
256 |
} |
c52c9a3a2046
http-client-branch: make PushPromiseHandlerMap package private
michaelm
parents:
56025
diff
changeset
|
257 |
|
48083 | 258 |
// Similar to Path body handler, but for file download. Supports setting ACC. |
259 |
static class FileDownloadBodyHandler implements UntrustedBodyHandler<Path> { |
|
260 |
private final Path directory; |
|
261 |
private final OpenOption[]openOptions; |
|
262 |
private volatile AccessControlContext acc; |
|
263 |
||
264 |
FileDownloadBodyHandler(Path directory, OpenOption... openOptions) { |
|
265 |
this.directory = directory; |
|
266 |
this.openOptions = openOptions; |
|
267 |
} |
|
268 |
||
269 |
@Override |
|
270 |
public void setAccessControlContext(AccessControlContext acc) { |
|
271 |
this.acc = acc; |
|
272 |
} |
|
273 |
||
274 |
@Override |
|
275 |
public BodySubscriber<Path> apply(int statusCode, HttpHeaders headers) { |
|
276 |
String dispoHeader = headers.firstValue("Content-Disposition") |
|
277 |
.orElseThrow(() -> unchecked(new IOException("No Content-Disposition"))); |
|
278 |
if (!dispoHeader.startsWith("attachment;")) { |
|
279 |
throw unchecked(new IOException("Unknown Content-Disposition type")); |
|
280 |
} |
|
281 |
int n = dispoHeader.indexOf("filename="); |
|
282 |
if (n == -1) { |
|
283 |
throw unchecked(new IOException("Bad Content-Disposition type")); |
|
284 |
} |
|
285 |
int lastsemi = dispoHeader.lastIndexOf(';'); |
|
286 |
String disposition; |
|
287 |
if (lastsemi < n) { |
|
288 |
disposition = dispoHeader.substring(n + 9); |
|
289 |
} else { |
|
290 |
disposition = dispoHeader.substring(n + 9, lastsemi); |
|
291 |
} |
|
292 |
Path file = Paths.get(directory.toString(), disposition); |
|
293 |
||
294 |
ResponseSubscribers.PathSubscriber bs = (ResponseSubscribers.PathSubscriber) |
|
295 |
BodySubscriber.asFileImpl(file, openOptions); |
|
296 |
bs.setAccessControlContext(acc); |
|
297 |
return bs; |
|
298 |
} |
|
299 |
} |
|
300 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
/** |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
302 |
* A handler for response bodies. |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
303 |
* {@Incubating} |
48083 | 304 |
* |
305 |
* <p> This is a function that takes two parameters: the response status code, |
|
306 |
* and the response headers, and which returns a {@linkplain BodySubscriber}. |
|
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
307 |
* The function is always called just before the response body is read. Its |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
308 |
* implementation may examine the status code or headers and must decide, |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
309 |
* whether to accept the response body or discard it, and if accepting it, |
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
310 |
* exactly how to handle it. |
48083 | 311 |
* |
312 |
* <p> Some pre-defined implementations which do not utilize the status code |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
* or headers (meaning the body is always accepted) are defined: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
* <ul><li>{@link #asByteArray() }</li> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
* <li>{@link #asByteArrayConsumer(java.util.function.Consumer) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
* asByteArrayConsumer(Consumer)}</li> |
48083 | 317 |
* <li>{@link #asString(java.nio.charset.Charset) asString(Charset)}</li> |
318 |
* <li>{@link #asFile(Path, OpenOption...) |
|
319 |
* asFile(Path,OpenOption...)}</li> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
* <li>{@link #asFileDownload(java.nio.file.Path,OpenOption...) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
* asFileDownload(Path,OpenOption...)}</li> |
48083 | 322 |
* <li>{@link #asInputStream() asInputStream()}</li> |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
* <li>{@link #discard(Object) }</li> |
48083 | 324 |
* <li>{@link #buffering(BodyHandler, int) |
325 |
* buffering(BodyHandler,int)}</li> |
|
326 |
* </ul> |
|
327 |
* |
|
328 |
* <p> These implementations return the equivalent {@link BodySubscriber}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
* Alternatively, the handler can be used to examine the status code |
48083 | 330 |
* or headers and return different body subscribers as appropriate. |
331 |
* |
|
332 |
* <p><b>Examples of handler usage</b> |
|
333 |
* |
|
334 |
* <p> The first example uses one of the predefined handler functions which |
|
335 |
* ignores the response headers and status, and always process the response |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
* body in the same way. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
* HttpResponse<Path> resp = HttpRequest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
* .create(URI.create("http://www.foo.com")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
* .GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
* .response(BodyHandler.asFile(Paths.get("/tmp/f"))); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
* } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
* </pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
* Note, that even though these pre-defined handlers ignore the status code |
48083 | 346 |
* and headers, this information is still accessible from the |
347 |
* {@code HttpResponse} when it is returned. |
|
348 |
* |
|
349 |
* <p> In the second example, the function returns a different subscriber |
|
350 |
* depending on the status code. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
* <pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
* {@code |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
* HttpResponse<Path> resp1 = HttpRequest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
* .create(URI.create("http://www.foo.com")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
* .GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
* .response( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
* (status, headers) -> status == 200 |
48083 | 358 |
* ? BodySubscriber.asFile(Paths.get("/tmp/f")) |
359 |
* : BodySubscriber.discard(Paths.get("/NULL"))); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
* } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
* </pre> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
* |
48083 | 363 |
* @param <T> the response body type |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
*/ |
56038
e663f24e3020
http-client-branch: restore inadvertantly removed @FunctionalInterface
chegar
parents:
56034
diff
changeset
|
365 |
@FunctionalInterface |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
public interface BodyHandler<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
/** |
48083 | 368 |
* Returns a {@link BodySubscriber BodySubscriber} considering the given |
369 |
* response status code and headers. This method is always called before |
|
370 |
* the body is read and its implementation can decide to keep the body |
|
371 |
* and store it somewhere, or else discard it by returning the {@code |
|
372 |
* BodySubscriber} returned from {@link BodySubscriber#discard(Object) |
|
373 |
* discard}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
* @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
|
376 |
* @param responseHeaders the response headers received |
48083 | 377 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
*/ |
48083 | 379 |
public BodySubscriber<T> apply(int statusCode, HttpHeaders responseHeaders); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
|
56010 | 381 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
383 |
* 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
|
384 |
* BodySubscriber}{@code <Void>} obtained from {@linkplain |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
385 |
* BodySubscriber#fromSubscriber(Subscriber)}, with the given |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
386 |
* {@code subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
387 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
388 |
* <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
|
389 |
* 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
|
390 |
* 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
|
391 |
* 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
|
392 |
* database, etc. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
393 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
394 |
* @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
|
395 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
396 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
397 |
* <p> For example: |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
398 |
* <pre> {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
399 |
* TextSubscriber subscriber = new TextSubscriber(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
400 |
* HttpResponse<Void> response = client.sendAsync(request, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
401 |
* BodyHandler.fromSubscriber(subscriber)).join(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
402 |
* System.out.println(response.statusCode()); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
403 |
* }</pre> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
404 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
405 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
406 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
407 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
408 |
public static BodyHandler<Void> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
409 |
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
|
410 |
Objects.requireNonNull(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
411 |
return (status, headers) -> BodySubscriber.fromSubscriber(subscriber, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
412 |
s -> null); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
413 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
414 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
415 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
416 |
* 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
|
417 |
* BodySubscriber}{@code <T>} obtained from {@link |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
418 |
* BodySubscriber#fromSubscriber(Subscriber, Function)}, with the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
419 |
* 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
|
420 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
421 |
* <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
|
422 |
* 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
|
423 |
* 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
|
424 |
* 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
|
425 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
426 |
* @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
|
427 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
428 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
429 |
* <p> For example: |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
430 |
* <pre> {@code |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
431 |
* TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
432 |
* HttpResponse<String> response = client.sendAsync(request, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
433 |
* BodyHandler.fromSubscriber(subscriber, TextSubscriber::getTextResult)).join(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
434 |
* String text = response.body(); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
435 |
* }</pre> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
436 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
437 |
* @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
|
438 |
* @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
|
439 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
440 |
* @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
|
441 |
* @return a response body handler |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
442 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
443 |
public static <S extends Subscriber<? super List<ByteBuffer>>,T> BodyHandler<T> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
444 |
fromSubscriber(S subscriber, Function<S,T> finisher) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
445 |
Objects.requireNonNull(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
446 |
Objects.requireNonNull(finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
447 |
return (status, headers) -> BodySubscriber.fromSubscriber(subscriber, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
448 |
finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
449 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
450 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
451 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
452 |
* 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
|
453 |
* BodySubscriber}{@code <Void>} obtained from {@link |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
454 |
* BodySubscriber#fromLineSubscriber(Subscriber, Function, Charset, String) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
455 |
* BodySubscriber.fromLineSubscriber(subscriber, s -> null, charset, null)}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
456 |
* with the given {@code subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
457 |
* The {@link Charset charset} used to decode the response body bytes is |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
458 |
* obtained from the HTTP response headers as specified by {@link #asString()}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
459 |
* 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
|
460 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
461 |
* <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
|
462 |
* 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
|
463 |
* 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
|
464 |
* 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
|
465 |
* database, etc. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
466 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
467 |
* @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
|
468 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
469 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
470 |
* <p> For example: |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
471 |
* <pre> {@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
472 |
* TextSubscriber subscriber = new TextSubscriber(); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
473 |
* HttpResponse<Void> response = client.sendAsync(request, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
474 |
* BodyHandler.fromLineSubscriber(subscriber, "\n")).join(); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
475 |
* System.out.println(response.statusCode()); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
476 |
* }</pre> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
477 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
478 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
479 |
* @return a response body handler |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
480 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
481 |
public static BodyHandler<Void> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
482 |
fromLineSubscriber(Subscriber<? super String> subscriber) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
483 |
Objects.requireNonNull(subscriber); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
484 |
return (status, headers) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
485 |
-> BodySubscriber.fromLineSubscriber(subscriber, s -> null, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
486 |
charsetFrom(headers), null); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
487 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
488 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
489 |
/** |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
490 |
* 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
|
491 |
* BodySubscriber}{@code <T>} obtained from {@link |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
492 |
* BodySubscriber#fromLineSubscriber(Subscriber, Function, Charset, String) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
493 |
* BodySubscriber.fromLineSubscriber(subscriber, finisher, charset, lineSeparator)}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
494 |
* 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
|
495 |
* The {@link Charset charset} used to decode the response body bytes is |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
496 |
* obtained from the HTTP response headers as specified by {@link #asString()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
497 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
498 |
* <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
|
499 |
* 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
|
500 |
* 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
|
501 |
* 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
|
502 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
503 |
* @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
|
504 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
505 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
506 |
* <p> For example: |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
507 |
* <pre> {@code |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
508 |
* TextSubscriber subscriber = ...; // accumulates bytes and transforms them into a String |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
509 |
* HttpResponse<String> response = client.sendAsync(request, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
510 |
* BodyHandler.fromSubscriber(subscriber, TextSubscriber::getTextResult, "\n")).join(); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
511 |
* String text = response.body(); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
512 |
* }</pre> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
513 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
514 |
* @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
|
515 |
* @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
|
516 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
517 |
* @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
|
518 |
* @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
|
519 |
* 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
|
520 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
521 |
* @return a response body handler |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
522 |
* @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
523 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
524 |
public static <S extends Subscriber<? super String>,T> BodyHandler<T> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
525 |
fromLineSubscriber(S subscriber, Function<S,T> finisher, String lineSeparator) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
526 |
Objects.requireNonNull(subscriber); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
527 |
Objects.requireNonNull(finisher); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
528 |
// implicit null check |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
529 |
if (lineSeparator != null && lineSeparator.isEmpty()) |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
530 |
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
|
531 |
return (status, headers) -> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
532 |
BodySubscriber.fromLineSubscriber(subscriber, finisher, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
533 |
charsetFrom(headers), lineSeparator); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
534 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
535 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
536 |
/** |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
* Returns a response body handler which discards the response body and |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
538 |
* uses the given value as a replacement for it. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
539 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
* @param <U> the response body type |
48083 | 541 |
* @param value the value of U to return as the body, may be {@code null} |
44847 | 542 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
public static <U> BodyHandler<U> discard(U value) { |
48083 | 545 |
return (status, headers) -> BodySubscriber.discard(value); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
546 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
547 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
548 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 550 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
551 |
* {@link BodySubscriber#asString(Charset) BodySubscriber.asString(Charset)}. |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
552 |
* 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
|
553 |
* |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
554 |
* @param charset the character set to convert the body with |
44847 | 555 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
556 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
557 |
public static BodyHandler<String> asString(Charset charset) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
558 |
Objects.requireNonNull(charset); |
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
559 |
return (status, headers) -> BodySubscriber.asString(charset); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
560 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
* Returns a {@code BodyHandler<Path>} that returns a |
48083 | 564 |
* {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from |
565 |
* {@link BodySubscriber#asFile(Path, OpenOption...) |
|
566 |
* BodySubscriber.asFile(Path,OpenOption...)}. |
|
567 |
* |
|
568 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
569 |
* been completely written to the file, and {@link #body()} returns a |
|
570 |
* reference to its {@link Path}. |
|
571 |
* |
|
572 |
* @param file the filename to store the body in |
|
573 |
* @param openOptions any options to use when opening/creating the file |
|
574 |
* @return a response body handler |
|
575 |
* @throws SecurityException If a security manager has been installed |
|
576 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
577 |
* write access} to the file. The {@link |
|
578 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
579 |
* invoked to check delete access if the file is opened with |
|
580 |
* the {@code DELETE_ON_CLOSE} option. |
|
581 |
*/ |
|
582 |
public static BodyHandler<Path> asFile(Path file, OpenOption... openOptions) { |
|
583 |
Objects.requireNonNull(file); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
584 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 585 |
SecurityManager sm = System.getSecurityManager(); |
586 |
if (sm != null) { |
|
587 |
String fn = pathForSecurityCheck(file); |
|
588 |
sm.checkWrite(fn); |
|
589 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
590 |
sm.checkDelete(fn); |
|
591 |
if (opts.contains(StandardOpenOption.READ)) |
|
592 |
sm.checkRead(fn); |
|
593 |
} |
|
594 |
return new PathBodyHandler(file, openOptions); |
|
595 |
} |
|
596 |
||
597 |
/** |
|
598 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
599 |
* {@link BodySubscriber BodySubscriber}{@code <Path>} obtained from |
|
600 |
* {@link BodySubscriber#asFile(Path) BodySubscriber.asFile(Path)}. |
|
601 |
* |
|
602 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
603 |
* been completely written to the file, and {@link #body()} returns a |
|
604 |
* reference to its {@link Path}. |
|
605 |
* |
|
606 |
* @param file the file to store the body in |
|
607 |
* @return a response body handler |
|
608 |
* @throws SecurityException if a security manager has been installed |
|
609 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
610 |
* write access} to the file |
|
611 |
*/ |
|
612 |
public static BodyHandler<Path> asFile(Path file) { |
|
613 |
return BodyHandler.asFile(file, StandardOpenOption.CREATE, |
|
614 |
StandardOpenOption.WRITE); |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Returns a {@code BodyHandler<Path>} that returns a |
|
619 |
* {@link BodySubscriber BodySubscriber}<{@link Path}> |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
620 |
* 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
|
621 |
* obtained from the {@code Content-Disposition} response header. The |
48083 | 622 |
* {@code Content-Disposition} header must specify the <i>attachment</i> |
623 |
* type and must also contain a <i>filename</i> parameter. If the |
|
624 |
* filename specifies multiple path components only the final component |
|
625 |
* is used as the filename (with the given directory name). |
|
626 |
* |
|
627 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
628 |
* been completely written to the file and {@link #body()} returns a |
|
629 |
* {@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
|
630 |
* 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
|
631 |
* 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
|
632 |
* 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
|
633 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
634 |
* @param directory the directory to store the file in |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
* @param openOptions open options |
44847 | 636 |
* @return a response body handler |
48083 | 637 |
* @throws SecurityException If a security manager has been installed |
638 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
639 |
* write access} to the file. The {@link |
|
640 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
641 |
* invoked to check delete access if the file is opened with |
|
642 |
* the {@code DELETE_ON_CLOSE} option. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
643 |
*/ |
48083 | 644 |
//####: check if the dir exists and is writable?? |
645 |
public static BodyHandler<Path> asFileDownload(Path directory, |
|
646 |
OpenOption... openOptions) { |
|
647 |
Objects.requireNonNull(directory); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
648 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 649 |
SecurityManager sm = System.getSecurityManager(); |
650 |
if (sm != null) { |
|
651 |
String fn = pathForSecurityCheck(directory); |
|
652 |
sm.checkWrite(fn); |
|
653 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
654 |
sm.checkDelete(fn); |
|
655 |
if (opts.contains(StandardOpenOption.READ)) |
|
656 |
sm.checkRead(fn); |
|
657 |
} |
|
658 |
return new FileDownloadBodyHandler(directory, openOptions); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
659 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
660 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
661 |
/** |
48083 | 662 |
* Returns a {@code BodyHandler<InputStream>} that returns a |
663 |
* {@link BodySubscriber BodySubscriber}{@code <InputStream>} obtained |
|
664 |
* from {@link BodySubscriber#asInputStream() BodySubscriber.asInputStream}. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
665 |
* |
48083 | 666 |
* <p> When the {@code HttpResponse} object is returned, the response |
667 |
* headers will have been completely read, but the body may not have |
|
668 |
* been fully received yet. The {@link #body()} method returns an |
|
669 |
* {@link InputStream} from which the body can be read as it is received. |
|
670 |
* |
|
671 |
* @apiNote See {@link BodySubscriber#asInputStream()} for more information. |
|
672 |
* |
|
44847 | 673 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
674 |
*/ |
48083 | 675 |
public static BodyHandler<InputStream> asInputStream() { |
676 |
return (status, headers) -> BodySubscriber.asInputStream(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
677 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
678 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
679 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
680 |
* Returns a {@code BodyHandler<Stream<String>>} that returns a |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
681 |
* {@link BodySubscriber BodySubscriber}{@code <Stream<String>>} obtained from |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
682 |
* {@link BodySubscriber#asLines(Charset)} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
683 |
* BodySubscriber.asLines(charset)}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
684 |
* The {@link Charset charset} used to decode the response body bytes is |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
685 |
* obtained from the HTTP response headers as specified by {@link #asString()}, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
686 |
* 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
|
687 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
688 |
* <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
|
689 |
* not have been completely received. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
690 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
691 |
* @return a response body handler |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
692 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
693 |
public static BodyHandler<Stream<String>> asLines() { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
694 |
return (status, headers) -> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
695 |
BodySubscriber.asLines(charsetFrom(headers)); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
696 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
697 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
698 |
/** |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
699 |
* Returns a {@code BodyHandler<Void>} that returns a |
48083 | 700 |
* {@link BodySubscriber BodySubscriber}{@code <Void>} obtained from |
701 |
* {@link BodySubscriber#asByteArrayConsumer(Consumer) |
|
702 |
* BodySubscriber.asByteArrayConsumer(Consumer)}. |
|
703 |
* |
|
704 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
705 |
* been completely written to the consumer. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
706 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
707 |
* @param consumer a Consumer to accept the response body |
44847 | 708 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
709 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
710 |
public static BodyHandler<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
711 |
Objects.requireNonNull(consumer); |
48083 | 712 |
return (status, headers) -> BodySubscriber.asByteArrayConsumer(consumer); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
713 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
714 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
715 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
716 |
* Returns a {@code BodyHandler<byte[]>} that returns a |
48083 | 717 |
* {@link BodySubscriber BodySubscriber}<{@code byte[]}> obtained |
718 |
* from {@link BodySubscriber#asByteArray() BodySubscriber.asByteArray()}. |
|
719 |
* |
|
720 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
721 |
* 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
|
722 |
* |
44847 | 723 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
724 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
725 |
public static BodyHandler<byte[]> asByteArray() { |
48083 | 726 |
return (status, headers) -> BodySubscriber.asByteArray(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
727 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
728 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
729 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
730 |
* Returns a {@code BodyHandler<String>} that returns a |
48083 | 731 |
* {@link BodySubscriber BodySubscriber}{@code <String>} obtained from |
732 |
* {@link BodySubscriber#asString(java.nio.charset.Charset) |
|
733 |
* BodySubscriber.asString(Charset)}. The body is |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
734 |
* decoded using the character set specified in |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
735 |
* 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
|
736 |
* header, or the character set is not supported, then |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
737 |
* {@link java.nio.charset.StandardCharsets#UTF_8 UTF_8} is used. |
48083 | 738 |
* |
739 |
* <p> When the {@code HttpResponse} object is returned, the body has |
|
740 |
* 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
|
741 |
* |
44847 | 742 |
* @return a response body handler |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
743 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
744 |
public static BodyHandler<String> asString() { |
48083 | 745 |
return (status, headers) -> BodySubscriber.asString(charsetFrom(headers)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
746 |
} |
48083 | 747 |
|
748 |
/** |
|
749 |
* Returns a {@code BodyHandler} which, when invoked, returns a {@linkplain |
|
750 |
* BodySubscriber#buffering(BodySubscriber,int) buffering BodySubscriber} |
|
751 |
* that buffers data before delivering it to the downstream subscriber. |
|
752 |
* These {@code BodySubscriber} instances are created by calling |
|
753 |
* {@linkplain BodySubscriber#buffering(BodySubscriber,int) |
|
754 |
* BodySubscriber.buffering} with a subscriber obtained from the given |
|
755 |
* downstream handler and the {@code bufferSize} parameter. |
|
756 |
* |
|
757 |
* @param downstreamHandler the downstream handler |
|
758 |
* @param bufferSize the buffer size parameter passed to {@linkplain |
|
759 |
* BodySubscriber#buffering(BodySubscriber,int) BodySubscriber.buffering} |
|
760 |
* @return a body handler |
|
761 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
762 |
*/ |
|
763 |
public static <T> BodyHandler<T> buffering(BodyHandler<T> downstreamHandler, |
|
764 |
int bufferSize) { |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
765 |
Objects.requireNonNull(downstreamHandler); |
48083 | 766 |
if (bufferSize <= 0) |
767 |
throw new IllegalArgumentException("must be greater than 0"); |
|
768 |
return (status, headers) -> BodySubscriber |
|
769 |
.buffering(downstreamHandler.apply(status, headers), |
|
770 |
bufferSize); |
|
771 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
772 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
773 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
774 |
/** |
56025 | 775 |
* A handler for push promises. |
776 |
* |
|
777 |
* <p> A <i>push promise</i> is a synthetic request sent by an HTTP/2 server |
|
778 |
* when retrieving an initiating client-sent request. The server has |
|
779 |
* determined, possibly through inspection of the initiating request, that |
|
780 |
* the client will likely need the promised resource, and hence pushes a |
|
781 |
* synthetic push request, in the form of a push promise, to the client. The |
|
782 |
* client can choose to accept or reject the push promise request. |
|
783 |
* |
|
784 |
* <p> A push promise request may be received up to the point where the |
|
785 |
* response body of the initiating client-sent request has been fully |
|
786 |
* received. The delivery of a push promise response, however, is not |
|
787 |
* coordinated with the delivery of the response to the initiating |
|
788 |
* client-sent request. |
|
789 |
* |
|
790 |
* @param <T> the push promise response body type |
|
56010 | 791 |
*/ |
792 |
public interface PushPromiseHandler<T> { |
|
793 |
/** |
|
56025 | 794 |
* Notification of an incoming push promise. |
795 |
* |
|
796 |
* <p> This method is invoked once for each push promise received, up |
|
797 |
* to the point where the response body of the initiating client-sent |
|
798 |
* request has been fully received. |
|
799 |
* |
|
800 |
* <p> A push promise is accepted by invoking the given {@code acceptor} |
|
801 |
* function. The {@code acceptor} function must be passed a non-null |
|
802 |
* {@code BodyHandler}, that is to be used to handle the promise's |
|
803 |
* response body. The acceptor function will return a {@code |
|
804 |
* CompletableFuture} that completes with the promise's response. |
|
805 |
* |
|
806 |
* <p> If the {@code acceptor} function is not successfully invoked, |
|
807 |
* then the push promise is rejected. The {@code acceptor} function will |
|
808 |
* throw an {@code IllegalStateException} if invoked more than once. |
|
809 |
* |
|
810 |
* @param initiatingRequest the initiating client-send request |
|
811 |
* @param pushPromiseRequest the synthetic push request |
|
812 |
* @param acceptor the acceptor function that must be successfully |
|
813 |
* invoked to accept the push promise |
|
56010 | 814 |
*/ |
815 |
public void applyPushPromise( |
|
56025 | 816 |
HttpRequest initiatingRequest, |
817 |
HttpRequest pushPromiseRequest, |
|
56010 | 818 |
Function<HttpResponse.BodyHandler<T>,CompletableFuture<HttpResponse<T>>> acceptor |
819 |
); |
|
820 |
||
821 |
||
822 |
/** |
|
823 |
* Returns a push promise handler that accumulates push promises, and |
|
824 |
* their responses, into the given map. |
|
825 |
* |
|
56025 | 826 |
* <p> Entries are added to the given map for each push promise accepted. |
827 |
* The entry's key is the push request, and the entry's value is a |
|
828 |
* {@code CompletableFuture} that completes with the response |
|
829 |
* corresponding to the key's push request. A push request is rejected / |
|
830 |
* cancelled if there is already an entry in the map whose key is |
|
831 |
* {@linkplain HttpRequest#equals equal} to it. A push request is |
|
832 |
* rejected / cancelled if it does not have the same origin as its |
|
833 |
* initiating request. |
|
56010 | 834 |
* |
835 |
* <p> Entries are added to the given map as soon as practically |
|
836 |
* possible when a push promise is received and accepted. That way code, |
|
837 |
* using such a map like a cache, can determine if a push promise has |
|
838 |
* been issued by the server and avoid making, possibly, unnecessary |
|
839 |
* requests. |
|
840 |
* |
|
56025 | 841 |
* <p> The delivery of a push promise response is not coordinated with |
842 |
* the delivery of the response to the initiating client-sent request. |
|
843 |
* However, when the response body for the initiating client-sent |
|
844 |
* request has been fully received, the map is guaranteed to be fully |
|
845 |
* populated, that is, no more entries will be added. The individual |
|
846 |
* {@code CompletableFutures} contained in the map may or may not |
|
847 |
* already be completed at this point. |
|
56010 | 848 |
* |
56025 | 849 |
* @param <T> the push promise response body type |
56010 | 850 |
* @param pushPromiseHandler t he body handler to use for push promises |
851 |
* @param pushPromisesMap a map to accumulate push promises into |
|
56025 | 852 |
* @return a push promise handler |
56010 | 853 |
*/ |
854 |
public static <T> PushPromiseHandler<T> |
|
56034
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56031
diff
changeset
|
855 |
of(Function<HttpRequest,BodyHandler<T>> pushPromiseHandler, |
43b531ed872b
http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents:
56031
diff
changeset
|
856 |
ConcurrentMap<HttpRequest,CompletableFuture<HttpResponse<T>>> pushPromisesMap) { |
56010 | 857 |
return new PushPromisesHandlerWithMap<>(pushPromiseHandler, pushPromisesMap); |
858 |
} |
|
859 |
} |
|
860 |
||
861 |
/** |
|
48083 | 862 |
* A subscriber for response bodies. |
43302
45de1dc92ecd
8173354: javadoc warning notice for types in Incubator Modules
chegar
parents:
42460
diff
changeset
|
863 |
* {@Incubating} |
48083 | 864 |
* |
865 |
* <p> The object acts as a {@link Flow.Subscriber}<{@link List}<{@link |
|
866 |
* ByteBuffer}>> to the HTTP client implementation, which publishes |
|
56071 | 867 |
* unmodifiable lists of read-only ByteBuffers containing the response body. |
868 |
* The Flow of data, as well as the order of ByteBuffers in the Flow lists, |
|
869 |
* is a strictly ordered representation of the response body. Both the Lists |
|
870 |
* and the ByteBuffers, once passed to the subscriber, are no longer used by |
|
871 |
* the HTTP client. The subscriber converts the incoming buffers of data to |
|
872 |
* some user-defined object type {@code T}. |
|
48083 | 873 |
* |
874 |
* <p> The {@link #getBody()} method returns a {@link CompletionStage}{@code |
|
875 |
* <T>} that provides the response body object. The {@code CompletionStage} |
|
876 |
* must be obtainable at any time. When it completes depends on the nature |
|
877 |
* of type {@code T}. In many cases, when {@code T} represents the entire |
|
878 |
* body after being read then it completes after the body has been read. If |
|
879 |
* {@code T} is a streaming type such as {@link java.io.InputStream} then it |
|
880 |
* completes before the body has been read, because the calling code uses it |
|
881 |
* to consume the data. |
|
882 |
* |
|
883 |
* @apiNote To ensure that all resources associated with the |
|
884 |
* corresponding exchange are properly released, an implementation |
|
885 |
* of {@code BodySubscriber} must ensure to {@linkplain |
|
886 |
* Flow.Subscription#request request} more data until {@link |
|
887 |
* #onComplete() onComplete} or {@link #onError(Throwable) onError} |
|
888 |
* are signalled, or {@linkplain Flow.Subscription#request cancel} its |
|
889 |
* {@linkplain #onSubscribe(Flow.Subscription) subscription} |
|
890 |
* if unable or unwilling to do so. |
|
891 |
* Calling {@code cancel} before exhausting the data may cause |
|
892 |
* the underlying HTTP connection to be closed and prevent it |
|
893 |
* 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
|
894 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
895 |
* @param <T> the response body type |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
896 |
*/ |
48083 | 897 |
public interface BodySubscriber<T> |
898 |
extends Flow.Subscriber<List<ByteBuffer>> { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
899 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
900 |
/** |
48083 | 901 |
* Returns a {@code CompletionStage} which when completed will return |
902 |
* the response body object. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
903 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
904 |
* @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
|
905 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
906 |
public CompletionStage<T> getBody(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
907 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
908 |
/** |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
909 |
* Returns a body subscriber that forwards all response body to the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
910 |
* given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
911 |
* stage} of the returned body subscriber completes after one of the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
912 |
* given subscribers {@code onComplete} or {@code onError} has been |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
913 |
* invoked. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
914 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
915 |
* @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
|
916 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
917 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
918 |
* @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
|
919 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
920 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
921 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
922 |
public static <S extends Subscriber<? super List<ByteBuffer>>> BodySubscriber<Void> |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
923 |
fromSubscriber(S subscriber) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
924 |
return new ResponseSubscribers.SubscriberAdapter<S,Void>(subscriber, s -> null); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
925 |
} |
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 |
/** |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
928 |
* Returns a body subscriber that forwards all response body to the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
929 |
* given {@code Flow.Subscriber}. The {@linkplain #getBody()} completion |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
930 |
* stage} of the returned body subscriber completes after one of the |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
931 |
* given subscribers {@code onComplete} or {@code onError} has been |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
932 |
* invoked. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
933 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
934 |
* <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
|
935 |
* 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
|
936 |
* 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
|
937 |
* 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
|
938 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
939 |
* @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
|
940 |
* BodySubscriber} and {@code Flow.Subscriber}. |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
941 |
* |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
942 |
* @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
|
943 |
* @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
|
944 |
* @param subscriber the subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
945 |
* @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
|
946 |
* completed |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
947 |
* @return a body subscriber |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
948 |
*/ |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
949 |
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
|
950 |
fromSubscriber(S subscriber, |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
951 |
Function<S,T> finisher) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
952 |
return new ResponseSubscribers.SubscriberAdapter<S,T>(subscriber, finisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
953 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
954 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
955 |
/** |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
956 |
* Returns a body subscriber that forwards all response body to the |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
957 |
* given {@code Flow.Subscriber}, lines by lines. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
958 |
* The {@linkplain #getBody()} completion |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
959 |
* 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
|
960 |
* 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
|
961 |
* invoked. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
962 |
* Bytes are decoded using the {@linkplain StandardCharsets#UTF_8 |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
963 |
* 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
|
964 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
965 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
966 |
* @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
|
967 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
968 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
969 |
* @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
|
970 |
* 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
|
971 |
* }</pre> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
972 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
973 |
* @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
|
974 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
975 |
* @return a body subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
976 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
977 |
public static <S extends Subscriber<? super String>> BodySubscriber<Void> |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
978 |
fromLineSubscriber(S subscriber) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
979 |
return fromLineSubscriber(subscriber, s -> null, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
980 |
StandardCharsets.UTF_8, null); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
981 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
982 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
983 |
/** |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
984 |
* Returns a body subscriber that forwards all response body to the |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
985 |
* given {@code Flow.Subscriber}, lines by lines. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
986 |
* The {@linkplain #getBody()} completion |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
987 |
* 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
|
988 |
* 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
|
989 |
* invoked. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
990 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
991 |
* <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
|
992 |
* 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
|
993 |
* 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
|
994 |
* 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
|
995 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
996 |
* @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
|
997 |
* BodySubscriber} and {@code Flow.Subscriber}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
998 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
999 |
* @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
|
1000 |
* @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
|
1001 |
* @param subscriber the subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1002 |
* @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
|
1003 |
* completed |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1004 |
* @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
|
1005 |
* @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
|
1006 |
* 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
|
1007 |
* {@link BufferedReader#readLine()}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1008 |
* @return a body subscriber |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1009 |
* @throws IllegalArgumentException if the supplied {@code lineSeparator} is the empty string. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1010 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1011 |
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
|
1012 |
fromLineSubscriber(S subscriber, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1013 |
Function<S,T> finisher, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1014 |
Charset charset, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1015 |
String lineSeparator) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1016 |
return LineSubscriberAdapter.create(subscriber, |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1017 |
finisher, charset, lineSeparator); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1018 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1019 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1020 |
/** |
48083 | 1021 |
* 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
|
1022 |
* String} converted using the given {@code Charset}. |
48083 | 1023 |
* |
1024 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1025 |
* 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
|
1026 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1027 |
* @param charset the character set to convert the String with |
48083 | 1028 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
*/ |
48083 | 1030 |
public static BodySubscriber<String> asString(Charset charset) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
1031 |
Objects.requireNonNull(charset); |
48083 | 1032 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1033 |
bytes -> new String(bytes, charset) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1034 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1035 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1036 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1037 |
/** |
48083 | 1038 |
* 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
|
1039 |
* byte array. |
48083 | 1040 |
* |
1041 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1042 |
* 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
|
1043 |
* |
48083 | 1044 |
* @return a body subscriber |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1045 |
*/ |
48083 | 1046 |
public static BodySubscriber<byte[]> asByteArray() { |
1047 |
return new ResponseSubscribers.ByteArraySubscriber<>( |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1048 |
Function.identity() // no conversion |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1049 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1050 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1051 |
|
48083 | 1052 |
// no security check |
1053 |
private static BodySubscriber<Path> asFileImpl(Path file, OpenOption... openOptions) { |
|
1054 |
return new ResponseSubscribers.PathSubscriber(file, openOptions); |
|
1055 |
} |
|
1056 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1057 |
/** |
48083 | 1058 |
* 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
|
1059 |
* file opened with the given options and name. The file will be opened |
48083 | 1060 |
* with the given options using {@link FileChannel#open(Path,OpenOption...) |
1061 |
* FileChannel.open} just before the body is read. Any exception thrown |
|
1062 |
* will be returned or thrown from {@link HttpClient#send(HttpRequest, |
|
1063 |
* BodyHandler) HttpClient::send} or {@link HttpClient#sendAsync(HttpRequest, |
|
1064 |
* BodyHandler) HttpClient::sendAsync} as appropriate. |
|
1065 |
* |
|
1066 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1067 |
* 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
|
1068 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1069 |
* @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
|
1070 |
* @param openOptions the list of options to open the file with |
48083 | 1071 |
* @return a body subscriber |
1072 |
* @throws SecurityException If a security manager has been installed |
|
1073 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
1074 |
* write access} to the file. The {@link |
|
1075 |
* SecurityManager#checkDelete(String) checkDelete} method is |
|
1076 |
* invoked to check delete access if the file is opened with the |
|
1077 |
* {@code DELETE_ON_CLOSE} option. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1078 |
*/ |
48083 | 1079 |
public static BodySubscriber<Path> asFile(Path file, OpenOption... openOptions) { |
1080 |
Objects.requireNonNull(file); |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
1081 |
List<OpenOption> opts = List.of(openOptions); |
48083 | 1082 |
SecurityManager sm = System.getSecurityManager(); |
1083 |
if (sm != null) { |
|
1084 |
String fn = pathForSecurityCheck(file); |
|
1085 |
sm.checkWrite(fn); |
|
1086 |
if (opts.contains(StandardOpenOption.DELETE_ON_CLOSE)) |
|
1087 |
sm.checkDelete(fn); |
|
1088 |
if (opts.contains(StandardOpenOption.READ)) |
|
1089 |
sm.checkRead(fn); |
|
1090 |
} |
|
1091 |
return asFileImpl(file, openOptions); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1092 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1093 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1094 |
/** |
48083 | 1095 |
* Returns a {@code BodySubscriber} which stores the response body in a |
1096 |
* file opened with the given name. Has the same effect as calling |
|
1097 |
* {@link #asFile(Path, OpenOption...) asFile} with the standard open |
|
1098 |
* options {@code CREATE} and {@code WRITE} |
|
1099 |
* |
|
1100 |
* <p> The {@link HttpResponse} using this subscriber is available after |
|
1101 |
* 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
|
1102 |
* |
48083 | 1103 |
* @param file the file to store the body in |
1104 |
* @return a body subscriber |
|
1105 |
* @throws SecurityException if a security manager has been installed |
|
1106 |
* and it denies {@link SecurityManager#checkWrite(String) |
|
1107 |
* write access} to the file |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1108 |
*/ |
48083 | 1109 |
public static BodySubscriber<Path> asFile(Path file) { |
1110 |
return asFile(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1111 |
} |
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 |
/** |
48083 | 1114 |
* Returns a {@code BodySubscriber} which provides the incoming body |
1115 |
* data to the provided Consumer of {@code Optional<byte[]>}. Each |
|
1116 |
* call to {@link Consumer#accept(java.lang.Object) Consumer.accept()} |
|
1117 |
* will contain a non empty {@code Optional}, except for the final |
|
1118 |
* invocation after all body data has been read, when the {@code |
|
1119 |
* Optional} will be empty. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1120 |
* |
48083 | 1121 |
* <p> The {@link HttpResponse} using this subscriber is available after |
1122 |
* the entire response has been read. |
|
1123 |
* |
|
1124 |
* @param consumer a Consumer of byte arrays |
|
1125 |
* @return a BodySubscriber |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1126 |
*/ |
48083 | 1127 |
public static BodySubscriber<Void> asByteArrayConsumer(Consumer<Optional<byte[]>> consumer) { |
1128 |
return new ResponseSubscribers.ConsumerSubscriber(consumer); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1129 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1130 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1131 |
/** |
48083 | 1132 |
* Returns a {@code BodySubscriber} which streams the response body as |
1133 |
* an {@link InputStream}. |
|
1134 |
* |
|
1135 |
* <p> The {@link HttpResponse} using this subscriber is available |
|
1136 |
* immediately after the response headers have been read, without |
|
1137 |
* requiring to wait for the entire body to be processed. The response |
|
1138 |
* body can then be read directly from the {@link InputStream}. |
|
1139 |
* |
|
1140 |
* @apiNote To ensure that all resources associated with the |
|
1141 |
* corresponding exchange are properly released the caller must |
|
1142 |
* ensure to either read all bytes until EOF is reached, or call |
|
1143 |
* {@link InputStream#close} if it is unable or unwilling to do so. |
|
1144 |
* Calling {@code close} before exhausting the stream may cause |
|
1145 |
* the underlying HTTP connection to be closed and prevent it |
|
1146 |
* from being reused for subsequent operations. |
|
1147 |
* |
|
1148 |
* @return a body subscriber that streams the response body as an |
|
1149 |
* {@link InputStream}. |
|
1150 |
*/ |
|
1151 |
public static BodySubscriber<InputStream> asInputStream() { |
|
1152 |
return new ResponseSubscribers.HttpResponseInputStream(); |
|
1153 |
} |
|
1154 |
||
1155 |
/** |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1156 |
* 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
|
1157 |
* 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
|
1158 |
* 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
|
1159 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1160 |
* <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
|
1161 |
* 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
|
1162 |
* 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
|
1163 |
* 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
|
1164 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1165 |
* @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
|
1166 |
* 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
|
1167 |
* 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
|
1168 |
* 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
|
1169 |
* 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
|
1170 |
* 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
|
1171 |
* 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
|
1172 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1173 |
* @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
|
1174 |
* {@link Stream Stream<String>}. |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1175 |
* |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1176 |
* @see BufferedReader#lines() |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1177 |
*/ |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1178 |
public static BodySubscriber<Stream<String>> asLines(Charset charset) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1179 |
return ResponseSubscribers.HttpLineStream.create(charset); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1180 |
} |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1181 |
|
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
56008
diff
changeset
|
1182 |
/** |
48083 | 1183 |
* 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
|
1184 |
* 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
|
1185 |
* {@link HttpResponse#body()}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1186 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1187 |
* @param <U> The type of the response body |
48083 | 1188 |
* @param value the value to return from HttpResponse.body(), may be {@code null} |
1189 |
* @return a {@code BodySubscriber} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1190 |
*/ |
48083 | 1191 |
public static <U> BodySubscriber<U> discard(U value) { |
1192 |
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
|
1193 |
} |
48083 | 1194 |
|
1195 |
/** |
|
1196 |
* Returns a {@code BodySubscriber} which buffers data before delivering |
|
1197 |
* it to the given downstream subscriber. The subscriber guarantees to |
|
1198 |
* deliver {@code buffersize} bytes of data to each invocation of the |
|
1199 |
* downstream's {@linkplain #onNext(Object) onNext} method, except for |
|
1200 |
* the final invocation, just before {@linkplain #onComplete() onComplete} |
|
1201 |
* is invoked. The final invocation of {@code onNext} may contain fewer |
|
1202 |
* than {@code buffersize} bytes. |
|
1203 |
* |
|
1204 |
* <p> The returned subscriber delegates its {@link #getBody()} method |
|
1205 |
* to the downstream subscriber. |
|
1206 |
* |
|
1207 |
* @param downstream the downstream subscriber |
|
1208 |
* @param bufferSize the buffer size |
|
1209 |
* @return a buffering body subscriber |
|
1210 |
* @throws IllegalArgumentException if {@code bufferSize <= 0} |
|
1211 |
*/ |
|
1212 |
public static <T> BodySubscriber<T> buffering(BodySubscriber<T> downstream, |
|
1213 |
int bufferSize) { |
|
1214 |
if (bufferSize <= 0) |
|
1215 |
throw new IllegalArgumentException("must be greater than 0"); |
|
1216 |
return new BufferingSubscriber<T>(downstream, bufferSize); |
|
1217 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1218 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1219 |
} |