forked from googleapis/google-api-java-client
/
BatchUnparsedResponse.java
416 lines (360 loc) · 13.2 KB
/
BatchUnparsedResponse.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright 2012 Google Inc.
*
* 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 com.google.api.client.googleapis.batch;
import com.google.api.client.googleapis.batch.BatchRequest.RequestInfo;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpStatusCodes;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
import com.google.api.client.http.LowLevelHttpRequest;
import com.google.api.client.http.LowLevelHttpResponse;
import com.google.api.client.util.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* The unparsed batch response.
*
* @author rmistry@google.com (Ravi Mistry)
*/
final class BatchUnparsedResponse {
/** The boundary used in the batch response to separate individual responses. */
private final String boundary;
/** List of request infos. */
private final List<RequestInfo<?, ?>> requestInfos;
/** Input stream that contains the batch response. */
private final InputStream inputStream;
/** Determines whether there are any responses to be parsed. */
boolean hasNext = true;
/** List of unsuccessful HTTP requests that can be retried. */
List<RequestInfo<?, ?>> unsuccessfulRequestInfos = new ArrayList<RequestInfo<?, ?>>();
/** The content Id the response is currently at. */
private int contentId = 0;
/** Whether unsuccessful HTTP requests can be retried. */
private final boolean retryAllowed;
/**
* Construct the {@link BatchUnparsedResponse}.
*
* @param inputStream Input stream that contains the batch response
* @param boundary The boundary of the batch response
* @param requestInfos List of request infos
* @param retryAllowed Whether unsuccessful HTTP requests can be retried
*/
BatchUnparsedResponse(InputStream inputStream, String boundary,
List<RequestInfo<?, ?>> requestInfos, boolean retryAllowed)
throws IOException {
this.boundary = boundary;
this.requestInfos = requestInfos;
this.retryAllowed = retryAllowed;
this.inputStream = inputStream;
// First line in the stream will be the boundary.
checkForFinalBoundary(readLine());
}
/**
* Parses the next response in the queue if a data class and a {@link BatchCallback} is specified.
*
* <p>
* This method closes the input stream if there are no more individual responses left.
* </p>
*/
void parseNextResponse() throws IOException {
contentId++;
// Extract the outer headers.
String line;
while ((line = readLine()) != null && !line.equals("")) {
// Do nothing.
}
// Extract the status code.
String statusLine = readLine();
String[] statusParts = statusLine.split(" ");
int statusCode = Integer.parseInt(statusParts[1]);
// Extract and store the inner headers.
// TODO(rmistry): Handle inner headers that span multiple lines. More details here:
// http://tools.ietf.org/html/rfc2616#section-2.2
List<String> headerNames = new ArrayList<String>();
List<String> headerValues = new ArrayList<String>();
long contentLength = -1L;
while ((line = readLine()) != null && !line.equals("")) {
String[] headerParts = line.split(": ", 2);
String headerName = headerParts[0];
String headerValue = headerParts[1];
headerNames.add(headerName);
headerValues.add(headerValue);
if ("Content-Length".equalsIgnoreCase(headerName.trim())) {
contentLength = Long.parseLong(headerValue);
}
}
InputStream body;
if (contentLength == -1) {
// This isn't very efficient, but most respectable servers should set the Content-Length
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while ((line = readRawLine()) != null && !line.startsWith(boundary)) {
// Convert characters back to bytes:
buffer.write(line.getBytes("ISO-8859-1"));
}
// Remove CRLF that separates body from boundary token
body = trimCrlf(buffer.toByteArray());
// Remove CRLF from the boundary token (to match readLine)
line = trimCrlf(line);
} else {
body = new FilterInputStream(ByteStreams.limit(inputStream, contentLength)) {
@Override
public void close() {
// Don't allow the parser to close the underlying stream
}
};
}
HttpResponse response =
getFakeResponse(statusCode, body, headerNames, headerValues);
parseAndCallback(requestInfos.get(contentId - 1), statusCode, response);
// Consume any bytes that were not consumed by the parser
while (body.skip(contentLength) > 0 || body.read() != -1) {
}
if (contentLength != -1) {
line = readLine();
} else {
// The line was already read
}
// Consume any blank lines that follow the response (not included in Content-Length)
while ((line != null) && (line.length() == 0)) {
line = readLine();
}
checkForFinalBoundary(line);
}
/**
* Parse an object into a new instance of the data class using
* {@link HttpResponse#parseAs(java.lang.reflect.Type)}.
*/
private <T, E> void parseAndCallback(
RequestInfo<T, E> requestInfo, int statusCode, HttpResponse response)
throws IOException {
BatchCallback<T, E> callback = requestInfo.callback;
HttpHeaders responseHeaders = response.getHeaders();
HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
requestInfo.request.getUnsuccessfulResponseHandler();
if (HttpStatusCodes.isSuccess(statusCode)) {
if (callback == null) {
// No point in parsing if there is no callback.
return;
}
T parsed = getParsedDataClass(requestInfo.dataClass, response, requestInfo);
callback.onSuccess(parsed, responseHeaders);
} else {
HttpContent content = requestInfo.request.getContent();
boolean retrySupported = retryAllowed && (content == null || content.retrySupported());
boolean errorHandled = false;
boolean redirectRequest = false;
if (unsuccessfulResponseHandler != null) {
errorHandled = unsuccessfulResponseHandler.handleResponse(
requestInfo.request, response, retrySupported);
}
if (!errorHandled) {
if (requestInfo.request.handleRedirect(response.getStatusCode(), response.getHeaders())) {
redirectRequest = true;
}
}
if (retrySupported && (errorHandled || redirectRequest)) {
unsuccessfulRequestInfos.add(requestInfo);
} else {
if (callback == null) {
// No point in parsing if there is no callback.
return;
}
E parsed = getParsedDataClass(requestInfo.errorClass, response, requestInfo);
callback.onFailure(parsed, responseHeaders);
}
}
}
private <A, T, E> A getParsedDataClass(
Class<A> dataClass, HttpResponse response, RequestInfo<T, E> requestInfo) throws IOException {
// TODO(yanivi): Remove the HttpResponse reference and directly parse the InputStream
if (dataClass == Void.class) {
return null;
}
return requestInfo.request.getParser().parseAndClose(
response.getContent(), response.getContentCharset(), dataClass);
}
/** Create a fake HTTP response object populated with the partContent and the statusCode. */
private HttpResponse getFakeResponse(final int statusCode, final InputStream partContent,
List<String> headerNames, List<String> headerValues)
throws IOException {
HttpRequest request = new FakeResponseHttpTransport(
statusCode, partContent, headerNames, headerValues).createRequestFactory()
.buildPostRequest(new GenericUrl("http://google.com/"), null);
request.setLoggingEnabled(false);
request.setThrowExceptionOnExecuteError(false);
return request.execute();
}
/**
* Reads an HTTP response line (ISO-8859-1 encoding).
*
* @return The line that was read, including CRLF.
*/
private String readRawLine() throws IOException {
int b = inputStream.read();
if (b == -1) {
return null;
} else {
StringBuilder buffer = new StringBuilder();
for (; b != -1; b = inputStream.read()) {
buffer.append((char) b);
if (b == '\n') {
break;
}
}
return buffer.toString();
}
}
/**
* Reads an HTTP response line (ISO-8859-1 encoding)
* <p>
* This method is similar to {@link java.io.BufferedReader#readLine()}, but handles newlines in a
* way that is consistent with the HTTP RFC 2616.
*
* @return The line that was read, excluding CRLF.
*/
private String readLine() throws IOException {
return trimCrlf(readRawLine());
}
private static String trimCrlf(String input) {
if (input.endsWith("\r\n")) {
return input.substring(0, input.length() - 2);
} else if (input.endsWith("\n")) {
return input.substring(0, input.length() - 1);
} else {
return input;
}
}
private static InputStream trimCrlf(byte[] bytes) {
int length = bytes.length;
if (length > 0 && bytes[length - 1] == '\n') {
length--;
}
if (length > 0 && bytes[length - 1] == '\r') {
length--;
}
return new ByteArrayInputStream(bytes, 0, length);
}
/**
* If the boundary line consists of the boundary and "--" then there are no more individual
* responses left to be parsed and the input stream is closed.
*/
private void checkForFinalBoundary(String boundaryLine) throws IOException {
if (boundaryLine.equals(boundary + "--")) {
hasNext = false;
inputStream.close();
}
}
private static class FakeResponseHttpTransport extends HttpTransport {
private int statusCode;
private InputStream partContent;
private List<String> headerNames;
private List<String> headerValues;
FakeResponseHttpTransport(int statusCode, InputStream partContent, List<String> headerNames,
List<String> headerValues) {
super();
this.statusCode = statusCode;
this.partContent = partContent;
this.headerNames = headerNames;
this.headerValues = headerValues;
}
@Override
protected LowLevelHttpRequest buildRequest(String method, String url) {
return new FakeLowLevelHttpRequest(partContent, statusCode, headerNames, headerValues);
}
}
private static class FakeLowLevelHttpRequest extends LowLevelHttpRequest {
private InputStream partContent;
private int statusCode;
private List<String> headerNames;
private List<String> headerValues;
FakeLowLevelHttpRequest(InputStream partContent, int statusCode, List<String> headerNames,
List<String> headerValues) {
this.partContent = partContent;
this.statusCode = statusCode;
this.headerNames = headerNames;
this.headerValues = headerValues;
}
@Override
public void addHeader(String name, String value) {
}
@Override
public LowLevelHttpResponse execute() {
FakeLowLevelHttpResponse response = new FakeLowLevelHttpResponse(
partContent, statusCode, headerNames, headerValues);
return response;
}
}
private static class FakeLowLevelHttpResponse extends LowLevelHttpResponse {
private InputStream partContent;
private int statusCode;
private List<String> headerNames = new ArrayList<String>();
private List<String> headerValues = new ArrayList<String>();
FakeLowLevelHttpResponse(InputStream partContent, int statusCode, List<String> headerNames,
List<String> headerValues) {
this.partContent = partContent;
this.statusCode = statusCode;
this.headerNames = headerNames;
this.headerValues = headerValues;
}
@Override
public InputStream getContent() {
return partContent;
}
@Override
public int getStatusCode() {
return statusCode;
}
@Override
public String getContentEncoding() {
return null;
}
@Override
public long getContentLength() {
return 0;
}
@Override
public String getContentType() {
return null;
}
@Override
public String getStatusLine() {
return null;
}
@Override
public String getReasonPhrase() {
return null;
}
@Override
public int getHeaderCount() {
return headerNames.size();
}
@Override
public String getHeaderName(int index) {
return headerNames.get(index);
}
@Override
public String getHeaderValue(int index) {
return headerValues.get(index);
}
}
}