forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonWebServiceRequest.java
More file actions
549 lines (507 loc) · 22.9 KB
/
AmazonWebServiceRequest.java
File metadata and controls
549 lines (507 loc) · 22.9 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.http.annotation.NotThreadSafe;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.metrics.RequestMetricCollector;
/**
* Base class for all user facing web service requests.
*/
@NotThreadSafe
public abstract class AmazonWebServiceRequest implements Cloneable, ReadLimitInfo {
public static final AmazonWebServiceRequest NOOP = new AmazonWebServiceRequest() {
};
/**
* The optional progress listener for receiving updates about the progress of the request.
*/
private ProgressListener progressListener = ProgressListener.NOOP;
/**
* Arbitrary options storage for individual {@link AmazonWebServiceRequest}s. This field is not
* intended to be used by clients.
*/
private final RequestClientOptions requestClientOptions = new RequestClientOptions();
/**
* A request metric collector used for this specific service request; or null if there is none.
* This collector always takes precedence over the ones specified at the http client level and
* AWS SDK level.
*/
private RequestMetricCollector requestMetricCollector;
/**
* The optional credentials to use for this request - overrides the default credentials set at
* the client level.
*/
private AWSCredentialsProvider credentialsProvider;
/**
* A map of custom header names to header values.
*/
private Map<String, String> customRequestHeaders;
/**
* Custom query parameters for the request.
*/
private Map<String, List<String>> customQueryParameters;
/**
* The source object from which the current object was cloned; or null if there isn't one.
*/
private AmazonWebServiceRequest cloneSource;
private Integer sdkRequestTimeout = null;
private Integer sdkClientExecutionTimeout = null;
/**
* Sets the optional credentials to use for this request, overriding the default credentials set
* at the client level.
*
* @param credentials
* The optional AWS security credentials to use for this request, overriding the
* default credentials set at the client level.
*
* @deprecated by {@link #setRequestCredentialsProvider(AWSCredentialsProvider)}
*/
@Deprecated
public void setRequestCredentials(AWSCredentials credentials) {
this.credentialsProvider = new StaticCredentialsProvider(credentials);
}
/**
* Returns the optional credentials to use to sign this request, overriding the default
* credentials set at the client level.
*
* @return The optional credentials to use to sign this request, overriding the default
* credentials set at the client level.
*
* @deprecated by {@link #getRequestCredentialsProvider()}
*/
@Deprecated
public AWSCredentials getRequestCredentials() {
return credentialsProvider == null ? null : credentialsProvider.getCredentials();
}
/**
* Sets the optional credentials provider to use for this request, overriding the default credentials
* provider at the client level.
*
* @param credentialsProvider
* The optional AWS security credentials provider to use for this request, overriding the
* default credentials provider at the client level.
*/
public void setRequestCredentialsProvider(AWSCredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
}
/**
* Returns the optional credentials provider to use to sign this request, overriding the default
* credentials provider at the client level.
*
* @return The optional credentials provider to use to sign this request, overriding the default
* credentials provider at the client level.
*/
public AWSCredentialsProvider getRequestCredentialsProvider() {
return credentialsProvider;
}
/**
* Gets the options stored with this request object. Intended for internal use only.
*/
public RequestClientOptions getRequestClientOptions() {
return requestClientOptions;
}
/**
* Returns a request level metric collector; or null if not specified.
*/
public RequestMetricCollector getRequestMetricCollector() {
return requestMetricCollector;
}
/**
* Sets a request level request metric collector which takes precedence over the ones at the
* http client level and AWS SDK level.
*/
public void setRequestMetricCollector(RequestMetricCollector requestMetricCollector) {
this.requestMetricCollector = requestMetricCollector;
}
/**
* Specifies a request level metric collector which takes precedence over the ones at the http
* client level and AWS SDK level.
*/
public <T extends AmazonWebServiceRequest> T withRequestMetricCollector(RequestMetricCollector metricCollector) {
setRequestMetricCollector(metricCollector);
@SuppressWarnings("unchecked")
T t = (T) this;
return t;
}
/**
* Sets the optional progress listener for receiving updates about the progress of the request.
*
* @param progressListener
* The new progress listener.
*/
public void setGeneralProgressListener(ProgressListener progressListener) {
this.progressListener = progressListener == null ? ProgressListener.NOOP : progressListener;
}
/**
* Returns the optional progress listener for receiving updates about the progress of the
* request.
*
* @return the optional progress listener for receiving updates about the progress of the
* request.
*/
public ProgressListener getGeneralProgressListener() {
return progressListener;
}
/**
* Sets the optional progress listener for receiving updates about the progress of the request,
* and returns a reference to this object so that method calls can be chained together.
*
* @param progressListener
* The new progress listener.
* @return A reference to this updated object so that method calls can be chained together.
*/
public <T extends AmazonWebServiceRequest> T withGeneralProgressListener(ProgressListener progressListener) {
setGeneralProgressListener(progressListener);
@SuppressWarnings("unchecked")
T t = (T) this;
return t;
}
/**
* Returns an immutable map of custom header names to header values.
*
* @return The immutable map of custom header names to header values.
*/
public Map<String, String> getCustomRequestHeaders() {
if (customRequestHeaders == null) {
return null;
}
return Collections.unmodifiableMap(customRequestHeaders);
}
/**
* Put a new custom header to the map of custom header names to custom header values, and return
* the previous value if the header has already been set in this map.
* <p>
* NOTE: Custom header values set via this method will overwrite any conflicting values coming
* from the request parameters.
*
* @param name
* The name of the header to add
* @param value
* The value of the header to add
* @return the previous value for the name if it was set, null otherwise
*/
public String putCustomRequestHeader(String name, String value) {
if (customRequestHeaders == null) {
customRequestHeaders = new HashMap<String, String>();
}
return customRequestHeaders.put(name, value);
}
/**
* @return the immutable map of custom query parameters. The parameter value is modeled as a
* list of strings because multiple values can be specified for the same parameter name.
*/
public Map<String, List<String>> getCustomQueryParameters() {
if (customQueryParameters == null) {
return null;
}
return Collections.unmodifiableMap(customQueryParameters);
}
/**
* Add a custom query parameter for the request. Since multiple values are allowed for the same
* query parameter, this method does NOT overwrite any existing parameter values in the request.
*
* @param name
* The name of the query parameter
* @param value
* The value of the query parameter. Only the parameter name will be added in the URI
* if the value is set to null. For example, putCustomQueryParameter("param", null)
* will be serialized to "?param", while putCustomQueryParameter("param", "") will be
* serialized to "?param=".
*/
public void putCustomQueryParameter(String name, String value) {
if (customQueryParameters == null) {
customQueryParameters = new HashMap<String, List<String>>();
}
List<String> paramList = customQueryParameters.get(name);
if (paramList == null) {
paramList = new LinkedList<String>();
customQueryParameters.put(name, paramList);
}
paramList.add(value);
}
@Override
public final int getReadLimit() {
return requestClientOptions.getReadLimit();
}
/**
* Copies the internal state of this base class to that of the target request.
*
* @return the target request
*/
protected final <T extends AmazonWebServiceRequest> T copyBaseTo(T target) {
if (customRequestHeaders != null) {
for (Map.Entry<String, String> e : customRequestHeaders.entrySet())
target.putCustomRequestHeader(e.getKey(), e.getValue());
}
if (customQueryParameters != null) {
for (Map.Entry<String, List<String>> e : customQueryParameters.entrySet()) {
if (e.getValue() != null) {
for (String value : e.getValue()) {
target.putCustomQueryParameter(e.getKey(), value);
}
}
}
}
target.setRequestCredentialsProvider(credentialsProvider);
target.setGeneralProgressListener(progressListener);
target.setRequestMetricCollector(requestMetricCollector);
requestClientOptions.copyTo(target.getRequestClientOptions());
return target;
}
/**
* Returns the source object from which the current object was cloned; or null if there isn't
* one.
*/
public AmazonWebServiceRequest getCloneSource() {
return cloneSource;
}
/**
* Returns the root object from which the current object was cloned; or null if there isn't one.
*/
public AmazonWebServiceRequest getCloneRoot() {
AmazonWebServiceRequest cloneRoot = cloneSource;
if (cloneRoot != null) {
while (cloneRoot.getCloneSource() != null) {
cloneRoot = cloneRoot.getCloneSource();
}
}
return cloneRoot;
}
private void setCloneSource(AmazonWebServiceRequest cloneSource) {
this.cloneSource = cloneSource;
}
/**
* Returns the amount of time to wait (in milliseconds) for the request to complete before
* giving up and timing out. A non-positive value disables this feature.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The request timeout feature doesn't have strict guarantees on how quickly a request is
* aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the request timeout feature should
* not be used when absolute precision is needed.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @return The amount of time to wait (in milliseconds) for the request to complete before
* giving up and timing out. A non-positive value disables the timeout for this request.
* @see {@link AmazonWebServiceRequest#setSdkClientExecutionTimeout(int)} to enforce a timeout
* across all retries
*/
public Integer getSdkRequestTimeout() {
return sdkRequestTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) for the request to complete before giving
* up and timing out. A non-positive value disables this feature.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The request timeout feature doesn't have strict guarantees on how quickly a request is
* aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the request timeout feature should
* not be used when absolute precision is needed.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @param sdkRequestTimeout
* The amount of time to wait (in milliseconds) for the request to complete before
* giving up and timing out. A non-positive value disables the timeout for this
* request.
* @see {@link AmazonWebServiceRequest#setSdkClientExecutionTimeout(int)} to enforce a timeout
* across all retries
*/
public void setSdkRequestTimeout(int sdkRequestTimeout) {
this.sdkRequestTimeout = sdkRequestTimeout;
}
/**
* Sets the amount of time to wait (in milliseconds) for the request to complete before giving
* up and timing out. A non-positive value disables this feature. Returns the updated
* AmazonWebServiceRequest object so that additional method calls may be chained together.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The request timeout feature doesn't have strict guarantees on how quickly a request is
* aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the request timeout feature should
* not be used when absolute precision is needed.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @param sdkRequestTimeout
* The amount of time to wait (in milliseconds) for the request to complete before
* giving up and timing out. A non-positive value disables the timeout for this
* request.
* @return The updated {@link AmazonWebServiceRequest} object.
* @see {@link AmazonWebServiceRequest#setSdkClientExecutionTimeout(int)} to enforce a timeout
* across all retries
*/
public <T extends AmazonWebServiceRequest> T withSdkRequestTimeout(int sdkRequestTimeout) {
setSdkRequestTimeout(sdkRequestTimeout);
@SuppressWarnings("unchecked")
T t = (T) this;
return t;
}
/**
* Returns the amount of time (in milliseconds) to allow the client to complete the execution of
* an API call. This timeout covers the entire client execution except for marshalling. This
* includes request handler execution, all HTTP request including retries, unmarshalling, etc.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The client execution timeout feature doesn't have strict guarantees on how quickly a request
* is aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the client execution timeout
* feature should not be used when absolute precision is needed.
* </p>
* <p>
* This may be used together with {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to
* enforce both a timeout on each individual HTTP request (i.e. each retry) and the total time
* spent on all requests across retries (i.e. the 'client execution' time). A non-positive value
* disables this feature.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @return The amount of time (in milliseconds) to allow the client to complete the execution of
* an API call. A non-positive value disables the timeout for this request.
* @see {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to enforce a timeout per HTTP
* request
*/
public Integer getSdkClientExecutionTimeout() {
return this.sdkClientExecutionTimeout;
}
/**
* Sets the amount of time (in milliseconds) to allow the client to complete the execution of
* an API call. This timeout covers the entire client execution except for marshalling. This
* includes request handler execution, all HTTP request including retries, unmarshalling, etc.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The client execution timeout feature doesn't have strict guarantees on how quickly a request
* is aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the client execution timeout
* feature should not be used when absolute precision is needed.
* </p>
* <p>
* This may be used together with {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to
* enforce both a timeout on each individual HTTP request (i.e. each retry) and the total time
* spent on all requests across retries (i.e. the 'client execution' time). A non-positive value
* disables this feature.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @param sdkClientExecutionTimeout
* The amount of time (in milliseconds) to allow the client to complete the execution
* of an API call. A non-positive value disables the timeout for this request.
* @see {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to enforce a timeout per HTTP
* request
*/
public void setSdkClientExecutionTimeout(int sdkClientExecutionTimeout) {
this.sdkClientExecutionTimeout = sdkClientExecutionTimeout;
}
/**
* Sets the amount of time (in milliseconds) to allow the client to complete the execution of
* an API call. This timeout covers the entire client execution except for marshalling. This
* includes request handler execution, all HTTP request including retries, unmarshalling, etc.
* <p>
* This feature requires buffering the entire response (for non-streaming APIs) into memory to
* enforce a hard timeout when reading the response. For APIs that return large responses this
* could be expensive.
* <p>
* <p>
* The client execution timeout feature doesn't have strict guarantees on how quickly a request
* is aborted when the timeout is breached. The typical case aborts the request within a few
* milliseconds but there may occasionally be requests that don't get aborted until several
* seconds after the timer has been breached. Because of this the client execution timeout
* feature should not be used when absolute precision is needed.
* </p>
* <p>
* This may be used together with {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to
* enforce both a timeout on each individual HTTP request (i.e. each retry) and the total time
* spent on all requests across retries (i.e. the 'client execution' time). A non-positive value
* disables this feature.
* </p>
* <p>
* <b>Note:</b> This feature is not compatible with Java 1.6.
* </p>
*
* @param sdkClientExecutionTimeout
* The amount of time (in milliseconds) to allow the client to complete the execution
* of an API call. A non-positive value disables the timeout for this request.
* @return The updated AmazonWebServiceRequest object for method chaining
* @see {@link AmazonWebServiceRequest#setSdkRequestTimeout(int)} to enforce a timeout per HTTP
* request
*/
public <T extends AmazonWebServiceRequest> T withSdkClientExecutionTimeout(int sdkClientExecutionTimeout) {
setSdkClientExecutionTimeout(sdkClientExecutionTimeout);
@SuppressWarnings("unchecked")
T t = (T) this;
return t;
}
/**
* Creates a shallow clone of this request. Explicitly does <em>not</em> clone the deep
* structure of the request object.
*
* @see Object#clone()
*/
@Override
public AmazonWebServiceRequest clone() {
try {
AmazonWebServiceRequest cloned = (AmazonWebServiceRequest) super.clone();
cloned.setCloneSource(this);
return cloned;
} catch (CloneNotSupportedException e) {
throw new IllegalStateException(
"Got a CloneNotSupportedException from Object.clone() " + "even though we're Cloneable!", e);
}
}
}