1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.onehippo.forge.hst.spring.support.session.servlet;
17
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpSession;
20
21 import org.springframework.session.SessionRepository;
22
23
24
25
26 public class HttpSessionDelegatingRepository implements SessionRepository<HttpSessionDelegatingSession> {
27
28 @Override
29 public HttpSessionDelegatingSession createSession() {
30 final HttpServletRequest request = HttpSessionDelegatingContext.getCurrentServletRequest();
31
32 if (request == null) {
33 throw new IllegalStateException("HttpServletRequest not found yet.");
34 }
35
36 final HttpSession httpSession = request.getSession(true);
37 final HttpSessionDelegatingSession session = new HttpSessionDelegatingSession(httpSession);
38 httpSession.setAttribute(HttpSessionDelegatingSession.NAME, session);
39
40 return session;
41 }
42
43 @Override
44 public void save(HttpSessionDelegatingSession session) {
45
46 }
47
48 @Override
49 public HttpSessionDelegatingSession findById(String id) {
50 final HttpServletRequest request = HttpSessionDelegatingContext.getCurrentServletRequest();
51 final HttpSession httpSession = (request != null) ? request.getSession(false) : null;
52
53 if (httpSession == null) {
54 return null;
55 }
56
57 HttpSessionDelegatingSession session = (HttpSessionDelegatingSession) httpSession.getAttribute(HttpSessionDelegatingSession.NAME);
58
59 if (session != null) {
60
61
62 if (!httpSession.getId().equals(id)) {
63 session = new HttpSessionDelegatingSession(httpSession);
64 httpSession.setAttribute(HttpSessionDelegatingSession.NAME, session);
65 }
66 }
67
68 return session;
69 }
70
71 @Override
72 public void deleteById(String id) {
73 final HttpServletRequest request = HttpSessionDelegatingContext.getCurrentServletRequest();
74 final HttpSession httpSession = (request != null) ? request.getSession(false) : null;
75
76 if (httpSession == null) {
77 return;
78 }
79
80 HttpSessionDelegatingSession session = (HttpSessionDelegatingSession) httpSession.getAttribute(HttpSessionDelegatingSession.NAME);
81
82 if (session != null) {
83 if (session.getId().equals(id)) {
84 httpSession.invalidate();
85 }
86 }
87 }
88 }