Skip to content

Commit 3e618c1

Browse files
committed
Session API: redo SessionStore and remove RequestSession
1 parent 98f9f7f commit 3e618c1

11 files changed

Lines changed: 103 additions & 246 deletions

File tree

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public interface DefaultContext extends Context {
9191
if (session == null) {
9292
Router router = getRouter();
9393
SessionOptions options = router.getSessionOptions();
94-
95-
String sessionId = options.generateId();
96-
session = Session.create(this, options.getStore().newSession(sessionId));
97-
options.getSessionId().saveSessionId(this, sessionId);
94+
SessionStore store = options.getStore();
95+
session = store.newSession(this);
96+
getAttributes().put(Session.NAME, session);
97+
// options.getSessionId().saveSessionId(this, sessionId);
9898
}
9999
return session;
100100
}
@@ -104,17 +104,13 @@ public interface DefaultContext extends Context {
104104
if (session == null) {
105105
Router router = getRouter();
106106
SessionOptions options = router.getSessionOptions();
107-
SessionId strategy = options.getSessionId();
108-
String sessionId = strategy.findSessionId(this);
109-
if (sessionId == null) {
110-
return null;
111-
}
112-
session = options.getStore().findSession(sessionId);
113-
if (session == null) {
114-
return null;
115-
}
116-
session = Session.create(this, session);
117-
strategy.saveSessionId(this, sessionId);
107+
SessionStore store = options.getStore();
108+
session = store.findSession(this);
109+
// if (session == null) {
110+
// return null;
111+
// }
112+
// session = Session.create(this, session);
113+
// strategy.saveSessionId(this, sessionId);
118114
}
119115
return session;
120116
}

jooby/src/main/java/io/jooby/Session.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
package io.jooby;
77

8-
import io.jooby.internal.RequestSession;
98
import io.jooby.internal.SessionImpl;
109

