This repository was archived by the owner on May 11, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashMapImpl.java
More file actions
154 lines (128 loc) · 4 KB
/
Copy pathFlashMapImpl.java
File metadata and controls
154 lines (128 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Context;
import io.jooby.Cookie;
import io.jooby.FlashMap;
import io.jooby.Value;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import static io.jooby.Cookie.decode;
import static java.util.Collections.emptyMap;
public class FlashMapImpl extends HashMap<String, String> implements FlashMap {
private Context ctx;
private boolean keep;
private Cookie template;
private Map<String, String> initialScope;
public FlashMapImpl(Context ctx, Cookie template) {
Value cookie = ctx.cookie(template.getName());
Map<String, String> seed = cookie.isMissing() ? emptyMap() : decode(cookie.value());
super.putAll(seed);
this.ctx = ctx;
this.template = template;
this.initialScope = seed;
if (seed.size() > 0) {
syncCookie();
}
}
@Override public FlashMap keep() {
if (size() > 0) {
Cookie cookie = this.template.clone().setValue(Cookie.encode(this));
ctx.setResponseCookie(cookie);
}
return this;
}
private Cookie toCookie() {
// 1. no change detect
if (this.equals(initialScope)) {
// 1.a. existing data available, discard
if (this.size() > 0) {
return template.clone().setMaxAge(0);
}
} else {
// 2. change detected
if (this.size() == 0) {
// 2.a everything was removed from app logic
return template.clone().setMaxAge(0);
} else {
// 2.b there is something to see in the next request
return template.clone().setValue(Cookie.encode(this));
}
}
return null;
}
private void syncCookie() {
Cookie cookie = toCookie();
if (cookie != null) {
ctx.setResponseCookie(cookie);
}
}
@Override public String compute(String key,
BiFunction<? super String, ? super String, ? extends String> remappingFunction) {
String result = super.compute(key, remappingFunction);
syncCookie();
return result;
}
@Override public String computeIfAbsent(String key,
Function<? super String, ? extends String> mappingFunction) {
String result = super.computeIfAbsent(key, mappingFunction);
syncCookie();
return result;
}
@Override public String computeIfPresent(String key,
BiFunction<? super String, ? super String, ? extends String> remappingFunction) {
String result = super.computeIfPresent(key, remappingFunction);
syncCookie();
return result;
}
@Override public String merge(String key, String value,
BiFunction<? super String, ? super String, ? extends String> remappingFunction) {
String result = super.merge(key, value, remappingFunction);
syncCookie();
return result;
}
@Override public String put(String key, String value) {
String result = super.put(key, value);
syncCookie();
return result;
}
@Override public String putIfAbsent(String key, String value) {
String result = super.putIfAbsent(key, value);
syncCookie();
return result;
}
@Override public void putAll(Map<? extends String, ? extends String> m) {
super.putAll(m);
syncCookie();
}
@Override public boolean remove(Object key, Object value) {
boolean result = super.remove(key, value);
syncCookie();
return result;
}
@Override public String remove(Object key) {
String result = super.remove(key);
syncCookie();
return result;
}
@Override public boolean replace(String key, String oldValue, String newValue) {
boolean result = super.replace(key, oldValue, newValue);
syncCookie();
return result;
}
@Override public String replace(String key, String value) {
String result = super.replace(key, value);
syncCookie();
return result;
}
@Override
public void replaceAll(BiFunction<? super String, ? super String, ? extends String> function) {
super.replaceAll(function);
syncCookie();
}
}