com.almende.util
Class HttpUtil

java.lang.Object
  extended by com.almende.util.HttpUtil

public class HttpUtil
extends Object


Constructor Summary
HttpUtil()
           
 
Method Summary
static String appendQueryParams(String url, Map<String,String> params)
          Append query parameters to given url
static String delete(String url)
          Send a delete request
static String delete(String url, Map<String,String> headers)
          Send a delete request
static String fetch(String method, String url, String body, Map<String,String> headers)
          Send a request
static String get(String url)
          Send a get request
static String get(String url, Map<String,String> headers)
          Send a get request
static Map<String,String> getQueryParams(String url)
          Retrieve the query parameters from given url
static Map<String,String> getTemplateParams(String template, String url)
          Retrieve the template parameters from an url.
static String post(String url, String body)
          Send a post request
static String post(String url, String body, Map<String,String> headers)
          Send a post request
static String postForm(String url, Map<String,String> params)
          Post a form with parameters
static String postForm(String url, Map<String,String> params, Map<String,String> headers)
          Post a form with parameters
static String put(String url, String body)
          Send a put request
static String put(String url, String body, Map<String,String> headers)
          Send a put request
static String removeQueryParams(String url)
          Returns the url without query parameters
static String setTemplateParams(String template, Map<String,String> params)
          Create an url from a template and a map with parameter values.
static String streamToString(InputStream in)
          Read an input stream into a string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpUtil

public HttpUtil()
Method Detail

get

public static String get(String url)
                  throws IOException
Send a get request

Parameters:
url -
Returns:
response
Throws:
IOException

get

public static String get(String url,
                         Map<String,String> headers)
                  throws IOException
Send a get request

Parameters:
url - Url as string
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

post

public static String post(String url,
                          String body,
                          Map<String,String> headers)
                   throws IOException
Send a post request

Parameters:
url - Url as string
body - Request body as string
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

post

public static String post(String url,
                          String body)
                   throws IOException
Send a post request

Parameters:
url - Url as string
body - Request body as string
Returns:
response Response as string
Throws:
IOException

postForm

public static String postForm(String url,
                              Map<String,String> params)
                       throws IOException
Post a form with parameters

Parameters:
url - Url as string
params - map with parameters/values
Returns:
response Response as string
Throws:
IOException

postForm

public static String postForm(String url,
                              Map<String,String> params,
                              Map<String,String> headers)
                       throws IOException
Post a form with parameters

Parameters:
url - Url as string
params - Map with parameters/values
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

put

public static String put(String url,
                         String body,
                         Map<String,String> headers)
                  throws IOException
Send a put request

Parameters:
url - Url as string
body - Request body as string
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

put

public static String put(String url,
                         String body)
                  throws IOException
Send a put request

Parameters:
url - Url as string
Returns:
response Response as string
Throws:
IOException

delete

public static String delete(String url,
                            Map<String,String> headers)
                     throws IOException
Send a delete request

Parameters:
url - Url as string
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

delete

public static String delete(String url)
                     throws IOException
Send a delete request

Parameters:
url - Url as string
Returns:
response Response as string
Throws:
IOException

appendQueryParams

public static String appendQueryParams(String url,
                                       Map<String,String> params)
                                throws IOException
Append query parameters to given url

Parameters:
url - Url as string
params - Map with query parameters
Returns:
url Url with query parameters appended
Throws:
IOException

getQueryParams

public static Map<String,String> getQueryParams(String url)
                                         throws IOException
Retrieve the query parameters from given url

Parameters:
url - Url containing query parameters
Returns:
params Map with query parameters
Throws:
IOException

getTemplateParams

public static Map<String,String> getTemplateParams(String template,
                                                   String url)
Retrieve the template parameters from an url. For example if template = "http://server.com/:db/:id", url = "http://server.com/maindb/1234" The method will return a map: params = {"db": "maindb", "id": "1234"}

Parameters:
template - A template url
url - A url with parameters
Returns:
params A map with all parameters defined in the template, with the value found in the url as value (or an empty string when not found)

setTemplateParams

public static String setTemplateParams(String template,
                                       Map<String,String> params)
Create an url from a template and a map with parameter values. For example if template = "http://server.com/:db/:id", params = {"db": "maindb", "id": "1234"} The method will return a map: url = "http://server.com/maindb/1234"

Parameters:
template - A template url
url - A url with parameters
Returns:
params A map with all parameters defined in the template, with the value found in the url as value (can be null)

removeQueryParams

public static String removeQueryParams(String url)
                                throws IOException
Returns the url without query parameters

Parameters:
url - Url containing query parameters
Returns:
url Url without query parameters
Throws:
IOException

fetch

public static String fetch(String method,
                           String url,
                           String body,
                           Map<String,String> headers)
                    throws IOException
Send a request

Parameters:
method - HTTP method, for example "GET" or "POST"
url - Url as string
body - Request body as string
headers - Optional map with headers
Returns:
response Response as string
Throws:
IOException

streamToString

public static String streamToString(InputStream in)
                             throws IOException
Read an input stream into a string

Parameters:
in -
Returns:
Throws:
IOException


Copyright © 2013 Almende BV. All Rights Reserved.