Class UrlUtils

java.lang.Object
com.restfb.util.UrlUtils

public final class UrlUtils extends Object
Since:
1.6.10
Author:
Mark Allen
  • Method Details

    • urlEncode

      public static String urlEncode(String string)
      URL-encodes a string.

      Assumes string is in StandardCharsets.UTF_8 format.

      Parameters:
      string - The string to URL-encode.
      Returns:
      The URL-encoded version of the input string, or null if string is null.
      Throws:
      IllegalStateException - If unable to URL-encode because the JVM doesn't support StandardCharsets.UTF_8.
    • urlDecode

      public static String urlDecode(String string)
      URL-decodes a string.

      Assumes string is in StandardCharsets.UTF_8 format.

      Parameters:
      string - The string to URL-decode.
      Returns:
      The URL-decoded version of the input string, or null if string is null.
      Throws:
      IllegalStateException - If unable to URL-decode because the JVM doesn't support StandardCharsets.UTF_8.
      Since:
      1.6.5
    • extractParametersFromQueryString

      public static Map<String,​List<String>> extractParametersFromQueryString(String queryString)
      For the given queryString, extract a mapping of query string parameter names to values.

      Example of a queryString is accessToken=123&expires=345.

      Parameters:
      queryString - The URL query string from which parameters are extracted.
      Returns:
      A mapping of query string parameter names to values. If queryString is null, an empty Map is returned.
      Throws:
      IllegalStateException - If unable to URL-decode because the JVM doesn't support StandardCharsets.UTF_8.
    • extractParametersFromUrl

      public static Map<String,​List<String>> extractParametersFromUrl(String url)
      For the given url, extract a mapping of query string parameter names to values.

      Adapted from an implementation by BalusC and dfrankow, available at http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java.

      Parameters:
      url - The URL from which parameters are extracted.
      Returns:
      A mapping of query string parameter names to values. If url is null, an empty Map is returned.
      Throws:
      IllegalStateException - If unable to URL-decode because the JVM doesn't support StandardCharsets.UTF_8.
    • replaceOrAddQueryParameter

      public static String replaceOrAddQueryParameter(String url, String key, String value)
      Modify the query string in the given url and return the new url as String.

      The given key/value pair is added to the url. If the key is already present, it is replaced with the new value.

      Parameters:
      url - The URL which parameters should be modified.
      key - the key, that should be modified or added
      value - the value of the key/value pair
      Returns:
      the modified URL as String
    • removeQueryParameter

      public static String removeQueryParameter(String url, String key)
      Remove the given key from the url query string and return the new URL as String.
      Parameters:
      url - The URL from which parameters are extracted.
      key - the key, that should be removed
      Returns:
      the modified URL as String