Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 5 of 7
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.
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.
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:
Figure 4 illustrates this flow of control.
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.
public abstract class Representation extends javax.activation.DataHandler
{
public Representation(DataSource dataSource)
{
super(dataSource);
}
}