Package edu.rit.http

Class HttpResponse

java.lang.Object
edu.rit.http.HttpResponse

public class HttpResponse extends Object
Class HttpResponse encapsulates an HTTP response returned to a web browser.

Only HTTP/1.0 responses are supported. This means that only one HTTP response message can be sent over the connection to the web browser; the connection is closed after sending the HTTP response message.

To send an HTTP response message:

  1. Create an instance of class HttpResponse, giving the socket object representing the open connection to the web browser.
     
  2. Call methods to set the status code and headers as necessary.
     
  3. Call the getPrintWriter() method, and write the entity body to the print writer that is returned.
     
  4. Call the close() method.
Version:
29-Jul-2010
Author:
Alan Kaminsky
  • Constructor Details

    • HttpResponse

      public HttpResponse(Socket theSocket)
      Construct a new HTTP response. The response is written to the output stream of the given socket.
      Parameters:
      theSocket - Socket.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theSocket is null.
  • Method Details

    • setStatusCode

      public void setStatusCode(HttpResponse.Status theStatusCode)
      Set this HTTP response's status code. If not set, the default status code is STATUS_200_OK.
      Parameters:
      theStatusCode - Status code.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theStatusCode is null.
      IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
    • setContentType

      public void setContentType(@Nullable String theContentType)
      Set this HTTP response's content type. If not set, the default content type is "text/html".
      Parameters:
      theContentType - Content type.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theContentType is null.
      IllegalArgumentException - (unchecked exception) Thrown if theContentType is zero length.
      IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
    • setCharset

      public void setCharset(Charset theCharset)
      Set this HTTP response's character set. If not set, the default character set is the platform default character set.
      Parameters:
      theCharset - Character set.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theCharset is null.
      IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
    • setContentLength

      public void setContentLength(int theContentLength)
      Set this HTTP response's content length. If not set, the default is to omit the Content-Length header; closing this HTTP response marks the end of the entity body.
      Parameters:
      theContentLength - Content length.
      Throws:
      IllegalArgumentException - (unchecked exception) Thrown if theContentLength is less than 0.
      IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
    • setHeader

      public void setHeader(@Nullable String theHeaderName, @Nullable String theHeaderValue)
      Set the given header in this HTTP response.
      Parameters:
      theHeaderName - Header name.
      theHeaderValue - Header value.
      Throws:
      NullPointerException - (unchecked exception) Thrown if theHeaderName or theHeaderValue is null.
      IllegalArgumentException - (unchecked exception) Thrown if theHeaderName or theHeaderValue is zero length.
      IllegalStateException - (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
    • getPrintWriter

      public PrintWriter getPrintWriter() throws IOException
      Obtain the print writer for writing the entity body to this HTTP response. As a side effect, the HTTP response headers are written to the socket output stream.
      Returns:
      Print writer.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.
    • close

      public void close() throws IOException
      Close this HTTP response. If necessary, the HTTP response headers are written to the socket output stream.
      Throws:
      IOException - Thrown if an I/O error occurred.
      IOException - if any.