Class PlatformRestClient
java.lang.Object
com.onenetwork.platform.integ.rest.PlatformRestClient
public class PlatformRestClient
extends java.lang.Object
Simple REST client for calling Platform REST APIs.
Sample usage:
PlatformRestClient rest = new PlatformRestClient("http://localhost/oms", "myuser", "password"); MapOne Network authenticates RESTful calls using Basic Access Authentication (see http://en.wikipedia.org/wiki/Basic_access_authentication). This utilizes the "Authorization" HTTP header with username and password delimited and Base64-encoded, prefixed with "Basic ". To generate a suitable Authorization string you can put in your HTTP Header, you can execute theparams = new HashMap (); params.put("someReqParam", "someValue"); PostMethod post = rest.post("/rest/SomeRestAPI", "application/json", params); int status = post.getStatusCode(); if (status != 200) { // log error } System.out.println(post.getResponseBodyAsString());
#main
method of this class.-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
REST_RESOURCE_URL_BASE
All Platform REST resources (including module resources) are mapped under this url base: /rest -
Constructor Summary
Constructors Constructor Description PlatformRestClient(java.lang.String urlBase, java.lang.String oAuthAccesstoken)
PlatformRestClient(java.lang.String urlBase, java.lang.String userName, java.lang.String pwd)
-
Method Summary
Modifier and Type Method Description static java.lang.String
buildAuthHeader(java.lang.String userName, java.lang.String pwd)
Builds an encoded Authorization header using the given user name and passwordorg.apache.commons.httpclient.methods.PostMethod
createPost(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params)
Construct a PostMethod for later execution.int
executeMethod(org.apache.commons.httpclient.HttpMethod method)
Executes the given http Get or POST method.org.apache.commons.httpclient.methods.GetMethod
get(java.lang.String pathAndParams, java.lang.String accept)
Executes a GET operation on the given path & returns a GetMethod object containing the resulting status code/response body/etc.java.util.Map<java.lang.String,java.lang.String>
getHeaders()
Returns a mutable Map where the client can provide additional HTTP Headers to go into the requestorg.apache.commons.httpclient.HttpClient
getHttpClient()
Returns the underlying Apache Commons HTTP Client which is used to initiate the posts/gets.org.apache.commons.httpclient.methods.PostMethod
post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params)
Executes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.org.apache.commons.httpclient.methods.PostMethod
post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params, java.io.InputStream body)
Executes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.org.apache.commons.httpclient.methods.PostMethod
post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params, org.apache.commons.httpclient.methods.RequestEntity requestEntity)
Executes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.void
setRetryCount(int retries)
By default, the PlatformRestClient will not retry the HTTP Request when an IOException is thrown.
-
Field Details
-
REST_RESOURCE_URL_BASE
public static final java.lang.String REST_RESOURCE_URL_BASEAll Platform REST resources (including module resources) are mapped under this url base: /rest- See Also:
- Constant Field Values
-
-
Constructor Details
-
PlatformRestClient
public PlatformRestClient(java.lang.String urlBase, java.lang.String userName, java.lang.String pwd)- Parameters:
urlBase
- base URL, for example "http://localhost"userName
- user name for user to use in HTTP auth headerpwd
- password for user to use in HTTP auth header
-
PlatformRestClient
public PlatformRestClient(java.lang.String urlBase, java.lang.String oAuthAccesstoken)- Parameters:
urlBase
- base URL, for example "http://localhost"oAuthAccesstoken
- OAuth access token
-
-
Method Details
-
setRetryCount
public void setRetryCount(int retries)By default, the PlatformRestClient will not retry the HTTP Request when an IOException is thrown. You may call this method to automatically retry the operation N times in those scenarios. Note that a retry will *only* be performed if an IOException occurs, NOT if the server returns a valid HTTP code such as 500. Please note that some other scenarios will also never retry, including UnknownHostException, NoRouteToHostException and a few others.- Parameters:
retries
- number of retries
-
getHttpClient
public org.apache.commons.httpclient.HttpClient getHttpClient()Returns the underlying Apache Commons HTTP Client which is used to initiate the posts/gets. If you want to change proxy settings/etc it can be done on this object. -
get
public org.apache.commons.httpclient.methods.GetMethod get(java.lang.String pathAndParams, java.lang.String accept) throws java.io.IOExceptionExecutes a GET operation on the given path & returns a GetMethod object containing the resulting status code/response body/etc.- Parameters:
pathAndParams
- path and query args after http://localhost/oms/rest, e.g. "/foo?bar=baz"Accept
- header (e.g. "application/xml") to be supplied when executing the GET- Returns:
- newly instantiated GetMethod, with Authorization headers set for ProgressiveRetailerVCAdmin
- Throws:
java.io.IOException
-
post
public org.apache.commons.httpclient.methods.PostMethod post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params, org.apache.commons.httpclient.methods.RequestEntity requestEntity) throws java.io.IOExceptionExecutes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.- Parameters:
pathAndParams
- path and query args after http://localhost/oms/rest, e.g. "/foo?bar=baz"Accept
- header (e.g. "application/xml") to be supplied when executing the GETparams
- form parametersrequestEntity
- - supports streaming implementation of sending payload.- Returns:
- newly instantiated PostMethod, with Authorization headers set for ProgressiveRetailerVCAdmin
- Throws:
java.io.IOException
-
post
public org.apache.commons.httpclient.methods.PostMethod post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params) throws java.io.IOExceptionExecutes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.- Parameters:
pathAndParams
- path and query args after http://localhost/oms/rest, e.g. "/foo?bar=baz"Accept
- header (e.g. "application/xml") to be supplied when executing the GETparams
- form parameters- Returns:
- newly instantiated PostMethod, with Authorization headers set for ProgressiveRetailerVCAdmin
- Throws:
java.io.IOException
-
post
public org.apache.commons.httpclient.methods.PostMethod post(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params, java.io.InputStream body) throws java.io.IOExceptionExecutes a POST operation on the given path & returns a PostMethod object containing the resulting status code/response body/etc.- Parameters:
pathAndParams
- path and query args after http://localhost/oms/rest, e.g. "/foo?bar=baz"Accept
- header (e.g. "application/xml") to be supplied when executing the GETparams
- form parametersbody
- content of the body of the POST request (may be null to send no bytes in the body)- Returns:
- newly instantiated PostMethod, with Authorization headers set for ProgressiveRetailerVCAdmin
- Throws:
java.io.IOException
-
createPost
public org.apache.commons.httpclient.methods.PostMethod createPost(java.lang.String pathAndParams, java.lang.String accept, java.util.Map<java.lang.String,java.lang.String> params)Construct a PostMethod for later execution. Callers should use this method when constructing a RequestEntity that requires access to the method's HttpParams (e.g. MultipartRequestEntity)- Parameters:
pathAndParams
- path and query args after http://localhost/oms/rest, e.g. "/foo?bar=baz"accept
- Accept header (e.g. "application/xml") to be supplied when executing the GETparams
- params form parameters- Returns:
- newly instantiated PostMethod, with Authorization headers set
-
executeMethod
public int executeMethod(org.apache.commons.httpclient.HttpMethod method) throws java.io.IOExceptionExecutes the given http Get or POST method.- Parameters:
method
-- Returns:
- the method's response code
- Throws:
java.io.IOException
-
buildAuthHeader
public static java.lang.String buildAuthHeader(java.lang.String userName, java.lang.String pwd)Builds an encoded Authorization header using the given user name and password -
getHeaders
public java.util.Map<java.lang.String,java.lang.String> getHeaders()Returns a mutable Map where the client can provide additional HTTP Headers to go into the request
-