Skip to content

Commit fb3e99f

Browse files
committed
Refactor example app to support testing nested values.
1 parent 456db4b commit fb3e99f

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

example-app/src/main/java/com/orangefunction/tomcatredissessionmanager/exampleapp/WebApp.java

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.orangefunction.tomcatredissessionmanager.exampleapp;
22

33
import java.util.Map;
4+
import java.util.Map.Entry;
45
import java.util.HashMap;
56
import java.util.Set;
67
import static spark.Spark.*;
@@ -18,8 +19,13 @@
1819
import java.lang.reflect.Field;
1920
import javax.servlet.*;
2021

22+
import org.apache.juli.logging.Log;
23+
import org.apache.juli.logging.LogFactory;
24+
2125
public class WebApp implements spark.servlet.SparkApplication {
2226

27+
private final Log log = LogFactory.getLog(WebApp.class);
28+
2329
protected String redisHost = "localhost";
2430
protected int redisPort = 6379;
2531
protected int redisDatabase = 0;
@@ -29,7 +35,6 @@ public class WebApp implements spark.servlet.SparkApplication {
2935

3036
private void initializeJedisConnectionPool() {
3137
try {
32-
// TODO: Allow configuration of pool (such as size...)
3338
redisConnectionPool = new JedisPool(new JedisPoolConfig(), redisHost, redisPort, redisTimeout, redisPassword);
3439
} catch (Exception e) {
3540
e.printStackTrace();
@@ -76,6 +81,50 @@ protected RedisSessionManager getRedisSessionManager(Request request) {
7681
return sessionManager;
7782
}
7883

84+
protected void updateSessionFromQueryParamsMap(Session session, QueryParamsMap queryParamsMap) {
85+
for (Entry<String, String[]> kv : queryParamsMap.toMap().entrySet()) {
86+
String key = kv.getKey();
87+
QueryParamsMap subParamsMap = queryParamsMap.get(kv.getKey());
88+
if (subParamsMap.hasKeys()) {
89+
Object currentValue = session.attribute(key);
90+
Map<String, Object> subMap;
91+
if (currentValue instanceof Map) {
92+
subMap = (Map<String, Object>)currentValue;
93+
} else {
94+
subMap = new HashMap<String, Object>();
95+
session.attribute(key, subMap);
96+
}
97+
updateMapFromQueryParamsMap(subMap, subParamsMap);
98+
} else if (subParamsMap.hasValue()) {
99+
Object value = subParamsMap.value();
100+
//log.info("found key " + key + " and value " + (null == value ? "`null`" : value.toString()));
101+
session.attribute(key, value);
102+
}
103+
}
104+
}
105+
106+
protected void updateMapFromQueryParamsMap(Map map, QueryParamsMap queryParamsMap) {
107+
for (Entry<String, String[]> kv : queryParamsMap.toMap().entrySet()) {
108+
String key = kv.getKey();
109+
QueryParamsMap subParamsMap = queryParamsMap.get(kv.getKey());
110+
if (subParamsMap.hasKeys()) {
111+
Object currentValue = map.get(key);
112+
Map<String, Object> subMap;
113+
if (currentValue instanceof Map) {
114+
subMap = (Map<String, Object>)currentValue;
115+
} else {
116+
subMap = new HashMap<String, Object>();
117+
map.put(key, subMap);
118+
}
119+
updateMapFromQueryParamsMap(subMap, subParamsMap);
120+
} else if (subParamsMap.hasValue()) {
121+
Object value = subParamsMap.value();
122+
//log.info("found key " + key + " and value " + (null == value ? "`null`" : value.toString()));
123+
map.put(key, value);
124+
}
125+
}
126+
}
127+
79128
public void init() {
80129

81130
// /session
@@ -87,13 +136,14 @@ public Object handle(Request request, Response response) {
87136
}
88137
});
89138

139+
140+
90141
put(new SessionJsonTransformerRoute("/session", "application/json") {
91142
@Override
92143
public Object handle(Request request, Response response) {
93144
Session session = request.session();
94-
for (String key : request.queryParams()) {
95-
session.attribute(key, request.queryParams(key));
96-
}
145+
QueryParamsMap queryMap = request.queryMap();
146+
updateSessionFromQueryParamsMap(session, queryMap);
97147
return session;
98148
}
99149
});
@@ -102,9 +152,8 @@ public Object handle(Request request, Response response) {
102152
@Override
103153
public Object handle(Request request, Response response) {
104154
Session session = request.session();
105-
for (String key : request.queryParams()) {
106-
session.attribute(key, request.queryParams(key));
107-
}
155+
QueryParamsMap queryMap = request.queryMap();
156+
updateSessionFromQueryParamsMap(session, queryMap);
108157
return session;
109158
}
110159
});

spec/requests/sessions_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
json['attributes']['param1'].should == '6'
4949
end
5050

51+
it 'should support setting a complex values in the session' do
52+
post(SESSION_PATH, body: {param1: {subparam: '5'}})
53+
json['attributes']['param1'].should have_key('subparam')
54+
json['attributes']['param1']['subparam'].should == '5'
55+
end
56+
5157
it 'should persist session attributes between requests' do
5258
post(SESSION_PATH, body: {param1: '5'})
5359
get(SESSION_PATH)

0 commit comments

Comments
 (0)