forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonWebServiceClient.java
More file actions
280 lines (253 loc) · 11 KB
/
AmazonWebServiceClient.java
File metadata and controls
280 lines (253 loc) · 11 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
/*
* Copyright 2010-2013 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.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.CopyOnWriteArrayList;
import com.amazonaws.handlers.RequestHandler;
import com.amazonaws.http.AmazonHttpClient;
import com.amazonaws.http.ExecutionContext;
import com.amazonaws.http.HttpMethodName;
import com.amazonaws.http.HttpRequest;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.ServiceAbbreviations;
/**
* Abstract base class for Amazon Web Service Java clients.
* <p>
* Responsible for basic client capabilities that are the same across all AWS
* SDK Java clients (ex: setting the client endpoint).
*/
public abstract class AmazonWebServiceClient {
/** The service endpoint to which this client will send requests. */
protected URI endpoint;
/** The client configuration */
protected ClientConfiguration clientConfiguration;
/** Low level client for sending requests to AWS services. */
protected AmazonHttpClient client;
/** Optional request handlers for additional request processing. */
protected final List<RequestHandler> requestHandlers;
/** Optional offset (in seconds) to use when signing requests */
protected int timeOffset;
/**
* Constructs a new AmazonWebServiceClient object using the specified
* configuration.
*
* @param clientConfiguration
* The client configuration for this client.
*/
public AmazonWebServiceClient(ClientConfiguration clientConfiguration) {
this.clientConfiguration = clientConfiguration;
client = new AmazonHttpClient(clientConfiguration);
requestHandlers = new CopyOnWriteArrayList<RequestHandler>();
}
/**
* Overrides the default endpoint for this client. Callers can use this
* method to control which AWS region they want to work with.
* <p>
* <b>This method is not threadsafe. Endpoints should be configured when the
* client is created and before any service requests are made. Changing it
* afterwards creates inevitable race conditions for any service requests in
* transit.</b>
* <p>
* Callers can pass in just the endpoint (ex: "ec2.amazonaws.com") or a full
* URL, including the protocol (ex: "https://ec2.amazonaws.com"). If the
* protocol is not specified here, the default protocol from this client's
* {@link ClientConfiguration} will be used, which by default is HTTPS.
* <p>
* For more information on using AWS regions with the AWS SDK for Java, and
* a complete list of all available endpoints for all AWS services, see:
* <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=3912">
* http://developer.amazonwebservices.com/connect/entry.jspa?externalID=3912</a>
*
* @param endpoint
* The endpoint (ex: "ec2.amazonaws.com") or a full URL,
* including the protocol (ex: "https://ec2.amazonaws.com") of
* the region specific AWS endpoint this client will communicate
* with.
* @throws IllegalArgumentException
* If any problems are detected with the specified endpoint.
*/
public void setEndpoint(String endpoint) throws IllegalArgumentException {
/*
* If the endpoint doesn't explicitly specify a protocol to use, then
* we'll defer to the default protocol specified in the client
* configuration.
*/
if (endpoint.contains("://") == false) {
endpoint = clientConfiguration.getProtocol().toString() + "://" + endpoint;
}
try {
this.endpoint = new URI(endpoint);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
/**
* An alternative to {@link AmazonWebServiceClient#setEndpoint(String)}, sets the
* regional endpoint for this client's service calls. Callers can use this
* method to control which AWS region they want to work with.
* <p>
* <b>This method is not threadsafe. A region should be configured when the
* client is created and before any service requests are made. Changing it
* afterwards creates inevitable race conditions for any service requests in
* transit or retrying.</b>
* <p>
* By default, all service endpoints in all regions use the https protocol.
* To use http instead, specify it in the {@link ClientConfiguration}
* supplied at construction.
*
* @param region
* The region this client will communicate with. See
* {@link Region#getRegion(com.amazonaws.regions.Regions)} for
* accessing a given region.
* @throws java.lang.IllegalArgumentException
* If the given region is null, or if this service isn't
* available in the given region. See
* {@link Region#isServiceSupported(String)}
* @see Region#getRegion(com.amazonaws.regions.Regions)
* @see Region#createClient(Class, com.amazonaws.auth.AWSCredentialsProvider, ClientConfiguration)
*/
public void setRegion(Region region) throws IllegalArgumentException {
if ( region == null )
throw new IllegalArgumentException("No region provided");
if ( !region.isServiceSupported(getServiceAbbreviation()) )
throw new IllegalArgumentException(getServiceAbbreviation() + " isn't supported in region "
+ region.getName());
String serviceEndpoint = region.getServiceEndpoint(getServiceAbbreviation());
int protocolIdx = serviceEndpoint.indexOf("://");
// Strip off the protocol to allow the client config to specify it
if ( protocolIdx >= 0 ) {
serviceEndpoint = serviceEndpoint.substring(protocolIdx + "://".length());
}
setEndpoint(serviceEndpoint);
}
/**
* Returns the service abbreviation for this service, used for identifying
* service endpoints by region.
*
* @see ServiceAbbreviations
*/
protected String getServiceAbbreviation() {
return "NO_SERVICE_ABBREVIATION_SPECIFIED";
}
public void setConfiguration(ClientConfiguration clientConfiguration) {
this.clientConfiguration = clientConfiguration;
client = new AmazonHttpClient(clientConfiguration);
}
/**
* Shuts down this client object, releasing any resources that might be held
* open. This is an optional method, and callers are not expected to call
* it, but can if they want to explicitly release any open resources. Once a
* client has been shutdown, it should not be used to make any more
* requests.
*/
public void shutdown() {
client.shutdown();
}
/**
* Converts a Request<T> object into an HttpRequest object. Copies all the
* headers, parameters, etc. from the Request into the new HttpRequest.
*
* @param request
* The request to convert.
* @param methodName
* The HTTP method (GET, PUT, DELETE, HEAD) to use in the
* converted HttpRequest object.
*
* @return A new HttpRequest object created from the details of the
* specified Request<T> object.
*/
@Deprecated
protected <T> HttpRequest convertToHttpRequest(Request<T> request, HttpMethodName methodName) {
HttpRequest httpRequest = new HttpRequest(methodName);
for (Entry<String, String> parameter : request.getParameters().entrySet()) {
httpRequest.addParameter(parameter.getKey(), parameter.getValue());
}
for (Entry<String, String> parameter : request.getHeaders().entrySet()) {
httpRequest.addHeader(parameter.getKey(), parameter.getValue());
}
httpRequest.setServiceName(request.getServiceName());
httpRequest.setEndpoint(request.getEndpoint());
httpRequest.setResourcePath(request.getResourcePath());
httpRequest.setOriginalRequest(request.getOriginalRequest());
return httpRequest;
}
/**
* Appends a request handler to the list of registered handlers that are run
* as part of a request's lifecycle.
*
* @param requestHandler
* The new handler to add to the current list of request
* handlers.
*/
public void addRequestHandler(RequestHandler requestHandler) {
requestHandlers.add(requestHandler);
}
/**
* Removes a request handler from the list of registered handlers that are run
* as part of a request's lifecycle.
*
* @param requestHandler
* The handler to remove from the current list of request
* handlers.
*/
public void removeRequestHandler(RequestHandler requestHandler) {
requestHandlers.remove(requestHandler);
}
protected ExecutionContext createExecutionContext() {
ExecutionContext executionContext = new ExecutionContext(requestHandlers);
return executionContext;
}
/**
* Sets the optional value for time offset for this client. This
* value will be applied to all requests processed through this client.
* Value is in seconds, positive values imply the current clock is "fast",
* negative values imply clock is slow.
*
* @param timeOffset
* The optional value for time offset (in seconds) for this client.
*/
public void setTimeOffset(int timeOffset) {
this.timeOffset = timeOffset;
}
/**
* Sets the optional value for time offset for this client. This
* value will be applied to all requests processed through this client.
* Value is in seconds, positive values imply the current clock is "fast",
* negative values imply clock is slow.
*
* @param timeOffset
* The optional value for time offset (in seconds) for this client.
*
* @return the updated web service client
*/
public AmazonWebServiceClient withTimeOffset(int timeOffset) {
setTimeOffset(timeOffset);
return this;
}
/**
* Returns the optional value for time offset for this client. This
* value will be applied to all requests processed through this client.
* Value is in seconds, positive values imply the current clock is "fast",
* negative values imply clock is slow.
*
* @return The optional value for time offset (in seconds) for this client.
*/
public int getTimeOffset() {
return timeOffset;
}
}