org.elkoserver.foundation.net
Class HTTPFramer

java.lang.Object
  extended by org.elkoserver.foundation.net.HTTPFramer
Direct Known Subclasses:
JSONHTTPFramer

public class HTTPFramer
extends Object

An HTTPFramer is responsible for extracting messages from HTTP POSTs arriving on an HTTP connection and doing something meaningful with them. It is also responsible for actually producing the HTTP replies that will be transmitted in response to HTTP requests sent to the server.

The base HTTPFramer treats the content of each HTTP POST (to the /xmit/ URL) as a simple string being delivered to the server.


Constructor Summary
HTTPFramer(Trace msgTrace)
          Constructor.
 
Method Summary
 String makeBadURLReply(String badURL)
          Produce the HTTP reply body for responding to an unrecognized URL (that is, a 404 error).
 String makeConnectReply(long sessionID)
          Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /connect/ URL.
 String makeDisconnectReply()
          Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /disconnect/ URL.
 String makeSelectReplySegment(Object message, int seqNumber, boolean start, boolean end)
          Produce a fragment of the HTTP reply body containing JSON messages in response to an HTTP GET or POST of the /select/ URL.
 String makeSequenceErrorReply(String error)
          Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST where the URL that was presented to the server had an out-of-sequence sequence number or a bad session ID number.
 String makeXmitReply(int seqNumber)
          Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /xmit/ URL used to transmit messages from the client to the server.
 Iterator postBodyUnpacker(String postBody)
          Return an iterator that will return the application-level message or messages (if any) in the body of a received HTTP POST.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPFramer

public HTTPFramer(Trace msgTrace)
Constructor.

Parameters:
msgTrace - Trace object for logging message traffic.
Method Detail

makeBadURLReply

public String makeBadURLReply(String badURL)
Produce the HTTP reply body for responding to an unrecognized URL (that is, a 404 error).

Parameters:
badURL - The URL that was no good.
Returns:
an appropriate HTTP reply body string to accompany the 404 error that will be returned.

makeConnectReply

public String makeConnectReply(long sessionID)
Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /connect/ URL.

Parameters:
sessionID - The session ID number for the session.
Returns:
an appropriate HTTP reply body JSON string for responding to a /connect/ request that created the given session.

makeDisconnectReply

public String makeDisconnectReply()
Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /disconnect/ URL.

Returns:
an appropriate HTTP reply body string for responding to a /disconnect/ request.

makeSelectReplySegment

public String makeSelectReplySegment(Object message,
                                     int seqNumber,
                                     boolean start,
                                     boolean end)
Produce a fragment of the HTTP reply body containing JSON messages in response to an HTTP GET or POST of the /select/ URL. This JSON text will either deliver a message (or messages) to the client or inform the client that there are no messages at this time.

This method is able to produce fragmentary output to support packing multiple messages into a single reply body: the reply consists of a prefix, the actual encoding of the message(s) being sent, and then a suffix. The prefix is only produced if the 'start' parameter is true; likewise for the suffix and the 'end' parameter. It is possible to concatenate the fragments generated by any number of calls to this method to produce a valid reply body, so long as the 'start' parameter is set on the first call (and not the others), and the 'end' parameter is set on the last call (and not the others).

Parameters:
message - The message to be sent, or null if no messages are to be sent.
seqNumber - The sequence number for the next select request.
start - true if this message is the first in a batch.
end - true if this message is the last in a batch.
Returns:
an appropriate HTTP reply body JSON fragment string for responding to a /select/ by delivering 'message' to the client.

makeSequenceErrorReply

public String makeSequenceErrorReply(String error)
Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST where the URL that was presented to the server had an out-of-sequence sequence number or a bad session ID number.

Parameters:
error - Error string to transmit.
Returns:
an appropriate HTTP reply body JSON string for responding to a URL with a bad sequence number.

makeXmitReply

public String makeXmitReply(int seqNumber)
Produce the HTTP reply body containing the JSON string for responding to an HTTP GET or POST of the /xmit/ URL used to transmit messages from the client to the server.

Parameters:
seqNumber - The sequence number for the next xmit request.
Returns:
an appropriate HTTP reply body JSON string for responding to a POST or GET delivering messages to the server.

postBodyUnpacker

public Iterator postBodyUnpacker(String postBody)
Return an iterator that will return the application-level message or messages (if any) in the body of a received HTTP POST.

Parameters:
postBody - The HTTP POST body in question.
Returns:
an iterator that can be called upon to return the application- level message(s) contained within 'postBody'.