1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sourceforge.statelessfilter.backend.session;
16
17 import java.io.IOException;
18 import java.util.List;
19 import java.util.Map;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 import javax.servlet.http.HttpSession;
24
25 import net.sourceforge.statelessfilter.backend.ISessionBackend;
26 import net.sourceforge.statelessfilter.backend.ISessionData;
27 import net.sourceforge.statelessfilter.backend.support.CookieDataSupport;
28 import net.sourceforge.statelessfilter.filter.Configuration;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class SessionBackend implements ISessionBackend {
49 private static final String ID = "session";
50 private static final String SESSION_SESSION = "stateless.session";
51 Logger logger = LoggerFactory.getLogger(SessionBackend.class);
52
53 public SessionBackend() {
54
55 }
56
57
58
59
60
61 public void destroy() {
62
63 }
64
65
66
67
68
69 public String getId() {
70 return ID;
71 }
72
73
74
75
76
77 public void init(Map<String, String> config) throws Exception {
78
79 }
80
81 public String isBufferingRequired() {
82 return Configuration.BUFFERING_HEADERS;
83 }
84
85
86
87
88
89 public ISessionData restore(HttpServletRequest request) {
90 HttpSession session = request.getSession(false);
91 if (session != null) {
92 CookieDataSupport s = (CookieDataSupport) session
93 .getAttribute(SESSION_SESSION);
94 if (s != null && s.isValid()) {
95 return s;
96 }
97 }
98
99 return null;
100 }
101
102
103
104
105
106
107 public void save(ISessionData session, List<String> dirtyAttributes,
108 HttpServletRequest request, HttpServletResponse response)
109 throws IOException {
110 if (session != null) {
111 HttpSession httpSession = request.getSession(true);
112
113 if (!session.isValid()) {
114 httpSession.invalidate();
115 } else {
116 CookieDataSupport cookieData = new CookieDataSupport(session);
117 httpSession.setAttribute(SESSION_SESSION, cookieData);
118 }
119 } else {
120
121 HttpSession httpSession = request.getSession(false);
122 if (httpSession != null) {
123 httpSession.setAttribute(SESSION_SESSION, null);
124 }
125 }
126
127 }
128 }