CASC-214 Improve Service Url Construction to Add Non-Standard Ports if Missing from Configuration

Problem: sometimes the port is missing from the configuration.  This generates the wrong service url.
Solution: Add the server port if the server configuration does not have one.
QA Notes: Added unit tests to confirm behavior (and old unit tests still pass)
This commit is contained in:
Scott Battaglia 2014-03-09 23:13:04 -04:00
parent 590a79c6a6
commit 75584a2c33
2 changed files with 49 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,24 @@ 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);
final boolean serverNameContainsPort = serverNameContainsPort(containsScheme, serverName);
System.out.println("serverNameContainsPort " + serverNameContainsPort);
final boolean requestIsOnStandardPort = requestIsOnStandardPort(request);
System.out.println("requestIsOnStandardPort " + requestIsOnStandardPort);
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/");