diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index 2e771aea9ca..42456b52147 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -94,15 +94,12 @@ public class UriUtils { if (pathStart > 0) { String[] tokens = url.substring(pathStart + 1).split("/"); - if (tokens != null) { - StringBuffer sb = new StringBuffer(); - sb.append(url.substring(0, pathStart)); - for (String token : tokens) { - sb.append("/").append(URLEncoder.encode(token)); - } - - return sb.toString(); + StringBuilder sb = new StringBuilder(url.substring(0, pathStart)); + for (String token : tokens) { + sb.append("/").append(URLEncoder.encode(token)); } + + return sb.toString(); } // no need to do URL component encoding diff --git a/utils/test/com/cloud/utils/UriUtilsTest.java b/utils/test/com/cloud/utils/UriUtilsTest.java new file mode 100644 index 00000000000..8474bd0a6ed --- /dev/null +++ b/utils/test/com/cloud/utils/UriUtilsTest.java @@ -0,0 +1,33 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License 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.cloud.utils; + +import junit.framework.Assert; + +import org.junit.Test; + +public class UriUtilsTest { + @Test + public void encodeURIComponent() { + Assert.assertEquals("http://localhost", + UriUtils.encodeURIComponent("http://localhost")); + Assert.assertEquals("http://localhost/", + UriUtils.encodeURIComponent("http://localhost/")); + Assert.assertEquals("http://localhost/foo/bar", + UriUtils.encodeURIComponent("http://localhost/foo/bar")); + } +}