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 5 of 7

Connectors in REST

A connector is a software entity exposing a generic interface that enables communication among REST components. A connector typically represents one endpoint or port of a network communication protocol. A connector can be a client connector or a server connector. User agents use client connectors to instigate requests to an origin server. Origin servers use server connectors to receive requests from user agents and to respond with representational state for targeted resources.

A Java implementation of a connector can be represented by the following interface:

public interface Connector
{
  public ServerComponent addServer(int port);
}

A factory class can be used to manage protocol-specific connectors in Java. Each connector can be pooled if needed. Listing 4 is an example of a Connector factory class.

Listing 4. A Connector factory class

public static Connector getConnector(Protocol protocol)
    throws ConnectorException
  {
    String key = protocol.getScheme();
    Connector instance = instances.get(key);
    
    if (instance == null)
    {
      if (key.equalsIgnoreCase(Protocol.HTTP.getScheme()))
      {
        instance = new HTTPConnector();
        instances.put(key, instance);
      }
      else
      {
        throw new ConnectorException("Invalid protocol: " + protocol.getScheme());
      }
    }
    
    return instance;
  }

An HTTP-specific server Connector implements the Connector interface and exposes HTTP-specific server components for each given port, as illustrated here:

public ServerComponent addServer(int port)
  {
    return new HTTPServerComponent(port);
  }


Connectors facilitate communication among client components and server components. Server components represent resources that can be implemented as discussed in the next section.

Resource representation

For each client request, a URI is passed that identifies the targeted resource for the request. Once a server component receives a request, it reads the URI, performs any necessary actions to carry out the request for a resource, and returns the resulting representational state of the resource.

The flow for instantiating necessary components and accessing each to handle a typical request from a client to server and back again could be as follows:

  • Create a connector and add to it an HTTP server component.
  • Add the context for a given resource.
  • Dispatch incoming client requests received by a server component to the appropriate business service.
  • Encapsulate the model returned from the business service as a resource representation.
  • Respond to the client with the state of the representation.

Figure 4 illustrates this flow of control.

A RESTful client-server request-response cycle.

Figure 4. A RESTful client-server request-response cycle

JAF's javax.activation.DataHandler class exposes a component that can embody representations of resources. Using the DataHandler class lets you present multiple types of resources of differing content types with a standard interface.

Listing 5 shows an abstract class that extends the DataHandler class to encapsulate a standard base class for objects representing a wide array of resources.

Listing 5. Representation encapsulates resource objects

public abstract class Representation extends javax.activation.DataHandler
{
  public Representation(DataSource dataSource)
  {
    super(dataSource);
  }
}

  • Print
  • Feedback

Resources