Primitive containers are separated from CookieSession. They are used by only JSONEncoder.
@@ -5,8 +5,6 @@ | ||
5 | 5 | import java.util.HashMap; |
6 | 6 | import java.util.Map; |
7 | 7 | |
8 | -import jp.sf.amateras.cookiesession.primitive.IntegerContainer; | |
9 | - | |
10 | 8 | import org.junit.Test; |
11 | 9 | |
12 | 10 |
@@ -15,7 +13,7 @@ | ||
15 | 13 | @Test |
16 | 14 | public void testEncodeAndDecode(){ |
17 | 15 | Map<String, Object> map = new HashMap<String, Object>(); |
18 | - map.put("int", new IntegerContainer(1)); | |
16 | + map.put("int", 1); | |
19 | 17 | map.put("array", new String[]{"aaa", "bbb"}); |
20 | 18 | |
21 | 19 | JSONEncoder encoder = new JSONEncoder(); |
@@ -23,7 +21,7 @@ | ||
23 | 21 | String encoded = encoder.encode(map); |
24 | 22 | Map<String, Object> result = encoder.decode(encoded); |
25 | 23 | |
26 | - Integer intValue = ((IntegerContainer) result.get("int")).value; | |
24 | + Integer intValue = (Integer) result.get("int"); | |
27 | 25 | assertEquals(1, intValue.intValue()); |
28 | 26 | |
29 | 27 | String[] arrayValue = (String[]) result.get("array"); |
@@ -4,6 +4,7 @@ | ||
4 | 4 | import static org.mockito.Matchers.*; |
5 | 5 | import static org.mockito.Mockito.*; |
6 | 6 | |
7 | +import java.io.Serializable; | |
7 | 8 | import java.util.List; |
8 | 9 | import java.util.Map; |
9 | 10 |
@@ -14,7 +15,7 @@ | ||
14 | 15 | import javax.servlet.http.HttpServletResponse; |
15 | 16 | |
16 | 17 | import jp.sf.amateras.cookiesession.cipher.BlowfishCipher; |
17 | -import jp.sf.amateras.cookiesession.encoder.JSONEncoder; | |
18 | +import jp.sf.amateras.cookiesession.encoder.BinaryEncoder; | |
18 | 19 | |
19 | 20 | import org.junit.Before; |
20 | 21 | import org.junit.Test; |
@@ -76,7 +77,7 @@ | ||
76 | 77 | BlowfishCipher chiper = new BlowfishCipher(); |
77 | 78 | chiper.init(config); |
78 | 79 | |
79 | - Map<String, Object> map = new JSONEncoder().decode(chiper.decrypt(cookies.get(0).getValue())); | |
80 | + Map<String, Object> map = new BinaryEncoder().decode(chiper.decrypt(cookies.get(0).getValue())); | |
80 | 81 | LoginInfo result = (LoginInfo) map.get("loginInfo"); |
81 | 82 | |
82 | 83 | assertEquals("testuser", result.userId); |
@@ -108,17 +109,19 @@ | ||
108 | 109 | filter.doFilter(request, response, chain); |
109 | 110 | |
110 | 111 | ArgumentCaptor<Cookie> captor = ArgumentCaptor.forClass(Cookie.class); |
111 | - verify(response, times(2)).addCookie(captor.capture()); | |
112 | + verify(response, times(3)).addCookie(captor.capture()); | |
112 | 113 | |
113 | 114 | List<Cookie> cookies = captor.getAllValues(); |
114 | - assertEquals(2, cookies.size()); | |
115 | + assertEquals(3, cookies.size()); | |
115 | 116 | assertEquals("session-cookie01", cookies.get(0).getName()); |
116 | 117 | assertEquals("session-cookie02", cookies.get(1).getName()); |
118 | + assertEquals("session-cookie03", cookies.get(2).getName()); | |
117 | 119 | |
118 | 120 | BlowfishCipher chiper = new BlowfishCipher(); |
119 | 121 | chiper.init(config); |
120 | 122 | |
121 | - Map<String, Object> map = new JSONEncoder().decode(chiper.decrypt(cookies.get(0).getValue() + cookies.get(1).getValue())); | |
123 | + Map<String, Object> map = new BinaryEncoder().decode(chiper.decrypt( | |
124 | + cookies.get(0).getValue() + cookies.get(1).getValue() + cookies.get(2).getValue())); | |
122 | 125 | LoginInfo result = (LoginInfo) map.get("loginInfo"); |
123 | 126 | |
124 | 127 | assertEquals("testuser", result.userId); |
@@ -200,7 +203,8 @@ | ||
200 | 203 | assertEquals(0, cookies.get(1).getMaxAge()); |
201 | 204 | } |
202 | 205 | |
203 | - public static class LoginInfo { | |
206 | + public static class LoginInfo implements Serializable { | |
207 | + private static final long serialVersionUID = 1L; | |
204 | 208 | public String userId; |
205 | 209 | public String userName; |
206 | 210 | public int userType; |
@@ -15,6 +15,7 @@ | ||
15 | 15 | |
16 | 16 | /** |
17 | 17 | * An implementation of {@link SessionEncoder} which encodes and decodes session attributes as Base64 encoded serialized binary. |
18 | + * If you want to use this encoder, all session attribute values have to implement java.io.Serializable. | |
18 | 19 | * |
19 | 20 | * @author Naoki Takezoe |
20 | 21 | */ |
@@ -8,6 +8,7 @@ | ||
8 | 8 | import jp.sf.amateras.cookiesession.exception.CookieSessionException; |
9 | 9 | import jp.sf.amateras.cookiesession.exception.EncoderException; |
10 | 10 | import jp.sf.amateras.cookiesession.exception.InitializationException; |
11 | +import jp.sf.amateras.cookiesession.primitive.PrimitiveWrapperUtil; | |
11 | 12 | import net.arnx.jsonic.JSON; |
12 | 13 | |
13 | 14 | /** |
@@ -26,7 +27,7 @@ | ||
26 | 27 | |
27 | 28 | for(Map.Entry<String, Object> entry: attributes.entrySet()){ |
28 | 29 | String name = entry.getKey(); |
29 | - Object value = entry.getValue(); | |
30 | + Object value = PrimitiveWrapperUtil.wrap(entry.getValue()); | |
30 | 31 | |
31 | 32 | Map<String, String> info = new HashMap<String, String>(); |
32 | 33 | info.put("type", value.getClass().getName()); |
@@ -54,7 +55,7 @@ | ||
54 | 55 | String json = info.get("json"); |
55 | 56 | |
56 | 57 | try { |
57 | - Object value = JSON.decode(json, Class.forName(type)); | |
58 | + Object value = PrimitiveWrapperUtil.getValue(JSON.decode(json, Class.forName(type))); | |
58 | 59 | attributes.put(name, value); |
59 | 60 | |
60 | 61 | } catch(ClassNotFoundException ex){ |
@@ -92,8 +92,10 @@ | ||
92 | 92 | } |
93 | 93 | } catch(ChiperException ex){ |
94 | 94 | // TODO warn log?? |
95 | + ex.printStackTrace(); | |
95 | 96 | } catch(EncoderException ex){ |
96 | 97 | // TODO warn log?? |
98 | + ex.printStackTrace(); | |
97 | 99 | } |
98 | 100 | } |
99 | 101 | } |
@@ -13,7 +13,6 @@ | ||
13 | 13 | import javax.servlet.http.HttpSessionContext; |
14 | 14 | |
15 | 15 | import jp.sf.amateras.cookiesession.CookieSessionConfig; |
16 | -import jp.sf.amateras.cookiesession.primitive.PrimitiveWrapperUtil; | |
17 | 16 | |
18 | 17 | @SuppressWarnings("deprecation") |
19 | 18 | public class CookieSession implements HttpSession { |
@@ -100,8 +99,7 @@ | ||
100 | 99 | |
101 | 100 | public Object getAttribute(String name) { |
102 | 101 | checkAvailable(); |
103 | - // TODO It's unnecessary in other than JSONEncoder. | |
104 | - return PrimitiveWrapperUtil.getValue(attributes.get(name)); | |
102 | + return attributes.get(name); | |
105 | 103 | } |
106 | 104 | |
107 | 105 | public Object getValue(String name) { |
@@ -124,8 +122,7 @@ | ||
124 | 122 | public void setAttribute(String name, Object value) { |
125 | 123 | checkAvailable(); |
126 | 124 | boolean update = attributes.containsKey(name); |
127 | - // TODO It's unnecessary in other than JSONEncoder. | |
128 | - attributes.put(name, PrimitiveWrapperUtil.wrap(value)); | |
125 | + attributes.put(name, value); | |
129 | 126 | |
130 | 127 | // fire event |
131 | 128 | for(HttpSessionAttributeListener listener: config.listeners){ |
@@ -147,7 +144,7 @@ | ||
147 | 144 | public void removeAttribute(String name) { |
148 | 145 | checkAvailable(); |
149 | 146 | if(attributes.containsKey(name)){ |
150 | - Object value = PrimitiveWrapperUtil.getValue(attributes.remove(name)); | |
147 | + Object value = attributes.remove(name); | |
151 | 148 | |
152 | 149 | // fire event |
153 | 150 | for(HttpSessionAttributeListener listener: config.listeners){ |