author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 56079 | src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/PushGroup.java@d23b02f37fce |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56062
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
26 |
package java.net.http.internal; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
48083 | 28 |
import java.security.AccessControlContext; |
56020 | 29 |
import java.security.AccessController; |
56010 | 30 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.util.concurrent.CompletableFuture; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
32 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
33 |
import java.net.http.HttpResponse; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
34 |
import java.net.http.HttpResponse.BodyHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
35 |
import java.net.http.HttpResponse.PushPromiseHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
36 |
import java.net.http.internal.common.MinimalFuture; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
37 |
import java.net.http.internal.common.Log; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
* One PushGroup object is associated with the parent Stream of the pushed |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
* Streams. This keeps track of all common state associated with the pushes. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
*/ |
56010 | 43 |
class PushGroup<T> { |
44 |
private final HttpRequest initiatingRequest; |
|
45 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
final CompletableFuture<Void> noMorePushesCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
48083 | 48 |
volatile Throwable error; // any exception that occurred during pushes |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
|
48083 | 50 |
// user's subscriber object |
56010 | 51 |
final PushPromiseHandler<T> pushPromiseHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
48083 | 53 |
private final AccessControlContext acc; |
54 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
int numberOfPushes; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
int remainingPushes; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
boolean noMorePushes = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
|
56010 | 59 |
PushGroup(PushPromiseHandler<T> pushPromiseHandler, |
60 |
HttpRequestImpl initiatingRequest, |
|
48083 | 61 |
AccessControlContext acc) { |
56010 | 62 |
this(pushPromiseHandler, initiatingRequest, new MinimalFuture<>(), acc); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
// Check mainBodyHandler before calling nested constructor. |
56010 | 66 |
private PushGroup(HttpResponse.PushPromiseHandler<T> pushPromiseHandler, |
67 |
HttpRequestImpl initiatingRequest, |
|
48083 | 68 |
CompletableFuture<HttpResponse<T>> mainResponse, |
69 |
AccessControlContext acc) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
this.noMorePushesCF = new MinimalFuture<>(); |
56010 | 71 |
this.pushPromiseHandler = pushPromiseHandler; |
72 |
this.initiatingRequest = initiatingRequest; |
|
73 |
// Restricts the file publisher with the senders ACC, if any |
|
74 |
if (pushPromiseHandler instanceof UntrustedBodyHandler) |
|
75 |
((UntrustedBodyHandler)this.pushPromiseHandler).setAccessControlContext(acc); |
|
48083 | 76 |
this.acc = acc; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
|
56020 | 79 |
interface Acceptor<T> { |
80 |
BodyHandler<T> bodyHandler(); |
|
81 |
CompletableFuture<HttpResponse<T>> cf(); |
|
82 |
boolean accepted(); |
|
83 |
} |
|
56010 | 84 |
|
56020 | 85 |
private static class AcceptorImpl<T> implements Acceptor<T> { |
86 |
private volatile HttpResponse.BodyHandler<T> bodyHandler; |
|
87 |
private volatile CompletableFuture<HttpResponse<T>> cf; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
|
56020 | 89 |
CompletableFuture<HttpResponse<T>> accept(BodyHandler<T> bodyHandler) { |
56010 | 90 |
Objects.requireNonNull(bodyHandler); |
56020 | 91 |
if (this.bodyHandler != null) |
92 |
throw new IllegalStateException("non-null bodyHandler"); |
|
93 |
this.bodyHandler = bodyHandler; |
|
56010 | 94 |
cf = new MinimalFuture<>(); |
95 |
return cf; |
|
96 |
} |
|
97 |
||
56020 | 98 |
@Override public BodyHandler<T> bodyHandler() { return bodyHandler; } |
56010 | 99 |
|
56020 | 100 |
@Override public CompletableFuture<HttpResponse<T>> cf() { return cf; } |
56010 | 101 |
|
56020 | 102 |
@Override public boolean accepted() { return cf != null; } |
48083 | 103 |
} |
104 |
||
56010 | 105 |
Acceptor<T> acceptPushRequest(HttpRequest pushRequest) { |
56020 | 106 |
AcceptorImpl<T> acceptor = new AcceptorImpl<>(); |
56010 | 107 |
|
108 |
pushPromiseHandler.applyPushPromise(initiatingRequest, pushRequest, acceptor::accept); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
|
56062
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
110 |
synchronized (this) { |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
111 |
if (acceptor.accepted()) { |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
112 |
if (acceptor.bodyHandler instanceof UntrustedBodyHandler) { |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
113 |
((UntrustedBodyHandler) acceptor.bodyHandler).setAccessControlContext(acc); |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
114 |
} |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
115 |
numberOfPushes++; |
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
116 |
remainingPushes++; |
56020 | 117 |
} |
56062
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
118 |
return acceptor; |
56010 | 119 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
// This is called when the main body response completes because it means |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
// no more PUSH_PROMISEs are possible |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
synchronized void noMorePushes(boolean noMore) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
noMorePushes = noMore; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
checkIfCompleted(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
noMorePushesCF.complete(null); |
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 |
|
56062
48afabd2de78
http-client-branch: inconsistent synchronization of PushGroup
chegar
parents:
56020
diff
changeset
|
131 |
synchronized CompletableFuture<Void> pushesCF() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
return noMorePushesCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
} |
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 |
synchronized boolean noMorePushes() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
return noMorePushes; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
synchronized void pushCompleted() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
remainingPushes--; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
checkIfCompleted(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
synchronized void checkIfCompleted() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
if (Log.trace()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
Log.logTrace("PushGroup remainingPushes={0} error={1} noMorePushes={2}", |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
remainingPushes, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
(error==null)?error:error.getClass().getSimpleName(), |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
noMorePushes); |
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 |
if (remainingPushes == 0 && error == null && noMorePushes) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
if (Log.trace()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
Log.logTrace("push completed"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
} |
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 |
} |
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 |
synchronized void pushError(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
if (t == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
return; |
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 |
this.error = t; |
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 |
} |