com.faunos.util.net.http
Class ResponseHeaderStage

java.lang.Object
  extended by com.faunos.util.net.http.ResponseHeaderStage
All Implemented Interfaces:
Handlet, Stagelet
Direct Known Subclasses:
NotFoundStage

public class ResponseHeaderStage
extends Object
implements Stagelet

Writes the HTTP response headers. This class is typically used in combination with a content Stagelet. This class is not itself stack-aware. That's because while the instance itself may be on the stack, it doesn't push any further stages on the stack: it's analogous to a function that calls no other function.

An instance must be configured before it becomes usable. At the very least, the status line must have been set (via one of the setStatusXxx() methods. The commit() method makes the configuration changes effective: it creates an array of ByteBuffers ready to be fed into the ScatteringByteChannel input parameter that will be passed in on the next invocation of write.

Author:
Babak Farhang

Field Summary
static String DEFAULT_SERVER_NAME
           
static CannedResponses HEADERS
           
protected  ByteBuffer[] responseHeaders
           
 
Constructor Summary
ResponseHeaderStage()
           
 
Method Summary
protected  void addContent(List<ByteBuffer> response)
          Hook exposed to subclasses for appending content to the end of the response header.
 void commit()
          Commits the changes to the instance.
 void discard()
          Discards the instance.
 IoState init(HandletContext context)
          Commits the changes.
protected  Object lock()
          Returns the object synchronized on when attempting to configure the instance.
 IoState read(ScatteringByteChannel in)
          Not implemented.
 void setAllowGetHead()
          Sets the "Allow" header to HEAD and GET (Allow: GET, HEAD).
 void setConnectionClose()
          Sets the Connection: close response header.
 void setContentBase(String uri)
           
 void setContentLength(long length)
          Set the content-length HTTP header.
 void setContentLocation(String uri)
          Sets the Content-Location header.
 void setContentType(ByteBuffer valueBuffer)
          Expert.
 void setContentType(String contentType)
          Sets the Content-Type HTTP header.
 void setContentTypeAppOctet()
           
 void setContentTypeHtml()
          Sets the content-type HTTP header to "text/html".
 void setContentTypePlainText()
          Sets the content-type HTTP header to "text/plain".
 void setLocation(String uri)
           
 void setProperty(String name, String value)
           
 void setServer(ByteBuffer name)
          Expert.
 void setServer(String name)
          Sets the server name.
 void setStatusBadRequest400()
          Sets the status line to 400 (Bad Request).
 void setStatusMethodNotAllowed405()
          Sets the status line to 405 (Method Not Allowed).
 void setStatusNotFound404()
          Sets the status line to 404 (Not Found).
 void setStatusNotImplemented501()
          Sets the status line to 501 (Not Implemented).
 void setStatusOkay200()
          Sets the status line to 200 (OK).
 void setStatusRequestTooLong414()
          Sets the status line to 414 (Request URI Too Long).
 void setStatusServerError500()
          Sets the status line to 500 (Internal Server Error).
 IoState state()
          Returns the state of the handlet.
 IoState unwind(IoState preState)
          Not invoked in the base implementation, since this stage does not push another stagelet onto the stack.
 IoState write(GatheringByteChannel out)
          Writes the HTTP headers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADERS

public static final CannedResponses HEADERS

DEFAULT_SERVER_NAME

public static final String DEFAULT_SERVER_NAME
See Also:
Constant Field Values

responseHeaders

protected ByteBuffer[] responseHeaders
Constructor Detail

ResponseHeaderStage

public ResponseHeaderStage()
Method Detail

commit

public void commit()
            throws IllegalStateException
Commits the changes to the instance. At the very least, the status line must have been set. The configured response headers plus an empty line (CRLF) are prepared for the next write operation.

Once invoked, the instance cannot be modified again.

Throws:
IllegalStateException - If the status line has not been set.
See Also:
clear()

addContent

protected void addContent(List<ByteBuffer> response)
Hook exposed to subclasses for appending content to the end of the response header. The advantage of doing it here is that the content may be written in a single gathering operation--which is potentially more efficient than writing the content in a separate write operation.

This method is invoked on commit.

Parameters:
response - the ByteBuffer contents can be added to this given list

setConnectionClose

public void setConnectionClose()
Sets the Connection: close response header.


setStatusOkay200

public void setStatusOkay200()
Sets the status line to 200 (OK).


setStatusBadRequest400

public void setStatusBadRequest400()
Sets the status line to 400 (Bad Request).


setStatusNotFound404

public void setStatusNotFound404()
Sets the status line to 404 (Not Found).


setStatusMethodNotAllowed405

public void setStatusMethodNotAllowed405()
Sets the status line to 405 (Method Not Allowed). Per the HTTP/1.1 protocol the "Allow: XX, XX, .." header must also be set.

See Also:
setAllowGetHead()

setStatusRequestTooLong414

public void setStatusRequestTooLong414()
Sets the status line to 414 (Request URI Too Long). We also use this in the event there were too many request headers.


setStatusServerError500

public void setStatusServerError500()
Sets the status line to 500 (Internal Server Error).


setStatusNotImplemented501

public void setStatusNotImplemented501()
Sets the status line to 501 (Not Implemented).


setAllowGetHead

public void setAllowGetHead()
Sets the "Allow" header to HEAD and GET (Allow: GET, HEAD).


setServer

public void setServer(String name)
Sets the server name. The name must not contain CRLF.


setServer

public void setServer(ByteBuffer name)
Expert. Sets the server name to the remaining bytes in the specified name buffer. The given buffer must end with CRLF.

This method is not fail-fast. No validation is performed on the input argument.


setContentLocation

public void setContentLocation(String uri)
Sets the Content-Location header.


setContentBase

public void setContentBase(String uri)

setLocation

public void setLocation(String uri)

setContentType

public void setContentType(String contentType)
Sets the Content-Type HTTP header.

Parameters:
contentType - the string value of the content-type, sans the CRLF
See Also:
setContentTypeHtml()

setContentType

public void setContentType(ByteBuffer valueBuffer)
Expert. Sets the content type to the remaining bytes in the specified valueBuffer. The given buffer must end with CRLF.

This method is not fail-fast. No validation is performed on the input argument.


setContentTypeHtml

public void setContentTypeHtml()
Sets the content-type HTTP header to "text/html".


setContentTypePlainText

public void setContentTypePlainText()
Sets the content-type HTTP header to "text/plain".


setContentTypeAppOctet

public void setContentTypeAppOctet()

setContentLength

public void setContentLength(long length)
Set the content-length HTTP header.

Parameters:
length - the content length in bytes

setProperty

public void setProperty(String name,
                        String value)

unwind

public IoState unwind(IoState preState)
Not invoked in the base implementation, since this stage does not push another stagelet onto the stack.

Specified by:
unwind in interface Stagelet
Parameters:
preState - the state of the previous stage prior to its flipping to DONE
Returns:
DONE

discard

public void discard()
Description copied from interface: Handlet
Discards the instance.

Specified by:
discard in interface Handlet

init

public IoState init(HandletContext context)
Commits the changes. By the time this method is invoked, per the Stagelet contract, the instance is already on the stack. This implementation does not use the passed in context argument.

Specified by:
init in interface Handlet
Parameters:
context - the context supplied by the instance's container (e.g. server

read

public IoState read(ScatteringByteChannel in)
             throws IOException,
                    UnsupportedOperationException
Not implemented. The instance is never in the read state.

Specified by:
read in interface Handlet
Throws:
IOException
UnsupportedOperationException

state

public IoState state()
Description copied from interface: Handlet
Returns the state of the handlet.

Specified by:
state in interface Handlet
Returns:
WRITING or DONE.

write

public IoState write(GatheringByteChannel out)
              throws IOException
Writes the HTTP headers.

Specified by:
write in interface Handlet
Throws:
IOException

lock

protected final Object lock()
Returns the object synchronized on when attempting to configure the instance. Synchronization is not strictly necessary if, as specified, the object is configured in the same thread. As a sanity check, however, we do lock, since the cost of acquiring an uncontended monitor lock is negligible compared to the expensive I/O we're trying to perform.



SourceForge.net Logo