Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customisation of Flash cookie attributes #31

Merged
merged 1 commit into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/com/googlecode/utterlyidle/flash/FlashCookieAttributes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.googlecode.utterlyidle.flash;

import com.googlecode.totallylazy.Sequence;
import com.googlecode.utterlyidle.BasePath;
import com.googlecode.utterlyidle.cookies.CookieAttribute;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;

import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.utterlyidle.cookies.CookieAttribute.path;

public class FlashCookieAttributes implements Iterable<CookieAttribute> {

private final Sequence<CookieAttribute> cookieAttributes;

public FlashCookieAttributes(BasePath basePath) {
this(basePath, new CookieAttribute[0]);
}

private FlashCookieAttributes(BasePath basePath, CookieAttribute... cookieAttributes) {
this.cookieAttributes = sequence(cookieAttributes)
.append(path(basePath.toString()));
}

public static FlashCookieAttributes flashCookieAttributes(BasePath basePath, CookieAttribute... cookieAttributes) {
return new FlashCookieAttributes(basePath, cookieAttributes);
}

@Override
public Iterator<CookieAttribute> iterator() {
return cookieAttributes.iterator();
}

@Override
public void forEach(final Consumer<? super CookieAttribute> action) {
cookieAttributes.forEach(action);
}

@Override
public Spliterator<CookieAttribute> spliterator() {
return cookieAttributes.spliterator();
}
}
122 changes: 64 additions & 58 deletions src/com/googlecode/utterlyidle/flash/FlashHandler.java
Original file line number Diff line number Diff line change
@@ -1,93 +1,99 @@
package com.googlecode.utterlyidle.flash;

import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.json.Json;
import com.googlecode.utterlyidle.BasePath;
import com.googlecode.utterlyidle.HttpHandler;
import com.googlecode.utterlyidle.Request;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.cookies.Cookie;
import com.googlecode.utterlyidle.cookies.CookieAttribute;
import com.googlecode.utterlyidle.cookies.CookieParameters;

import static com.googlecode.totallylazy.Pair.pair;
import static com.googlecode.totallylazy.Sequences.sequence;
import static com.googlecode.totallylazy.Strings.isBlank;
import static com.googlecode.utterlyidle.cookies.CookieAttribute.path;

