Merge pull request #67 from battags/CASC-214

CASC-214 Improve Service Url Construction to Add Non-Standard Ports
This commit is contained in:
Misagh Moayyed 2014-03-17 09:15:29 -07:00
commit 286105266f
2 changed files with 44 additions and 0 deletions

View File

@ -225,6 +225,21 @@ public final class CommonUtils {
return serverNames[0];
}
private static boolean serverNameContainsPort(final boolean containsScheme, final String serverName) {
if (!containsScheme && serverName.contains(":")) {
return true;
}
final int schemeIndex = serverName.indexOf(":");
final int portIndex = serverName.lastIndexOf(":");
return schemeIndex != portIndex;
}
private static boolean requestIsOnStandardPort(final HttpServletRequest request) {
final int serverPort = request.getServerPort();
return serverPort == 80 || serverPort == 443;
}
/**
* Constructs a service url from the HttpServletRequest or from the given
* serviceUrl. Prefers the serviceUrl provided if both a serviceUrl and a
@ -250,11 +265,19 @@ public final class CommonUtils {
final String serverName = findMatchingServerName(request, serverNames);
boolean containsScheme = true;
if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) {
buffer.append(request.isSecure() ? "https://" : "http://");
containsScheme = false;
}
buffer.append(serverName);
if (!serverNameContainsPort(containsScheme, serverName) && !requestIsOnStandardPort(request)) {
buffer.append(":");
buffer.append(request.getServerPort());
}
buffer.append(request.getRequestURI());
if (CommonUtils.isNotBlank(request.getQueryString())) {

View File

@ -136,6 +136,27 @@ public final class CommonUtilsTests extends TestCase {
assertEquals(CONST_MY_URL, constructedUrl);
}
private void constructUrlNonStandardPortAndNoPortInConfigTest(final String serverNameList) {
final String CONST_MY_URL = "https://www.myserver.com:555/hello/hithere/";
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");
request.addHeader("Host", "www.myserver.com");
request.setScheme("https");
request.setSecure(true);
request.setServerPort(555);
final MockHttpServletResponse response = new MockHttpServletResponse();
final String constructedUrl = CommonUtils.constructServiceUrl(request, response, null,
serverNameList, "ticket", false);
assertEquals(CONST_MY_URL, constructedUrl);
}
public void testConstructUrlNonStandardPortAndNoScheme() {
constructUrlNonStandardPortAndNoPortInConfigTest("www.myserver.com");
}
public void testConstructUrlNonStandardPortAndScheme() {
constructUrlNonStandardPortAndNoPortInConfigTest("https://www.myserver.com");
}
public void testConstructUrlWithMultipleHostsNoPortsOrProtocol() {
final String CONST_MY_URL = "https://www.myserver.com/hello/hithere/";
final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello/hithere/");