JBoss.orgCommunity Documentation

Chapter 2. Resource Adaptor Type

2.1. Activities
2.2. Events
2.3. Activity Context Interface Factory
2.4. Resource Adaptor Interface
2.5. Restrictions
2.6. Sbb Code Examples
2.6.1. GET Request Event Handling
2.6.2. PUT Request Event Handling With Session Creation

The Resource Adaptor Type is the interface which defines the contract between the RA implementations, the SLEE container, and the Applications running in it.

The name of the RA Type is HttpServletResourceAdaptorType, its vendor is org.mobicents and its version is 1.0.

The Resource Adaptor Type defines two activity objects, the types net.java.slee.resource.http.HttpServletRequestActivity and net.java.slee.resource.http.HttpSessionActivity.

The HttpServletRequestActivity represents a specific incoming request. It is created by the Resource Adaptor when processing the incoming event, unless the request is received with an HttpSession. The activity ending is done by the Resource Adaptor once the related request event is unreferenced inside the SLEE Container. The activity object interface is defined as follows:



        
package net.java.slee.resource.http;
public interface HttpServletRequestActivity {
    /**
     * Method to fetch the Request ID for this request.
     * 
     * @return 
     */
    public Object getRequestID();
    
}
     

The HttpSessionActivity represents an HttpSession. It is created on demand by an SBB, through the RA SBB Interface, and ends on timeout, managed by the underlying HTTP Servlet framework, or by an SBB, invoking the invalidate() on the related HttpSession Object. The activity object interface is defined as follows:



        
package net.java.slee.resource.http;
public interface HttpSessionActivity {
    public String getSessionId();
    
}
     

The Events fired by HTTP Servlet Resource Adaptor represent an incoming HTTP Request, and for each HTTP Request a different event type is used for each activity type. The table below lists the Resource Adaptor Type event types.


Important

Spaces where introduced in Name and Event Class column values, to correctly render the table. Please remove them when using copy/paste.


Important

Spaces where introduced in Name and Event Class column values, to correctly render the table. Please remove them when using copy/paste.

All event types use the same type net.java.slee.resource.http.events.HttpServletRequestEvent. It's interface is as follows:



        
package net.java.slee.resource.http.events;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface HttpServletRequestEvent {
    
    public HttpServletRequest getRequest(); 
    
    public HttpServletResponse getResponse();
    
    public String getId();
}
     
The getRequest() method:

Retrieves the HttpServletRequest which is associated with the event.

The getResponse() method:

Retrieves the HttpServletResponse which is associated with the request, which can be used to reply to the incoming request.

The getId() method:

Retrieves unique event ID.

The Resource Adaptor's Activity Context Interface Factory is of type net.java.slee.resource.http.HttpServletRaActivityContextInterfaceFactory, it allows the SBB to retrieve the ActivityContextInterface related with an existing Resource Adaptor activity object. The interface is defined as follows:



        
package net.java.slee.resource.http;
import javax.slee.ActivityContextInterface;
import javax.slee.FactoryException;
import javax.slee.UnrecognizedActivityException;
public interface HttpServletRaActivityContextInterfaceFactory {
    public ActivityContextInterface getActivityContextInterface(
            HttpSessionActivity activity) throws NullPointerException,
            UnrecognizedActivityException, FactoryException;
    
    public ActivityContextInterface getActivityContextInterface(
            HttpServletRequestActivity activity) throws NullPointerException,
            UnrecognizedActivityException, FactoryException;
}
     

The HTTP Servlet Resource Adaptor interface, of type net.java.slee.resource.http.HttpServletRaSbbInterface , which an SBB uses to create HtppSessionActivity instances, is defined as follows:



        
package net.java.slee.resource.http;
import javax.servlet.http.HttpSession;
import javax.slee.SLEEException;
import javax.slee.resource.ActivityAlreadyExistsException;
import javax.slee.resource.StartActivityException;
public interface HttpServletRaSbbInterface {
    public HttpSessionActivity getHttpSessionActivity(HttpSession httpSession)
            throws  NullPointerException, IllegalArgumentException, IllegalStateException,
            ActivityAlreadyExistsException, StartActivityException,
            SLEEException;
            
}
    

The HTTP Servlet does imposes some restrictions on the usage of the HTTP Servlet API java objects:

The following code examples shows how to use the Resource Adaptor Type for common functionalities

The following code examples the handling of an HTTP PUT request, and the creation of an HttpSessionActivity:



            
    public void onPut(HttpServletRequestEvent event,
            ActivityContextInterface aci) {
        
        SbbLocalObject sbbLocalObject = sbbContext.getSbbLocalObject();
        // detach from HttpServletRequestActivity
        aci.detach(sbbLocalObject);
        try {
            // here we will setup a session activity before sending the response back
            if (httpServletRaSbbInterface == null) {
                Context myEnv = (Context) new InitialContext().lookup("java:comp/env"); 
                httpServletRaSbbInterface = (HttpServletRaSbbInterface) myEnv.lookup(
                    "slee/resources/mobicents/httpservlet/sbbrainterface");
                httpServletRaActivityContextInterfaceFactory = 
                    (HttpServletRaActivityContextInterfaceFactory) myEnv.lookup(
                        "slee/resources/mobicents/httpservlet/acifactory");
            }
            HttpSession httpSession = event.getRequest().getSession();
            HttpSessionActivity httpSessionActivity = httpServletRaSbbInterface
                    .getHttpSessionActivity(httpSession);
            ActivityContextInterface httpSessionActivityContextInterface = 
                httpServletRaActivityContextInterfaceFactory
                    .getActivityContextInterface(httpSessionActivity);
            httpSessionActivityContextInterface.attach(sbbLocalObject);
            HttpServletResponse response = event.getResponse();
            PrintWriter w = response.getWriter();
            w.print("onPut OK! Served by SBB = " + getSbbId());
            w.flush();
            response.flushBuffer();
            log.info("HttpServletRAExampleSbb: PUT Request received and OK! response sent.");
        } catch (Exception e) {
            log.error(e);
        }
    }