Skip to content

Commit 37a9c97

Browse files
committed
Add Session.Destroyed exception and Session.isDestroyed method
- session: add isDestroyed method fix jooby-project#892 - session: throw Session.Destroyed when access to a destroyed session fix jooby-project#891 - Exception while accessing session variable in Freemarker template fix jooby-project#838
1 parent 2a14425 commit 37a9c97

File tree

8 files changed

+302
-205
lines changed

8 files changed

+302
-205
lines changed

jooby/src/main/java/org/jooby/Session.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@
294294
*/
295295
public interface Session {
296296

297+
/**
298+
* Throw when session access is required but the session has been destroyed.\
299+
*
300+
* See {@link Session#destroy()}.
301+
*/
302+
class Destroyed extends RuntimeException {
303+
public Destroyed() {
304+
super("Session has been destroyed.");
305+
}
306+
}
307+
297308
/** Global/Shared id of cookie sessions. */
298309
String COOKIE_SESSION = "cookieSession";
299310

@@ -740,8 +751,15 @@ default Session set(final String name, final CharSequence value) {
740751
Session unset();
741752

742753
/**
743-
* Invalidates this session then unset any objects bound to it.
754+
* Invalidates this session then unset any objects bound to it. This is a noop if the session has
755+
* been destroyed.
744756
*/
745757
void destroy();
746758

759+
/**
760+
* True if the session was {@link #destroy()}.
761+
*
762+
* @return True if the session was {@link #destroy()}.
763+
*/
764+
boolean isDestroyed();
747765
}

jooby/src/main/java/org/jooby/internal/RequestScopedSession.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,57 +248,68 @@ public RequestScopedSession(final SessionManager sm, final Response rsp,
248248

249249
@Override
250250
public String id() {
251+
notDestroyed();
251252
return session.id();
252253
}
253254

254255
@Override
255256
public long createdAt() {
257+
notDestroyed();
256258
return session.createdAt();
257259
}
258260

259261
@Override
260262
public long accessedAt() {
263+
notDestroyed();
261264
return session.accessedAt();
262265
}
263266

264267
@Override
265268
public long savedAt() {
269+
notDestroyed();
266270
return session.savedAt();
267271
}
268272

269273
@Override
270274
public long expiryAt() {
275+
notDestroyed();
271276
return session.expiryAt();
272277
}
273278

274279
@Override
275280
public Mutant get(final String name) {
281+
notDestroyed();
276282
return session.get(name);
277283
}
278284

279285
@Override
280286
public Map<String, String> attributes() {
287+
notDestroyed();
281288
return session.attributes();
282289
}
283290

284291
@Override
285292
public Session set(final String name, final String value) {
293+
notDestroyed();
286294
session.set(name, value);
287295
return this;
288296
}
289297

290298
@Override
291299
public boolean isSet(final String name) {
300+
notDestroyed();
292301
return session.isSet(name);
293302
}
294303

295304
@Override
296305
public Mutant unset(final String name) {
306+
notDestroyed();
297307
return session.unset(name);
298308
}
299309

300310
@Override
301311
public Session unset() {
312+
notDestroyed();
302313
session.unset();
303314
return this;
304315
}
@@ -326,12 +337,24 @@ public void destroy() {
326337
}
327338
}
328339

340+
@Override public boolean isDestroyed() {
341+
return session == null || session.isDestroyed();
342+
}
343+
329344
@Override
330345
public String toString() {
346+
notDestroyed();
331347
return session.toString();
332348
}
333349

334350
public Session session() {
351+
notDestroyed();
335352
return session;
336353
}
354+
355+
private void notDestroyed() {
356+
if (isDestroyed()) {
357+
throw new Session.Destroyed();
358+
}
359+
}
337360
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ public Session build() {
301301

302302
private volatile long savedAt;
303303

304+
private volatile boolean destroyed;
305+
304306
private ParserExecutor resolver;
305307

306308
public SessionImpl(final ParserExecutor resolver, final boolean isNew, final String sessionId,
@@ -384,9 +386,14 @@ public Session unset() {
384386

385387
@Override
386388
public void destroy() {
389+
destroyed = true;
387390
unset();
388391
}
389392

393+
@Override public boolean isDestroyed() {
394+
return destroyed;
395+
}
396+
390397
public boolean isNew() {
391398
return isNew;
392399
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.Results;
4+
import org.jooby.ftl.Ftl;
5+
import org.jooby.test.ServerFeature;
6+
import org.junit.Test;
7+
8+
public class Issue838 extends ServerFeature {
9+
10+
{
11+
use(new Ftl());
12+
13+
before((req, rsp) -> {
14+
req.set("session", req.session());
15+
});
16+
17+
get("/838", req -> Results.html("org/jooby/ftl/838"));
18+
}
19+
20+
@Test
21+
public void shouldIgnoreDestroyedSession() throws Exception {
22+
request()
23+
.get("/838")
24+
.expect("");
25+
}
26+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<#if (session.uname)??>
2+
....
3+
</#if>

0 commit comments

Comments
 (0)