The Hyper-Text Transfer Protocol (HTTP) is perhaps the most significant protocol used on the Internet today. Web services, network-enabled appliances and the growth of network computing continue to expand the role of the HTTP protocol beyond user-driven web browsers, while increasing the number of applications that require HTTP support; for example Application developed using SLEE. The Jakarta Commons HttpClient component provides an efficient, up-to-date, and feature-rich package implementing the client side of the most recent HTTP standards and recommendations. Http Client RA provides the client side HTTP standard within the SLEE environment. The Http Client RA uses the popular Apache Commons Http Client library.
An SBB can use the HTTP Client RA to make a request and get the Response Synchronously or Asynchronously.
The class diagram follows:

HttpClientResourceAdaptorSbbInterface
Provides SBB with the interface to interact with Http Client Resource Adaptor. HttpClientResourceAdaptorSbbInterface is wrapper over org.apache.commons.httpclient.HttpClient and exposes most commonly used methods of HttpClient
createHttpMethod(String method, String uri)
HttpClientResourceAdaptorSbbInterface is used by the SBB to generate the GET, POST, HEAD etc request (HttpMethod) by calling this method. The URI can be any external URI.
public void executeMethod(HttpMethod method)
Calling this method will send the request synchronously and the calling application can process the HttpMethod to get the response
public HttpClientActivity createHttpClientActivity()
Creates instance of HttpClientActivity for service that wants to send the Request asynchronously. By default the value for endOnReceivingResponse is set to false and the service has to explicitly end Activity
public HttpClientActivity createHttpClientActivity(boolean endOnReceivingResponse)
Same as above the only difference being service can pass endOnReceivingResponse as true which means the Activity ends as soon as Resource Adaptor executes the Method and emits ResponseEvent
Methods that are inherited from HttpClient
public HttpClientParams getParams()
HTTP protocol parameters associated with this HttpClient
public HttpClientParams getState()
HTTP state associated with the HttpClient
public void setParams(HttpClientParams params)
HTTP protocol parameters for this HttpClien
public void setState(HttpState state)
Assigns HTTP state for the HttpClient
HttpClientActivity
Http Client RA can be used Sync or Async way. HttpClientActivity is useful only when Http Client RA is used to send the Request async way.
public String getSessionId()
To get the unique session ID
public void endActivity();
To end the HttpClientActivity. If endOnReceivingResponse is set to true and if SBB tries to forcefully end activity by calling endActivity() it should throw exception. endActivity() should only be allowed when endOnReceivingResponse is set to false.
public boolean getEndOnReceivingResponse()
Returns true or false depending on value passed when the Request is sent asynchronously
public void executeMethod(HttpMethod httpMethod)
The service that wants to send the Request asynchronously has to first create instance of HttpMethod by calling createHttpMethod() of HttpClientResourceAdaptorSbbInterface, the service also creates Activity and attaches itself to this Activity and then calls executeMethod passing the instance of HttpMethod
ResponseEvent
ResponseEvent is fired by HttpClientRA as soon as HttpClient.executeMethod() returns. Response can be a proper response or an exception depending on the environment and application logic
public Response getResponse()
The interested SBB receives this event and can act on Response
public Exception getException()
There may be exception due to network failure or application logic. Interested SBB can get the exact Exception using this method
Response
This is the wrapper over response part of org.apache.commons.httpclient.HttpMethod
public byte[] getResponseBody()
Returns the response body of the HTTP method, if any, as an array of bytes
public String getResponseBodyAsString()
The response body of the HTTP method, if any, as a String
public int getStatusCode()
The status code associated with the latest response
public Header[] getResponseHeaders()
The response headers from the most recent execution of this request
Example
This is simple example of code, to look at full working example look at httpclientra-example at
https://mobicents-examples.dev.java.net/source/browse/mobicents-examples/httpclientra-example/
Sync way of sending Request
//get RA Sbb Interface
HttpClientResourceAdaptorSbbInterface raSbbInterface = ;
//create HttpMethod passing the link
HttpMethod httpMethod = raSbbInterface.createHttpMethod("GET", "http://www.mobicents.org-a.googlepages.com/index.html"));
//send the request and get Response
Response response = raSbbInterface.executeMethod(httpMethod);
//get ResponseBody
String responseBody = response.getResponseBodyAsString();
Async way of sending Request
//get RA Sbb Interface and aci factory
HttpClientResourceAdaptorSbbInterface raSbbInterface = ;
HttpClientActivityContextInterfaceFactory httpClientAci = ;
//create HttpMethod passing the link
HttpMethod httpMethod = raSbbInterface.createHttpMethod("GET", "http://www.mobicents.org-a.googlepages.com/index.html"));
//create HttpClientActivity
HttpClientActivity clientActivity = raSbbInterface.createHttpClientActivity(true);
// get aci for activity and attach sbb local object
ActivityContextInterface clientAci = httpClientAci.getActivityContextInterface(clientActivity);
clientAci.attach(sbbContext.getSbbLocalObject());
//send request by calling executeMethod
clientActivity.executeMethod(httpMethod);
The Response arrives asynchronously
public void onResponseEvent(ResponseEvent event, ActivityContextInterface aci) {
//Get the Response from Event
Response response = event.getResponse();
//get ResponseBody
String responseBody = response.getResponseBodyAsString();
If you have any suggestions/feedback or find a bug please discuss at
http://forums.java.net/jive/thread.jspa?messageID=228634𷴚
Http Servlet RA
and Http Client RA compliments each other in creating a complete SLEE
Application capable of receiving Request as well as initiating one.