|
27 | 27 | import org.jooby.FlashScope; |
28 | 28 | import org.jooby.Request; |
29 | 29 | import org.jooby.Response; |
30 | | -import org.jooby.Result; |
31 | 30 | import org.jooby.Route; |
32 | 31 |
|
33 | 32 | public class FlashScopeHandler implements Route.Filter { |
@@ -56,30 +55,32 @@ public void handle(final Request req, final Response rsp, final Route.Chain chai |
56 | 55 | req.set(FlashScope.NAME, flashScope); |
57 | 56 |
|
58 | 57 | // 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)); |
68 | 77 | } 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)); |
77 | 80 | } |
78 | | - // send |
79 | | - super.send(result); |
80 | 81 | } |
81 | | - }); |
82 | | - |
| 82 | + return result; |
| 83 | + }; |
83 | 84 | } |
84 | 85 |
|
85 | 86 | } |
0 commit comments