Skip to content

Commit 53b61f8

Browse files
committed
Session API: remove SessionOptions
1 parent fec780f commit 53b61f8

File tree

13 files changed

+50
-115
lines changed

13 files changed

+50
-115
lines changed

examples/src/main/java/examples/SessionApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
import io.jooby.ExecutionMode;
99
import io.jooby.Jooby;
1010
import io.jooby.Session;
11-
import io.jooby.SessionOptions;
11+
import io.jooby.SessionStore;
1212

1313
public class SessionApp extends Jooby {
1414

1515
{
16-
setSessionOptions(new SessionOptions());
16+
setSessionStore(SessionStore.memory());
1717
get("/exists", ctx -> ctx.sessionOrNull() != null);
1818

1919
get("/create", ctx -> {

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,9 @@ public interface DefaultContext extends Context {
8989
@Override default @Nonnull Session session() {
9090
Session session = sessionOrNull();
9191
if (session == null) {
92-
Router router = getRouter();
93-
SessionOptions options = router.getSessionOptions();
94-
SessionStore store = options.getStore();
92+
SessionStore store = getRouter().getSessionStore();
9593
session = store.newSession(this);
9694
getAttributes().put(Session.NAME, session);
97-
// options.getSessionId().saveSessionId(this, sessionId);
9895
}
9996
return session;
10097
}
@@ -103,14 +100,8 @@ public interface DefaultContext extends Context {
103100
Session session = (Session) getAttributes().get(Session.NAME);
104101
if (session == null) {
105102
Router router = getRouter();
106-
SessionOptions options = router.getSessionOptions();
107-
SessionStore store = options.getStore();
103+
SessionStore store = router.getSessionStore();
108104
session = store.findSession(this);
109-
// if (session == null) {
110-
// return null;
111-
// }
112-
// session = Session.create(this, session);
113-
// strategy.saveSessionId(this, sessionId);
114105
}
115106
return session;
116107
}

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,12 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
517517
return System.getProperty(BASE_PACKAGE);
518518
}
519519

520-
@Nonnull @Override public SessionOptions getSessionOptions() {
521-
return router.getSessionOptions();
520+
@Nonnull @Override public SessionStore getSessionStore() {
521+
return router.getSessionStore();
522522
}
523523

524-
@Nonnull @Override public Jooby setSessionOptions(@Nonnull SessionOptions options) {
525-
router.setSessionOptions(options);
524+
@Nonnull @Override public Jooby setSessionStore(@Nonnull SessionStore store) {
525+
router.setSessionStore(store);
526526
return this;
527527
}
528528

jooby/src/main/java/io/jooby/Router.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,19 +628,21 @@ default Router error(@Nonnull Predicate<StatusCode> predicate,
628628
@Nonnull Router setRouterOptions(@Nonnull RouterOptions options);
629629

630630
/**
631-
* Get session options. Session store defines how HTTP session is managed it.
631+
* Session store. Default use a cookie ID with a memory storage.
632632
*
633-
* @return Session options.
633+
* See {@link SessionStore#memory()}.
634+
*
635+
* @return Session store.
634636
*/
635-
@Nonnull SessionOptions getSessionOptions();
637+
@Nonnull SessionStore getSessionStore();
636638

637639
/**
638-
* Set session options.
640+
* Set session store.
639641
*
640-
* @param options Session options.
642+
* @param store Session store.
641643
* @return This router.
642644
*/
643-
@Nonnull Router setSessionOptions(@Nonnull SessionOptions options);
645+
@Nonnull Router setSessionStore(@Nonnull SessionStore store);
644646

645647
/**
646648
* Get an executor from application registry.

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

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

jooby/src/main/java/io/jooby/SessionToken.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.annotation.Nullable;
1212
import java.security.SecureRandom;
1313
import java.time.Duration;
14+
import java.util.Base64;
1415

1516
/**
1617
* Find, save and delete a session token (cookie, header, parameter, etc)
@@ -99,6 +100,24 @@ public HeaderID(@Nonnull String name) {
99100
.setHttpOnly(true)
100101
.setPath("/");
101102

103+
/** Secure random for default session token generator. */
104+
SecureRandom RND = new SecureRandom();
105+
106+
/** Size of default token generator. */
107+
int ID_SIZE = 30;
108+
109+
/**
110+
* Generate a new token. This implementation produces an url encoder ID using a secure random
111+
* of {@link #ID_SIZE}.
112+
*
113+
* @return A new token.
114+
*/
115+
default @Nonnull String newToken() {
116+
byte[] bytes = new byte[ID_SIZE];
117+
RND.nextBytes(bytes);
118+
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
119+
}
120+
102121
/**
103122
* Find session ID.
104123
*

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
package io.jooby.internal;
77

88
import io.jooby.Context;
9-
import io.jooby.Router;
109
import io.jooby.Session;
1110
import io.jooby.SessionToken;
12-
import io.jooby.SessionOptions;
1311
import io.jooby.SessionStore;
1412

1513
import javax.annotation.Nonnull;
@@ -39,8 +37,7 @@ public MemorySessionStore(SessionToken token) {
3937
}
4038

4139
@Override public Session newSession(Context ctx) {
42-
SessionOptions options = sessionOptions(ctx);
43-
String sessionId = options.generateId();
40+
String sessionId = token.newToken();
4441
Instant now = Instant.now();
4542
Session session = Session.create(ctx, sessionId)
4643
.setCreationTime(now)
@@ -82,9 +79,4 @@ public MemorySessionStore(SessionToken token) {
8279
new SessionData(session.getCreationTime(), Instant.now(), session.toMap()));
8380
token.saveToken(ctx, sessionId);
8481
}
85-
86-
private static SessionOptions sessionOptions(Context ctx) {
87-
Router router = ctx.getRouter();
88-
return router.getSessionOptions();
89-
}
9082
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.jooby.Context;
1010
import io.jooby.RegistryException;
1111
import io.jooby.ServiceKey;
12+
import io.jooby.SessionStore;
1213
import io.jooby.StatusCodeException;
1314
import io.jooby.ErrorHandler;
1415
import io.jooby.ExecutionMode;
@@ -21,7 +22,6 @@
2122
import io.jooby.Router;
2223
import io.jooby.RouterOptions;
2324
import io.jooby.ServiceRegistry;
24-
import io.jooby.SessionOptions;
2525
import io.jooby.StatusCode;
2626
import io.jooby.TemplateEngine;
2727
import io.jooby.internal.asm.ClassSource;
@@ -134,7 +134,7 @@ public Stack executor(Executor executor) {
134134

135135
private ClassSource source;
136136

137-
private SessionOptions sessionOptions = new SessionOptions();
137+
private SessionStore sessionStore = SessionStore.memory();
138138

139139
private String flashName = "jooby.flash";
140140

@@ -304,12 +304,12 @@ public Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder en
304304
return newStack(pattern, action);
305305
}
306306

307-
@Nonnull @Override public SessionOptions getSessionOptions() {
308-
return sessionOptions;
307+
@Nonnull @Override public SessionStore getSessionStore() {
308+
return sessionStore;
309309
}
310310

311-
@Nonnull @Override public Router setSessionOptions(SessionOptions sessionOptions) {
312-
this.sessionOptions = sessionOptions;
311+
@Nonnull @Override public Router setSessionStore(SessionStore sessionStore) {
312+
this.sessionStore = sessionStore;
313313
return this;
314314
}
315315

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
package io.jooby.internal;
77

88
import io.jooby.Context;
9-
import io.jooby.Router;
109
import io.jooby.Session;
11-
import io.jooby.SessionOptions;
1210
import io.jooby.SessionStore;
1311
import io.jooby.Value;
1412
import io.jooby.ValueNode;
@@ -122,13 +120,8 @@ private void updateState() {
122120
store(ctx).touchSession(ctx, this);
123121
}
124122

125-
private static SessionOptions options(Context ctx) {
126-
Router router = ctx.getRouter();
127-
return router.getSessionOptions();
128-
}
129-
130123
private static SessionStore store(Context ctx) {
131-
return options(ctx).getStore();
124+
return ctx.getRouter().getSessionStore();
132125
}
133126

134127
}

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ void complete(Throwable x) {
494494
private void ifSaveSession() {
495495
Session session = (Session) getAttributes().get(Session.NAME);
496496
if (session != null && (session.isNew() || session.isModify())) {
497-
SessionStore store = getRouter().getSessionOptions().getStore();
497+
SessionStore store = router.getSessionStore();
498498
store.saveSession(this, session);
499499
}
500500
}

0 commit comments

Comments
 (0)