diff --git a/src/main/java/org/takes/facets/flash/RsFlash.java b/src/main/java/org/takes/facets/flash/RsFlash.java
index 144cb8c29..1e8d0d6c8 100644
--- a/src/main/java/org/takes/facets/flash/RsFlash.java
+++ b/src/main/java/org/takes/facets/flash/RsFlash.java
@@ -23,8 +23,9 @@
*/
package org.takes.facets.flash;
-import java.nio.charset.StandardCharsets;
+import java.nio.charset.Charset;
import java.util.Date;
+import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
@@ -121,14 +122,15 @@ public RsFlash(final String msg, final Level level, final String cookie) {
new RsWithCookie(
cookie,
DatatypeConverter.printBase64Binary(
- new StringBuilder(level.getName())
+ new StringBuilder(msg)
.append('/')
- .append(msg)
+ .append(level.getName())
.toString()
- .getBytes(StandardCharsets.UTF_8)
+ .getBytes(Charset.defaultCharset())
),
"Path=/",
String.format(
+ Locale.ENGLISH,
"Expires=%1$ta, %1$td %1$tb %1$tY %1$tT GMT",
new Date(
System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1L)
diff --git a/src/main/java/org/takes/facets/flash/XeFlash.java b/src/main/java/org/takes/facets/flash/XeFlash.java
index 2b835816f..06aca3e45 100644
--- a/src/main/java/org/takes/facets/flash/XeFlash.java
+++ b/src/main/java/org/takes/facets/flash/XeFlash.java
@@ -24,8 +24,11 @@
package org.takes.facets.flash;
import java.io.IOException;
+import java.nio.charset.Charset;
import java.util.Iterator;
-import java.util.logging.Level;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.xml.bind.DatatypeConverter;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.rq.RqCookies;
@@ -37,13 +40,18 @@
* Xembly source to show flash message in XML.
*
*
The class is immutable and thread-safe.
- *
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.1
*/
-@EqualsAndHashCode(of = { "req", "cookie" })
+@EqualsAndHashCode(of = {"req", "cookie" })
public final class XeFlash implements XeSource {
+ /**
+ * Compiled RsFlash message regexp pattern.
+ */
+ private static final Pattern RS_FLASH_MSG = Pattern.compile(
+ "^(.*?)/(.*?)$"
+ );
/**
* Request.
@@ -79,10 +87,17 @@ public Iterable toXembly() throws IOException {
new RqCookies(this.req).cookie(this.cookie).iterator();
final Directives dirs = new Directives();
if (cookies.hasNext()) {
- final String value = cookies.next();
- dirs.add("flash")
- .add("message").set(value).up()
- .add("level").set(Level.INFO.toString());
+ final Matcher matcher = RS_FLASH_MSG.matcher(
+ new String(
+ DatatypeConverter.parseBase64Binary(cookies.next()),
+ Charset.defaultCharset()
+ )
+ );
+ if (matcher.find()) {
+ dirs.add("flash")
+ .add("message").set(matcher.group(1)).up()
+ .add("level").set(matcher.group(2));
+ }
}
return dirs;
}
diff --git a/src/test/java/org/takes/facets/flash/RsFlashTest.java b/src/test/java/org/takes/facets/flash/RsFlashTest.java
index d96624658..5bde5fb14 100644
--- a/src/test/java/org/takes/facets/flash/RsFlashTest.java
+++ b/src/test/java/org/takes/facets/flash/RsFlashTest.java
@@ -24,7 +24,7 @@
package org.takes.facets.flash;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
+import java.nio.charset.Charset;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
import org.hamcrest.MatcherAssert;
@@ -59,7 +59,7 @@ public void addsCookieToResponse() throws IOException {
.append('/')
.append(msg)
.toString()
- .getBytes(StandardCharsets.UTF_8)
+ .getBytes(Charset.defaultCharset())
)
).toString()
)
diff --git a/src/test/java/org/takes/facets/flash/XeFlashTest.java b/src/test/java/org/takes/facets/flash/XeFlashTest.java
index 5ac8ff566..ad3d75a1e 100644
--- a/src/test/java/org/takes/facets/flash/XeFlashTest.java
+++ b/src/test/java/org/takes/facets/flash/XeFlashTest.java
@@ -25,6 +25,10 @@
import com.jcabi.matchers.XhtmlMatchers;
import java.io.IOException;
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
@@ -42,29 +46,41 @@
public final class XeFlashTest {
/**
- * XeFlash can create a flash data.
+ * XeFlash can accept RsFlash cookie.
* @throws IOException If some problem inside
*/
@Test
- public void generatesFlashData() throws IOException {
- MatcherAssert.assertThat(
- IOUtils.toString(
- new RsXembly(
- new XeAppend(
- "root",
- new XeFlash(
- new RqWithHeader(
- new RqFake(), "Cookie", "RsFlash=how are you"
+ public void acceptsRsFlashCookie() throws IOException {
+ final String msg = "how are you";
+ final Pattern pattern = Pattern.compile(
+ "^Set-Cookie: RsFlash=(.*?);Path.*"
+ );
+ final Iterator itr = new RsFlash(msg, Level.FINE)
+ .head().iterator();
+ if (itr.hasNext()) {
+ final Matcher matcher = pattern.matcher(itr.next());
+ if (matcher.find()) {
+ MatcherAssert.assertThat(
+ IOUtils.toString(
+ new RsXembly(
+ new XeAppend(
+ "root",
+ new XeFlash(
+ new RqWithHeader(
+ new RqFake(),
+ "Cookie",
+ "RsFlash=".concat(matcher.group(1))
+ )
+ )
)
- )
+ ).body()
+ ),
+ XhtmlMatchers.hasXPaths(
+ "/root/flash[message='how are you']",
+ "/root/flash[level='FINE']"
)
- ).body()
- ),
- XhtmlMatchers.hasXPaths(
- "/root/flash[message='how are you']",
- "/root/flash[level='INFO']"
- )
- );
+ );
+ }
+ }
}
-
}