WebContentTools { public static String normalizeURLLink(final String urlLink) { if (null == urlLink || 0 == urlLink.length()) { return urlLink; } try { String processedLink = urlLink; for (int i = 0; i < URL_FRAGMENT_REPLACE_SRC.length; i++) { processedLink = processedLink.replaceAll(URL_FRAGMENT_REPLACE_SRC[i], URL_FRAGMENT_REPLACE_DEST[i]); } URL url = new URL(processedLink); final StringBuilder linkBuilder = new StringBuilder(); if (WebClient.PROTOCOL_HTTPS.equals(url.getProtocol())) { linkBuilder.append(WebClient.PROTOCOL_HTTP).append(": } else { linkBuilder.append(url.getProtocol()).append(": } String host = url.getHost(); if (null != host) { int dotsCount = 0; for (int i = 0; i < host.length(); i++) { if (host.charAt(i) == '.') { dotsCount++; } } if (dotsCount == 1 && !host.startsWith("www.")) { host = "www." + host; } } linkBuilder.append(host.toLowerCase()); linkBuilder.append(processURLPort(url.getProtocol(), url.getPort())); String urlPath = url.getPath(); for (int i = 0; i < URL_FRAGMENT_PATH_REPLACE_SRC.length; i++) { urlPath = urlPath.replaceAll(URL_FRAGMENT_PATH_REPLACE_SRC[i], URL_FRAGMENT_PATH_REPLACE_DEST[i]); } linkBuilder.append(urlPath); if (null != url.getQuery()) { linkBuilder.append("?").append(url.getQuery()); } return linkBuilder.toString(); } catch (MalformedURLException exc) { LOG.error("Will not normalize URL: " + urlLink + " - " + exc.getMessage()); } return urlLink; }  static String normalizeURLLink(final String urlLink); static String[] normalizeURLLinks(final String[] urlLinks);  }
@Test public void testNormalizeURLLink_lowerCase() throws Exception { final String inLink = "HTTP: final String outLink = "http: assertEquals("Lower case conversion failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_decodeEncoded() throws Exception { final String inLink = "http: final String outLink = "http: assertEquals("Decode percent-encoded octets of unreserved characters failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_defaultPorts() throws Exception { final String inLink = "http: final String outLink = "http: assertEquals("Removing the default port failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_RemoveDots() throws Exception { final String inLink = "http: final String outLink = "http: assertEquals("Removing dot-segments failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_limitProtocols() throws Exception { final String inLink = "https: final String outLink = "http: assertEquals("Limiting protocols failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_removeSlashes() throws Exception { final String inLink = "http: final String outLink = "http: assertEquals("Removing duplicate slashes failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
@Test public void testNormalizeURLLink_addWww() throws Exception { final String inLink = "http: final String outLink = "http: assertEquals("Adding \"www\" failed", outLink, WebContentTools.normalizeURLLink(inLink)); }