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
229 lines (206 loc) · 7.85 KB
/
AmazonWebServiceRequest.java
File metadata and controls
229 lines (206 loc) · 7.85 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
/*
* Copyright 2010-2014 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.Map;
import org.apache.http.annotation.NotThreadSafe;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.metrics.RequestMetricCollector;
/**
* Base class for all user facing web service requests.
*/
@NotThreadSafe
public abstract class AmazonWebServiceRequest {
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 AWSCredentials credentials;
/**
* A map of custom header names to header values.
*/
private Map<String, String> customRequestHeaders;
/**
* 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.
*/
public void setRequestCredentials(AWSCredentials credentials) {
this.credentials = 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.
*/
public AWSCredentials getRequestCredentials() {
return credentials;
}
/**
* Internal only method for accessing private, internal request parameters.
* Not intended for direct use by callers.
*
* @return private, internal request parameter information.
*/
public Map<String, String> copyPrivateRequestParameters() {
return new HashMap<String, String>();
}
/**
* 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);
}
/**
* Convenient method to return the optional read limit for mark-and-reset
* during retries.
*/
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());
}
target.setRequestCredentials(credentials);
target.setGeneralProgressListener(progressListener);
target.setRequestMetricCollector(requestMetricCollector);
requestClientOptions.copyTo(target.getRequestClientOptions());
return target;
}
}