java.lang.Object
org.bzdev.ejws.WebMap.Info
- All Implemented Interfaces:
HttpServerResponse
- Enclosing class:
- WebMap
Info for accessing a resource.
an Instance of this class is returned by the WebMap methods
getWebxml, getWelcomeInfo, getInfoFromPath, getErrorInfo, and
getInfo.
This class can be used in two modes:
- In the first mode, for successful requests, the Info
object will provide an
InputStream
that will allow an object to be read. If the response is an HTTP redirect, the input stream should be null and the length should be -1, and the location should not be null. The methodsetRedirect(boolean)
must be called with an argument equal totrue
if the response is an HTTP redirect. If a content-encoding (e.g., gzip) is used, the methodsetEncoding(String)
must be called. The input stream must then provide encoded data. All the other values can be set using the constructorInfo(InputStream,long,String,String)
. - In the second mode, one will create an instance of
WebMap.Info
by calling the constructorInfo(RequestInfo)
, and then using several methods to create a response:-
setHeader(String,String)
oraddHeader(String,String)
as many times as needed to create most response headers. -
sendResponseHeaders(int,long)
to create the content-length header and to set the response code (this should be done after the other headers were created). -
getOutputStream()
to get the output stream used to write the response. The length of the response must match the one provided in the (previous) call tosendResponseHeaders(int,long)
, and the stream should be closed when the response has been written.
-
WebMap.Info
methods are used by FileHandler
and typically not by subclasses of WebMap
.
When compatibility with servlets is desired, the second mode should
be used, and the methods used should be those that are specified
by the interface HttpServerResponse
.-
Constructor Summary
ConstructorsConstructorDescriptionInfo
(InputStream is, long length, String mimeType, String location) Constructor.Info
(WebMap.RequestInfo rinfo) Constructor given aWebMap.RequestInfo
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCookie
(ServerCookie cookie) Add a cookie to the response headers.void
Add a response header.boolean
containsHeader
(String name) Determine if a response header with the specified name exists.encodeRedirectURL
(String url) Encode a URL for use inHttpServerResponse.sendRedirect(String)
.Encode a URL so that it includes a session ID if necessary.boolean
Indicate if a response uses compression.Get the current encoding.Get the names of the current response header names.getHeaders
(String name) Get the headers with a given name.Get an input stream to read a resourcelong
Get the length of a resource.Get a location for an HTTP redirect.Get the MIME type associated with a resource.Get the output stream for responses.boolean
Indicate if a response is an HTTP redirect.boolean
Check if the response was generated and sent.boolean
Determine if this response has been committed.void
sendError
(int rc) Sends a response with a given HTML response code for an error with no content.void
sendRedirect
(String location) Sends an HTTP redirect response with a given location.void
sendResponseHeaders
(int code, long length) Set the response code and content length.void
setEncoding
(String encoding) Set whether or not a response uses compression or some other transfer encoding.void
Set the value of a response header that has a single value.void
setRedirect
(boolean value) Specify if the response is an HTTP redirect.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.bzdev.net.HttpServerResponse
addDateHeader, addIntHeader, setDateHeader, setIntHeader
-
Constructor Details
-
Info
Constructor. For an HTTP redirect, the location must not be null, the length should be -1, the input stream should be null, andsetRedirect(boolean)
must be called.- Parameters:
is
- an input stream provided an HTML resourcelength
- the number of bytes in the resource; 0 if unknown and -1 if no response data will be sentmimeType
- the media type of the resourcelocation
- the location for an HTTP redirect; null if there is none
-
Info
Constructor given aWebMap.RequestInfo
instance.- Parameters:
rinfo
- aWebMap.RequestInfo
used to complete an HTTP transaction
-
-
Method Details
-
setRedirect
public void setRedirect(boolean value) Specify if the response is an HTTP redirect. The default value is false. The value set is ignored if the constructorInfo(org.bzdev.ejws.WebMap.RequestInfo)
was used so thatsendResponseHeaders(int,long)
sets the response status code.- Parameters:
value
- true if the response is an HTTP redirect; false otherwise
-
getRedirect
public boolean getRedirect()Indicate if a response is an HTTP redirect. The value returned is ignored if the constructorInfo(org.bzdev.ejws.WebMap.RequestInfo)
was used so thatsendResponseHeaders(int,long)
sets the response status code.- Returns:
- true if the response is a redirect; false otherwise.
-
setEncoding
Set whether or not a response uses compression or some other transfer encoding.- Specified by:
setEncoding
in interfaceHttpServerResponse
- Parameters:
encoding
- the encoding ("identity", "gzip", "compress", and "deflate" are standard values); null defaults to "identity"
-
getEncoded
public boolean getEncoded()Indicate if a response uses compression.- Returns:
- true if the response uses gzip compression; false otherwise
-
getEncoding
Get the current encoding.- Returns:
- the current encoding; null if there is none defined.
-
setHeader
Set the value of a response header that has a single value.- Specified by:
setHeader
in interfaceHttpServerResponse
- Parameters:
key
- the key for the headervalue
- the value for the header- Throws:
IllegalStateException
- this object's constructor's argument was not an instance of WebMap.RequestInfo
-
addHeader
Add a response header. A new header will be added in addition to any headers with the same name.- Specified by:
addHeader
in interfaceHttpServerResponse
- Parameters:
key
- the key for the headervalue
- a value to add to the header- Throws:
IllegalStateException
- this object's constructor's argument was not an instance of WebMap.RequestInfo
-
encodeURL
Description copied from interface:HttpServerResponse
Encode a URL so that it includes a session ID if necessary. This method is needed by the Servlet specification where encoding may be necessary to include a session ID.- Specified by:
encodeURL
in interfaceHttpServerResponse
- Parameters:
url
- the URL- Returns:
- the encoded URL
-
encodeRedirectURL
Description copied from interface:HttpServerResponse
Encode a URL for use inHttpServerResponse.sendRedirect(String)
. This method is needed by the Servlet specification where encoding may be necessary to include a session ID.- Specified by:
encodeRedirectURL
in interfaceHttpServerResponse
- Parameters:
url
- the URL- Returns:
- the encoded URL
-
getHeaders
Description copied from interface:HttpServerResponse
Get the headers with a given name.- Specified by:
getHeaders
in interfaceHttpServerResponse
- Parameters:
name
- the name- Returns:
- a collection of the header values for a given name
- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
getHeaderNames
Description copied from interface:HttpServerResponse
Get the names of the current response header names.- Specified by:
getHeaderNames
in interfaceHttpServerResponse
- Returns:
- a collection of the response header names
- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
containsHeader
Description copied from interface:HttpServerResponse
Determine if a response header with the specified name exists.- Specified by:
containsHeader
in interfaceHttpServerResponse
- Parameters:
name
- the header name- Returns:
- true if the header exists; false otherwise
- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
addCookie
Description copied from interface:HttpServerResponse
Add a cookie to the response headers.- Specified by:
addCookie
in interfaceHttpServerResponse
- Parameters:
cookie
- the cookie- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
sendRedirect
Description copied from interface:HttpServerResponse
Sends an HTTP redirect response with a given location.- Specified by:
sendRedirect
in interfaceHttpServerResponse
- Parameters:
location
- a URL providing the location- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
sendError
Description copied from interface:HttpServerResponse
Sends a response with a given HTML response code for an error with no content. If this method is called,HttpServerResponse.sendResponseHeaders(int,long)
should not be called.- Specified by:
sendError
in interfaceHttpServerResponse
- Parameters:
rc
- the response code- Throws:
IllegalStateException
- an implementation-dependent error occurred (forWebMap.Info
the constructor was not given an instance ofWebMap.RequestInfo
)
-
sendResponseHeaders
Set the response code and content length. If the length is 0, indicating that the length is not known, chunked transfer encoding is automatically used. As a result, one should generally avoid the explicit use of the transfer-encoding header.- Specified by:
sendResponseHeaders
in interfaceHttpServerResponse
- Parameters:
code
- the response codelength
- the length of the response; 0 if the length is not known, and -1 if there is no response.- Throws:
IllegalStateException
- this object's constructor's argument was not an instance of WebMap.RequestInfo
-
isCommitted
public boolean isCommitted()Description copied from interface:HttpServerResponse
Determine if this response has been committed. A response is committed if its status code and headers have been written.- Specified by:
isCommitted
in interfaceHttpServerResponse
- Returns:
- true if this response has been committed; false otherwise
-
getOutputStream
Get the output stream for responses.If the request method was HEAD, everything written to this stream will be discarded.
- Specified by:
getOutputStream
in interfaceHttpServerResponse
- Returns:
- the output stream.
- Throws:
IllegalStateException
- this object's constructor's argument was not an instance of WebMap.RequestInfoIOException
- an IO error occurred
-
handledResponse
public boolean handledResponse()Check if the response was generated and sent.- Returns:
- true if the response was sent; false otherwise
-
getLength
public long getLength()Get the length of a resource.- Returns:
- the resource length; 0 if unknown and -1 if no response is sent.
-
getInputStream
Get an input stream to read a resource- Returns:
- an input stream; null if not found or if an HTTP
redirect should be used instead or if
handledResponse()
would return true
-
getMIMEType
Get the MIME type associated with a resource.- Returns:
- the MIME type; null if unknown
-
getLocation
Get a location for an HTTP redirect.- Returns:
- the location; null if there is none
-