Skip to content

Commit 351ccf3

Browse files
committed
Cleanup in UriUtils.getUpdateUri
- String instantiation replaced with StringBuilder and empty string constant Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 59364ee commit 351ccf3

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

utils/src/com/cloud/utils/UriUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static String getUpdateUri(String url, boolean encrypt) {
143143
URIBuilder builder = new URIBuilder(url);
144144
builder.removeQuery();
145145

146-
String updatedQuery = new String();
146+
StringBuilder updatedQuery = new StringBuilder();
147147
List<NameValuePair> queryParams = getUserDetails(query);
148148
ListIterator<NameValuePair> iterator = queryParams.listIterator();
149149
while (iterator.hasNext()) {
@@ -156,14 +156,16 @@ public static String getUpdateUri(String url, boolean encrypt) {
156156
value = param.getValue();
157157
}
158158

159-
if (updatedQuery.isEmpty()) {
160-
updatedQuery += (param.getName() + "=" + value);
159+
if (updatedQuery.length() == 0) {
160+
updatedQuery.append(param.getName()).append('=')
161+
.append(value);
161162
} else {
162-
updatedQuery += ("&" + param.getName() + "=" + value);
163+
updatedQuery.append('&').append(param.getName())
164+
.append('=').append(value);
163165
}
164166
}
165167

166-
String schemeAndHost = new String();
168+
String schemeAndHost = "";
167169
URI newUri = builder.build();
168170
if (newUri.getScheme() != null) {
169171
schemeAndHost = newUri.getScheme() + "://" + newUri.getHost();

utils/test/com/cloud/utils/UriUtilsTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public void getUpdateUri() {
4747
"http://localhost/foo/bar?password=1234", true).startsWith(
4848
"http://localhost/foo/bar"));
4949

50+
//just to see if it is still ok with multiple parameters
51+
Assert.assertEquals("http://localhost/foo/bar?param1=true&param2=12345", UriUtils
52+
.getUpdateUri("http://localhost/foo/bar?param1=true&param2=12345", false));
53+
5054
//XXX: Interesting cases not covered:
5155
// * port is ignored and left out from the return value
5256
}

0 commit comments

Comments
 (0)