edu.rit.http
Class HttpResponse
- java.lang.Object
-
- edu.rit.http.HttpResponse
-
public class HttpResponse extends java.lang.ObjectClass 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:
-
Create an instance of class HttpResponse, giving the socket object
representing the open connection to the web browser.
-
Call methods to set the status code and headers as necessary.
-
Call the getPrintWriter() method, and write the entity body to the
print writer that is returned.
- Call the close() method.
-
Create an instance of class HttpResponse, giving the socket object
representing the open connection to the web browser.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classHttpResponse.StatusEnumeration HttpResponse.Status enumerates the status codes for an HTTP response message.
-
Constructor Summary
Constructors Constructor and Description HttpResponse(java.net.Socket theSocket)Construct a new HTTP response.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidclose()Close this HTTP response.java.io.PrintWritergetPrintWriter()Obtain the print writer for writing the entity body to this HTTP response.voidsetCharset(java.nio.charset.Charset theCharset)Set this HTTP response's character set.voidsetContentLength(int theContentLength)Set this HTTP response's content length.voidsetContentType(java.lang.String theContentType)Set this HTTP response's content type.voidsetHeader(java.lang.String theHeaderName, java.lang.String theHeaderValue)Set the given header in this HTTP response.voidsetStatusCode(HttpResponse.Status theStatusCode)Set this HTTP response's status code.
-
-
-
Constructor Detail
-
HttpResponse
public HttpResponse(java.net.Socket theSocket)
Construct a new HTTP response. The response is written to the output stream of the given socket.- Parameters:
theSocket- Socket.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theSocket is null.
-
-
Method Detail
-
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:
java.lang.NullPointerException- (unchecked exception) Thrown if theStatusCode is null.java.lang.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setContentType
public void setContentType(java.lang.String theContentType)
Set this HTTP response's content type. If not set, the default content type is "text/html".- Parameters:
theContentType- Content type.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theContentType is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if theContentType is zero length.java.lang.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setCharset
public void setCharset(java.nio.charset.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:
java.lang.NullPointerException- (unchecked exception) Thrown if theCharset is null.java.lang.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:
java.lang.IllegalArgumentException- (unchecked exception) Thrown if theContentLength is less than 0.java.lang.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setHeader
public void setHeader(java.lang.String theHeaderName, java.lang.String theHeaderValue)Set the given header in this HTTP response.- Parameters:
theHeaderName- Header name.theHeaderValue- Header value.- Throws:
java.lang.NullPointerException- (unchecked exception) Thrown if theHeaderName or theHeaderValue is null.java.lang.IllegalArgumentException- (unchecked exception) Thrown if theHeaderName or theHeaderValue is zero length.java.lang.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
getPrintWriter
public java.io.PrintWriter getPrintWriter() throws java.io.IOExceptionObtain 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:
java.io.IOException- Thrown if an I/O error occurred.
-
close
public void close() throws java.io.IOExceptionClose this HTTP response. If necessary, the HTTP response headers are written to the socket output stream.- Throws:
java.io.IOException- Thrown if an I/O error occurred.
-
-
DMelt 3.0 © DataMelt by jWork.ORG