public static String urlEncode(String in, Iterable<Character> skipEncode) { if (isUrlEncoded(in)) return in; try { String returnVal = URLEncoder.encode(in, "UTF-8"); returnVal = returnVal.replace("+", "%20"); returnVal = returnVal.replace("*", "%2A"); for (char c : skipEncode) { returnVal = returnVal.replace(CHAR_TO_ENCODED.get(c), c + ""); } return returnVal; } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Bad encoding on input: " + in, e); } catch (ExecutionException e) { throw new IllegalStateException("error creating pattern: " + in, e); } }
public void testIsEncoded() { assert Strings2.isUrlEncoded("/read-tests/%73%6f%6d%65%20%66%69%6c%65"); assert !Strings2.isUrlEncoded("/read-tests/ tep"); }
public static String urlEncode(String in, Iterable<Character> skipEncode) { if (isUrlEncoded(in)) return in; try { String returnVal = URLEncoder.encode(in, "UTF-8"); returnVal = returnVal.replace("+", "%20"); returnVal = returnVal.replace("*", "%2A"); for (char c : skipEncode) { returnVal = returnVal.replace(CHAR_TO_ENCODED.get(c), c + ""); } return returnVal; } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Bad encoding on input: " + in, e); } catch (ExecutionException e) { throw new IllegalStateException("error creating pattern: " + in, e); } }
public static String urlEncode(String in, Iterable<Character> skipEncode) { if (isUrlEncoded(in)) return in; try { String returnVal = URLEncoder.encode(in, "UTF-8"); returnVal = returnVal.replace("+", "%20"); returnVal = returnVal.replace("*", "%2A"); for (char c : skipEncode) { returnVal = returnVal.replace(CHAR_TO_ENCODED.get(c), c + ""); } return returnVal; } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Bad encoding on input: " + in, e); } catch (ExecutionException e) { throw new IllegalStateException("error creating pattern: " + in, e); } }
/** * url decodes the input param, if set. * * @param in * nullable * @return null if input was null * @throws IllegalStateException * if encoding isn't {@code UTF-8} */ public static String urlDecode(@Nullable String in) { if (in == null) return null; String input = in.toString(); // Don't double decode if (!isUrlEncoded(input)) { return input; } try { return URLDecoder.decode(input, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Bad encoding on input: " + in, e); } }