public final class RsFlash extends RsWrap
This class helps you to automate flash message mechanism, by adding flash messages to your responses, for example:
public final class TkDiscussion implements Take { @Override public Response act(final Request req) { // save the post to the database return new RsFlash( new RsForward(), "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) { if (failed) { throw new RsFlash( new RsForward(), "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
,
but it's not recommended. It's better to use the default name.
The class is immutable and thread-safe.
public RsFlash(String msg) throws UnsupportedEncodingException
msg
- Message to showUnsupportedEncodingException
- If failspublic RsFlash(Throwable err) throws UnsupportedEncodingException
err
- ErrorUnsupportedEncodingException
- If failspublic RsFlash(Throwable err, Level level) throws UnsupportedEncodingException
err
- Errorlevel
- LevelUnsupportedEncodingException
- If failspublic RsFlash(String msg, Level level) throws UnsupportedEncodingException
msg
- Messagelevel
- LevelUnsupportedEncodingException
- If failspublic RsFlash(String msg, Level level, String cookie) throws UnsupportedEncodingException
msg
- Messagelevel
- Levelcookie
- Cookie nameUnsupportedEncodingException
- If failsCopyright © 2015 Take. All rights reserved.