Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

REST easy with the JavaBeans Activation Framework

Use JAF for seamless data exchange in RESTful systems

  • Print
  • Feedback

Page 4 of 7

Java components for a REST implementation

REST components are entities that are defined by the role they play in a given enterprise system. Typical components in a REST system are as follows:

User agents
A user agent initiates requests to some form of server component. The user agent then handles the response from the server component. A user agent can be a Web browser, a mobile device or another type of Web-enabled client.
Origin servers
An origin server embodies the namespace for a given collection of resources. An origin server is the source of representations of the state of the resources it embodies. An origin server receives requests from user agents, performs the necessary actions on the targeted resources, and responds to the user agent with the appropriate representational state for the resource and the actions performed by the origin server. An origin server typically exposes resources using a hierarchical interface.
Intermediary components
Intermediary components forward requests and responses to and from user agents and/or server components. An intermediary component might perform some type of transformation on a request or response as needed by a given system or application, such as data conversion, authentication and authorization, or performance optimization. Proxies and gateways are typical intermediary components.

In Java, you might represent a component using a marker interface, like so:

public interface Component
{
}

A server component in Java can expose the properties and methods by which resource contexts are managed, as illustrated in Listing 1.

Listing 1. ServerComponent exposes properties and methods

public abstract class ServerComponent
  implements Component
{
  private java.util.HashMap<String, Context> contexts =
    new java.util.HashMap<String, Context>();
  
  abstract public Context addContext(String contextRootPath, String contextPath);
  
  public java.util.Iterator<String> getContextPaths()
  {
    return contexts.keySet().iterator();
  }
  
  public Context getContext(String contextPath)
  {
    return contexts.get(contextPath);
  }
  
  protected java.util.HashMap<String, Context> getContexts()
  {
    return contexts;
  }
}


An HTTP-specific server component can expose HTTP-specific contexts, as shown in Listing 2.

Listing 2. ServerComponent exposes HTTP-specific contexts

public Context addContext(String contextRootPath, String contextPath)
  {
    Context context = getContexts().get(contextPath);
    if (context == null)
    {
      context = new HTTPContext(contextRootPath, contextPath);
      getContexts().put(contextPath, context);
    }
    
    return context;
  }

An HTTP-specific context implements the Context interface, where each HTTP method is handled and resource representations are manipulated and accessed. Listing 3 shows a REST implementation representing the HTTP GET method. Note that the details of business logic for a particular vertical domain are encapsulated within individual business services.

Listing 3. A REST implementation representing HTTP GET

public Representation handleGet(Request request)
    throws ContextRequestException
  {
    String uri = ((HTTPRequest)request).getRequest().getRequestURI();
    if (uri.startsWith(contextPath))
    {
      uri = reqURI.substring(contextPath.length());
    }

    BusinessService businessService =
      ServiceLocator.locateService(contextRootPath, contextPath, uri);

    Representation representation = null;
    
    try
    {
      representation = businessService.read(request);
    }
    catch (ServiceExecutionException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
    
    return representation;
  }

REST components use connectors to interact with each other.

  • Print
  • Feedback

Resources