@Test public void testEncode() { assertEquals("/p", URIEncoder.encode("/p")); assertEquals("/p%20d", URIEncoder.encode("/p d")); }
public static String encode(final String input) { try { final StringBuilder b = new StringBuilder(); final StringTokenizer t = new StringTokenizer(input, "/"); if(!t.hasMoreTokens()) { return input; } if(StringUtils.startsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } while(t.hasMoreTokens()) { b.append(URLEncoder.encode(t.nextToken(), StandardCharsets.UTF_8.name())); if(t.hasMoreTokens()) { b.append(Path.DELIMITER); } } if(StringUtils.endsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } return StringUtils.replaceEach(b.toString(), new String[]{"+", "*", "%7E", "%40"}, new String[]{"%20", "%2A", "~", "@"}); } catch(UnsupportedEncodingException e) { log.warn(String.format("Failure %s encoding input %s", e, input)); return input; } }
URIEncoder { public static String encode(final String input) { try { final StringBuilder b = new StringBuilder(); final StringTokenizer t = new StringTokenizer(input, "/"); if(!t.hasMoreTokens()) { return input; } if(StringUtils.startsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } while(t.hasMoreTokens()) { b.append(URLEncoder.encode(t.nextToken(), StandardCharsets.UTF_8.name())); if(t.hasMoreTokens()) { b.append(Path.DELIMITER); } } if(StringUtils.endsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } return StringUtils.replaceEach(b.toString(), new String[]{"+", "*", "%7E", "%40"}, new String[]{"%20", "%2A", "~", "@"}); } catch(UnsupportedEncodingException e) { log.warn(String.format("Failure %s encoding input %s", e, input)); return input; } } }
URIEncoder { public static String encode(final String input) { try { final StringBuilder b = new StringBuilder(); final StringTokenizer t = new StringTokenizer(input, "/"); if(!t.hasMoreTokens()) { return input; } if(StringUtils.startsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } while(t.hasMoreTokens()) { b.append(URLEncoder.encode(t.nextToken(), StandardCharsets.UTF_8.name())); if(t.hasMoreTokens()) { b.append(Path.DELIMITER); } } if(StringUtils.endsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } return StringUtils.replaceEach(b.toString(), new String[]{"+", "*", "%7E", "%40"}, new String[]{"%20", "%2A", "~", "@"}); } catch(UnsupportedEncodingException e) { log.warn(String.format("Failure %s encoding input %s", e, input)); return input; } } private  URIEncoder(); }
URIEncoder { public static String encode(final String input) { try { final StringBuilder b = new StringBuilder(); final StringTokenizer t = new StringTokenizer(input, "/"); if(!t.hasMoreTokens()) { return input; } if(StringUtils.startsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } while(t.hasMoreTokens()) { b.append(URLEncoder.encode(t.nextToken(), StandardCharsets.UTF_8.name())); if(t.hasMoreTokens()) { b.append(Path.DELIMITER); } } if(StringUtils.endsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } return StringUtils.replaceEach(b.toString(), new String[]{"+", "*", "%7E", "%40"}, new String[]{"%20", "%2A", "~", "@"}); } catch(UnsupportedEncodingException e) { log.warn(String.format("Failure %s encoding input %s", e, input)); return input; } } private  URIEncoder(); static String encode(final String input); static String decode(final String input); }
URIEncoder { public static String encode(final String input) { try { final StringBuilder b = new StringBuilder(); final StringTokenizer t = new StringTokenizer(input, "/"); if(!t.hasMoreTokens()) { return input; } if(StringUtils.startsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } while(t.hasMoreTokens()) { b.append(URLEncoder.encode(t.nextToken(), StandardCharsets.UTF_8.name())); if(t.hasMoreTokens()) { b.append(Path.DELIMITER); } } if(StringUtils.endsWith(input, String.valueOf(Path.DELIMITER))) { b.append(Path.DELIMITER); } return StringUtils.replaceEach(b.toString(), new String[]{"+", "*", "%7E", "%40"}, new String[]{"%20", "%2A", "~", "@"}); } catch(UnsupportedEncodingException e) { log.warn(String.format("Failure %s encoding input %s", e, input)); return input; } } private  URIEncoder(); static String encode(final String input); static String decode(final String input);  }