Skip to content

Commit

Permalink
Uri Builder can be done without manual string path concatentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Jan 28, 2015
1 parent 89e5f0c commit 465bcb0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
Expand Up @@ -14,19 +14,13 @@
*/
package org.cloudfoundry.identity.uaa.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.utils.URIBuilder;
import org.cloudfoundry.identity.uaa.zone.IdentityZoneHolder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import java.net.URISyntaxException;
import org.springframework.web.util.UriComponentsBuilder;

public class UaaUrlUtils {

private final Log logger = LogFactory.getLog(getClass());

private final String uaaBaseUrl;

public UaaUrlUtils(@Value("${uaaBaseUrl}") String uaaBaseUrl) {
Expand All @@ -38,24 +32,18 @@ public String getUaaUrl() {
}

public String getUaaUrl(String path) {
return getURIBuilder(path).toString();
return getURIBuilder(path).build().toUriString();
}

public String getUaaHost() {
return getURIBuilder("").getHost();
return getURIBuilder("").build().getHost();
}

private URIBuilder getURIBuilder(String path) {
URIBuilder builder = null;
try {
builder = new URIBuilder(uaaBaseUrl + path);
String subdomain = IdentityZoneHolder.get().getSubdomain();
if (!StringUtils.isEmpty(subdomain)) {
builder.setHost(subdomain + "." + builder.getHost());
}
return builder;
} catch (URISyntaxException e) {
logger.error("Exception raised when building URI " + e);
private UriComponentsBuilder getURIBuilder(String path) {
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uaaBaseUrl).path(path);
String subdomain = IdentityZoneHolder.get().getSubdomain();
if (!StringUtils.isEmpty(subdomain)) {
builder.host(subdomain + "." + builder.build().getHost());
}
return builder;
}
Expand Down
Expand Up @@ -42,6 +42,7 @@ public void testGetUaaUrl() throws Exception {
@Test
public void testGetUaaUrlWithPath() throws Exception {
assertEquals("http://uaa.example.com/login", uaaURLUtils.getUaaUrl("/login"));
assertEquals("http://uaa.example.com/login", uaaURLUtils.getUaaUrl("login"));
}

@Test
Expand Down

0 comments on commit 465bcb0

Please sign in to comment.