1110
import javax.annotation.Nonnull;
@@ -131,11 +130,6 @@ public interface Session {
131130
*/
132131
@Nonnull ValueNode remove(@Nonnull String name);
133132

134-
/**
135-
* Read-only attributes.
136-
*
137-
* @return Read-only attributes.
138-
*/
139133
@Nonnull Map<String, String> toMap();
140134

141135
/**
@@ -216,18 +210,11 @@ public interface Session {
216210
* @param id Session ID.
217211
* @return A new session.
218212
*/
219-
static @Nonnull Session create(@Nonnull String id) {
220-
return new SessionImpl(id);
213+
static @Nonnull Session create(@Nonnull Context ctx, @Nonnull String id) {
214+
return new SessionImpl(ctx, id);
221215
}
222216

223-
/**
224-
* Creates a new session and attach it to current request.
225-
*
226-
* @param context Web context.
227-
* @param session HTTP session.
228-
* @return A request attached session.
229-
*/
230-
static @Nonnull Session create(@Nonnull Context context, @Nonnull Session session) {
231-
return new RequestSession(context, session);
217+
static @Nonnull Session create(@Nonnull Context ctx, @Nonnull String id, @Nonnull Map<String, String> data) {
218+
return new SessionImpl(ctx, id, data);
232219
}
233220
}

jooby/src/main/java/io/jooby/SessionOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import io.jooby.internal.InMemorySessionStore;
99
import io.jooby.internal.MultipleSessionId;
10-
import io.jooby.internal.RequestSessionStore;
1110
import io.jooby.internal.SecretSessionId;
1211

1312
import javax.annotation.Nonnull;
@@ -73,7 +72,7 @@ public SessionId getSessionId() {
7372
* @return Session store (defaults uses memory).
7473
*/
7574
public @Nonnull SessionStore getStore() {
76-
return new RequestSessionStore(store);
75+
return store;
7776
}
7877

7978
/**

jooby/src/main/java/io/jooby/SessionStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface SessionStore {
2727
* @param id Session ID.
2828
* @return A new session.
2929
*/
30-
@Nonnull Session newSession(@Nonnull String id);
30+
@Nonnull Session newSession(@Nonnull Context ctx);
3131

3232
/**
3333
* Find an existing session by ID. For existing session this method must:
@@ -38,14 +38,14 @@ public interface SessionStore {
3838
* @param id Session ID.
3939
* @return An existing session or <code>null</code>.
4040
*/
41-
@Nullable Session findSession(@Nonnull String id);
41+
@Nullable Session findSession(@Nonnull Context ctx);
4242

4343
/**
4444
* Delete a session from store. This method must NOT call {@link Session#destroy()}.
4545
*
4646
* @param id Session ID.
4747
*/
48-
void deleteSession(@Nonnull String id);
48+
void deleteSession(@Nonnull Context ctx);
4949

5050
/**
5151
* Save a session. This method must save:
@@ -55,5 +55,5 @@ public interface SessionStore {
5555
*
5656
* @param session Session to save.
5757
*/
58-
void save(@Nonnull Session session);
58+
void save(@Nonnull Context ctx);
5959
}

jooby/src/main/java/io/jooby/internal/InMemorySessionStore.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,80 @@
55
*/
66
package io.jooby.internal;
77

8+
import io.jooby.Context;
89
import io.jooby.Session;
10+
import io.jooby.SessionId;
11+
import io.jooby.SessionOptions;
912
import io.jooby.SessionStore;
1013

1114
import java.time.Instant;
15+
import java.util.Map;
1216
import java.util.concurrent.ConcurrentHashMap;
1317

1418
public class InMemorySessionStore implements SessionStore {
15-
private ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>();
19+
private static class SessionData {
20+
private Instant lastAccessedTime;
21+
private Instant creationTime;
22+
private Map hash;
1623

17-
@Override public Session newSession(String id) {
24+
public SessionData(Instant creationTime, Instant lastAccessedTime, Map hash) {
25+
this.creationTime = creationTime;
26+
this.lastAccessedTime = lastAccessedTime;
27+
this.hash = hash;
28+
}
29+
}
30+
31+
private ConcurrentHashMap<String, SessionData> sessions = new ConcurrentHashMap<>();
32+
33+
@Override public Session newSession(Context ctx) {
34+
SessionOptions options = sessionOptions(ctx);
35+
String sessionId = options.generateId();
1836
Instant now = Instant.now();
19-
Session session = Session.create(id)
37+
Session session = Session.create(ctx, sessionId)
2038
.setCreationTime(now)
2139
.setLastAccessedTime(now)
2240
.setNew(true);
41+
options.getSessionId().saveSessionId(ctx, sessionId);
2342
return session;
2443
}
2544

26-
@Override public Session findSession(String id) {
27-
Session session = sessions.get(id);
28-
if (session != null) {
29-
session.setLastAccessedTime(Instant.now());
45+
@Override public Session findSession(Context ctx) {
46+
SessionOptions options = sessionOptions(ctx);
47+
String sessionId = options.getSessionId().findSessionId(ctx);
48+
if (sessionId == null) {
49+
return null;
3050
}
31-
return session;
51+
SessionData data = sessions.get(sessionId);
52+
if (data != null) {
53+
Session session = Session.create(ctx, sessionId, data.hash);
54+
session.setLastAccessedTime(data.lastAccessedTime);
55+
session.setCreationTime(data.creationTime);
56+
options.getSessionId().saveSessionId(ctx, sessionId);
57+
return session;
58+
}
59+
return null;
60+
}
61+
62+
@Override public void deleteSession(Context ctx) {
63+
String sessionId = ctx.session().getId();
64+
sessions.remove(sessionId);
65+
SessionOptions options = sessionOptions(ctx);
66+
SessionId store = options.getSessionId();
67+
store.deleteSessionId(ctx, sessionId);
3268
}
3369

34-
@Override public void deleteSession(String id) {
35-
sessions.remove(id);
70+
@Override public void save(Context ctx) {
71+
Session session = ctx.session();
72+
String sessionId = ctx.session().getId();
73+
sessions.put(sessionId,
74+
new SessionData(session.getCreationTime(), Instant.now(), session.toMap()));
75+
// session
76+
// .setNew(false)
77+
// .setModify(false)
78+
// .setLastAccessedTime(Instant.now());
3679
}
3780

38-
@Override public void save(Session session) {
39-
sessions.put(session.getId(), session);
40-
session
41-
.setNew(false)
42-
.setModify(false)
43-
.setLastAccessedTime(Instant.now());
81+
private static SessionOptions sessionOptions(Context ctx) {
82+
return ctx.getRouter().getSessionOptions();
4483
}
4584
}

jooby/src/main/java/io/jooby/internal/RequestSession.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)