-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
DefaultWebRequestor.java
424 lines (348 loc) · 15.4 KB
/
DefaultWebRequestor.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
417
418
419
420
421
422
423
424
/*
* Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb;
import static com.restfb.logging.RestFBLogger.HTTP_LOGGER;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.function.BiConsumer;
import com.restfb.util.StringUtils;
import com.restfb.util.UrlUtils;
/**
* Default implementation of a service that sends HTTP requests to the Facebook API endpoint.
*
* @author <a href="http://restfb.com">Mark Allen</a>
*/
public class DefaultWebRequestor implements WebRequestor {
/**
* Arbitrary unique boundary marker for multipart {@code POST}s.
*/
private static final String MULTIPART_BOUNDARY = "**boundarystringwhichwill**neverbeencounteredinthewild**";
/**
* Line separator for multipart {@code POST}s.
*/
private static final String MULTIPART_CARRIAGE_RETURN_AND_NEWLINE = "\r\n";
/**
* Hyphens for multipart {@code POST}s.
*/
private static final String MULTIPART_TWO_HYPHENS = "--";
/**
* Default buffer size for multipart {@code POST}s.
*/
private static final int MULTIPART_DEFAULT_BUFFER_SIZE = 8192;
/**
* By default, how long should we wait for a response (in ms)?
*/
private static final int DEFAULT_READ_TIMEOUT_IN_MS = 180000;
private Map<String, List<String>> currentHeaders;
private DebugHeaderInfo debugHeaderInfo;
private ThreadLocal<String> accessToken = new ThreadLocal<>();
/**
* By default this is true, to prevent breaking existing usage
*/
private boolean autocloseBinaryAttachmentStream = true;
protected enum HttpMethod {
GET, DELETE, POST
}
@Override
public void setAccessToken(final String accessToken) {
this.accessToken = ThreadLocal.withInitial(() -> accessToken);
}
@Override
public Response executeGet(String url) throws IOException {
return execute(url, HttpMethod.GET);
}
@Override
public Response executePost(String url, String parameters) throws IOException {
return executePost(url, parameters, null);
}
@Override
public Response executePost(String url, String parameters, List<BinaryAttachment> binaryAttachments)
throws IOException {
binaryAttachments = Optional.ofNullable(binaryAttachments).orElse(new ArrayList<>());
if (HTTP_LOGGER.isDebugEnabled()) {
HTTP_LOGGER.debug("Executing a POST to " + url + " with parameters "
+ (!binaryAttachments.isEmpty() ? "" : "(sent in request body): ") + UrlUtils.urlDecode(parameters)
+ (!binaryAttachments.isEmpty() ? " and " + binaryAttachments.size() + " binary attachment[s]." : ""));
}
HttpURLConnection httpUrlConnection = null;
OutputStream outputStream = null;
try {
httpUrlConnection = openConnection(new URL(url + (!binaryAttachments.isEmpty() ? "?" + parameters : "")));
httpUrlConnection.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_MS);
// Allow subclasses to customize the connection if they'd like to - set
// their own headers, timeouts, etc.
customizeConnection(httpUrlConnection);
httpUrlConnection.setRequestMethod(HttpMethod.POST.name());
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
initHeaderAccessToken(httpUrlConnection);
if (!binaryAttachments.isEmpty()) {
httpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
httpUrlConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY);
}
httpUrlConnection.connect();
outputStream = httpUrlConnection.getOutputStream();
// If we have binary attachments, the body is just the attachments and the
// other parameters are passed in via the URL.
// Otherwise the body is the URL parameter string.
if (!binaryAttachments.isEmpty()) {
for (BinaryAttachment binaryAttachment : binaryAttachments) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(MULTIPART_TWO_HYPHENS).append(MULTIPART_BOUNDARY)
.append(MULTIPART_CARRIAGE_RETURN_AND_NEWLINE).append("Content-Disposition: form-data; name=\"")
.append(createFormFieldName(binaryAttachment)).append("\"; filename=\"")
.append(binaryAttachment.getFilename()).append("\"");
stringBuilder.append(MULTIPART_CARRIAGE_RETURN_AND_NEWLINE).append("Content-Type: ")
.append(binaryAttachment.getContentType());
stringBuilder.append(MULTIPART_CARRIAGE_RETURN_AND_NEWLINE).append(MULTIPART_CARRIAGE_RETURN_AND_NEWLINE);
outputStream.write(stringBuilder.toString().getBytes(StringUtils.ENCODING_CHARSET));
write(binaryAttachment.getData(), outputStream, MULTIPART_DEFAULT_BUFFER_SIZE);
outputStream.write((MULTIPART_CARRIAGE_RETURN_AND_NEWLINE + MULTIPART_TWO_HYPHENS + MULTIPART_BOUNDARY
+ MULTIPART_TWO_HYPHENS + MULTIPART_CARRIAGE_RETURN_AND_NEWLINE).getBytes(StringUtils.ENCODING_CHARSET));
}
} else {
outputStream.write(parameters.getBytes(StringUtils.ENCODING_CHARSET));
}
HTTP_LOGGER.debug("Response headers: {}", httpUrlConnection.getHeaderFields());
fillHeaderAndDebugInfo(httpUrlConnection);
Response response = fetchResponse(httpUrlConnection);
HTTP_LOGGER.debug("Facebook responded with {}", response);
return response;
} finally {
if (autocloseBinaryAttachmentStream && !binaryAttachments.isEmpty()) {
binaryAttachments.forEach(binaryAttachment -> closeQuietly(binaryAttachment.getData()));
}
closeQuietly(outputStream);
closeQuietly(httpUrlConnection);
}
}
protected void initHeaderAccessToken(HttpURLConnection httpUrlConnection) {
if (accessToken.get() != null) {
httpUrlConnection.setRequestProperty("Authorization", "Bearer " + accessToken.get());
}
}
/**
* Given a {@code url}, opens and returns a connection to it.
* <p>
* If you'd like to pipe your connection through a proxy, this is the place to do so.
*
* @param url
* The URL to connect to.
* @return A connection to the URL.
* @throws IOException
* If an error occurs while establishing the connection.
* @since 1.6.3
*/
protected HttpURLConnection openConnection(URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
}
/**
* Hook method which allows subclasses to easily customize the {@code connection}s created by
* {@link #executeGet(String)} and {@link #executePost(String, String)} - for example, setting a custom read timeout
* or request header.
* <p>
* This implementation is a no-op.
*
* @param connection
* The connection to customize.
*/
protected void customizeConnection(HttpURLConnection connection) {
// This implementation is a no-op
}
/**
* Attempts to cleanly close a resource, swallowing any exceptions that might occur since there's no way to recover
* anyway.
* <p>
* It's OK to pass {@code null} in, this method will no-op in that case.
*
* @param closeable
* The resource to close.
*/
protected void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (Exception t) {
HTTP_LOGGER.warn("Unable to close {}: ", closeable, t);
}
}
/**
* Attempts to cleanly close an {@code HttpURLConnection}, swallowing any exceptions that might occur since there's no
* way to recover anyway.
* <p>
* It's OK to pass {@code null} in, this method will no-op in that case.
*
* @param httpUrlConnection
* The connection to close.
*/
protected void closeQuietly(HttpURLConnection httpUrlConnection) {
try {
Optional.ofNullable(httpUrlConnection).ifPresent(HttpURLConnection::disconnect);
} catch (Exception t) {
HTTP_LOGGER.warn("Unable to disconnect {}: ", httpUrlConnection, t);
}
}
/**
* Writes the contents of the {@code source} stream to the {@code destination} stream using the given
* {@code bufferSize}.
*
* @param source
* The source stream to copy from.
* @param destination
* The destination stream to copy to.
* @param bufferSize
* The size of the buffer to use during the copy operation.
* @throws IOException
* If an error occurs when reading from {@code source} or writing to {@code destination}.
* @throws NullPointerException
* If either {@code source} or @{code destination} is {@code null}.
*/
protected void write(InputStream source, OutputStream destination, int bufferSize) throws IOException {
if (source == null || destination == null) {
throw new IllegalArgumentException("Must provide non-null source and destination streams.");
}
int read;
byte[] chunk = new byte[bufferSize];
while ((read = source.read(chunk)) > 0)
destination.write(chunk, 0, read);
}
/**
* Creates the form field name for the binary attachment filename by stripping off the file extension - for example,
* the filename "test.png" would return "test".
*
* @param binaryAttachment
* The binary attachment for which to create the form field name.
* @return The form field name for the given binary attachment.
*/
protected String createFormFieldName(BinaryAttachment binaryAttachment) {
if (binaryAttachment.getFieldName() != null) {
return binaryAttachment.getFieldName();
}
String name = binaryAttachment.getFilename();
return Optional.ofNullable(name).filter(f -> f.contains(".")).map(f -> f.substring(0, f.lastIndexOf('.'))).orElse(name);
}
/**
* returns if the binary attachment stream is closed automatically
*
* @since 1.7.0
* @return {@code true} if the binary stream should be closed automatically, {@code false} otherwise
*/
public boolean isAutocloseBinaryAttachmentStream() {
return autocloseBinaryAttachmentStream;
}
/**
* define if the binary attachment stream is closed automatically after sending the content to facebook
*
* @since 1.7.0
* @param autocloseBinaryAttachmentStream
* {@code true} if the {@link BinaryAttachment} stream should be closed automatically, {@code false}
* otherwise
*/
public void setAutocloseBinaryAttachmentStream(boolean autocloseBinaryAttachmentStream) {
this.autocloseBinaryAttachmentStream = autocloseBinaryAttachmentStream;
}
/**
* access to the current response headers
*
* @return the current reponse header map
*/
public Map<String, List<String>> getCurrentHeaders() {
return currentHeaders;
}
@Override
public Response executeDelete(String url) throws IOException {
return execute(url, HttpMethod.DELETE);
}
@Override
public DebugHeaderInfo getDebugHeaderInfo() {
return debugHeaderInfo;
}
private Response execute(String url, HttpMethod httpMethod) throws IOException {
HTTP_LOGGER.debug("Making a {} request to {}", httpMethod.name(), url);
HttpURLConnection httpUrlConnection = null;
try {
httpUrlConnection = openConnection(new URL(url));
httpUrlConnection.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_MS);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod(httpMethod.name());
initHeaderAccessToken(httpUrlConnection);
// Allow subclasses to customize the connection if they'd like to - set
// their own headers, timeouts, etc.
customizeConnection(httpUrlConnection);
httpUrlConnection.connect();
HTTP_LOGGER.trace("Response headers: {}", httpUrlConnection.getHeaderFields());
fillHeaderAndDebugInfo(httpUrlConnection);
Response response = fetchResponse(httpUrlConnection);
HTTP_LOGGER.debug("Facebook responded with {}", response);
return response;
} finally {
closeQuietly(httpUrlConnection);
}
}
protected void fillHeaderAndDebugInfo(HttpURLConnection httpUrlConnection) {
currentHeaders = Collections.unmodifiableMap(httpUrlConnection.getHeaderFields());
String usedApiVersion = StringUtils.trimToEmpty(httpUrlConnection.getHeaderField("facebook-api-version"));
HTTP_LOGGER.debug("Facebook used the API {} to answer your request", usedApiVersion);
Version usedVersion = Version.getVersionFromString(usedApiVersion);
DebugHeaderInfo.DebugHeaderInfoFactory factory = DebugHeaderInfo.DebugHeaderInfoFactory.create().setVersion(usedVersion);
Arrays.stream(FbHeaderField.values()).forEach(f -> f.getPutHeader().accept(httpUrlConnection, factory));
debugHeaderInfo = factory.build();
}
protected Response fetchResponse(HttpURLConnection httpUrlConnection) throws IOException {
InputStream inputStream = null;
try {
inputStream =
httpUrlConnection.getResponseCode() != HttpURLConnection.HTTP_OK ? httpUrlConnection.getErrorStream()
: httpUrlConnection.getInputStream();
} catch (IOException e) {
HTTP_LOGGER.warn("An error occurred while making a {} request to {}:", httpUrlConnection.getRequestMethod(),
httpUrlConnection.getURL(), e);
}
return new Response(httpUrlConnection.getResponseCode(), StringUtils.fromInputStream(inputStream));
}
private enum FbHeaderField {
X_FB_TRACE_ID((c, f) -> f.setTraceId(getHeaderOrEmpty(c,"x-fb-trace-id"))), //
X_FB_REV((c, f) -> f.setRev(getHeaderOrEmpty(c,"x-fb-rev"))),
X_FB_DEBUG((c, f) -> f.setDebug(getHeaderOrEmpty(c,"x-fb-debug"))),
X_APP_USAGE((c, f) -> f.setAppUsage(getHeaderOrEmpty(c,"x-app-usage"))),
X_PAGE_USAGE((c, f) -> f.setPageUsage(getHeaderOrEmpty(c,"x-page-usage"))),
X_AD_ACCOUNT_USAGE((c, f) -> f.setAdAccountUsage(getHeaderOrEmpty(c,"x-ad-account-usage"))),
X_BUSINESS_USE_CASE_USAGE((c, f) -> f.setBusinessUseCaseUsage(getHeaderOrEmpty(c,"x-business-use-case-usage")));
private final BiConsumer<HttpURLConnection, DebugHeaderInfo.DebugHeaderInfoFactory> putHeader;
FbHeaderField(BiConsumer<HttpURLConnection, DebugHeaderInfo.DebugHeaderInfoFactory> headerFunction) {
this.putHeader = headerFunction;
}
public BiConsumer<HttpURLConnection, DebugHeaderInfo.DebugHeaderInfoFactory> getPutHeader() {
return putHeader;
}
private static String getHeaderOrEmpty(HttpURLConnection connection, String fieldName) {
return StringUtils.trimToEmpty(connection.getHeaderField(fieldName));
}
}
}