Skip to content

Commit 97a56b9

Browse files
committed
support request host prefix in core sdk (#2198)
* support request host prefix in core sdk * address pr comments
1 parent b23646c commit 97a56b9

File tree

6 files changed

+411
-44
lines changed

6 files changed

+411
-44
lines changed

aws-android-sdk-core/src/main/java/com/amazonaws/DefaultRequest.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public class DefaultRequest<T> implements Request<T> {
7373
/** All AWS Request metrics are collected into this object. */
7474
private AWSRequestMetrics metrics;
7575

76+
/** An optional prefix to prepend to the request endpoint host */
77+
private String hostPrefix;
78+
7679
/**
7780
* Constructs a new DefaultRequest with the specified service name and the
7881
* original, user facing request object.
@@ -111,57 +114,55 @@ public AmazonWebServiceRequest getOriginalRequest() {
111114
}
112115

113116
/**
114-
* @see com.amazonaws.Request#addHeader(java.lang.String, java.lang.String)
117+
* {@inheritDoc}
115118
*/
116119
@Override
117120
public void addHeader(String name, String value) {
118121
headers.put(name, value);
119122
}
120123

121124
/**
122-
* @see com.amazonaws.Request#getHeaders()
125+
* {@inheritDoc}
123126
*/
124127
@Override
125128
public Map<String, String> getHeaders() {
126129
return headers;
127130
}
128131

129132
/**
130-
* @see com.amazonaws.Request#setResourcePath(java.lang.String)
133+
* {@inheritDoc}
131134
*/
132135
@Override
133136
public void setResourcePath(String resourcePath) {
134137
this.resourcePath = resourcePath;
135138
}
136139

137140
/**
138-
* @see com.amazonaws.Request#getResourcePath()
141+
* {@inheritDoc}
139142
*/
140143
@Override
141144
public String getResourcePath() {
142145
return resourcePath;
143146
}
144147

145148
/**
146-
* @see com.amazonaws.Request#addParameter(java.lang.String,
147-
* java.lang.String)
149+
* {@inheritDoc}
148150
*/
149151
@Override
150152
public void addParameter(String name, String value) {
151153
parameters.put(name, value);
152154
}
153155

154156
/**
155-
* @see com.amazonaws.Request#getParameters()
157+
* {@inheritDoc}
156158
*/
157159
@Override
158160
public Map<String, String> getParameters() {
159161
return parameters;
160162
}
161163

162164
/**
163-
* @see com.amazonaws.Request#withParameter(java.lang.String,
164-
* java.lang.String)
165+
* {@inheritDoc}
165166
*/
166167
@Override
167168
public Request<T> withParameter(String name, String value) {
@@ -170,63 +171,63 @@ public Request<T> withParameter(String name, String value) {
170171
}
171172

172173
/**
173-
* @see com.amazonaws.Request#getHttpMethod()
174+
* {@inheritDoc}
174175
*/
175176
@Override
176177
public HttpMethodName getHttpMethod() {
177178
return httpMethod;
178179
}
179180

180181
/**
181-
* @see com.amazonaws.Request#setHttpMethod(com.amazonaws.http.HttpMethodName)
182+
* {@inheritDoc}
182183
*/
183184
@Override
184185
public void setHttpMethod(HttpMethodName httpMethod) {
185186
this.httpMethod = httpMethod;
186187
}
187188

188189
/**
189-
* @see com.amazonaws.Request#setEndpoint(java.net.URI)
190+
* {@inheritDoc}
190191
*/
191192
@Override
192193
public void setEndpoint(URI endpoint) {
193194
this.endpoint = endpoint;
194195
}
195196

196197
/**
197-
* @see com.amazonaws.Request#getEndpoint()
198+
* {@inheritDoc}
198199
*/
199200
@Override
200201
public URI getEndpoint() {
201202
return endpoint;
202203
}
203204

204205
/**
205-
* @see com.amazonaws.Request#getServiceName()
206+
* {@inheritDoc}
206207
*/
207208
@Override
208209
public String getServiceName() {
209210
return serviceName;
210211
}
211212

212213
/**
213-
* @see com.amazonaws.Request#getContent()
214+
* {@inheritDoc}
214215
*/
215216
@Override
216217
public InputStream getContent() {
217218
return content;
218219
}
219220

220221
/**
221-
* @see com.amazonaws.Request#setContent(java.io.InputStream)
222+
* {@inheritDoc}
222223
*/
223224
@Override
224225
public void setContent(InputStream content) {
225226
this.content = content;
226227
}
227228

228229
/**
229-
* @see com.amazonaws.Request#setHeaders(java.util.Map)
230+
* {@inheritDoc}
230231
*/
231232
@Override
232233
public void setHeaders(Map<String, String> headers) {
@@ -235,7 +236,7 @@ public void setHeaders(Map<String, String> headers) {
235236
}
236237

237238
/**
238-
* @see com.amazonaws.Request#setParameters(java.util.Map)
239+
* {@inheritDoc}
239240
*/
240241
@Override
241242
public void setParameters(Map<String, String> parameters) {
@@ -244,23 +245,39 @@ public void setParameters(Map<String, String> parameters) {
244245
}
245246

246247
/**
247-
* @see com.amazonaws.Request#getTimeOffset
248+
* {@inheritDoc}
249+
*/
250+
@Override
251+
public String getHostPrefix() {
252+
return this.hostPrefix;
253+
}
254+
255+
/**
256+
* {@inheritDoc}
257+
*/
258+
@Override
259+
public void setHostPrefix(String hostPrefix) {
260+
this.hostPrefix = hostPrefix;
261+
}
262+
263+
/**
264+
* {@inheritDoc}
248265
*/
249266
@Override
250267
public int getTimeOffset() {
251268
return timeOffset;
252269
}
253270

254271
/**
255-
* @see Request#setTimeOffset(int)
272+
* {@inheritDoc}
256273
*/
257274
@Override
258275
public void setTimeOffset(int timeOffset) {
259276
this.timeOffset = timeOffset;
260277
}
261278

262279
/**
263-
* @see Request#setTimeOffset(int)
280+
* {@inheritDoc}
264281
*/
265282
@Override
266283
@SuppressWarnings("checkstyle:hiddenfield")

aws-android-sdk-core/src/main/java/com/amazonaws/Request.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -151,6 +151,24 @@ public interface Request<T> {
151151
*/
152152
public void setContent(InputStream content);
153153

154+
/**
155+
* Returns the optional prefix to prepend to the request endpoint's host
156+
* before sending this request.
157+
*
158+
* @return the optional prefix to prepend to the request endpoint host
159+
* before sending this request.
160+
*/
161+
public String getHostPrefix();
162+
163+
/**
164+
* Sets the optional prefix to prepend to the request endpoint's host before
165+
* sending this request.
166+
*
167+
* @param hostPrefix The optional prefix to prepend to the request endpoint
168+
* host.
169+
*/
170+
public void setHostPrefix(String hostPrefix);
171+
154172
/**
155173
* Returns the name of the Amazon service this request is for.
156174
*

aws-android-sdk-core/src/main/java/com/amazonaws/http/AmazonHttpClient.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -43,9 +43,12 @@
4343

4444
import com.amazonaws.logging.Log;
4545
import com.amazonaws.logging.LogFactory;
46+
import com.amazonaws.util.URIBuilder;
47+
4648
import java.io.IOException;
4749
import java.io.InputStream;
4850
import java.net.URI;
51+
import java.net.URISyntaxException;
4952
import java.util.Collections;
5053
import java.util.Date;
5154
import java.util.HashMap;
@@ -201,6 +204,20 @@ public <T> Response<T> execute(Request<?> request,
201204
HttpResponseHandler<AmazonWebServiceResponse<T>> responseHandler,
202205
HttpResponseHandler<AmazonServiceException> errorResponseHandler,
203206
ExecutionContext executionContext) {
207+
// Prepend host prefix if specified in the request.
208+
if (request.getHostPrefix() != null) {
209+
try {
210+
URI endpoint = request.getEndpoint();
211+
String host = request.getHostPrefix() + endpoint.getHost();
212+
request.setEndpoint(URIBuilder.builder(endpoint)
213+
.host(host)
214+
.build());
215+
} catch (URISyntaxException error) {
216+
if (log.isDebugEnabled()) {
217+
log.debug("Failed to prepend host prefix: " + error.getMessage(), error);
218+
}
219+
}
220+
}
204221
if (executionContext == null) {
205222
throw new AmazonClientException(
206223
"Internal SDK Error: No execution context parameter specified.");
@@ -261,8 +278,6 @@ List<RequestHandler2> requestHandler2s(Request<?> request,
261278
* Internal method to execute the HTTP method given.
262279
*
263280
* @see AmazonHttpClient#execute(Request, HttpResponseHandler,
264-
* HttpResponseHandler)
265-
* @see AmazonHttpClient#execute(Request, HttpResponseHandler,
266281
* HttpResponseHandler, ExecutionContext)
267282
*/
268283
@SuppressWarnings("checkstyle:methodlength")
@@ -564,7 +579,6 @@ public void shutdown() {
564579
*
565580
* @param originalRequest The original service request that is being
566581
* executed.
567-
* @param method The current HTTP method being executed.
568582
* @param exception The client/service exception from the failed request.
569583
* @param requestCount The number of times the current request has been
570584
* attempted.
@@ -628,8 +642,6 @@ private boolean isRequestSuccessful(HttpResponse response) {
628642
* handled.
629643
* @param responseHandler The response unmarshaller used to interpret the
630644
* contents of the response.
631-
* @param method The HTTP method that was invoked, and contains the contents
632-
* of the response.
633645
* @param executionContext Extra state information about the request
634646
* currently being executed.
635647
* @return The contents of the response, unmarshalled using the specified
@@ -686,7 +698,6 @@ <T> T handleResponse(Request<?> request,
686698
* handled.
687699
* @param errorResponseHandler The response handler responsible for
688700
* unmarshalling the error response.
689-
* @param method The HTTP method containing the actual response content.
690701
* @throws IOException If any problems are encountering reading the error
691702
* response.
692703
*/

0 commit comments

Comments
 (0)