public class FlashHandler implements HttpHandler {
public static final String FLASH_COOKIE = "f";
private static final String EMPTY_FLASH_COOKIE_JSON = "{}";
private final HttpHandler decorated;
private final Flash flash;
private final ClearFlashPredicate clearFlashPredicate;
private final BasePath basePath;
public static final String FLASH_COOKIE = "f";
private static final String EMPTY_FLASH_COOKIE_JSON = "{}";
private final HttpHandler decorated;
private final Flash flash;
private final ClearFlashPredicate clearFlashPredicate;
private final FlashCookieAttributes flashCookieAttributes;

/**
* Flash is request-scoped storage for values
* that will only be present for the next request.
*
* Flash is request-scoped storage for values
* that will only be present for the next request.
* <p>
* Our current use case is to display messages to the user after
* a redirect.
*
* By default, FlashHandler will clear the flash on each
* successful HTML response. Override ClearFlashPredicate
* <p>
* By default, FlashHandler will clear the flash on each
* successful HTML response. Override ClearFlashPredicate
* to change this behaviour.
*
* <p>
* It is expected that this behaviour will be superseded by a
* more fully-featured implementation in the future.
*
*/
public FlashHandler(Flash flash, HttpHandler decorated, ClearFlashPredicate clearFlashPredicate, BasePath basePath) {
this.flash = flash;
this.decorated = decorated;
this.clearFlashPredicate = clearFlashPredicate;
this.basePath = basePath;
*/
public FlashHandler(Flash flash, HttpHandler decorated, ClearFlashPredicate clearFlashPredicate, FlashCookieAttributes flashCookieAttributes) {
this.flash = flash;
this.decorated = decorated;
this.clearFlashPredicate = clearFlashPredicate;
this.flashCookieAttributes = flashCookieAttributes;
}

@Override
public Response handle(Request request) throws Exception {
setIncomingFlashValues(request, flash);
return setFlashCookie(request, decorated.handle(request));
}
@Override
public Response handle(Request request) throws Exception {
setIncomingFlashValues(request, flash);
return setFlashCookie(request, decorated.handle(request));
}

private static void setIncomingFlashValues(Request request, Flash flash) {
CookieParameters requestCookies = request.cookies();
private static void setIncomingFlashValues(Request request, Flash flash) {
CookieParameters requestCookies = request.cookies();

if(!requestCookies.contains(FLASH_COOKIE) || isEmptyJson(requestCookies.getValue(FLASH_COOKIE)) || isBlank(requestCookies.getValue(FLASH_COOKIE))) return;
if (!requestCookies.contains(FLASH_COOKIE) || isEmptyJson(requestCookies.getValue(FLASH_COOKIE)) || isBlank(requestCookies.getValue(FLASH_COOKIE)))
return;

flash.merge(Json.parseMap(requestCookies.getValue(FLASH_COOKIE)).value());
}
flash.merge(Json.parseMap(requestCookies.getValue(FLASH_COOKIE)).value());
}

private Response setFlashCookie(Request request, Response response) {
return set(request, response, shouldClearFlash(request, response) ? clearCookie() : flashCookie());
}
private Response setFlashCookie(Request request, Response response) {
return set(request, response, shouldClearFlash(request, response) ? clearCookie() : flashCookie());
}

private Response set(final Request request, final Response response, final Cookie cookie) {
if (cookieAlreadyHasSameValue(request, cookie) || (isEmptyJson(cookie.value()) && !request.cookies().contains(FLASH_COOKIE))) {
return response;
}
return response.cookie(cookie);
}
private Response set(final Request request, final Response response, final Cookie cookie) {
if (cookieAlreadyHasSameValue(request, cookie) || (isEmptyJson(cookie.value()) && !request.cookies().contains(FLASH_COOKIE))) {
return response;
}
return response.cookie(cookie);
}

private boolean shouldClearFlash(Request request, Response response) {
return clearFlashPredicate.matches(pair(request, response));
}
private boolean shouldClearFlash(Request request, Response response) {
return clearFlashPredicate.matches(pair(request, response));
}

private boolean cookieAlreadyHasSameValue(final Request request, final Cookie cookie) {
return cookie.value().equals(request.cookies().getValue(FLASH_COOKIE));
}
private boolean cookieAlreadyHasSameValue(final Request request, final Cookie cookie) {
return cookie.value().equals(request.cookies().getValue(FLASH_COOKIE));
}

private static boolean isEmptyJson(final String value) {
return EMPTY_FLASH_COOKIE_JSON.equals(value);
}
private static boolean isEmptyJson(final String value) {
return EMPTY_FLASH_COOKIE_JSON.equals(value);
}

private Cookie clearCookie() {
return flashCookie(EMPTY_FLASH_COOKIE_JSON);
}
private Cookie clearCookie() {
return flashCookie(EMPTY_FLASH_COOKIE_JSON);
}

private Cookie flashCookie() {
return flashCookie(Json.json(flash.state()));
}
private Cookie flashCookie() {
return flashCookie(Json.json(flash.state()));
}

private Cookie flashCookie(String json) {
return new Cookie(FLASH_COOKIE, json, cookieAttributes());
}

private Cookie flashCookie(String json) {
return new Cookie(FLASH_COOKIE, json, path(basePath.toString()));
private CookieAttribute[] cookieAttributes() {
final Sequence<CookieAttribute> attributes = sequence(flashCookieAttributes);
return attributes.toArray(new CookieAttribute[attributes.size()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class FlashMessagesModule implements RequestScopedModule {
public Container addPerRequestObjects(Container container) throws Exception {
return container.
add(ClearFlashPredicate.class, ClearFlashOnSuccessfulNonInternalHtmlResponse.class).
add(FlashCookieAttributes.class).
add(Flash.class).
decorate(HttpHandler.class, FlashHandler.class);
}
Expand Down
Loading