public abstract static class H.Response<T extends H.Response> extends Object
| Modifier and Type | Field and Description |
|---|---|
protected OutputStream |
outputStream |
protected Writer |
writer |
| Constructor and Description |
|---|
Response() |
| Modifier and Type | Method and Description |
|---|---|
protected abstract Class<T> |
_impl()
Returns the class of the implementation.
|
protected abstract void |
_setContentType(String type)
Sub class to overwrite this method to set content type to
the response
|
protected abstract void |
_setLocale(Locale loc)
Sets the locale of the response, setting the headers (including the
Content-Type's charset) as appropriate.
|
abstract void |
addCookie(H.Cookie cookie)
Adds the specified cookie to the response.
|
abstract T |
addHeader(String name,
String value)
Adds a response header with the given name and value.
|
T |
addHeaderIfNotAdded(String name,
String value)
Adds a response header with given name and value if the header
with the same name has not been added yet
|
abstract String |
characterEncoding()
Returns the name of the character encoding (MIME charset)
used for the body sent in this response.
|
abstract T |
characterEncoding(String encoding)
Returns the content type used for the MIME body
sent in this response.
|
abstract void |
commit()
Calling this method commits the response, meaning the status
code and headers will be written to the client
|
abstract boolean |
containsHeader(String name)
Returns a boolean indicating whether the named response header
has already been set.
|
T |
contentDisposition(String filename,
boolean inline) |
abstract T |
contentLength(long len)
Set the length of the content to be write to the response
|
T |
contentType(String type)
Sets the content type of the response being sent to
the client.
|
<CONTEXT> CONTEXT |
context()
Get the context object from the response instance
|
T |
context(Object context)
Attach a context object to the response instance
|
protected abstract OutputStream |
createOutputStream() |
static <T extends H.Response> |
current()
Return a request instance of the current execution context,
For example from a
ThreadLocal |
static <T extends H.Response> |
current(T response)
Set a request instance into the current execution context,
for example into a
ThreadLocal |
T |
etag(String etag)
Set the etag header
|
abstract T |
header(String name,
String value)
Sets a response header with the given name and value.
|
T |
initContentType(String type)
This method set the content type to the response if there
is no content type been set already.
|
abstract Locale |
locale()
Returns the locale assigned to the response.
|
T |
locale(Locale locale) |
protected T |
me() |
OutputStream |
outputStream()
Returns the output stream to write to the response
|
T |
prepareDownload(String filename)
This method will prepare response header for file download.
|
PrintWriter |
printWriter()
Returns a print writer to write to the response
|
abstract T |
sendError(int sc)
Sends an error response to the client using the specified status
code and clearing the buffer.
|
abstract T |
sendError(int sc,
String msg)
Sends an error response to the client using the specified
status.
|
T |
sendError(int sc,
String msg,
Object... args)
Sames as
sendError(int, String) but accept message format
arguments |
abstract T |
sendRedirect(String location)
Sends a temporary redirect response to the client using the
specified redirect location URL.
|
T |
status(H.Status s)
Sets the status for this response.
|
abstract T |
status(int sc)
Sets the status code for this response.
|
T |
writeBinary(org.osgl.storage.ISObject binary) |
abstract T |
writeContent(ByteBuffer buffer) |
T |
writeContent(String s)
Write a string to the response
|
T |
writeHtml(String content)
Write content to the response
|
T |
writeJSON(String content)
Write content to the response
|
Writer |
writer()
Returns the writer to write to the response
|
boolean |
writerCreated() |
T |
writeText(String content)
Write content to the response
|
protected volatile OutputStream outputStream
protected volatile Writer writer
public T context(Object context)
context - the context objectpublic <CONTEXT> CONTEXT context()
CONTEXT - the generic type of the context objectprotected abstract Class<T> _impl()
public boolean writerCreated()
protected abstract OutputStream createOutputStream()
public OutputStream outputStream() throws IllegalStateException, org.osgl.exception.UnexpectedIOException
IllegalStateException - if
writer() is called alreadyorg.osgl.exception.UnexpectedIOException - if
there are output exceptionpublic Writer writer() throws IllegalStateException, org.osgl.exception.UnexpectedIOException
IllegalStateException - if outputStream() is called alreadyorg.osgl.exception.UnexpectedIOException - if there are output exceptionpublic PrintWriter printWriter()
IllegalStateException - if outputStream() is called alreadyorg.osgl.exception.UnexpectedIOException - if there are output exceptionpublic abstract String characterEncoding()
characterEncoding(String) or
contentType(String) methods, or implicitly using the
locale(java.util.Locale) method. Explicit specifications take
precedence over implicit specifications. Calls made
to these methods after getWriter has been
called or after the response has been committed have no
effect on the character encoding. If no character encoding
has been specified, ISO-8859-1 is returned.
See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt) for more information about character encoding and MIME.
String specifying the
name of the character encoding, for
example, UTF-8public abstract T characterEncoding(String encoding)
contentType(String)
before the response is committed. If no content type
has been specified, this method returns null.
If a content type has been specified, and a
character encoding has been explicitly or implicitly
specified as described in characterEncoding()
or writer() has been called,
the charset parameter is included in the string returned.
If no character encoding has been specified, the
charset parameter is omitted.encoding - the encodingString specifying the
content type, for example,
text/html; charset=UTF-8,
or nullpublic abstract T contentLength(long len)
len - an long value specifying the length of the
content being returned to the client; sets
the Content-Length headeroutputStream,
writerprotected abstract void _setContentType(String type)
type - a String specifying the MIME
type of the contentpublic T contentType(String type)
text/html; charset=ISO-8859-4.
If content type has already been set to the response, this method
will update the content type with the new value
this method must be called before calling writer()
or outputStream()
type - a String specifying the MIME
type of the contentoutputStream,
writer,
initContentType(String)public T initContentType(String type)
type - a String specifying the MIME
type of the contentcontentType(String)public T prepareDownload(String filename)
filename - the filenamepublic T etag(String etag)
etag - the etag contentprotected abstract void _setLocale(Locale loc)
writer(). By default, the response locale
is the default locale for the server.loc - the locale of the responselocale()public abstract Locale locale()
locale(java.util.Locale)public abstract void addCookie(H.Cookie cookie)
cookie - the Cookie to return to the clientpublic abstract boolean containsHeader(String name)
name - the header nametrue if the named response header
has already been set;
false otherwisepublic abstract T sendError(int sc, String msg)
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
sc - the error status codemsg - the descriptive messageorg.osgl.exception.UnexpectedIOException - If an input or output exception occursIllegalStateException - If the response was committedpublic T sendError(int sc, String msg, Object... args)
sendError(int, String) but accept message format
argumentssc - the error status codemsg - the descriptive message templateargs - the descriptive message argumentsorg.osgl.exception.UnexpectedIOException - If an input or output exception occursIllegalStateException - If the response was committedpublic abstract T sendError(int sc)
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
sc - the error status codeorg.osgl.exception.UnexpectedIOException - If the response was committed before this method callpublic abstract T sendRedirect(String location)
If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.
location - the redirect location URLorg.osgl.exception.UnexpectedIOException - If the response was committed before this method callIllegalStateException - If the response was committed or
if a partial URL is given and cannot be converted into a valid URLpublic abstract T header(String name, String value)
containsHeader method can be
used to test for the presence of a header before setting its
value.name - the name of the headervalue - the header value If it contains octet string,
it should be encoded according to RFC 2047
(http://www.ietf.org/rfc/rfc2047.txt)containsHeader(java.lang.String),
addHeader(java.lang.String, java.lang.String)public abstract T status(int sc)
sendError method should be used
instead.
The container clears the buffer and sets the Location header, preserving cookies and other headers.
sc - the status codesendError(int, java.lang.String),
status(int)public T status(H.Status s)
sendError method should be used
instead.
The container clears the buffer and sets the Location header, preserving cookies and other headers.
s - the statussendError(int, java.lang.String)public abstract T addHeader(String name, String value)
name - the name of the headervalue - the additional header value If it contains
octet string, it should be encoded
according to RFC 2047
(http://www.ietf.org/rfc/rfc2047.txt)header(String, String)public T addHeaderIfNotAdded(String name, String value)
name - the name of the headervalue - the header valueaddHeader(String, String)public T writeBinary(org.osgl.storage.ISObject binary)
public T writeContent(String s)
s - the string to write to the responsepublic abstract T writeContent(ByteBuffer buffer)
public T writeText(String content)
content - the content to writepublic T writeHtml(String content)
content - the content to writepublic T writeJSON(String content)
content - the content to writepublic abstract void commit()
public static <T extends H.Response> T current()
ThreadLocalT - the response typepublic static <T extends H.Response> void current(T response)
ThreadLocalT - the sub type of responseresponse - the request to be set to current execution contextprotected T me()
Copyright © 2017. All Rights Reserved.