Skip to content

Commit faddcb8

Browse files
committed
rewrite flash scope handler to new after processing
1 parent 3c836c7 commit faddcb8

1 file changed

Lines changed: 23 additions & 22 deletions

File tree

jooby/src/main/java/org/jooby/internal/handlers/FlashScopeHandler.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.jooby.FlashScope;
2828
import org.jooby.Request;
2929
import org.jooby.Response;
30-
import org.jooby.Result;
3130
import org.jooby.Route;
3231

3332
public class FlashScopeHandler implements Route.Filter {
@@ -56,30 +55,32 @@ public void handle(final Request req, final Response rsp, final Route.Chain chai
5655
req.set(FlashScope.NAME, flashScope);
5756

5857
// wrap & proceed
59-
chain.next(req, new Response.Forwarding(rsp) {
60-
@Override
61-
public void send(final Result result) throws Throwable {
62-
// 1. no change detect
63-
if (flashScope.equals(copy)) {
64-
// 1.a. existing data available, discard
65-
if (flashScope.size() > 0) {
66-
rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
67-
}
58+
rsp.push(finalizeFlash(copy, flashScope));
59+
60+
chain.next(req, rsp);
61+
}
62+
63+
private Route.After finalizeFlash(final Map<String, String> initialScope,
64+
final Map<String, String> scope) {
65+
return (req, rsp, result) -> {
66+
// 1. no change detect
67+
if (scope.equals(initialScope)) {
68+
// 1.a. existing data available, discard
69+
if (scope.size() > 0) {
70+
rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
71+
}
72+
} else {
73+
// 2. change detected
74+
if (scope.size() == 0) {
75+
// 2.a everything was removed from app logic
76+
rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
6877
} else {
69-
// 2. change detected
70-
if (flashScope.size() == 0) {
71-
// 2.a everything was removed from app logic
72-
rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
73-
} else {
74-
// 2.b there is something to see in the next request
75-
rsp.cookie(name, encoder.apply(flashScope));
76-
}
78+
// 2.b there is something to see in the next request
79+
rsp.cookie(name, encoder.apply(scope));
7780
}
78-
// send
79-
super.send(result);
8081
}
81-
});
82-
82+
return result;
83+
};
8384
}
8485

8586
}

0 commit comments

Comments
 (0)