Class HttpRESTUtils
- java.lang.Object
-
- org.sakaiproject.entitybroker.util.http.HttpRESTUtils
-
public class HttpRESTUtils extends Object
Utilities for generating and processing http requests, allows the developer to fire off a request and get back information about the response
All encoding is automatically UTF-8- Author:
- Aaron Zeckoski (azeckoski @ gmail.com)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHttpRESTUtils.HttpIOExceptionIndicates an IO failurestatic classHttpRESTUtils.HttpRequestExceptionIndicates a general failurestatic classHttpRESTUtils.Method
-
Field Summary
Fields Modifier and Type Field Description static StringCONTENT_TYPE_UTF8static StringENCODING_UTF8static intMAX_RESPONSE_SIZE_CHARS
-
Constructor Summary
Constructors Constructor Description HttpRESTUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringencodeDateHttp(Date date)Encode a date into an http date stringstatic HttpResponsefireRequest(String URL, HttpRESTUtils.Method method)Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried outstatic HttpResponsefireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params)Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried outstatic HttpResponsefireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params, Object data, boolean guaranteeSSL)Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried outstatic HttpResponsefireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params, Map<String,String> headers, Object data, boolean guaranteeSSL)Fire off a request to a URL using the specified method, include optional params, headers, and data in the request, the response data will be returned in the object if the request can be carried outstatic HttpResponsefireRequest(HttpClientWrapper httpClientWrapper, String URL, HttpRESTUtils.Method method, Map<String,String> params, Object data, boolean guaranteeSSL)Fire off a request to a URL using the specified method but reuse the client for efficiency, include optional params and data in the request, the response data will be returned in the object if the request can be carried outstatic HttpResponsefireRequest(HttpClientWrapper httpClientWrapper, String URL, HttpRESTUtils.Method method, Map<String,String> params, Map<String,String> headers, Object data, boolean guaranteeSSL)Fire off a request to a URL using the specified method but reuse the client for efficiency, include optional params and data in the request, the response data will be returned in the object if the request can be carried outprotected static voidhandleRequestData(org.apache.commons.httpclient.methods.EntityEnclosingMethod method, Object data)static HttpRESTUtils.MethodmakeMethodFromString(String method)Turns a method string ("get") into aHttpRESTUtils.Methodenum objectstatic HttpClientWrappermakeReusableHttpClient(boolean multiThreaded, int idleConnectionTimeout, javax.servlet.http.Cookie[] cookies)Generates a reusable http client wrapper which can be given tofireRequest(HttpClientWrapper, String, Method, Map, Object, boolean)as an efficiency mechanismstatic StringmergeQueryStringWithParams(String queryString, Map<String,String> params)Merges an existing queryString with a set of params to create one queryString, this basically just adds the params to the end of the existing query string and will not insert a "?"static URLDataparseURL(String urlString)Parses a url string into component pieces, unlike the java URL class, this will work with partial urlsstatic Map<String,String>parseURLintoParams(String urlString)Get the query parameters out of a query string and return them as a map, this can beprotected static StringurlEncodeUTF8(String toEncode)
-
-
-
Field Detail
-
CONTENT_TYPE_UTF8
public static final String CONTENT_TYPE_UTF8
- See Also:
- Constant Field Values
-
ENCODING_UTF8
public static final String ENCODING_UTF8
- See Also:
- Constant Field Values
-
MAX_RESPONSE_SIZE_CHARS
public static final int MAX_RESPONSE_SIZE_CHARS
- See Also:
- Constant Field Values
-
-
Method Detail
-
fireRequest
public static HttpResponse fireRequest(String URL, HttpRESTUtils.Method method)
Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
URL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
fireRequest
public static HttpResponse fireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params)
Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
URL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)params- (optional) params to send along with the request, will be encoded in the query string or in the body depending on the method- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
fireRequest
public static HttpResponse fireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params, Object data, boolean guaranteeSSL)
Fire off a request to a URL using the specified method, include optional params and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
URL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)params- (optional) params to send along with the request, will be encoded in the query string or in the body depending on the methoddata- (optional) data to send along in the body of the request, this only works for POST and PUT requests, ignored for the other typesguaranteeSSL- if this is true then the request is sent in a mode which will allow self signed certs to work, otherwise https requests will fail if the certs cannot be centrally verified- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
fireRequest
public static HttpResponse fireRequest(String URL, HttpRESTUtils.Method method, Map<String,String> params, Map<String,String> headers, Object data, boolean guaranteeSSL)
Fire off a request to a URL using the specified method, include optional params, headers, and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
URL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)params- (optional) params to send along with the request, will be encoded in the query string or in the body depending on the methodparams- (optional) headers to send along with the request, will be encoded in the headersdata- (optional) data to send along in the body of the request, this only works for POST and PUT requests, ignored for the other typesguaranteeSSL- if this is true then the request is sent in a mode which will allow self signed certs to work, otherwise https requests will fail if the certs cannot be centrally verified- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
fireRequest
public static HttpResponse fireRequest(HttpClientWrapper httpClientWrapper, String URL, HttpRESTUtils.Method method, Map<String,String> params, Object data, boolean guaranteeSSL)
Fire off a request to a URL using the specified method but reuse the client for efficiency, include optional params and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
httpClientWrapper- (optional) allows the http client to be reused for efficiency, if null a new one will be created each time, use#makeReusableHttpClient(boolean, int)to create a reusable instanceURL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)params- (optional) params to send along with the request, will be encoded in the query string or in the body depending on the methoddata- (optional) data to send along in the body of the request, this only works for POST and PUT requests, ignored for the other typesguaranteeSSL- if this is true then the request is sent in a mode which will allow self signed certs to work, otherwise https requests will fail if the certs cannot be centrally verified- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
fireRequest
public static HttpResponse fireRequest(HttpClientWrapper httpClientWrapper, String URL, HttpRESTUtils.Method method, Map<String,String> params, Map<String,String> headers, Object data, boolean guaranteeSSL)
Fire off a request to a URL using the specified method but reuse the client for efficiency, include optional params and data in the request, the response data will be returned in the object if the request can be carried out- Parameters:
httpClientWrapper- (optional) allows the http client to be reused for efficiency, if null a new one will be created each time, use#makeReusableHttpClient(boolean, int)to create a reusable instanceURL- the url to send the request to (absolute or relative, can include query params)method- the method to use (e.g. GET, POST, etc.)params- (optional) params to send along with the request, will be encoded in the query string or in the body depending on the methodparams- (optional) headers to send along with the request, will be encoded in the headersdata- (optional) data to send along in the body of the request, this only works for POST and PUT requests, ignored for the other typesguaranteeSSL- if this is true then the request is sent in a mode which will allow self signed certs to work, otherwise https requests will fail if the certs cannot be centrally verified- Returns:
- an object representing the response, includes data about the response
- Throws:
HttpRESTUtils.HttpRequestException- if the request cannot be processed for some reason (this is unrecoverable)
-
mergeQueryStringWithParams
public static String mergeQueryStringWithParams(String queryString, Map<String,String> params)
Merges an existing queryString with a set of params to create one queryString, this basically just adds the params to the end of the existing query string and will not insert a "?" but will take care of the "&"s- Parameters:
queryString- the query string in URL encoded form, without a leading '?'params- a set of key->value strings to use as params for the request- Returns:
- the merged queryString with the params included
-
parseURLintoParams
public static Map<String,String> parseURLintoParams(String urlString)
Get the query parameters out of a query string and return them as a map, this can be- Parameters:
urlString- a complete URL with query string at the end or a partial URL or just a query string only (e.g. /thing/stuff?blah=1&apple=fruit- Returns:
- the map of all query string parameters (e.g. blah => 1, apple => fruit)
-
handleRequestData
protected static void handleRequestData(org.apache.commons.httpclient.methods.EntityEnclosingMethod method, Object data)
-
parseURL
public static URLData parseURL(String urlString)
Parses a url string into component pieces, unlike the java URL class, this will work with partial urls- Parameters:
urlString- any URL string- Returns:
- the URL data object
-
makeMethodFromString
public static HttpRESTUtils.Method makeMethodFromString(String method)
Turns a method string ("get") into aHttpRESTUtils.Methodenum object- Parameters:
method- a method string (case is not important) e.g. GET, Post, put, DeLeTe- Returns:
- the corresponding
HttpRESTUtils.Methodenum
-
makeReusableHttpClient
public static HttpClientWrapper makeReusableHttpClient(boolean multiThreaded, int idleConnectionTimeout, javax.servlet.http.Cookie[] cookies)
Generates a reusable http client wrapper which can be given tofireRequest(HttpClientWrapper, String, Method, Map, Object, boolean)as an efficiency mechanism- Parameters:
multiThreaded- true if you want to allow the client to run in multiple threadsidleConnectionTimeout- if this is 0 then it will use the defaults, otherwise connections will be timed out after this long (ms)cookies- to send along with every request from this client- Returns:
- the reusable http client wrapper
-
-