forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionImpl.java
More file actions
139 lines (107 loc) · 3.06 KB
/
SessionImpl.java
File metadata and controls
139 lines (107 loc) · 3.06 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.internal;
import io.jooby.Context;
import io.jooby.Session;
import io.jooby.SessionStore;
import io.jooby.Value;
import io.jooby.ValueNode;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class SessionImpl implements Session {
private Context ctx;
private boolean isNew;
private String id;
private Map<String, String> attributes;
private Instant creationTime;
private Instant lastAccessedTime;
private boolean modify;
public SessionImpl(Context ctx, String id) {
this(ctx, id, new ConcurrentHashMap<>());
}
public SessionImpl(Context ctx, String id, Map<String, String> attributes) {
this.ctx = ctx;
this.id = id;
this.attributes = attributes;
}
@Override public boolean isNew() {
return isNew;
}
@Nonnull @Override public Session setNew(boolean aNew) {
this.isNew = aNew;
return this;
}
@Override public boolean isModify() {
return modify;
}
@Nonnull @Override public Session setModify(boolean modify) {
this.modify = modify;
return this;
}
@Override public @Nullable String getId() {
return id;
}
@Nonnull @Override public Session setId(@Nullable String id) {
this.id = id;
return this;
}
@Override public @Nonnull Value get(@Nonnull String name) {
return Value.create(ctx, name, attributes.get(name));
}
@Override public @Nonnull Session put(@Nonnull String name, String value) {
attributes.put(name, value);
updateState();
return this;
}
@Override public @Nonnull ValueNode remove(@Nonnull String name) {
String value = attributes.remove(name);
updateState();
return value == null ? Value.missing(name) : Value.value(ctx, name, value);
}
@Override public @Nonnull Map<String, String> toMap() {
return attributes;
}
@Override public @Nonnull Instant getCreationTime() {
return creationTime;
}
@Nonnull @Override public Session setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
return this;
}
@Override public @Nonnull Instant getLastAccessedTime() {
return lastAccessedTime;
}
@Override public @Nonnull Session setLastAccessedTime(@Nonnull Instant lastAccessedTime) {
this.lastAccessedTime = lastAccessedTime;
return this;
}
@Override public Session clear() {
attributes.clear();
updateState();
return this;
}
@Override public void destroy() {
ctx.getAttributes().remove(NAME);
attributes.clear();
store(ctx).deleteSession(ctx, this);
}
@Override public Session renewId() {
store(ctx).renewSessionId(ctx, this);
updateState();
return this;
}
private void updateState() {
modify = true;
lastAccessedTime = Instant.now();
store(ctx).touchSession(ctx, this);
}
private static SessionStore store(Context ctx) {
return ctx.getRouter().getSessionStore();
}
}