Skip to content

Commit c5a320e

Browse files
committed
fix exception thrown for malformed URI with newURI method
1 parent a31c1e0 commit c5a320e

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

utils/src/main/java/software/amazon/awssdk/utils/uri/SdkURI.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,46 @@ public URI newURI(String s) throws URISyntaxException {
8080
public URI newURI(String scheme,
8181
String userInfo, String host, int port,
8282
String path, String query, String fragment)
83-
throws URISyntaxException {
83+
throws URISyntaxException {
8484
if (!isAccountIdUri(host)) {
8585
log.trace(() -> "skipping cache for host" + host);
8686
return new URI(scheme, userInfo, host, port, path, query, fragment);
8787
}
88-
HostConstructorArgs key = new HostConstructorArgs(scheme, userInfo, host, port, path, query, fragment);
89-
boolean containsK = cache.contains(key);
90-
URI uri = cache.get(key);
91-
logCacheUsage(containsK, uri);
92-
return uri;
88+
try {
89+
HostConstructorArgs key = new HostConstructorArgs(scheme, userInfo, host, port, path, query, fragment);
90+
boolean containsK = cache.contains(key);
91+
URI uri = cache.get(key);
92+
logCacheUsage(containsK, uri);
93+
return uri;
94+
} catch (IllegalArgumentException e) {
95+
if (e.getCause() instanceof URISyntaxException) {
96+
throw (URISyntaxException) e.getCause();
97+
}
98+
throw e;
99+
}
100+
93101
}
94102

95103
public URI newURI(String scheme,
96104
String authority,
97105
String path, String query, String fragment)
98-
throws URISyntaxException {
106+
throws URISyntaxException {
99107
if (!isAccountIdUri(authority)) {
100108
log.trace(() -> "skipping cache for authority" + authority);
101109
return new URI(scheme, authority, path, query, fragment);
102110
}
103-
AuthorityConstructorArgs key = new AuthorityConstructorArgs(scheme, authority, path, query, fragment);
104-
boolean containsK = cache.contains(key);
105-
URI uri = cache.get(key);
106-
logCacheUsage(containsK, uri);
107-
return uri;
111+
try {
112+
AuthorityConstructorArgs key = new AuthorityConstructorArgs(scheme, authority, path, query, fragment);
113+
boolean containsK = cache.contains(key);
114+
URI uri = cache.get(key);
115+
logCacheUsage(containsK, uri);
116+
return uri;
117+
} catch (IllegalArgumentException e) {
118+
if (e.getCause() instanceof URISyntaxException) {
119+
throw (URISyntaxException) e.getCause();
120+
}
121+
throw e;
122+
}
108123
}
109124

110125
/*

utils/src/test/java/software/amazon/awssdk/utils/SdkURITest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ void notCached_shouldCreateNewInstance() {
212212
}
213213

214214
@ParameterizedTest
215-
@ValueSource(strings = {"potatoes tomatoes", "123412341234. potatoes tomatoes"})
216-
void malformedURI_shouldThrowsSameExceptionAsUriClass(String malformedURI) {
217-
String malformedUri = "potatoes tomatoes"; // not cached by SdkURI
215+
@ValueSource(strings = {"potatoes tomatoes", "123412341234 potatoes tomatoes"})
216+
void malformedURI_shouldThrowsSameExceptionAsUriClass(String malformedUri) {
218217

219218
assertThatThrownBy(() -> SdkURI.getInstance().create(malformedUri))
220219
.as("Malformed uri should throw IllegalArgumentException using the create method")
@@ -234,6 +233,13 @@ void malformedURI_shouldThrowsSameExceptionAsUriClass(String malformedURI) {
234233
assertThat(getCache().size()).as("Cache should be empty if create URI fails")
235234
.isEqualTo(0);
236235

236+
assertThatThrownBy(() -> new URI("scheme", malformedUri, "path", "query", "fragment"))
237+
.as("CONSTRUCTOR")
238+
.isInstanceOf(URISyntaxException.class);
239+
assertThat(getCache().size()).as("Cache should be empty if create URI fails")
240+
.isEqualTo(0);
241+
242+
237243
assertThatThrownBy(() -> SdkURI.getInstance().newURI("scheme", "userInfo", malformedUri,
238244
444, "path", "query", "fragment"))
239245
.as("Malformed uri should throw URISyntaxException using the newURI with host method")

0 commit comments

Comments
 (0)