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
Modifier and TypeClassDescriptionstatic enum
Enumeration HttpResponse.Status enumerates the status codes for an HTTP response message. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this HTTP response.Obtain the print writer for writing the entity body to this HTTP response.void
setCharset
(Charset theCharset) Set this HTTP response's character set.void
setContentLength
(int theContentLength) Set this HTTP response's content length.void
setContentType
(String theContentType) Set this HTTP response's content type.void
Set the given header in this HTTP response.void
setStatusCode
(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 iftheSocket
is 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 iftheStatusCode
is 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 iftheContentType
is null.IllegalArgumentException
- (unchecked exception) Thrown iftheContentType
is 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 iftheCharset
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 iftheContentLength
is 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 iftheHeaderName
ortheHeaderValue
is null.IllegalArgumentException
- (unchecked exception) Thrown iftheHeaderName
ortheHeaderValue
is 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.
-