forked from AsyncHttpClient/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.java
226 lines (197 loc) · 6.78 KB
/
Response.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright 2010 Ning, Inc.
*
* This program is licensed to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.asynchttpclient;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.cookie.Cookie;
import org.asynchttpclient.netty.NettyResponse;
import org.asynchttpclient.uri.Uri;
import org.jetbrains.annotations.Nullable;
import java.io.InputStream;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the asynchronous HTTP response callback for an {@link AsyncCompletionHandler}
*/
public interface Response {
/**
* Returns the status code for the request.
*
* @return The status code
*/
int getStatusCode();
/**
* Returns the status text for the request.
*
* @return The status text
*/
String getStatusText();
/**
* Return the entire response body as a byte[].
*
* @return the entire response body as a byte[].
*/
byte[] getResponseBodyAsBytes();
/**
* Return the entire response body as a ByteBuffer.
*
* @return the entire response body as a ByteBuffer.
*/
ByteBuffer getResponseBodyAsByteBuffer();
/**
* Return the entire response body as a ByteBuf.
*
* @return the entire response body as a ByteBuf.
*/
ByteBuf getResponseBodyAsByteBuf();
/**
* Returns an input stream for the response body. Note that you should not try to get this more than once, and that you should not close the stream.
*
* @return The input stream
*/
InputStream getResponseBodyAsStream();
/**
* Return the entire response body as a String.
*
* @param charset the charset to use when decoding the stream
* @return the entire response body as a String.
*/
String getResponseBody(Charset charset);
/**
* Return the entire response body as a String.
*
* @return the entire response body as a String.
*/
String getResponseBody();
/**
* Return the request {@link Uri}. Note that if the request got redirected, the value of the {@link Uri} will be the last valid redirect url.
*
* @return the request {@link Uri}.
*/
Uri getUri();
/**
* Return the content-type header value.
*
* @return the content-type header value.
*/
String getContentType();
/**
* @param name the header name
* @return the first response header value
*/
String getHeader(CharSequence name);
/**
* Return a {@link List} of the response header value.
*
* @param name the header name
* @return the response header value
*/
List<String> getHeaders(CharSequence name);
HttpHeaders getHeaders();
/**
* Return true if the response redirects to another object.
*
* @return True if the response redirects to another object.
*/
boolean isRedirected();
/**
* Subclasses SHOULD implement toString() in a way that identifies the response for logging.
*
* @return the textual representation
*/
@Override
String toString();
/**
* @return the list of {@link Cookie}.
*/
List<Cookie> getCookies();
/**
* Return true if the response's status has been computed by an {@link AsyncHandler}
*
* @return true if the response's status has been computed by an {@link AsyncHandler}
*/
boolean hasResponseStatus();
/**
* Return true if the response's headers has been computed by an {@link AsyncHandler} It will return false if the either
* {@link AsyncHandler#onStatusReceived(HttpResponseStatus)} or {@link AsyncHandler#onHeadersReceived(HttpHeaders)} returned {@link AsyncHandler.State#ABORT}
*
* @return true if the response's headers has been computed by an {@link AsyncHandler}
*/
boolean hasResponseHeaders();
/**
* Return true if the response's body has been computed by an {@link AsyncHandler}.
* It will return false if:
* <ul>
* <li>either the {@link AsyncHandler#onStatusReceived(HttpResponseStatus)} returned {@link AsyncHandler.State#ABORT}</li>
* <li>or {@link AsyncHandler#onHeadersReceived(HttpHeaders)} returned {@link AsyncHandler.State#ABORT}</li>
* <li>response body was empty</li>
* </ul>
*
* @return true if the response's body has been computed by an {@link AsyncHandler} to new empty bytes
*/
boolean hasResponseBody();
/**
* Get the remote address that the client initiated the request to.
*
* @return The remote address that the client initiated the request to. May be {@code null} if asynchronous provider is unable to provide the remote address
*/
SocketAddress getRemoteAddress();
/**
* Get the local address that the client initiated the request from.
*
* @return The local address that the client initiated the request from. May be {@code null} if asynchronous provider is unable to provide the local address
*/
SocketAddress getLocalAddress();
class ResponseBuilder {
private final List<HttpResponseBodyPart> bodyParts = new ArrayList<>(1);
private @Nullable HttpResponseStatus status;
private @Nullable HttpHeaders headers;
public void accumulate(HttpResponseStatus status) {
this.status = status;
}
public void accumulate(HttpHeaders headers) {
this.headers = this.headers == null ? headers : this.headers.add(headers);
}
/**
* @param bodyPart a body part (possibly empty, but will be filtered out)
*/
public void accumulate(HttpResponseBodyPart bodyPart) {
if (bodyPart.length() > 0) {
bodyParts.add(bodyPart);
}
}
/**
* Build a {@link Response} instance
*
* @return a {@link Response} instance
*/
public @Nullable Response build() {
return status == null ? null : new NettyResponse(status, headers, bodyParts);
}
/**
* Reset the internal state of this builder.
*/
public void reset() {
bodyParts.clear();
status = null;
headers = null;
}
}
}