@Test public void testUrlShortener() { String shortUrl; shortUrl = UrlHelper.shortenUrl("http: null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: "?huhu=value", null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: "?abc=12&abd=d", null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: null); Assert.assertEquals("http: shortUrl = UrlHelper.shortenUrl("http: ";identifier=2321"); Assert.assertEquals("http: }
public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier) { StringBuilder url = new StringBuilder(); url.append(protocolHostPart); StringBuilder pathQueryFragmentPart = new StringBuilder(); if (pathPart != null) { pathQueryFragmentPart.append(pathPart); } if (queryPart != null) { pathQueryFragmentPart.append(queryPart); } if (fragmentIdentifier != null) { pathQueryFragmentPart.append(fragmentIdentifier); } int pathQueryFragmentLength = pathQueryFragmentPart.length(); if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR .length()) { int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length(); if (pathCharLimit <= 0) { int limit = 0; if (pathQueryFragmentLength > 0) { limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1 : pathQueryFragmentLength; } pathCharLimit = limit; } url.append(pathQueryFragmentPart.substring(0, pathCharLimit)); if (pathCharLimit < pathQueryFragmentLength) { url.append(URL_SHORTENING_INDICATOR); } } else { url.append(pathQueryFragmentPart); } return url.toString(); }
UrlHelper { public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier) { StringBuilder url = new StringBuilder(); url.append(protocolHostPart); StringBuilder pathQueryFragmentPart = new StringBuilder(); if (pathPart != null) { pathQueryFragmentPart.append(pathPart); } if (queryPart != null) { pathQueryFragmentPart.append(queryPart); } if (fragmentIdentifier != null) { pathQueryFragmentPart.append(fragmentIdentifier); } int pathQueryFragmentLength = pathQueryFragmentPart.length(); if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR .length()) { int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length(); if (pathCharLimit <= 0) { int limit = 0; if (pathQueryFragmentLength > 0) { limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1 : pathQueryFragmentLength; } pathCharLimit = limit; } url.append(pathQueryFragmentPart.substring(0, pathCharLimit)); if (pathCharLimit < pathQueryFragmentLength) { url.append(URL_SHORTENING_INDICATOR); } } else { url.append(pathQueryFragmentPart); } return url.toString(); } }
UrlHelper { public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier) { StringBuilder url = new StringBuilder(); url.append(protocolHostPart); StringBuilder pathQueryFragmentPart = new StringBuilder(); if (pathPart != null) { pathQueryFragmentPart.append(pathPart); } if (queryPart != null) { pathQueryFragmentPart.append(queryPart); } if (fragmentIdentifier != null) { pathQueryFragmentPart.append(fragmentIdentifier); } int pathQueryFragmentLength = pathQueryFragmentPart.length(); if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR .length()) { int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length(); if (pathCharLimit <= 0) { int limit = 0; if (pathQueryFragmentLength > 0) { limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1 : pathQueryFragmentLength; } pathCharLimit = limit; } url.append(pathQueryFragmentPart.substring(0, pathCharLimit)); if (pathCharLimit < pathQueryFragmentLength) { url.append(URL_SHORTENING_INDICATOR); } } else { url.append(pathQueryFragmentPart); } return url.toString(); } private  UrlHelper(); }
UrlHelper { public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier) { StringBuilder url = new StringBuilder(); url.append(protocolHostPart); StringBuilder pathQueryFragmentPart = new StringBuilder(); if (pathPart != null) { pathQueryFragmentPart.append(pathPart); } if (queryPart != null) { pathQueryFragmentPart.append(queryPart); } if (fragmentIdentifier != null) { pathQueryFragmentPart.append(fragmentIdentifier); } int pathQueryFragmentLength = pathQueryFragmentPart.length(); if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR .length()) { int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length(); if (pathCharLimit <= 0) { int limit = 0; if (pathQueryFragmentLength > 0) { limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1 : pathQueryFragmentLength; } pathCharLimit = limit; } url.append(pathQueryFragmentPart.substring(0, pathCharLimit)); if (pathCharLimit < pathQueryFragmentLength) { url.append(URL_SHORTENING_INDICATOR); } } else { url.append(pathQueryFragmentPart); } return url.toString(); } private  UrlHelper(); static void assertIsValidUrl(String url); static String convertAnchorsToString(String content); static String getProtocolHostPort(String url, boolean stripUserInfo); static String insertSessionIdInUrl(String url, String sessionId); static boolean isAbsoluteHttpUrl(String url); static boolean isAbsoluteUrl(String url); static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
            String fragmentIdentifier); static String urlEncodeUrlPath(String path); }
UrlHelper { public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier) { StringBuilder url = new StringBuilder(); url.append(protocolHostPart); StringBuilder pathQueryFragmentPart = new StringBuilder(); if (pathPart != null) { pathQueryFragmentPart.append(pathPart); } if (queryPart != null) { pathQueryFragmentPart.append(queryPart); } if (fragmentIdentifier != null) { pathQueryFragmentPart.append(fragmentIdentifier); } int pathQueryFragmentLength = pathQueryFragmentPart.length(); if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR .length()) { int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length(); if (pathCharLimit <= 0) { int limit = 0; if (pathQueryFragmentLength > 0) { limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1 : pathQueryFragmentLength; } pathCharLimit = limit; } url.append(pathQueryFragmentPart.substring(0, pathCharLimit)); if (pathCharLimit < pathQueryFragmentLength) { url.append(URL_SHORTENING_INDICATOR); } } else { url.append(pathQueryFragmentPart); } return url.toString(); } private  UrlHelper(); static void assertIsValidUrl(String url); static String convertAnchorsToString(String content); static String getProtocolHostPort(String url, boolean stripUserInfo); static String insertSessionIdInUrl(String url, String sessionId); static boolean isAbsoluteHttpUrl(String url); static boolean isAbsoluteUrl(String url); static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
            String fragmentIdentifier); static String urlEncodeUrlPath(String path); static final Pattern URL_PATTERN; }