Skip to content

Commit 8c30697

Browse files
committed
session: doc + bugs
1 parent 78a7c64 commit 8c30697

File tree

17 files changed

+455
-63
lines changed

17 files changed

+455
-63
lines changed

TODO

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
* tests and coverage
22

3-
* cookie (request and response)
43
* rename application.port and server related properties to server.port
5-
* allow to configure context path and port + server properties from configuration file.
4+
* allow to configure context pa./javath and port + server properties from configuration file.
65
* websockets

etc/source/jooby-style.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@
8585
<module name="JavadocMethod">
8686
<property name="scope" value="public" />
8787
</module>
88-
<module name="JavadocType"/>
88+
<module name="JavadocType">
89+
<property name="scope" value="public" />
90+
</module>
8991
<module name="JavadocVariable">
9092
<property name="scope" value="public" />
9193
</module>

jooby/src/main/java/io/jooby/Context.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import io.jooby.internal.HashValue;
1919
import io.jooby.internal.MissingValue;
20-
import io.jooby.internal.RequestSessionStore;
21-
import io.jooby.internal.SessionImpl;
2220
import io.jooby.internal.SingleValue;
2321
import io.jooby.internal.UrlParser;
2422
import io.netty.buffer.ByteBuf;
@@ -122,6 +120,11 @@ public interface Context {
122120
* **********************************************************************************************
123121
*/
124122

123+
/**
124+
* Find a session or creates a new session.
125+
*
126+
* @return Session.
127+
*/
125128
default @Nonnull Session session() {
126129
Session session = sessionOrNull();
127130
if (session == null) {
@@ -135,12 +138,15 @@ public interface Context {
135138
setResponseCookie(sessionOptions.getCookie().setValue(newSession.getId()));
136139

137140
session = Session.create(this, newSession);
138-
139-
attribute("session", session);
140141
}
141142
return session;
142143
}
143144

145+
/**
146+
* Find an existing session.
147+
*
148+
* @return Exsiting session or <code>null</code>.
149+
*/
144150
default @Nullable Session sessionOrNull() {
145151
Session session = (Session) getAttributes().get("session");
146152
if (session == null) {
@@ -162,11 +168,16 @@ public interface Context {
162168
setResponseCookie(cookie.setValue(id));
163169
}
164170
session = Session.create(this, session);
165-
attribute("session", session);
166171
}
167172
return session;
168173
}
169174

175+
/**
176+
* Get a cookie matching the given name.
177+
*
178+
* @param name Cookie's name.
179+
* @return Cookie value.
180+
*/
170181
default @Nonnull Value cookie(@Nonnull String name) {
171182
String value = cookieMap().get(name);
172183
return value == null ? Value.missing(name) : Value.value(name, value);
@@ -933,6 +944,12 @@ default long getRequestLength() {
933944
*/
934945
@Nonnull Context setResponseLength(long length);
935946

947+
/**
948+
* Set/add a cookie to response.
949+
*
950+
* @param cookie Cookie to add.
951+
* @return This context.
952+
*/
936953
@Nonnull Context setResponseCookie(@Nonnull Cookie cookie);
937954

938955
/**

jooby/src/main/java/io/jooby/Cookie.java

Lines changed: 150 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,27 @@ public class Cookie {
4545
*/
4646
private long maxAge = -1;
4747

48+
/**
49+
* Creates a response cookie.
50+
*
51+
* @param name Cookie's name.
52+
* @param value Cookie's value or <code>null</code>.
53+
*/
4854
public Cookie(@Nonnull String name, @Nullable String value) {
4955
this.name = name;
5056
this.value = value;
5157
}
5258

59+
/**
60+
* Creates a response cookie without a value.
61+
*
62+
* @param name Cookie's name.
63+
*/
5364
public Cookie(@Nonnull String name) {
5465
this(name, null);
5566
}
5667

57-
public Cookie(@Nonnull Cookie cookie) {
68+
private Cookie(@Nonnull Cookie cookie) {
5869
this.domain = cookie.domain;
5970
this.value = cookie.value;
6071
this.name = cookie.name;
@@ -64,90 +75,220 @@ public Cookie(@Nonnull Cookie cookie) {
6475
this.httpOnly = cookie.httpOnly;
6576
}
6677

67-
public Cookie clone() {
78+
/**
79+
* Copy all state from this cookie and creates a new cookie.
80+
*
81+
* @return New cookie.
82+
*/
83+
public @Nonnull Cookie clone() {
6884
return new Cookie(this);
6985
}
7086

87+
/**
88+
* Cookie's name.
89+
*
90+
* @return Cookie's name.
91+
*/
7192
public @Nonnull String getName() {
7293
return name;
7394
}
7495

96+
/**
97+
* Set cookie's name.
98+
*
99+
* @param name Cookie's name.
100+
* @return This cookie.
101+
*/
75102
public @Nonnull Cookie setName(@Nonnull String name) {
76103
this.name = name;
77104
return this;
78105
}
79106

107+
/**
108+
* Cookie's value.
109+
*
110+
* @return Cookie's value.
111+
*/
80112
public @Nullable String getValue() {
81113
return value;
82114
}
83115

116+
/**
117+
* Set cookie's value.
118+
*
119+
* @param value Cookie's value.
120+
* @return This cookie.
121+
*/
84122
public @Nonnull Cookie setValue(@Nonnull String value) {
85123
this.value = value;
86124
return this;
87125
}
88126

127+
/**
128+
* Cookie's domain.
129+
*
130+
* @return Cookie's domain.
131+
*/
89132
public @Nullable String getDomain() {
90133
return domain;
91134
}
92135

136+
/**
137+
* Get cookie's domain.
138+
*
139+
* @param domain Defaults cookie's domain.
140+
* @return Cookie's domain..
141+
*/
93142
public @Nonnull String getDomain(@Nonnull String domain) {
94143
return this.domain == null ? domain : domain;
95144
}
96145

146+
/**
147+
* Set cookie's domain.
148+
*
149+
* @param domain Cookie's domain.
150+
* @return This cookie.
151+
*/
97152
public @Nonnull Cookie setDomain(@Nonnull String domain) {
98153
this.domain = domain;
99154
return this;
100155
}
101156

157+
/**
158+
* Cookie's path.
159+
*
160+
* @return Cookie's path.
161+
*/
102162
public @Nullable String getPath() {
103163
return path;
104164
}
105165

166+
/**
167+
* Cookie's path.
168+
*
169+
* @param path Defaults path.
170+
* @return Cookie's path.
171+
*/
106172
public @Nonnull String getPath(@Nonnull String path) {
107173
return this.path == null ? path : this.path;
108174
}
109175

176+
/**
177+
* Set cookie's path.
178+
*
179+
* @param path Cookie's path.
180+
* @return This cookie.
181+
*/
110182
public @Nonnull Cookie setPath(@Nonnull String path) {
111183
this.path = path;
112184
return this;
113185
}
114186

187+
/**
188+
* Cookie's http-only flag.
189+
*
190+
* @return Htto-only flag.
191+
*/
115192
public boolean isHttpOnly() {
116193
return httpOnly;
117194
}
118195

196+
/**
197+
* Set cookie's http-only.
198+
*
199+
* @param httpOnly Cookie's http-only.
200+
* @return This cookie.
201+
*/
119202
public Cookie setHttpOnly(boolean httpOnly) {
120203
this.httpOnly = httpOnly;
121204
return this;
122205
}
123206

207+
/**
208+
* Secure cookie.
209+
*
210+
* @return Secure cookie flag.
211+
*/
124212
public boolean isSecure() {
125213
return secure;
126214
}
127215

216+
/**
217+
* Set cookie secure flag..
218+
*
219+
* @param secure Cookie's secure.
220+
* @return This cookie.
221+
*/
128222
public @Nonnull Cookie setSecure(boolean secure) {
129223
this.secure = secure;
130224
return this;
131225
}
132226

227+
/**
228+
* Max age value:
229+
*
230+
* - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
231+
* - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
232+
* - <code>positive value</code>: indicates the number of seconds from current date, where browser
233+
* must expires the cookie.
234+
*
235+
* @return Max age, in seconds.
236+
*/
133237
public long getMaxAge() {
134238
return maxAge;
135239
}
136240

137-
public @Nonnull Cookie setMaxAge(long maxAge) {
138-
this.maxAge = maxAge;
139-
return this;
140-
}
141-
241+
/**
242+
* Set max age value:
243+
*
244+
* - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
245+
* - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
246+
* - <code>positive value</code>: indicates the number of seconds from current date, where browser
247+
* must expires the cookie.
248+
*
249+
* @param maxAge Cookie max age.
250+
* @return This options.
251+
*/
142252
public @Nonnull Cookie setMaxAge(@Nonnull Duration maxAge) {
143253
return setMaxAge(maxAge.getSeconds());
144254
}
145255

256+
/**
257+
* Set max age value:
258+
*
259+
* - <code>-1</code>: indicates a browser session. It is deleted when user closed the browser.
260+
* - <code>0</code>: indicates a cookie has expired and browser must delete the cookie.
261+
* - <code>positive value</code>: indicates the number of seconds from current date, where browser
262+
* must expires the cookie.
263+
*
264+
* @param maxAge Cookie max age, in seconds.
265+
* @return This options.
266+
*/
267+
public @Nonnull Cookie setMaxAge(long maxAge) {
268+
if (maxAge >= 0) {
269+
this.maxAge = maxAge;
270+
} else {
271+
this.maxAge = -1;
272+
}
273+
return this;
274+
}
275+
146276
@Override public String toString() {
147-
return name;
277+
StringBuilder buff = new StringBuilder();
278+
buff.append(name).append("=");
279+
if (value != null) {
280+
buff.append(value);
281+
}
282+
return buff.toString();
148283
}
149284

150-
public String toCookieString() {
285+
/**
286+
* Generates a cookie string. This is the value we sent to the client as <code>Set-Cookie</code>
287+
* header.
288+
*
289+
* @return Cookie string.
290+
*/
291+
public @Nonnull String toCookieString() {
151292
StringBuilder sb = new StringBuilder();
152293

153294
// name = value

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.slf4j.Logger;
1919

2020
import javax.annotation.Nonnull;
21-
import javax.annotation.Nullable;
2221
import javax.inject.Provider;
2322
import java.nio.file.Path;
2423
import java.util.ArrayList;
@@ -657,8 +656,19 @@ default Router error(@Nonnull Predicate<StatusCode> predicate,
657656
*/
658657
@Nonnull Router setRouterOptions(@Nonnull RouterOptions options);
659658

659+
/**
660+
* Get session options. Session store defines how HTTP session is managed it.
661+
*
662+
* @return Session options.
663+
*/
660664
@Nonnull SessionOptions getSessionOptions();
661665

666+
/**
667+
* Set session options.
668+
*
669+
* @param options Session options.
670+
* @return This router.
671+
*/
662672
@Nonnull Router setSessionOptions(@Nonnull SessionOptions options);
663673

664674
/**

0 commit comments

Comments
 (0)