36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.net.http;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.net.URI;
|
|
30 |
import java.nio.ByteBuffer;
|
|
31 |
import java.security.AccessControlContext;
|
|
32 |
import java.security.AccessController;
|
|
33 |
import java.util.concurrent.CompletableFuture;
|
|
34 |
import java.util.function.LongConsumer;
|
|
35 |
import javax.net.ssl.SSLParameters;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* The implementation class for HttpResponse
|
|
39 |
*/
|
|
40 |
class HttpResponseImpl extends HttpResponse {
|
|
41 |
|
|
42 |
int responseCode;
|
|
43 |
Exchange exchange;
|
|
44 |
HttpRequestImpl request;
|
|
45 |
HttpHeaders1 headers;
|
|
46 |
HttpHeaders1 trailers;
|
|
47 |
SSLParameters sslParameters;
|
|
48 |
URI uri;
|
|
49 |
HttpClient.Version version;
|
|
50 |
AccessControlContext acc;
|
|
51 |
RawChannel rawchan;
|
|
52 |
HttpConnection connection;
|
|
53 |
|
|
54 |
public HttpResponseImpl(int responseCode, Exchange exch, HttpHeaders1 headers,
|
|
55 |
HttpHeaders1 trailers, SSLParameters sslParameters,
|
|
56 |
HttpClient.Version version, HttpConnection connection) {
|
|
57 |
this.responseCode = responseCode;
|
|
58 |
this.exchange = exch;
|
|
59 |
this.request = exchange.request();
|
|
60 |
this.headers = headers;
|
|
61 |
this.trailers = trailers;
|
|
62 |
this.sslParameters = sslParameters;
|
|
63 |
this.uri = request.uri();
|
|
64 |
this.version = version;
|
|
65 |
this.connection = connection;
|
|
66 |
}
|
|
67 |
|
|
68 |
@Override
|
|
69 |
public int statusCode() {
|
|
70 |
return responseCode;
|
|
71 |
}
|
|
72 |
|
|
73 |
@Override
|
|
74 |
public HttpRequestImpl request() {
|
|
75 |
return request;
|
|
76 |
}
|
|
77 |
|
|
78 |
@Override
|
|
79 |
public HttpHeaders headers() {
|
|
80 |
headers.makeUnmodifiable();
|
|
81 |
return headers;
|
|
82 |
}
|
|
83 |
|
|
84 |
@Override
|
|
85 |
public HttpHeaders trailers() {
|
|
86 |
trailers.makeUnmodifiable();
|
|
87 |
return trailers;
|
|
88 |
}
|
|
89 |
|
|
90 |
|
|
91 |
@Override
|
|
92 |
public <T> T body(java.net.http.HttpResponse.BodyProcessor<T> processor) {
|
|
93 |
return exchange.responseBody(processor);
|
|
94 |
}
|
|
95 |
|
|
96 |
@Override
|
|
97 |
public <T> CompletableFuture<T> bodyAsync(java.net.http.HttpResponse.BodyProcessor<T> processor) {
|
|
98 |
acc = AccessController.getContext();
|
|
99 |
return exchange.responseBodyAsync(processor);
|
|
100 |
}
|
|
101 |
|
|
102 |
@Override
|
|
103 |
public SSLParameters sslParameters() {
|
|
104 |
return sslParameters;
|
|
105 |
}
|
|
106 |
|
|
107 |
public AccessControlContext getAccessControlContext() {
|
|
108 |
return acc;
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
public URI uri() {
|
|
113 |
return uri;
|
|
114 |
}
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public HttpClient.Version version() {
|
|
118 |
return version;
|
|
119 |
}
|
|
120 |
// keepalive flag determines whether connection is closed or kept alive
|
|
121 |
// by reading/skipping data
|
|
122 |
|
|
123 |
public static java.net.http.HttpResponse.BodyProcessor<Void> ignoreBody(boolean keepalive) {
|
|
124 |
return new java.net.http.HttpResponse.BodyProcessor<Void>() {
|
|
125 |
|
|
126 |
@Override
|
|
127 |
public Void onResponseBodyStart(long clen, HttpHeaders h,
|
|
128 |
LongConsumer flowController) throws IOException {
|
|
129 |
return null;
|
|
130 |
}
|
|
131 |
|
|
132 |
@Override
|
|
133 |
public void onResponseBodyChunk(ByteBuffer b) throws IOException {
|
|
134 |
}
|
|
135 |
|
|
136 |
@Override
|
|
137 |
public Void onResponseComplete() throws IOException {
|
|
138 |
return null;
|
|
139 |
}
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public void onResponseError(Throwable t) {
|
|
143 |
}
|
|
144 |
};
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
*
|
|
149 |
* @return
|
|
150 |
*/
|
|
151 |
RawChannel rawChannel() {
|
|
152 |
if (rawchan == null) {
|
|
153 |
rawchan = new RawChannel(request.client(), connection);
|
|
154 |
}
|
|
155 |
return rawchan;
|
|
156 |
}
|
|
157 |
}
|