Package helma.framework
Class ResponseTrans
- java.lang.Object
-
- java.io.Writer
-
- helma.framework.ResponseTrans
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.io.Serializable
,java.lang.Appendable
,java.lang.AutoCloseable
public final class ResponseTrans extends java.io.Writer implements java.io.Serializable
A Transmitter for a response to the servlet client. Objects of this class are directly exposed to JavaScript as global property res.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ResponseTrans(Application app, RequestTrans req)
Creates a new ResponseTrans object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDateHeader(java.lang.String name, java.util.Date value)
Proxy to HttpServletResponse.addDateHeader()void
addHeader(java.lang.String name, java.lang.String value)
Proxy to HttpServletResponse.addHeader()void
cacheSkin(java.lang.Object id, Skin skin)
Cache a skin for the length of this response.void
close()
This has to be called after writing to this response has finished and before it is shipped back to the web server.void
close(java.lang.String defaultCharset)
This has to be called after writing to this response has finished and before it is shipped back to the web server.int
countCookies()
Get the number of cookies set in this response.void
debug(java.lang.Object message)
Insert string somewhere in the response buffer.void
dependsOn(java.lang.Object what)
Add a dependency to this response.void
digestDependencies()
Digest all dependencies to a checksum to see if the response has changed.void
encode(java.lang.Object what)
Replace special characters with entities, including <, > and ", thus allowing no HTML tags.void
encodeForm(java.lang.Object what)
Encode HTML entities, but leave newlines alone.void
encodeXml(java.lang.Object what)
Replace special characters with entities, including <, > and ", thus allowing no HTML tags.void
flush()
void
format(java.lang.Object what)
Replace special characters with entities but pass through HTML tagsvoid
forward(java.lang.String url)
java.lang.Object
get(java.lang.String name)
Get a value from the responses map by key.Skin
getActiveSkin()
Return the skin currently being rendered, or none.java.lang.StringBuffer
getBuffer()
Get the response buffer, creating it if it doesn't existSkin
getCachedSkin(java.lang.Object id)
Look up a cached skin.java.lang.String
getCharset()
Get the charset/encoding for this responsebyte[]
getContent()
Get the body content for this response as byte array, encoded using the response's charset.int
getContentLength()
Get the number of bytes of the response body.java.lang.String
getContentType()
Get the response's MIME content typeCookieTrans[]
getCookies()
Get the cookies set in this response.java.lang.StringBuffer
getDebugBuffer()
Get debug messages to append to the response, if any.java.lang.Throwable
getError()
Get the error message to display to the user, if any.java.lang.String
getErrorMessage()
java.lang.String
getETag()
Get the ETag header value for this response.java.lang.String
getForward()
long
getLastModified()
Get the value of the Last-Modified header for this response.java.util.Map
getMacroHandlers()
Get the macro handlers map for this response transmitter.java.lang.String
getMessage()
Get the message to display to the user, if any.java.util.Map
getMetaData()
Get the meta info map for this response transmitter.boolean
getNotModified()
Check if this response should generate a Not-Modified response.java.lang.String
getRealm()
Get the HTTP authentication realmjava.lang.String
getRedirect()
java.util.Map
getResponseData()
Get the data map for this response transmitter.javax.servlet.http.HttpServletResponse
getServletResponse()
Returns the ServletResponse instance for this ResponseTrans.java.lang.Object[]
getSkinpath()
Get the path in which to look for skins.int
getStatus()
Get the HTTP response status codeboolean
isCacheable()
Returns true if this response may be cached by the clientjava.lang.StringBuffer
popBuffer()
java.lang.String
popString()
Returns the content of the current string buffer and switches back to the previos one.java.lang.StringBuffer
pushBuffer(java.lang.StringBuffer buf)
This is called before a skin is rendered as string (renderSkinAsString) to redirect the output to a new string buffer.void
redirect(java.lang.String url)
void
reportError(java.lang.String errorMessage)
Write a vanilla error report.void
reportError(java.lang.Throwable throwable)
Write a vanilla error report.void
reset()
Reset the response object to its initial empty state.void
resetBuffer()
Reset the current response buffer.void
resetCookies()
Reset all previously set cookies.void
setCacheable(boolean cache)
Set the cacheability of this responsevoid
setCharset(java.lang.String charset)
Set the charset/encoding for this responsevoid
setContentType(java.lang.String contentType)
Set the response's MIME content typevoid
setCookie(java.lang.String key, java.lang.String value, int days, java.lang.String path, java.lang.String domain)
Set a cookie.void
setDateHeader(java.lang.String name, java.util.Date value)
Proxy to HttpServletResponse.setDateHeader()void
setDebugBuffer(java.lang.StringBuffer debugBuffer)
Set debug messages to append to the response.void
setError(java.lang.Throwable error)
Set a message to display to the user.void
setETag(java.lang.String value)
Set the ETag header value for this response.void
setHeader(java.lang.String name, java.lang.String value)
Proxy to HttpServletResponse.setHeader()void
setLastModified(long modified)
Set the Last-Modified header for this responsevoid
setMessage(java.lang.String message)
Set a message to display to the user.void
setRealm(java.lang.String realm)
Set the HTTP authentication realmvoid
setSkinpath(java.lang.Object[] arr)
Set the path in which to look for skins.void
setStatus(int status)
Set the HTTP response status codeSkin
switchActiveSkin(Skin skin)
Set the skin currently being rendered, returning the previously active skin.void
waitForClose()
If we just attached to evaluation we call this instead of close because only the primary thread is responsible for closing the resultvoid
write(char[] chars)
Appends a char array to the response buffer.void
write(char[] chars, int offset, int length)
Appends a part from a char array to the response buffer.void
write(int c)
Appends a signle character to the response buffer.void
write(java.lang.Object what)
Appends a objct to the response unchanged.void
write(java.lang.String str)
Append a string to the response unchanged.void
write(java.lang.String str, int offset, int length)
Appends a part from a string to the response buffer.void
writeBinary(byte[] bytes)
Allow to directly set the byte array for the response.void
writeln()
Writes a platform dependent newline sequence to response buffer.void
writeln(java.lang.Object what)
Write object to response buffer and append a platform dependent newline sequence.void
writeXmlRpcError(java.lang.Exception x)
void
writeXmlRpcResponse(java.lang.Object result)
-
-
-
Constructor Detail
-
ResponseTrans
public ResponseTrans(Application app, RequestTrans req)
Creates a new ResponseTrans object.- Parameters:
req
- the RequestTrans for this response
-
-
Method Detail
-
get
public java.lang.Object get(java.lang.String name)
Get a value from the responses map by key.
-
getResponseData
public java.util.Map getResponseData()
Get the data map for this response transmitter.
-
getMacroHandlers
public java.util.Map getMacroHandlers()
Get the macro handlers map for this response transmitter.
-
getMetaData
public java.util.Map getMetaData()
Get the meta info map for this response transmitter.
-
getServletResponse
public javax.servlet.http.HttpServletResponse getServletResponse()
Returns the ServletResponse instance for this ResponseTrans. Returns null for internal and XML-RPC requests.
-
resetBuffer
public void resetBuffer()
Reset the current response buffer.
-
reset
public void reset()
Reset the response object to its initial empty state.
-
pushBuffer
public java.lang.StringBuffer pushBuffer(java.lang.StringBuffer buf)
This is called before a skin is rendered as string (renderSkinAsString) to redirect the output to a new string buffer.- Parameters:
buf
- the StringBuffer to use, or null- Returns:
- the new StringBuffer instance
-
popString
public java.lang.String popString()
Returns the content of the current string buffer and switches back to the previos one.
-
popBuffer
public java.lang.StringBuffer popBuffer()
-
getBuffer
public java.lang.StringBuffer getBuffer()
Get the response buffer, creating it if it doesn't exist
-
write
public void write(java.lang.String str)
Append a string to the response unchanged.- Overrides:
write
in classjava.io.Writer
-
write
public void write(java.lang.Object what)
Appends a objct to the response unchanged. The object is first converted to a string.
-
write
public void write(char[] chars, int offset, int length)
Appends a part from a char array to the response buffer.- Specified by:
write
in classjava.io.Writer
- Parameters:
chars
-offset
-length
-
-
write
public void write(char[] chars)
Appends a char array to the response buffer.- Overrides:
write
in classjava.io.Writer
- Parameters:
chars
-
-
write
public void write(int c)
Appends a signle character to the response buffer.- Overrides:
write
in classjava.io.Writer
- Parameters:
c
-
-
write
public void write(java.lang.String str, int offset, int length)
Appends a part from a string to the response buffer.- Overrides:
write
in classjava.io.Writer
- Parameters:
str
-offset
-length
-
-
writeln
public void writeln(java.lang.Object what)
Write object to response buffer and append a platform dependent newline sequence.
-
writeln
public void writeln()
Writes a platform dependent newline sequence to response buffer.
-
debug
public void debug(java.lang.Object message)
Insert string somewhere in the response buffer. Caller has to make sure that buffer exists and its length is larger than offset. str may be null, in which case nothing happens.
-
encode
public void encode(java.lang.Object what)
Replace special characters with entities, including <, > and ", thus allowing no HTML tags.
-
format
public void format(java.lang.Object what)
Replace special characters with entities but pass through HTML tags
-
encodeXml
public void encodeXml(java.lang.Object what)
Replace special characters with entities, including <, > and ", thus allowing no HTML tags.
-
encodeForm
public void encodeForm(java.lang.Object what)
Encode HTML entities, but leave newlines alone. This is for the content of textarea forms.
-
redirect
public void redirect(java.lang.String url) throws RedirectException
- Parameters:
url
- ...- Throws:
RedirectException
- ...
-
getRedirect
public java.lang.String getRedirect()
- Returns:
- ...
-
forward
public void forward(java.lang.String url) throws RedirectException
- Parameters:
url
- ...- Throws:
RedirectException
- ...
-
getForward
public java.lang.String getForward()
- Returns:
- ...
-
writeBinary
public void writeBinary(byte[] bytes)
Allow to directly set the byte array for the response. Calling this more than once will overwrite the previous output.- Parameters:
bytes
- an arbitrary byte array
-
addHeader
public void addHeader(java.lang.String name, java.lang.String value)
Proxy to HttpServletResponse.addHeader()- Parameters:
name
- the header namevalue
- the header value
-
addDateHeader
public void addDateHeader(java.lang.String name, java.util.Date value)
Proxy to HttpServletResponse.addDateHeader()- Parameters:
name
- the header namevalue
- the header value
-
setHeader
public void setHeader(java.lang.String name, java.lang.String value)
Proxy to HttpServletResponse.setHeader()- Parameters:
name
- the header namevalue
- the header value
-
setDateHeader
public void setDateHeader(java.lang.String name, java.util.Date value)
Proxy to HttpServletResponse.setDateHeader()- Parameters:
name
- the header namevalue
- the header value
-
reportError
public void reportError(java.lang.Throwable throwable)
Write a vanilla error report. Callers should make sure the ResponeTrans is new or has been reset.- Parameters:
throwable
- the error
-
reportError
public void reportError(java.lang.String errorMessage)
Write a vanilla error report. Callers should make sure the ResponeTrans is new or has been reset.- Parameters:
errorMessage
- the error message
-
writeXmlRpcResponse
public void writeXmlRpcResponse(java.lang.Object result)
-
writeXmlRpcError
public void writeXmlRpcError(java.lang.Exception x)
-
flush
public void flush()
- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classjava.io.Writer
-
close
public void close() throws java.io.UnsupportedEncodingException
This has to be called after writing to this response has finished and before it is shipped back to the web server. Transforms the string buffer into a byte array for transmission.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
- Throws:
java.io.UnsupportedEncodingException
-
close
public void close(java.lang.String defaultCharset) throws java.io.UnsupportedEncodingException
This has to be called after writing to this response has finished and before it is shipped back to the web server. Transforms the string buffer into a byte array for transmission.- Parameters:
defaultCharset
- the charset to use if no explicit charset has been set on the response- Throws:
java.io.UnsupportedEncodingException
- if the charset is not a valid encoding name
-
waitForClose
public void waitForClose()
If we just attached to evaluation we call this instead of close because only the primary thread is responsible for closing the result
-
getContent
public byte[] getContent()
Get the body content for this response as byte array, encoded using the response's charset.- Returns:
- the response body
-
getContentLength
public int getContentLength()
Get the number of bytes of the response body.- Returns:
- the length of the response body
-
getContentType
public java.lang.String getContentType()
Get the response's MIME content type- Returns:
- the MIME type for this response
-
setContentType
public void setContentType(java.lang.String contentType)
Set the response's MIME content type- Parameters:
contentType
- MIME type for this response
-
setLastModified
public void setLastModified(long modified)
Set the Last-Modified header for this response- Parameters:
modified
- the Last-Modified header in milliseconds
-
getLastModified
public long getLastModified()
Get the value of the Last-Modified header for this response.- Returns:
- the Last-Modified header in milliseconds
-
setETag
public void setETag(java.lang.String value)
Set the ETag header value for this response.- Parameters:
value
- the ETag header value
-
getETag
public java.lang.String getETag()
Get the ETag header value for this response.- Returns:
- the ETag header value
-
getNotModified
public boolean getNotModified()
Check if this response should generate a Not-Modified response.- Returns:
- true if the the response wasn't modified since the client last saw it.
-
dependsOn
public void dependsOn(java.lang.Object what)
Add a dependency to this response.- Parameters:
what
- an item this response's output depends on.
-
digestDependencies
public void digestDependencies()
Digest all dependencies to a checksum to see if the response has changed.
-
setSkinpath
public void setSkinpath(java.lang.Object[] arr)
Set the path in which to look for skins. This may contain file locations and HopObjects.- Parameters:
arr
- the skin path
-
getSkinpath
public java.lang.Object[] getSkinpath()
Get the path in which to look for skins. This may contain file locations and HopObjects.- Returns:
- the skin path
-
getCachedSkin
public Skin getCachedSkin(java.lang.Object id)
Look up a cached skin.- Parameters:
id
- the skin key- Returns:
- the skin, or null if no skin is cached for the given key
-
cacheSkin
public void cacheSkin(java.lang.Object id, Skin skin)
Cache a skin for the length of this response.- Parameters:
id
- the skin keyskin
- the skin to cache
-
switchActiveSkin
public Skin switchActiveSkin(Skin skin)
Set the skin currently being rendered, returning the previously active skin.- Parameters:
skin
- the new active skin- Returns:
- the previously active skin
-
getActiveSkin
public Skin getActiveSkin()
Return the skin currently being rendered, or none.- Returns:
- the currently active skin
-
setCookie
public void setCookie(java.lang.String key, java.lang.String value, int days, java.lang.String path, java.lang.String domain)
Set a cookie.- Parameters:
key
- the cookie keyvalue
- the cookie valuedays
- the cookie's lifespan in dayspath
- the URL path to apply the cookie todomain
- the domain to apply the cookie to
-
resetCookies
public void resetCookies()
Reset all previously set cookies.
-
countCookies
public int countCookies()
Get the number of cookies set in this response.- Returns:
- the number of cookies
-
getCookies
public CookieTrans[] getCookies()
Get the cookies set in this response.- Returns:
- the cookies
-
getMessage
public java.lang.String getMessage()
Get the message to display to the user, if any.- Returns:
- the message
-
setMessage
public void setMessage(java.lang.String message)
Set a message to display to the user.- Parameters:
message
- the message
-
getError
public java.lang.Throwable getError()
Get the error message to display to the user, if any.- Returns:
- the error message
-
setError
public void setError(java.lang.Throwable error)
Set a message to display to the user.- Parameters:
error
- the error message
-
getErrorMessage
public java.lang.String getErrorMessage()
-
getDebugBuffer
public java.lang.StringBuffer getDebugBuffer()
Get debug messages to append to the response, if any.- Returns:
- the response's debug buffer
-
setDebugBuffer
public void setDebugBuffer(java.lang.StringBuffer debugBuffer)
Set debug messages to append to the response.- Parameters:
debugBuffer
- the response's debug buffer
-
getCharset
public java.lang.String getCharset()
Get the charset/encoding for this response- Returns:
- the charset name
-
setCharset
public void setCharset(java.lang.String charset)
Set the charset/encoding for this response- Parameters:
charset
- the charset name
-
isCacheable
public boolean isCacheable()
Returns true if this response may be cached by the client- Returns:
- true if the response may be cached
-
setCacheable
public void setCacheable(boolean cache)
Set the cacheability of this response- Parameters:
cache
- true if the response may be cached
-
getStatus
public int getStatus()
Get the HTTP response status code- Returns:
- the HTTP response code
-
setStatus
public void setStatus(int status)
Set the HTTP response status code- Parameters:
status
- the HTTP response code
-
getRealm
public java.lang.String getRealm()
Get the HTTP authentication realm- Returns:
- the name of the authentication realm
-
setRealm
public void setRealm(java.lang.String realm)
Set the HTTP authentication realm- Parameters:
realm
- the name of the authentication realm
-
-