Repository navigation
Expand file tree
/
Copy pathInvocationContext.java
More file actions
executable file
·278 lines (251 loc) · 8.68 KB
/
Copy pathInvocationContext.java
File metadata and controls
executable file
·278 lines (251 loc) · 8.68 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright © 2012 The Feign Authors (feign@commonhaus.dev)
*
* Licensed 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 feign;
import static feign.FeignException.errorReading;
import static feign.Util.ensureClosed;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import feign.codec.PredicatedDecoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.lang.reflect.Type;
public class InvocationContext {
private static final long MAX_RESPONSE_BUFFER_SIZE = 8192L;
private final String configKey;
private final Decoder decoder;
private final ErrorDecoder errorDecoder;
private final boolean dismiss404;
private final boolean closeAfterDecode;
private final boolean decodeVoid;
private final boolean decodeErrorResponses;
private final Response response;
private final Type returnType;
private final long elapsedTime;
InvocationContext(
String configKey,
Decoder decoder,
ErrorDecoder errorDecoder,
boolean dismiss404,
boolean closeAfterDecode,
boolean decodeVoid,
Response response,
Type returnType) {
this(
configKey,
decoder,
errorDecoder,
dismiss404,
closeAfterDecode,
decodeVoid,
false,
response,
returnType,
0L);
}
InvocationContext(
String configKey,
Decoder decoder,
ErrorDecoder errorDecoder,
boolean dismiss404,
boolean closeAfterDecode,
boolean decodeVoid,
boolean decodeErrorResponses,
Response response,
Type returnType,
long elapsedTime) {
this.configKey = configKey;
this.decoder = decoder;
this.errorDecoder = errorDecoder;
this.dismiss404 = dismiss404;
this.closeAfterDecode = closeAfterDecode;
this.decodeVoid = decodeVoid;
this.decodeErrorResponses = decodeErrorResponses;
this.response = response;
this.returnType = returnType;
this.elapsedTime = elapsedTime;
}
public Decoder decoder() {
return decoder;
}
public Type returnType() {
return returnType;
}
public Response response() {
return response;
}
/**
* The elapsed time in milliseconds of the request that produced this response, as measured by the
* invoking {@link Feign} client. Available to {@link ResponseInterceptor} implementations.
*/
public long elapsedTime() {
return elapsedTime;
}
public Object proceed() throws Exception {
if (returnType == Response.class) {
return disconnectResponseBodyIfNeeded(response);
}
Response response = this.response;
try {
final boolean shouldDecodeResponseBody =
(response.status() >= 200 && response.status() < 300)
|| (response.status() == 404 && dismiss404 && !isVoidType(returnType));
if (!shouldDecodeResponseBody) {
if (!shouldDecodeErrorResponseBody(response)) {
throw decodeError(configKey, response);
}
response = bufferBodyWithinLimit(response);
if (!isBufferedAndNonEmpty(response.body())) {
throw decodeError(configKey, response);
}
Exception error = errorDecoder.decode(configKey, response);
if (error instanceof RetryableException) {
ensureClosed(response.body());
throw error;
}
return decodeErrorResponseBody(response, error);
}
if (isVoidType(returnType) && !decodeVoid) {
ensureClosed(response.body());
return kotlinUnitInstance(returnType);
}
Class<?> rawType = Types.getRawType(returnType);
if (TypedResponse.class.isAssignableFrom(rawType)) {
Type bodyType = Types.resolveLastTypeParameter(returnType, TypedResponse.class);
return TypedResponse.builder(response).body(decode(response, bodyType)).build();
}
return decode(response, returnType);
} finally {
if (closeAfterDecode) {
ensureClosed(response.body());
}
}
}
private boolean shouldDecodeErrorResponseBody(Response response) {
if (!decodeErrorResponses
|| response.status() < 400
|| response.status() == 404
|| response.body() == null
|| isVoidType(returnType)) {
return false;
}
return !(decoder instanceof PredicatedDecoder)
|| ((PredicatedDecoder) decoder).canDecode(response, returnType);
}
private Object decodeErrorResponseBody(Response response, Exception error) throws Exception {
Class<?> rawType = Types.getRawType(returnType);
try {
if (TypedResponse.class.isAssignableFrom(rawType)) {
Type bodyType = Types.resolveLastTypeParameter(returnType, TypedResponse.class);
return TypedResponse.builder(response).body(decode(response, bodyType)).build();
}
return decode(response, returnType);
} catch (RuntimeException e) {
if (error == null) {
throw e;
}
error.addSuppressed(e);
throw error;
}
}
private static boolean isBufferedAndNonEmpty(Response.Body body) {
return body.isRepeatable()
&& body.length() != null
&& body.length() > 0
&& body.length() <= MAX_RESPONSE_BUFFER_SIZE;
}
private static Response bufferBodyWithinLimit(Response response) throws IOException {
Integer length = response.body().length();
if (length != null && length > MAX_RESPONSE_BUFFER_SIZE) {
return response;
}
InputStream stream = response.body().asInputStream();
byte[] head = readAtMost(stream, (int) MAX_RESPONSE_BUFFER_SIZE + 1);
if (head.length > MAX_RESPONSE_BUFFER_SIZE) {
return response.toBuilder()
.body(new SequenceInputStream(new ByteArrayInputStream(head), stream), null)
.build();
}
ensureClosed(response.body());
return response.toBuilder().body(head).build();
}
private static byte[] readAtMost(InputStream stream, int limit) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] chunk = new byte[1024];
int read;
while (out.size() < limit
&& (read = stream.read(chunk, 0, Math.min(chunk.length, limit - out.size()))) != -1) {
out.write(chunk, 0, read);
}
return out.toByteArray();
}
private static Response disconnectResponseBodyIfNeeded(Response response) throws IOException {
final boolean shouldDisconnectResponseBody =
response.body() != null
&& response.body().length() != null
&& response.body().length() <= MAX_RESPONSE_BUFFER_SIZE;
if (!shouldDisconnectResponseBody) {
return response;
}
try {
final byte[] bodyData = Util.toByteArray(response.body().asInputStream());
return response.toBuilder().body(bodyData).build();
} finally {
ensureClosed(response.body());
}
}
private Object decode(Response response, Type returnType) {
try {
return decoder.decode(response, returnType);
} catch (final FeignException e) {
throw e;
} catch (final RuntimeException e) {
throw new DecodeException(response.status(), e.getMessage(), response.request(), e);
} catch (IOException e) {
throw errorReading(response.request(), response, e);
}
}
private Exception decodeError(String methodKey, Response response) {
try {
return errorDecoder.decode(methodKey, response);
} finally {
ensureClosed(response.body());
}
}
private boolean isVoidType(Type returnType) {
return returnType == Void.class
|| returnType == void.class
|| returnType.getTypeName().equals("kotlin.Unit");
}
/**
* Kotlin's {@code Unit} is non-nullable, so a suspend function declared to return it must get
* back the singleton {@code Unit.INSTANCE} rather than {@code null}. Resolved reflectively since
* feign-core has no compile-time dependency on kotlin-stdlib.
*/
private static Object kotlinUnitInstance(Type returnType) {
if (!(returnType instanceof Class) || !returnType.getTypeName().equals("kotlin.Unit")) {
return null;
}
try {
return ((Class<?>) returnType).getField("INSTANCE").get(null);
} catch (ReflectiveOperationException e) {
return null;
}
}
}