Skip to content

Commit c9a9402

Browse files
authored
Changes to support encoded resource path coming in request. (#2546)
* Changes to support encoded resource path coming in request. * Making get and set encoded uri methods on request public.
1 parent 58ad144 commit c9a9402

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public class DefaultRequest<T> implements Request<T> {
7676
/** An optional prefix to prepend to the request endpoint host */
7777
private String hostPrefix;
7878

79+
/** encoded uri resource path */
80+
private String encodedUriResourcePath;
81+
7982
/**
8083
* Constructs a new DefaultRequest with the specified service name and the
8184
* original, user facing request object.
@@ -371,4 +374,14 @@ public boolean isStreaming() {
371374
public void setStreaming(boolean streaming) {
372375
this.streaming = streaming;
373376
}
377+
378+
@Override
379+
public String getEncodedUriResourcePath() {
380+
return encodedUriResourcePath;
381+
}
382+
383+
@Override
384+
public void setEncodedResourcePath(String encodedUriResourcePath) {
385+
this.encodedUriResourcePath = encodedUriResourcePath;
386+
}
374387
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ public interface Request<T> {
6161
*
6262
* @param path The path to the resource being requested.
6363
*/
64+
@Deprecated
6465
public void setResourcePath(String path);
6566

6667
/**
6768
* Returns the path to the resource being requested.
6869
*
6970
* @return The path to the resource being requested.
7071
*/
72+
@Deprecated
7173
public String getResourcePath();
7274

7375
/**
@@ -262,4 +264,16 @@ public interface Request<T> {
262264
* @param streaming the streaming flag.
263265
*/
264266
public void setStreaming(boolean streaming);
267+
268+
/**
269+
* Get Encoded Uri Resource Path
270+
*/
271+
public String getEncodedUriResourcePath();
272+
273+
/**
274+
*
275+
* Set Encoded Uri Resource Path
276+
* @param encodedUriResourcePath encoded uri resource path
277+
*/
278+
public void setEncodedResourcePath(String encodedUriResourcePath);
265279
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,14 @@ protected String getSignedHeadersString(Request<?> request) {
250250

251251
protected String getCanonicalRequest(Request<?> request, String contentSha256) {
252252
/* This would url-encode the resource path for the first time */
253-
final String path = HttpUtils.appendUri(request.getEndpoint().getPath(),
254-
request.getResourcePath());
253+
String path;
254+
if(request.getEncodedUriResourcePath()!= null){
255+
path = HttpUtils.appendUriEncoded(request.getEndpoint().getPath(),
256+
request.getEncodedUriResourcePath());
257+
}else{
258+
path = HttpUtils.appendUri(request.getEndpoint().getPath(),
259+
request.getResourcePath());
260+
};
255261

256262
final String canonicalRequest =
257263
request.getHttpMethod().toString() + "\n" +

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ public HttpRequest createHttpRequest(Request<?> request,
5454
* have to escape the double-slash between endpoint and resource-path
5555
* into "/%2F"
5656
*/
57-
String uri = HttpUtils.appendUri(endpoint.toString(), request.getResourcePath(), true);
57+
String uri;
58+
//The if here is for backward compatibility
59+
if(request.getEncodedUriResourcePath()!= null){
60+
uri = HttpUtils.appendUriEncoded(endpoint.toString(), request.getEncodedUriResourcePath());
61+
}else{
62+
uri = HttpUtils.appendUri(endpoint.toString(), request.getResourcePath(), true);
63+
}
5864
final String encodedParams = HttpUtils.encodeParameters(request);
5965
HttpMethodName method = request.getHttpMethod();
6066

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ public static String appendUri(final String baseUri, String path,
257257
return resultUri;
258258
}
259259

260+
/**
261+
* Append the given path to the given baseUri.
262+
* <p>
263+
* This method will encode the given path but not the given baseUri.
264+
* </p>
265+
*
266+
* @param baseUri The URI to append to (required, may be relative)
267+
* @param paths The path array to append (may be null or empty)
268+
* @return The baseUri with the (encoded) path appended
269+
*/
270+
public static String appendUriEncoded(final String baseUri, String paths) {
271+
String resultUri = baseUri;
272+
if (paths != null) {
273+
resultUri += paths;
274+
}
275+
return resultUri;
276+
}
277+
260278
/**
261279
* Fetches a file from the URI given and returns an input stream to it.
262280
*

aws-android-sdk-core/src/test/java/com/amazonaws/http/HttpRequestFactoryTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public class HttpRequestFactoryTest {
3535
private Request<?> request;
3636
private ClientConfiguration clientConfiguration;
3737
private ExecutionContext context;
38-
38+
private final String endpoint = "https://s3.amazonaws.com";
3939
@Before
4040
public void setup() {
4141
factory = new HttpRequestFactory();
4242
request = new DefaultRequest<Object>("dummy");
43-
request.setEndpoint(URI.create("https://s3.amazonaws.com"));
43+
request.setEndpoint(URI.create(endpoint));
4444
clientConfiguration = new ClientConfiguration();
4545
context = new ExecutionContext();
4646
}
@@ -70,4 +70,15 @@ public void testEnableCompression() {
7070
final Map<String, String> headers = httpRequest.getHeaders();
7171
assertEquals("accept encoding is gzip", "gzip", headers.get("Accept-Encoding"));
7272
}
73+
74+
@Test
75+
public void testHttpRequestCreatedWithEncodedResourcePath() {
76+
String expectedResourcePath = "/android%2F/sdk";
77+
String expectedUri = endpoint+expectedResourcePath;
78+
clientConfiguration.withEnableGzip(true);
79+
request.setEncodedResourcePath(expectedResourcePath);
80+
final HttpRequest httpRequest = factory.createHttpRequest(request, clientConfiguration, context);
81+
assertEquals(URI.create(expectedUri), httpRequest.getUri());
82+
}
83+
7384
}

aws-android-sdk-core/src/test/java/com/amazonaws/util/HttpUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,13 @@ public void testAppendUriNoEscapeDoubleSlash() {
224224
Assert.assertEquals(HttpUtils.appendUri(host, resourcePath, false),
225225
"foo.com/aws//android/sdk");
226226
}
227+
228+
@Test
229+
public void testAppendEncodedUri() {
230+
final String host = "foo.com";
231+
final String resourcePath = "/android%2F/sdk";
232+
Assert.assertEquals(HttpUtils.appendUriEncoded(host, resourcePath),
233+
"foo.com/android%2F/sdk");
234+
}
235+
227236
}

0 commit comments

Comments
 (0)