simple.http.serve
Class Component

java.lang.Object
  extended by simple.http.serve.Component
All Implemented Interfaces:
Resource
Direct Known Subclasses:
Service

public abstract class Component
extends java.lang.Object
implements Resource

This is an abstract Resource that handles the basic HTTP status reports. For example messages like '404 Not Found' are represented using this abstraction. This Resource can be subclassed to give a Resource the ability to handle all generic status reports for 3xx, 4xx and 5xx defined by RFC 2616.

This uses a various Report objects to generate error and status messages using the Format supplied. This also enables exceptions that propagate from the service objects to be classified and for descriptions of those exceptions to be represented as Report objects that can be be used to generate a formatted message that can be presented to the client.

Author:
Niall Gallagher

Field Summary
protected  Context context
          The Context that this resource is in.
 
Constructor Summary
protected Component()
          Constructor that creates a Component without any Context object.
protected Component(Context context)
          Constructor for the Component is given the Context so that it can generate status reports.
 
Method Summary
 void handle(Request req, Response resp)
          This handle is provided so that if any errors occur when processing a HTTP transaction a '500 Server Error' message will be sent to the client.
 void handle(Request req, Response resp, int code)
          This is used to generate the status report from a status code.
 void handle(Request req, Response resp, Report report)
          This is used to generate a formatted message using a report to describe the change in status.
protected abstract  void process(Request req, Response resp)
          This method is used to handle the HTTP transaction by subclasses of the Component.
protected  void process(Request req, Response resp, int code)
          This method is used to handle the HTTP status reports so that if any Exception occurs the handle method can capture and deal with the exception.
protected  void process(Request req, Response resp, Report report)
          This method is used to handle the HTTP status reports so that if any Exception occurs the handle method can capture and deal with the exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected Context context
The Context that this resource is in.

Constructor Detail

Component

protected Component()
Constructor that creates a Component without any Context object. This is used so that if the resource is a ProtocolHandler or some other form of resource that does not require a context then this will allow that resource to inherit the functionality of this. The typical implementation however will not use this constructor. If this is used and a context is not set then there will be an exception in handle(Request,Response,int).


Component

protected Component(Context context)
Constructor for the Component is given the Context so that it can generate status reports. Every implementation of the Component needs to be constructed with a Context to ensure that the generation of error and status messages is successful.

Parameters:
context - the context that this resource is in
Method Detail

handle

public void handle(Request req,
                   Response resp)
This handle is provided so that if any errors occur when processing a HTTP transaction a '500 Server Error' message will be sent to the client. This is used to invoke the process method which subclasses should implement to process the HTTP transaction. If the exception is a SecurityException '403 Forbidden' is used and a FileNotFoundException is '404 Not Found'.

Any Exception thrown from the process method will be captured and the Request and Response are handled by the default error handler method handle(Request,Response,int) with the code 500 which indicates the HTTP/1.1 error message 'Server Error'

This does not throw ant Exception however any user should handle RuntimeException's that may be thrown from handle(Request,Response,int).

Specified by:
handle in interface Resource
Parameters:
req - the Request object to be processed
resp - the Response object to be processed

handle

public void handle(Request req,
                   Response resp,
                   int code)
This is used to generate the status report from a status code. The set of status reports that can have a valid response are the messages that are defined in RFC 2616. If the status code given does not have a valid entry then this will result in an status message description of 'Unknown'.

This does not throw Exception's but users should be prepared to handle any RuntimeException's that could propagate from this. If the Response has been committed then this will return quietly. Typically there will be an IOException writing the content body if the stream has been closed. This will not report such exceptions.

Specified by:
handle in interface Resource
Parameters:
req - the Request object to be processed
resp - the Response object to be processed
code - this is the HTTP status code of the response

handle

public void handle(Request req,
                   Response resp,
                   Report report)
This is used to generate a formatted message using a report to describe the change in status. This uses the Format object to prepare a formatted message that can be presented to the client. This message will describe the status using the issued Report obejct. If the status code given does not have a valid entry then this will result in an status message description of 'Unknown'.

This does not throw Exception's but users should be prepared to handle any RuntimeException's that could propagate from this. If the Response has been committed then this will return quietly. Typically there will be an IOException writing the content body if the stream has been closed. This will not report such exceptions.

Specified by:
handle in interface Resource
Parameters:
req - the Request object to be processed
resp - the Response object to be processed
report - this is used to describe the change in status

process

protected void process(Request req,
                       Response resp,
                       int code)
                throws java.lang.Exception
This method is used to handle the HTTP status reports so that if any Exception occurs the handle method can capture and deal with the exception.

Subclasses should treat this as the handle method of the ProtocolHandler. Exceptions that cannot be recovered from, particularly RuntimeException's should be left propagate so the handle method can deal with the Exception appropriately.

Parameters:
req - the Request object to be processed
resp - the Response object to be processed
code - this is the HTTP status code of the response
Throws:
java.lang.Exception - this can throw an error for anything

process

protected void process(Request req,
                       Response resp,
                       Report report)
                throws java.lang.Exception
This method is used to handle the HTTP status reports so that if any Exception occurs the handle method can capture and deal with the exception.

Subclasses should treat this as the handle method of the ProtocolHandler. Exceptions that cannot be recovered from, particularly RuntimeException's should be left propagate so the handle method can deal with the Exception appropriately.

Parameters:
req - the Request object to be processed
resp - the Response object to be processed
report - this is used to describe the change in status
Throws:
java.lang.Exception - this can throw an error for anything

process

protected abstract void process(Request req,
                                Response resp)
                         throws java.lang.Exception
This method is used to handle the HTTP transaction by subclasses of the Component. This is used so that if there are any Exception's thrown while processing the HTTP transaction they can be captured and the HTTP status line will convey the status to the client.

Subclasses should treat this as the handle method of the ProtocolHandler. Exceptions that cannot be recovered from, particularly RuntimeException's should be left propagate so the handle method can prepare an appropriate response.

Parameters:
req - the Request object to be processed
resp - the Response object to be processed
Throws:
java.lang.Exception - this can throw an error for anything