Package com.restfb.util
Class UrlUtils
- java.lang.Object
-
- com.restfb.util.UrlUtils
-
public final class UrlUtils extends Object
- Since:
- 1.6.10
- Author:
- Mark Allen
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<String,List<String>>
extractParametersFromQueryString(String queryString)
For the givenqueryString
, extract a mapping of query string parameter names to values.static Map<String,List<String>>
extractParametersFromUrl(String url)
For the givenurl
, extract a mapping of query string parameter names to values.static String
removeQueryParameter(String url, String key)
Remove the given key from the url query string and return the new URL as String.static String
replaceOrAddQueryParameter(String url, String key, String value)
Modify the query string in the givenurl
and return the new url as String.static String
urlDecode(String string)
URL-decodes a string.static String
urlEncode(String string)
URL-encodes a string.
-
-
-
Method Detail
-
urlEncode
public static String urlEncode(String string)
URL-encodes a string.Assumes
string
is inStandardCharsets.UTF_8
format.- Parameters:
string
- The string to URL-encode.- Returns:
- The URL-encoded version of the input string, or
null
ifstring
isnull
. - Throws:
IllegalStateException
- If unable to URL-encode because the JVM doesn't supportStandardCharsets.UTF_8
.
-
urlDecode
public static String urlDecode(String string)
URL-decodes a string.Assumes
string
is inStandardCharsets.UTF_8
format.- Parameters:
string
- The string to URL-decode.- Returns:
- The URL-decoded version of the input string, or
null
ifstring
isnull
. - Throws:
IllegalStateException
- If unable to URL-decode because the JVM doesn't supportStandardCharsets.UTF_8
.- Since:
- 1.6.5
-
extractParametersFromQueryString
public static Map<String,List<String>> extractParametersFromQueryString(String queryString)
For the givenqueryString
, extract a mapping of query string parameter names to values.Example of a
queryString
isaccessToken=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
isnull
, an emptyMap
is returned. - Throws:
IllegalStateException
- If unable to URL-decode because the JVM doesn't supportStandardCharsets.UTF_8
.
-
extractParametersFromUrl
public static Map<String,List<String>> extractParametersFromUrl(String url)
For the givenurl
, 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
isnull
, an emptyMap
is returned. - Throws:
IllegalStateException
- If unable to URL-decode because the JVM doesn't supportStandardCharsets.UTF_8
.
-
replaceOrAddQueryParameter
public static String replaceOrAddQueryParameter(String url, String key, String value)
Modify the query string in the givenurl
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 addedvalue
- 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
-
-