Package edu.rit.http
Class HttpResponse
java.lang.Object
edu.rit.http.HttpResponse
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:
-
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.
- Version:
- 29-Jul-2010
- Author:
- Alan Kaminsky
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration HttpResponse.Status enumerates the status codes for an HTTP response message. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this HTTP response.Obtain the print writer for writing the entity body to this HTTP response.voidsetCharset(Charset theCharset) Set this HTTP response's character set.voidsetContentLength(int theContentLength) Set this HTTP response's content length.voidsetContentType(String theContentType) Set this HTTP response's content type.voidSet the given header in this HTTP response.voidsetStatusCode(HttpResponse.Status theStatusCode) Set this HTTP response's status code.
-
Constructor Details
-
HttpResponse
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 iftheSocketis null.
-
-
Method Details
-
setStatusCode
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 iftheStatusCodeis null.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setContentType
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 iftheContentTypeis null.IllegalArgumentException- (unchecked exception) Thrown iftheContentTypeis zero length.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setCharset
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 iftheCharsetis 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 iftheContentLengthis less than 0.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
setHeader
Set the given header in this HTTP response.- Parameters:
theHeaderName- Header name.theHeaderValue- Header value.- Throws:
NullPointerException- (unchecked exception) Thrown iftheHeaderNameortheHeaderValueis null.IllegalArgumentException- (unchecked exception) Thrown iftheHeaderNameortheHeaderValueis zero length.IllegalStateException- (unchecked exception) Thrown if the HTTP response headers have already been written to the socket output stream.
-
getPrintWriter
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
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.
-