forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.java
More file actions
513 lines (456 loc) · 13.6 KB
/
Copy pathSession.java
File metadata and controls
513 lines (456 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby;
import static java.util.Objects.requireNonNull;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* <p>
* Sessions are created on demand via: {@link Request#session()}.
* </p>
*
* <p>
* Sessions have a lot of uses cases but most commons are: auth, store information about current
* user, etc.
* </p>
*
* <p>
* A session attribute must be {@link String} or a primitive. Session doesn't allow to store
* arbitrary objects. It is a simple mechanism to store basic data.
* </p>
*
* <h1>Session configuration</h1>
*
* <h2>No timeout</h2>
* <p>
* There is no timeout for sessions from server perspective. By default, a session will expire when
* the user close the browser (a.k.a session cookie).
* </p>
*
* <h2>Session store</h2>
* <p>
* A {@link Session.Store} is responsible for saving session data. Sessions are kept in memory, by
* default using the {@link Session.Mem} store, which is useful for development, but wont scale well
* on production environments. An redis, memcached, ehcache store will be a better option.
* </p>
*
* <h3>Store life-cycle</h3>
* <p>
* Sessions are persisted every time a request exit, if they are dirty. A session get dirty if an
* attribute is added or removed from it.
* </p>
* <p>
* The <code>session.saveInterval</code> property indicates how frequently a session will be
* persisted (in millis).
* </p>
* <p>
* In short, a session is persisted when: 1) it is dirty; or 2) save interval has expired it.
* </p>
*
* <h1>Cookie configuration</h1>
* <p>
* Next session describe the most important options:
* </p>
*
* <h2>max-age</h2>
* <p>
* The <code>session.cookie.maxAge</code> sets the maximum age in seconds. A positive value
* indicates that the cookie will expire after that many seconds have passed. Note that the value is
* the <i>maximum</i> age when the cookie will expire, not the cookie's current age.
*
* A negative value means that the cookie is not stored persistently and will be deleted when the
* Web browser exits.
*
* Default maxAge is: <code>-1</code>.
*
* </p>
*
* <h2>signed cookie</h2>
* <p>
* If the <code>application.secret</code> property has been set, then the session cookie will be
* signed it with it.
* </p>
*
* <h2>cookie's name</h2>
* <p>
* The <code>session.cookie.name</code> indicates the name of the cookie that hold the session ID,
* by defaults: <code>jooby.sid</code>. Cookie's name can be explicitly set with
* {@link Cookie.Definition#name(String)} on {@link Session.Definition#cookie()}.
* </p>
*
* @author edgar
* @since 0.1.0
*/
public interface Session {
/**
* Hold session related configuration parameters.
*
* @author edgar
* @since 0.1.0
*/
class Definition {
/** Session store. */
private Object store;
/** Session cookie. */
private Cookie.Definition cookie;
/** Save interval. */
private Long saveInterval;
/**
* Creates a new session definition.
*
* @param store A session store.
*/
public Definition(final Class<? extends Store> store) {
this.store = requireNonNull(store, "A session store is required.");
cookie = new Cookie.Definition();
}
/**
* Creates a new session definition.
*
* @param store A session store.
*/
public Definition(final Store store) {
this.store = requireNonNull(store, "A session store is required.");
cookie = new Cookie.Definition();
}
/**
* Indicates how frequently a no-dirty session should be persisted (in millis).
*
* @return A save interval that indicates how frequently no dirty session should be persisted.
*/
public Optional<Long> saveInterval() {
return Optional.ofNullable(saveInterval);
}
/**
* Set/override how frequently a no-dirty session should be persisted (in millis).
*
* @param saveInterval Save interval in millis or <code>-1</code> for turning it off.
* @return This definition.
*/
public Definition saveInterval(final long saveInterval) {
this.saveInterval = saveInterval;
return this;
}
/**
* @return A session store instance or class.
*/
public Object store() {
return store;
}
/**
* @return Configure cookie session.
*/
public Cookie.Definition cookie() {
return cookie;
}
}
/**
* Read, save and delete sessions from a persistent storage.
*
* @author edgar
* @since 0.1.0
*/
interface Store {
/**
* Get a session by ID (if any).
*
* @param builder A session builder.
* @return A session or <code>null</code>.
*/
Session get(Session.Builder builder);
/**
* Save/persist a session.
*
* @param session A session to be persisted.
*/
void save(Session session);
void create(final Session session);
/**
* Delete a session by ID.
*
* @param id A session ID.
*/
void delete(String id);
/**
* Generate a session ID, default algorithm use an {@link UUID}.
*
* @return A unique session ID.
*/
default String generateID() {
UUID uuid = UUID.randomUUID();
return Long.toString(Math.abs(uuid.getMostSignificantBits()), 36)
+ Long.toString(Math.abs(uuid.getLeastSignificantBits()), 36);
}
}
/**
* A keep in memory session store.
*
* @author edgar
*/
class Mem implements Store {
private ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<String, Session>();
@Override
public void create(final Session session) {
sessions.putIfAbsent(session.id(), session);
}
@Override
public void save(final Session session) {
sessions.put(session.id(), session);
}
@Override
public Session get(final Session.Builder builder) {
return sessions.get(builder.sessionId());
}
@Override
public void delete(final String id) {
sessions.remove(id);
}
}
/**
* Build or restore a session from a persistent storage.
*
* @author edgar
*/
interface Builder {
/**
* @return Session ID.
*/
String sessionId();
/**
* Set a session local attribute.
*
* @param name Attribute's name.
* @param value Attribute's value.
* @return This builder.
*/
Builder set(final String name, final String value);
/**
* Set one ore more session local attributes.
*
* @param attributes Attributes to add.
* @return This builder.
*/
Builder set(final Map<String, String> attributes);
/**
* Set session created date.
*
* @param createdAt Session created date.
* @return This builder.
*/
Builder createdAt(long createdAt);
/**
* Set session last accessed date.
*
* @param accessedAt Session last accessed date.
* @return This builder.
*/
Builder accessedAt(long accessedAt);
/**
* Set session last saved it date.
*
* @param savedAt Session last saved it date.
* @return This builder.
*/
Builder savedAt(final long savedAt);
/**
* Final step to build a new session.
*
* @return A session.
*/
Session build();
}
/**
* @return Session ID.
*/
String id();
/**
* @return The time when this session was created, measured in milliseconds since midnight January
* 1, 1970 GMT.
*/
long createdAt();
/**
* @return Last time the session was save it.
*/
long savedAt();
/**
* The last time the client sent a request associated with this session, as the number of
* milliseconds since midnight January 1, 1970 GMT, and marked by the time the container
* received the request.
*
* <p>
* Actions that your application takes, such as getting or setting a value associated with the
* session, do not affect the access time.
* </p>
*
* @return Last time the client sent a request.
*/
long accessedAt();
/**
* @return The time when this session is going to expire, measured in milliseconds since midnight
* January 1, 1970 GMT.
*/
long expiryAt();
/**
* Get a object from this session. If the object isn't found this method returns an empty
* optional.
*
* @param name A local var's name.
* @return A value or empty optional.
*/
Mutant get(final String name);
/**
* @return An immutable copy of local attributes.
*/
Map<String, String> attributes();
/**
* Test if the var name exists inside the session local attributes.
*
* @param name A local var's name.
* @return True, for existing locals.
*/
boolean isSet(final String name);
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final byte value) {
return set(name, Byte.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final char value) {
return set(name, Character.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final boolean value) {
return set(name, Boolean.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final short value) {
return set(name, Short.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final int value) {
return set(name, Integer.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final long value) {
return set(name, Long.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final float value) {
return set(name, Float.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final double value) {
return set(name, Double.toString(value));
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
default Session set(final String name, final CharSequence value) {
return set(name, value.toString());
}
/**
* Set a session local using a the given name. If a local already exists, it will be replaced
* with the new value. Keep in mind that null values are NOT allowed.
*
* @param name A local's name.
* @param value A local's value.
* @return This session.
*/
Session set(final String name, final String value);
/**
* Remove a local value (if any) from session locals.
*
* @param name A local var's name.
* @return Existing value or empty optional.
*/
Mutant unset(final String name);
/**
* Unset/remove all the session data.
*
* @return This session.
*/
Session unset();
/**
* Invalidates this session then unset any objects bound to it.
*/
void destroy();
}