@Test public void testUrlEncodeUrlPath() { String urlPath = "/abc/test/"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "/abc/test"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "abc/test/"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "abc/test"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "/abc"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "abc/"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); urlPath = "abc"; Assert.assertEquals(UrlHelper.urlEncodeUrlPath(urlPath), urlPath); Assert.assertEquals(UrlHelper.urlEncodeUrlPath("/abc\u00f6_ro/~"), "/abc%C3%B6_ro/%7E"); Assert.assertEquals(UrlHelper.urlEncodeUrlPath("/abc\u3072\u3069\u3044_ro/~"), "/abc%E3%81%B2%E3%81%A9%E3%81%84_ro/%7E"); Assert.assertEquals(UrlHelper.urlEncodeUrlPath("/abc%C3%B6_ro/~"), "/abc%C3%B6_ro/%7E"); }
public static String urlEncodeUrlPath(String path) { String decodePath; try { decodePath = URLDecoder.decode(path, "UTF-8"); String[] splitted = decodePath.split("/"); StringBuilder result = new StringBuilder(); for (int i = 0; i < splitted.length; i++) { if (splitted[i].length() == 0) { result.append('/'); } else { result.append(URLEncoder.encode(splitted[i], "UTF-8")); result.append('/'); } } if (path.charAt(path.length() - 1) != '/') { return result.substring(0, result.length() - 1); } return result.toString(); } catch (UnsupportedEncodingException e) { LOGGER.error("Unexpected exception while encoding URL path", e); } return path; }
UrlHelper { public static String urlEncodeUrlPath(String path) { String decodePath; try { decodePath = URLDecoder.decode(path, "UTF-8"); String[] splitted = decodePath.split("/"); StringBuilder result = new StringBuilder(); for (int i = 0; i < splitted.length; i++) { if (splitted[i].length() == 0) { result.append('/'); } else { result.append(URLEncoder.encode(splitted[i], "UTF-8")); result.append('/'); } } if (path.charAt(path.length() - 1) != '/') { return result.substring(0, result.length() - 1); } return result.toString(); } catch (UnsupportedEncodingException e) { LOGGER.error("Unexpected exception while encoding URL path", e); } return path; } }
UrlHelper { public static String urlEncodeUrlPath(String path) { String decodePath; try { decodePath = URLDecoder.decode(path, "UTF-8"); String[] splitted = decodePath.split("/"); StringBuilder result = new StringBuilder(); for (int i = 0; i < splitted.length; i++) { if (splitted[i].length() == 0) { result.append('/'); } else { result.append(URLEncoder.encode(splitted[i], "UTF-8")); result.append('/'); } } if (path.charAt(path.length() - 1) != '/') { return result.substring(0, result.length() - 1); } return result.toString(); } catch (UnsupportedEncodingException e) { LOGGER.error("Unexpected exception while encoding URL path", e); } return path; } private  UrlHelper(); }
UrlHelper { public static String urlEncodeUrlPath(String path) { String decodePath; try { decodePath = URLDecoder.decode(path, "UTF-8"); String[] splitted = decodePath.split("/"); StringBuilder result = new StringBuilder(); for (int i = 0; i < splitted.length; i++) { if (splitted[i].length() == 0) { result.append('/'); } else { result.append(URLEncoder.encode(splitted[i], "UTF-8")); result.append('/'); } } if (path.charAt(path.length() - 1) != '/') { return result.substring(0, result.length() - 1); } return result.toString(); } catch (UnsupportedEncodingException e) { LOGGER.error("Unexpected exception while encoding URL path", e); } return path; } 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 urlEncodeUrlPath(String path) { String decodePath; try { decodePath = URLDecoder.decode(path, "UTF-8"); String[] splitted = decodePath.split("/"); StringBuilder result = new StringBuilder(); for (int i = 0; i < splitted.length; i++) { if (splitted[i].length() == 0) { result.append('/'); } else { result.append(URLEncoder.encode(splitted[i], "UTF-8")); result.append('/'); } } if (path.charAt(path.length() - 1) != '/') { return result.substring(0, result.length() - 1); } return result.toString(); } catch (UnsupportedEncodingException e) { LOGGER.error("Unexpected exception while encoding URL path", e); } return path; } 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; }