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