juglr.net
Class HTTPResponseWriter

java.lang.Object
  extended by juglr.net.HTTPWriter
      extended by juglr.net.HTTPResponseWriter

public class HTTPResponseWriter
extends HTTPWriter

A Writer-like class for writing HTTP responses to a SocketChannel.This class does not automatically respect the HTTP protocol and you must manually call the the write*-methods in the correct order to build a valid HTTP response. This is because it must be possible to use this class in a fully optimized HTTP server.

Writing a correct HTTP response looks something like:

 String msgString("Hello world of HTTP!");
 resp.writeVersion(HTTP.Version.ONE_ZERO);
 resp.writeStatus(HTTP.Status.OK);
 resp.writeHeader("Content-Length", "" + msgString.length());
 resp.writeHeader("Server", "juglr");
 resp.startBody();
 resp.writeBody(msgString);
 resp.close();
 


Field Summary
 
Fields inherited from class juglr.net.HTTPWriter
buf, channel
 
Constructor Summary
HTTPResponseWriter(java.nio.channels.SocketChannel channel)
           
HTTPResponseWriter(java.nio.channels.SocketChannel channel, java.nio.ByteBuffer buf)
           
 
Method Summary
 HTTPResponseWriter reset(java.nio.channels.SocketChannel channel)
          Clear all state and reset the writer to start writing a new response tp channel.
 void writeStatus(HTTP.Status status)
           
 void writeVersion(HTTP.Version version)
           
 
Methods inherited from class juglr.net.HTTPWriter
close, flush, startBody, writeBody, writeBody, writeBody, writeBody, writeHeader, writeLF, writeSpace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPResponseWriter

public HTTPResponseWriter(java.nio.channels.SocketChannel channel,
                          java.nio.ByteBuffer buf)

HTTPResponseWriter

public HTTPResponseWriter(java.nio.channels.SocketChannel channel)
Method Detail

reset

public HTTPResponseWriter reset(java.nio.channels.SocketChannel channel)
                         throws java.io.IOException
Clear all state and reset the writer to start writing a new response tp channel. If the previous channel is open it will be closed.

Parameters:
channel - the new socket channel to write to
Returns:
always returns this
Throws:
java.io.IOException - if the previous channel was open and there was an error when closing it

writeVersion

public void writeVersion(HTTP.Version version)
                  throws java.io.IOException
Overrides:
writeVersion in class HTTPWriter
Throws:
java.io.IOException

writeStatus

public void writeStatus(HTTP.Status status)
                 throws java.io.IOException
Throws:
java.io.IOException