com.worklight.wlclient.api
Class WLResourceRequest
- java.lang.Object
-
- com.worklight.wlclient.api.WLResourceRequest
-
public class WLResourceRequest extends java.lang.Object
This class encapsulates a resource request. The resource may be an adapter on the MobileFirst Server, or an external resource. The class provides several 'send' methods, with different inputs for the body of a request. In addition, the 'send' methods support two types of response listener:-
WLResponseListener
- The onSuccess method of this listener is called and provided with an instance of theWLResponse
class. The content of the response is be read into the WLResponse instance by the platform, and will be accessible through methods of WLResponse. In case of a failure, the onFailure method of the listener is called and provided with an instance of the WLFailResponse class, that will contain all the information about the failure. -
WLHttpResponseListener
- The onSuccess method of this listener is called and provided with the original HTTP response object that was received from the server. The platform does not attempt to read or parse the response in any way. In case of a failure the onFailure method is called and provided with either the response from the server if one was received, or the exception that was thrown during the execution of this request.
-
-
-
Field Summary
Fields Modifier and Type Field and Description static java.lang.String
DELETE
String constant for the HTTP DELETE method.static java.lang.String
GET
String constant for the HTTP GET method.static java.lang.String
HEAD
String constant for the HTTP HEAD method.static java.lang.String
OPTIONS
String constant for the HTTP OPTIONS method.static java.lang.String
POST
String constant for the HTTP POST method.static java.lang.String
PUT
String constant for the HTTP PUT method.static java.lang.String
TRACE
String constant for the HTTP TRACE method.
-
Constructor Summary
Constructors Constructor and Description WLResourceRequest(java.net.URI url, java.lang.String method)
Constructs a new resource request with the specified URL, using the specified HTTP method.WLResourceRequest(java.net.URI url, java.lang.String method, int timeout)
Constructs a new resource request with the specified URL, using the specified HTTP method.
-
Method Summary
Methods Modifier and Type Method and Description void
addHeader(org.apache.http.Header header)
Adds a header to this resource request.org.apache.http.Header[]
getAllHeaders()
Returns all the headers that were set for this resource request.org.apache.http.Header
getFirstHeader(java.lang.String headerName)
Returns the first header for this resource request with the given name.java.lang.String[]
getHeaderNames()
Returns the names of all the headers that were set for this resource request.org.apache.http.Header[]
getHeaders(java.lang.String headerName)
Returns all the headers for this resource request that have the given name.java.lang.String
getMethod()
Returns the HTTP method for this resource request.java.util.Map
getQueryParameters()
Returns the query parameters set for this resource request.int
getTimeout()
Returns the timeout in milliseconds for this resource request.java.net.URL
getUrl()
Returns the URL for this resource request.void
removeHeaders(java.lang.String headerName)
Returns all the headers for this resource request with the given name.void
send(byte[] data, WLHttpResponseListener listener)
Send this resource request asynchronously, with the content of the given byte array as the request body.void
send(byte[] data, WLResponseListener listener)
Send this resource request asynchronously, with the content of the given byte array as the request body.void
send(java.util.HashMap formParameters, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given form parameters as the request body.void
send(java.util.HashMap formParameters, WLResponseListener listener)
Send this resource request asynchronously, with the given form parameters as the request body.void
send(org.json.JSONObject json, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given JSON object as the request body.void
send(org.json.JSONObject json, WLResponseListener listener)
Send this resource request asynchronously, with the given JSON object as the request body.void
send(java.lang.String requestBody, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given string as the request body.void
send(java.lang.String requestBody, WLResponseListener listener)
Send this resource request asynchronously, with the given string as the request body.void
send(WLHttpResponseListener listener)
Send this resource request asynchronously, without a request body.void
send(WLResponseListener listener)
Send this resource request asynchronously, without a request body.void
setHeader(org.apache.http.Header header)
Sets a header for this resource request.void
setHeaders(org.apache.http.Header[] headers)
Sets headers for this resource request.void
setQueryParameter(java.lang.String name, java.lang.String value)
Sets the value of the given query parameter name to the given value.void
setQueryParameters(java.util.HashMap parameters)
Sets the query parameters for this resource request.void
setTimeout(int timeout)
Sets the timeout in milliseconds for this resource request.
-
-
-
Field Detail
-
GET
public static final java.lang.String GET
String constant for the HTTP GET method.- See Also:
- Constant Field Values
-
POST
public static final java.lang.String POST
String constant for the HTTP POST method.- See Also:
- Constant Field Values
-
PUT
public static final java.lang.String PUT
String constant for the HTTP PUT method.- See Also:
- Constant Field Values
-
DELETE
public static final java.lang.String DELETE
String constant for the HTTP DELETE method.- See Also:
- Constant Field Values
-
TRACE
public static final java.lang.String TRACE
String constant for the HTTP TRACE method.- See Also:
- Constant Field Values
-
HEAD
public static final java.lang.String HEAD
String constant for the HTTP HEAD method.- See Also:
- Constant Field Values
-
OPTIONS
public static final java.lang.String OPTIONS
String constant for the HTTP OPTIONS method.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
WLResourceRequest
public WLResourceRequest(java.net.URI url, java.lang.String method)
Constructs a new resource request with the specified URL, using the specified HTTP method.- Parameters:
url
- Resource URL. May be relative or absolute.method
- HTTP method to use.- Throws:
IlleagalArgumentException
- if the method name is not one of the valid HTTP method names.
-
WLResourceRequest
public WLResourceRequest(java.net.URI url, java.lang.String method, int timeout)
Constructs a new resource request with the specified URL, using the specified HTTP method. Additionally this constructor sets a custom timeout.- Parameters:
url
- The resource URL, may be either relative or absolute.method
- The HTTP method to use.timeout
- The timeout in milliseconds for this request.- Throws:
IlleagalArgumentException
- if the method name is not one of the valid HTTP method names.
-
-
Method Detail
-
getUrl
public java.net.URL getUrl()
Returns the URL for this resource request. The URL returned by this method is always the absolute URL.- Returns:
- The URL object representing the path for this resource request.
-
getMethod
public java.lang.String getMethod()
Returns the HTTP method for this resource request.- Returns:
- A string containing the name of the HTTP method.
-
getQueryParameters
public java.util.Map getQueryParameters()
Returns the query parameters set for this resource request.- Returns:
- A Map containing the query parameters
-
setQueryParameters
public void setQueryParameters(java.util.HashMap parameters)
Sets the query parameters for this resource request.- Parameters:
parameters
- A HashMap containing the query parameters
-
setQueryParameter
public void setQueryParameter(java.lang.String name, java.lang.String value)
Sets the value of the given query parameter name to the given value. If no such parameter exists, it will be added.- Parameters:
name
- The name of the parameter to setvalue
- The value of the parameter to set
-
getHeaderNames
public java.lang.String[] getHeaderNames()
Returns the names of all the headers that were set for this resource request.- Returns:
- A string array containing the header names
-
getAllHeaders
public org.apache.http.Header[] getAllHeaders()
Returns all the headers that were set for this resource request.- Returns:
- An array of Headers
-
getHeaders
public org.apache.http.Header[] getHeaders(java.lang.String headerName)
Returns all the headers for this resource request that have the given name.- Parameters:
headerName
- The name of the headers to return- Returns:
- An array of Headers
-
getFirstHeader
public org.apache.http.Header getFirstHeader(java.lang.String headerName)
Returns the first header for this resource request with the given name.- Returns:
- The first header corresponding the given name, or null if no such headers exist.
-
removeHeaders
public void removeHeaders(java.lang.String headerName)
Returns all the headers for this resource request with the given name.- Parameters:
headerName
- The name of the headers to remove
-
setHeaders
public void setHeaders(org.apache.http.Header[] headers)
Sets headers for this resource request. If any of the headers had already been set, the new value overwrites the previous one.- Parameters:
headers
- An array of headers to set
-
setHeader
public void setHeader(org.apache.http.Header header)
Sets a header for this resource request. If the header had already been set, the new value overwrites the previous one.- Parameters:
header
- The header to set
-
addHeader
public void addHeader(org.apache.http.Header header)
Adds a header to this resource request. This method allows response headers to have multiple values.- Parameters:
header
- The header to add
-
getTimeout
public int getTimeout()
Returns the timeout in milliseconds for this resource request.- Returns:
- the timeout for this resource request
-
setTimeout
public void setTimeout(int timeout)
Sets the timeout in milliseconds for this resource request.- Parameters:
timeout
- The timeout for this resource request.
-
send
public void send(WLResponseListener listener)
Send this resource request asynchronously, without a request body.- Parameters:
listener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(WLHttpResponseListener listener)
Send this resource request asynchronously, without a request body.- Parameters:
listener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(java.lang.String requestBody, WLResponseListener listener)
Send this resource request asynchronously, with the given string as the request body. If no content type header was set, this method will set it to "text/plain".- Parameters:
requestBody
- The request body textlistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(java.lang.String requestBody, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given string as the request body. If no content type header was set, this method will set it to "text/plain".- Parameters:
requestBody
- The request body textlistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(java.util.HashMap formParameters, WLResponseListener listener)
Send this resource request asynchronously, with the given form parameters as the request body. If no content type header was set, this method will set it to "application/x-www-form-urlencoded".- Parameters:
formParameters
- The parameters to put in the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(java.util.HashMap formParameters, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given form parameters as the request body. If no content type header was set, this method will set it to "application/x-www-form-urlencoded".- Parameters:
formParameters
- The parameters to put in the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(org.json.JSONObject json, WLResponseListener listener)
Send this resource request asynchronously, with the given JSON object as the request body. If no content type header was set, this method will set it to "application/json".- Parameters:
json
- The JSON object to put in the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(org.json.JSONObject json, WLHttpResponseListener listener)
Send this resource request asynchronously, with the given JSON object as the request body. If no content type header was set, this method will set it to "application/json".- Parameters:
json
- The JSON object to put in the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(byte[] data, WLResponseListener listener)
Send this resource request asynchronously, with the content of the given byte array as the request body. Note that this method does not set any content type header, if such a header is required it must be set before calling this method.- Parameters:
data
- The byte array containing the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
send
public void send(byte[] data, WLHttpResponseListener listener)
Send this resource request asynchronously, with the content of the given byte array as the request body. Note that this method does not set any content type header, if such a header is required it must be set before calling this method.- Parameters:
data
- The byte array containing the request bodylistener
- The listener whose onSuccess or onFailure methods will be called when this request finishes.
-
-