|
15 | 15 | */ |
16 | 16 | package com.innoq.spring.cookie.flash; |
17 | 17 |
|
18 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
19 | | -import com.fasterxml.jackson.core.type.TypeReference; |
20 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
21 | | -import com.fasterxml.jackson.databind.module.SimpleModule; |
| 18 | +import org.springframework.util.Assert; |
22 | 19 | import org.springframework.web.servlet.FlashMap; |
23 | 20 | import org.springframework.web.servlet.support.AbstractFlashMapManager; |
24 | | -import org.springframework.web.util.WebUtils; |
25 | 21 |
|
26 | 22 | import javax.servlet.http.Cookie; |
27 | 23 | import javax.servlet.http.HttpServletRequest; |
28 | 24 | import javax.servlet.http.HttpServletResponse; |
29 | | -import java.io.IOException; |
30 | | -import java.util.Base64; |
31 | 25 | import java.util.List; |
32 | 26 |
|
| 27 | +import static org.springframework.web.util.WebUtils.getCookie; |
| 28 | + |
33 | 29 | public final class CookieFlashMapManager extends AbstractFlashMapManager { |
34 | 30 |
|
35 | 31 | private static final String DEFAULT_COOKIE_NAME = "flash"; |
36 | 32 |
|
37 | | - private final ObjectMapper objectMapper = new ObjectMapper() |
38 | | - .registerModule(new SimpleModule() |
39 | | - .addSerializer(FlashMap.class, new FlashMapSerializer()) |
40 | | - .addDeserializer(FlashMap.class, new FlashMapDeserializer())); |
| 33 | + private final FlashMapListCodec codec; |
41 | 34 | private final String cookieName; |
42 | 35 |
|
43 | | - public CookieFlashMapManager() { |
44 | | - this(DEFAULT_COOKIE_NAME); |
| 36 | + public CookieFlashMapManager(FlashMapListCodec codec) { |
| 37 | + this(codec, DEFAULT_COOKIE_NAME); |
45 | 38 | } |
46 | 39 |
|
47 | | - public CookieFlashMapManager(String cookieName) { |
48 | | - // TODO: assert not null/empty |
| 40 | + public CookieFlashMapManager(FlashMapListCodec codec, String cookieName) { |
| 41 | + Assert.notNull(codec, "FlashMapListCodec must not be null"); |
| 42 | + Assert.hasText(cookieName, "Cookie name must not be null or empty"); |
| 43 | + this.codec = codec; |
49 | 44 | this.cookieName = cookieName; |
50 | 45 | } |
51 | 46 |
|
52 | 47 | @Override |
53 | 48 | protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) { |
54 | | - Cookie cookie = WebUtils.getCookie(request, cookieName); |
| 49 | + final Cookie cookie = getCookie(request, cookieName); |
55 | 50 | if (cookie == null) { |
56 | 51 | return null; |
57 | 52 | } |
58 | 53 |
|
59 | | - String value = cookie.getValue(); |
60 | | - byte[] payload = Base64.getDecoder().decode(value); |
61 | | - |
62 | | - try { |
63 | | - return this.objectMapper.readValue(payload, new TypeReference<List<FlashMap>>() {}); |
64 | | - } catch (IOException e) { |
65 | | - // TODO |
66 | | - e.printStackTrace(); |
67 | | - return null; |
68 | | - } |
| 54 | + final String value = cookie.getValue(); |
| 55 | + return codec.decode(value); |
69 | 56 | } |
70 | 57 |
|
71 | 58 | @Override |
72 | | - protected void updateFlashMaps(List<FlashMap> flashMaps, HttpServletRequest request, HttpServletResponse response) { |
73 | | - Cookie cookie = new Cookie(cookieName, null); |
| 59 | + protected void updateFlashMaps(List<FlashMap> flashMaps, |
| 60 | + HttpServletRequest request, HttpServletResponse response) { |
| 61 | + final Cookie cookie = new Cookie(cookieName, null); |
74 | 62 | cookie.setHttpOnly(true); |
75 | 63 | cookie.setPath("/"); |
76 | 64 |
|
77 | 65 | if (flashMaps.isEmpty()) { |
78 | 66 | cookie.setMaxAge(0); |
79 | 67 | } else { |
80 | | - try { |
81 | | - byte[] payload = this.objectMapper.writeValueAsBytes(flashMaps); |
82 | | - String value = Base64.getEncoder().encodeToString(payload); |
83 | | - |
84 | | - // TODO: max-age? |
85 | | - cookie.setValue(value); |
86 | | - } catch (JsonProcessingException e) { |
87 | | - // TODO |
88 | | - e.printStackTrace(); |
89 | | - } |
| 68 | + final String value = codec.encode(flashMaps); |
| 69 | + cookie.setValue(value); |
90 | 70 | } |
91 | 71 | response.addCookie(cookie); |
92 | 72 | } |
|
0 commit comments