@Test public void testNormalizeURLLink_lowerCase() throws Exception { final String inLink = "HTTP: final String outLink = "http: assertEquals("Lower case conversion failed", outLink, WebContentTools.normalizeURLLink(inLink)); }
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; }
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; } }
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; }  }
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); }
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);  }