Example #1
0
 /**
  * URL encodes a string
  *
  * @param plain
  * @return percent encoded string
  */
 public static String urlEncodeWrapper(String string) {
   Preconditions.checkNotNull(string, "Cannot encode null string");
   try {
     return URLEncoder.encode(string, UTF_8);
   } catch (UnsupportedEncodingException uee) {
     throw new IllegalStateException(ERROR_MSG, uee);
   }
 }
Example #2
0
  /**
   * Append given parameters to the query string of the url
   *
   * @param url the url to append parameters to
   * @param params any map
   * @return new url with parameters on query string
   */
  public static String appendParametersToQueryString(String url, Map<String, String> params) {
    Preconditions.checkNotNull(url, "Cannot append to null URL");
    String queryString = URLUtils.formURLEncodeMap(params);
    if (queryString.length() == 0) return url;

    // Check if there are parameters in the url already and use '&' instead of '?'
    url += url.indexOf(QUERY_STRING_SEPARATOR) != -1 ? PARAM_SEPARATOR : QUERY_STRING_SEPARATOR;
    url += queryString;
    return url;
  }
Example #3
0
 /**
  * Percent encodes a string
  *
  * @param plain
  * @return percent encoded string
  */
 public static String percentEncode(String string) {
   Preconditions.checkNotNull(string, "Cannot encode null string");
   try {
     String encoded = URLEncoder.encode(string, UTF_8);
     for (EncodingRule rule : ENCODING_RULES) {
       encoded = rule.apply(encoded);
     }
     return encoded;
   } catch (UnsupportedEncodingException uee) {
     throw new IllegalStateException(ERROR_MSG, uee);
   }
 }
Example #4
0
 /**
  * Turns a map into a form-url-encoded string (key=value&key2&key3=value)
  *
  * @param map any map
  * @return form-url-encoded string
  */
 public static String formURLEncodeMap(Map<String, String> map) {
   Preconditions.checkNotNull(map, "Cannot url-encode a null object");
   return (map.size() <= 0) ? EMPTY_STRING : doFormUrlEncode(map);
 }