public final class RsFlash extends RsWrap
This class helps you to automate the flash message mechanism, by adding flash messages to your responses.
The flash concept is taken from Ruby on Rails, it is actually the ability to pass temporary variables between requests which is particularly helpful especially in case of a redirect.
The flash message mechanism is meant to be used in case you have a dynamic content to render in which you want to add success or error messages. The typical use case is when you have a form that the user can submit and you want to be able to indicate whether the request was successful or not.
The flash mechanism is a stateful mechanism based on a cookie so it is not meant to be used to implement stateless components such as a RESTful service.
Here is a simple example that shows how to properly use it:
public final class TkDiscussion implements Take { @Override public Response act(final Request req) throws IOException { return new RsForward(new RsFlash("thanks for the post")); } }
This decorator will add the required "Set-Cookie" header to the response. This is all it is doing. The response is added to the cookie in URL-encoded format, together with the logging level. Flash messages could be of different severity, we're using Java logging levels for that, for example:
public final class TkDiscussion implements Take { @Override public Response act(final Request req) throws IOException { return new RsForward( new RsFlash( "can't save your post, sorry", java.util.logging.Level.SEVERE ) ); } }
This is how the HTTP response will look like (simplified):
HTTP/1.1 303 See Other Set-Cookie: RsFlash=can%27t%20save%20your%20post%2C%20sorry/SEVERE
Here, the name of the cookie is RsFlash
. You can change this
default name using a constructor of RsFlash
.
To clean up the cookie in the following requests, you will need to
decorate your Take
with TkFlash
.
The class is immutable and thread-safe.
Constructor and Description |
---|
RsFlash(CharSequence msg)
Constructs a
RsFlash with the specified message. |
RsFlash(CharSequence msg,
ExpirationDate expires)
Constructs a
RsFlash with the specified message and expiration
date of the cookie. |
RsFlash(CharSequence msg,
Level level)
Constructs a
RsFlash with the specified message and logging
level. |
RsFlash(CharSequence msg,
Level level,
ExpirationDate expires)
Constructs a
RsFlash with the specified message, logging level
and cookie expiration date. |
RsFlash(CharSequence msg,
Level level,
String cookie)
Constructs a
RsFlash with the specified message, logging level
and cookie name. |
RsFlash(CharSequence msg,
Level level,
String cookie,
ExpirationDate expires)
Constructs a
RsFlash with the specified message, logging level
and cookie name. |
RsFlash(Throwable err)
Constructs a
RsFlash with the specified error. |
RsFlash(Throwable err,
ExpirationDate expires)
Constructs a
RsFlash with the specified error and cookie
expiration date. |
RsFlash(Throwable err,
Level level)
Constructs a
RsFlash with the specified error and logging level. |
RsFlash(Throwable err,
Level level,
ExpirationDate expires)
Constructs a
RsFlash with the specified error, logging level
and cookie expiration date. |
Modifier and Type | Method and Description |
---|---|
String |
toString() |
public RsFlash(CharSequence msg) throws UnsupportedEncodingException
RsFlash
with the specified message.
By default it will use RsFlash
as cookie name.
msg
- Message to showUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(CharSequence msg, ExpirationDate expires) throws UnsupportedEncodingException
RsFlash
with the specified message and expiration
date of the cookie.
By default it will use RsFlash
as cookie name.
msg
- Messageexpires
- ExpirationDate of the cookieUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(Throwable err) throws UnsupportedEncodingException
RsFlash
with the specified error.
The error is converted into a flash message by calling
Throwable.getLocalizedMessage()
}.
By default it will use RsFlash
as cookie name and expiration
date of the cookie will be 1 hour after instance creation.
err
- ErrorUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(Throwable err, ExpirationDate expires) throws UnsupportedEncodingException
RsFlash
with the specified error and cookie
expiration date.
The error is converted into a flash message by calling
Throwable.getLocalizedMessage()
}.
By default it will use RsFlash
as cookie name.
err
- Errorexpires
- ExpirationDate of the cookieUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(Throwable err, Level level) throws UnsupportedEncodingException
RsFlash
with the specified error and logging level.
The error is converted into a flash message by calling
Throwable.getLocalizedMessage()
}.
By default it will use RsFlash
as cookie name and expiration
date of the cookie will be 1 hour after instance creation.
err
- Errorlevel
- LevelUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(Throwable err, Level level, ExpirationDate expires) throws UnsupportedEncodingException
RsFlash
with the specified error, logging level
and cookie expiration date.
The error is converted into a flash message by calling
Throwable.getLocalizedMessage()
}.
By default it will use RsFlash
as cookie name.
err
- Errorlevel
- Levelexpires
- ExpirationDate of the cookieUnsupportedEncodingException
- In case the default encoding is
not supportedpublic RsFlash(CharSequence msg, Level level) throws UnsupportedEncodingException
RsFlash
with the specified message and logging
level.
By default it will use RsFlash
as cookie name and default
cookie expiration date will be 1 hour after instance creation.
msg
- Messagelevel
- LevelUnsupportedEncodingException
- In case the default encoding is not
supportedpublic RsFlash(CharSequence msg, Level level, ExpirationDate expires) throws UnsupportedEncodingException
RsFlash
with the specified message, logging level
and cookie expiration date.
By default it will use RsFlash
as cookie name
msg
- Messagelevel
- Levelexpires
- ExpirationDate of the cookieUnsupportedEncodingException
- In case the default encoding is
not supportedpublic RsFlash(CharSequence msg, Level level, String cookie) throws UnsupportedEncodingException
RsFlash
with the specified message, logging level
and cookie name.
By default cookie expiration will be 1 hour after object creation.
msg
- Messagelevel
- Levelcookie
- Cookie nameUnsupportedEncodingException
- In case the default encoding is
not supportedpublic RsFlash(CharSequence msg, Level level, String cookie, ExpirationDate expires) throws UnsupportedEncodingException
RsFlash
with the specified message, logging level
and cookie name.msg
- Messagelevel
- Levelcookie
- Cookie nameexpires
- ExpirationDate of the cookieUnsupportedEncodingException
- In case the default encoding is not
supportedCopyright © 2015–2018 Take. All rights reserved.