Class RsFlash
- java.lang.Object
-
- org.takes.rs.RsWrap
-
- org.takes.facets.flash.RsFlash
-
public final class RsFlash extends RsWrap
Forwarding response.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 ofRsFlash
.To clean up the cookie in the following requests, you will need to decorate your
Take
withTkFlash
.The class is immutable and thread-safe.
- Since:
- 0.1
-
-
Constructor Summary
Constructors Constructor Description RsFlash(CharSequence msg)
Constructs aRsFlash
with the specified message.RsFlash(CharSequence msg, Level level)
Constructs aRsFlash
with the specified message and logging level.RsFlash(CharSequence msg, Level level, String cookie)
Constructs aRsFlash
with the specified message, logging level and cookie name.RsFlash(CharSequence msg, Level level, String cookie, Expires.Date expires)
Constructs aRsFlash
with the specified message, logging level and cookie name.RsFlash(CharSequence msg, Level level, Expires.Date expires)
Constructs aRsFlash
with the specified message, logging level and cookie expiration date.RsFlash(CharSequence msg, Expires.Date expires)
Constructs aRsFlash
with the specified message and expiration date of the cookie.RsFlash(Throwable err)
Constructs aRsFlash
with the specified error.RsFlash(Throwable err, Level level)
Constructs aRsFlash
with the specified error and logging level.RsFlash(Throwable err, Level level, Expires.Date expires)
Constructs aRsFlash
with the specified error, logging level and cookie expiration date.RsFlash(Throwable err, Expires.Date expires)
Constructs aRsFlash
with the specified error and cookie expiration date.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
toString()
-
-
-
Constructor Detail
-
RsFlash
public RsFlash(CharSequence msg) throws UnsupportedEncodingException
Constructs aRsFlash
with the specified message.By default it will use
RsFlash
as cookie name.- Parameters:
msg
- Message to show- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported
-
RsFlash
public RsFlash(CharSequence msg, Expires.Date expires) throws UnsupportedEncodingException
Constructs aRsFlash
with the specified message and expiration date of the cookie.By default it will use
RsFlash
as cookie name.- Parameters:
msg
- Messageexpires
- Date of the cookie- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported
-
RsFlash
public RsFlash(Throwable err) throws UnsupportedEncodingException
Constructs aRsFlash
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.- Parameters:
err
- Error- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported
-
RsFlash
public RsFlash(Throwable err, Expires.Date expires) throws UnsupportedEncodingException
Constructs aRsFlash
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.- Parameters:
err
- Errorexpires
- Date of the cookie- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 2.0
-
RsFlash
public RsFlash(Throwable err, Level level) throws UnsupportedEncodingException
Constructs aRsFlash
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.- Parameters:
err
- Errorlevel
- Level- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 0.17
-
RsFlash
public RsFlash(Throwable err, Level level, Expires.Date expires) throws UnsupportedEncodingException
Constructs aRsFlash
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.- Parameters:
err
- Errorlevel
- Levelexpires
- Date of the cookie- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 2.0
-
RsFlash
public RsFlash(CharSequence msg, Level level) throws UnsupportedEncodingException
Constructs aRsFlash
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.- Parameters:
msg
- Messagelevel
- Level- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported
-
RsFlash
public RsFlash(CharSequence msg, Level level, Expires.Date expires) throws UnsupportedEncodingException
Constructs aRsFlash
with the specified message, logging level and cookie expiration date.By default it will use
RsFlash
as cookie name- Parameters:
msg
- Messagelevel
- Levelexpires
- Date of the cookie- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 2.0
-
RsFlash
public RsFlash(CharSequence msg, Level level, String cookie) throws UnsupportedEncodingException
Constructs aRsFlash
with the specified message, logging level and cookie name.By default cookie expiration will be 1 hour after object creation.
- Parameters:
msg
- Messagelevel
- Levelcookie
- Cookie name- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 2.0
-
RsFlash
public RsFlash(CharSequence msg, Level level, String cookie, Expires.Date expires) throws UnsupportedEncodingException
Constructs aRsFlash
with the specified message, logging level and cookie name.- Parameters:
msg
- Messagelevel
- Levelcookie
- Cookie nameexpires
- Date of the cookie- Throws:
UnsupportedEncodingException
- In case the default encoding is not supported- Since:
- 2.0
-
-