org.clazzes.util.http
Class UrlHelper

java.lang.Object
  extended by org.clazzes.util.http.UrlHelper

public class UrlHelper
extends Object

Static helpers for manipulating URLs and URIs.


Field Summary
static String UTF_8
          The name of the UTF-8 encoding for Charset.forName(String).
 
Constructor Summary
UrlHelper()
           
 
Method Summary
static String appendQueryParameter(String q, String key, String value)
          Append additional query parameters to the end of a given query string.
static String appendQueryParameterToUrl(String url, String key, String value)
          Append additional query parameters to the end of a given URL.
static String decodeUtf8(String s)
          Decodes a application/x-www-form-urlencoded string using UTF-8 encoding as stated in the World Wide Web Consortium Recommendation.
static String encodeUtf8(String s)
          Translates a string into application/x-www-form-urlencoded format using UTF-8 encoding as stated in the World Wide Web Consortium Recommendation.
static Boolean getBooleanParameter(Map<String,List<String>> parameters, String key)
          Get the value of the query parameter key and parse it using Boolean.parseBoolean(String) if a value for this query parameter has been found.
static Double getDoubleParameter(Map<String,List<String>> parameters, String key)
          Get the value of the query parameter key and parse it using Double.parseDouble(String) if a value for this query parameter has been found.
static Integer getIntegerParameter(Map<String,List<String>> parameters, String key)
          Get the value of the query parameter key and parse it using Integer.parseInt(String) if a value for this query parameter has been found.
static Long getLongParameter(Map<String,List<String>> parameters, String key)
          Get the value of the query parameter key and parse it using Long.parseLong(String) if a value for this query parameter has been found.
static String getStringParameter(Map<String,List<String>> parameters, String key)
          Get the first string value given in the specified map of query parameters.
static Map<String,List<String>> parseQuery(String q)
          Parse a given query string in application/x-www-form-urlencoded format using UTF-8 encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF_8

public static final String UTF_8
The name of the UTF-8 encoding for Charset.forName(String).

See Also:
Constant Field Values
Constructor Detail

UrlHelper

public UrlHelper()
Method Detail

encodeUtf8

public static final String encodeUtf8(String s)
Translates a string into application/x-www-form-urlencoded format using UTF-8 encoding as stated in the World Wide Web Consortium Recommendation.

In contrast to URLEncoder.encode(s,"UTF-8") this function does not throw any checked exception.

Parameters:
s - String to be translated.
Returns:
the translated String.
See Also:
URLEncoder.encode(java.lang.String, java.lang.String)

decodeUtf8

public static final String decodeUtf8(String s)
Decodes a application/x-www-form-urlencoded string using UTF-8 encoding as stated in the World Wide Web Consortium Recommendation.

In contrast to URLDecoder.decode(s,"UTF-8") this function does not throw any checked exception.

Parameters:
s - the String to decode.
Returns:
the newly decoded String
See Also:
URLDecoder.decode(String, String)

appendQueryParameterToUrl

public static final String appendQueryParameterToUrl(String url,
                                                     String key,
                                                     String value)
Append additional query parameters to the end of a given URL.

Parameters:
url - The base URL, which my already contain other query parameters.
key - The name of the parameter.
value - The value, which will be appended using encodeUtf8(String).
Returns:
The base URL extended by an additional query parameter.

appendQueryParameter

public static final String appendQueryParameter(String q,
                                                String key,
                                                String value)
Append additional query parameters to the end of a given query string.

Parameters:
q - The query part of an URL or URI as returned by URL.getQuery() or URI.getQuery().
key - The name of the parameter.
value - The value, which will be appended using encodeUtf8(String).
Returns:
The query extended by an additional query parameter.

parseQuery

public static final Map<String,List<String>> parseQuery(String q)
Parse a given query string in application/x-www-form-urlencoded format using UTF-8 encoding.

Parameters:
q - The query part of an URL or URI as returned by URL.getQuery() or URI.getQuery().
Returns:
A map of query parameters with their values. A parameter value may be manifold, so the value is represented by a list of strings. If q==null, the return value is also null.
See Also:
getStringParameter(Map, String), getDoubleParameter(Map, String), getLongParameter(Map, String), getIntegerParameter(Map, String), getBooleanParameter(Map, String)

getStringParameter

public static String getStringParameter(Map<String,List<String>> parameters,
                                        String key)
Get the first string value given in the specified map of query parameters.

Parameters:
parameters - A map of query parameters as returned by parseQuery(String).
key - The name of the parameter to query.
Returns:
The first string value for the given parameter name or null, if no such parameter is given.

getDoubleParameter

public static Double getDoubleParameter(Map<String,List<String>> parameters,
                                        String key)
                                 throws NumberFormatException
Get the value of the query parameter key and parse it using Double.parseDouble(String) if a value for this query parameter has been found.

Parameters:
parameters - A map of query parameters as returned by parseQuery(String).
key - The key of the double parameter to parse.
Returns:
The double value of the query parameter with the given key or null if no query parameter with name key is given.
Throws:
NumberFormatException - If the value of the query parameter is not a double value as per Double.parseDouble(String).

getLongParameter

public static Long getLongParameter(Map<String,List<String>> parameters,
                                    String key)
                             throws NumberFormatException
Get the value of the query parameter key and parse it using Long.parseLong(String) if a value for this query parameter has been found.

Parameters:
parameters - A map of query parameters as returned by parseQuery(String).
key - The key of the long parameter to parse.
Returns:
The long value of the query parameter with the given key or null if no query parameter with name key is given.
Throws:
NumberFormatException - If the value of the query parameter is not a long value as per Long.parseLong(String).

getIntegerParameter

public static Integer getIntegerParameter(Map<String,List<String>> parameters,
                                          String key)
                                   throws NumberFormatException
Get the value of the query parameter key and parse it using Integer.parseInt(String) if a value for this query parameter has been found.

Parameters:
parameters - A map of query parameters as returned by parseQuery(String).
key - The key of the long parameter to parse.
Returns:
The integer value of the query parameter with the given key or null if no query parameter with name key is given.
Throws:
NumberFormatException - If the value of the query parameter is not an integer value as per Integer.parseInt(String).

getBooleanParameter

public static Boolean getBooleanParameter(Map<String,List<String>> parameters,
                                          String key)
Get the value of the query parameter key and parse it using Boolean.parseBoolean(String) if a value for this query parameter has been found.

Parameters:
parameters - A map of query parameters as returned by parseQuery(String).
key - The key of the long parameter to parse.
Returns:
The boolean value of the query parameter with the given key or null if no query parameter with name key is given.


Copyright © 2013. All Rights Reserved.