Skip to content

Instantly share code, notes, and snippets.

@danhyun
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danhyun/bfeb82ef96674d8d90a3 to your computer and use it in GitHub Desktop.
Save danhyun/bfeb82ef96674d8d90a3 to your computer and use it in GitHub Desktop.
Using sessions in Ratpack - `0.9.17` + `0.9.18-SNAPSHOT`
import ratpack.exec.Operation;
import ratpack.exec.Promise;
import ratpack.guice.Guice;
import ratpack.handling.Context;
import ratpack.session.Session;
import ratpack.session.SessionKey;
import ratpack.session.SessionModule;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.assertEquals;
public class Example1 {
public static void main(String[] args) throws Exception {
EmbeddedApp.of(s -> s
.registry(
Guice.registry(b -> b.module(SessionModule.class)))
.handlers(c -> c
.get(":username", ctx -> {
String username = ctx.getPathTokens().get("username");
setSessionValue(ctx, "username", username)
.then(() -> ctx.getResponse().send("Set " + username));
})
.all(ctx -> {
getSessionValue(ctx, "username")
.map(username ->
username == null ? "anon" : username)
.then(username ->
ctx.getResponse()
.send("Welcome, " + username + "!"));
})
))
.test(testClient -> {
assertEquals("Welcome, anon!", testClient.getText());
assertEquals("set ratpack", testClient.getText("/ratpack"));
assertEquals("Welcome, ratpack!", testClient.getText());
});
}
private static Operation setSessionValue(Context ctx, String keyName, String value) {
return ctx.get(Session.class).getData().operation(sessionData -> {
SessionKey<String> key = SessionKey.of(keyName, String.class);
sessionData.set(key, value);
});
}
private static Promise<String> getSessionValue(Context ctx, String keyName) {
return ctx.get(Session.class).getData().map(sessionData -> {
SessionKey<String> key = SessionKey.of(keyName, String.class);
return sessionData.get(key).orElse(null);
});
}
}
/*
* Copyright 2015 the original author or authors.
*
* Licensed 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.
*/
import ratpack.exec.Operation;
import ratpack.exec.Promise;
import ratpack.guice.Guice;
import ratpack.handling.Context;
import ratpack.session.Session;
import ratpack.session.SessionKey;
import ratpack.session.SessionModule;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.assertEquals;
public class Example2 {
public static void main(String[] args) throws Exception {
EmbeddedApp.of(s -> s
.registry(
Guice.registry(b -> b.module(SessionModule.class)))
.handlers(c -> c
.get(":username", ctx -> {
String username = ctx.getPathTokens().get("username");
ctx.render(
setSessionValue(ctx, "username", username)
.map(() -> "Set " + username));
})
.all(ctx -> {
ctx.render(
getSessionValue(ctx, "username")
.map(username ->
username == null ? "anon" : username)
.map(username -> "Welcome, " + username + "!"));
})
))
.test(testClient -> {
assertEquals("Welcome, anon!", testClient.getText());
assertEquals("Set ratpack", testClient.getText("/ratpack"));
assertEquals("Welcome, ratpack!", testClient.getText());
});
}
private static Operation setSessionValue(Context ctx, String keyName, String value) {
return ctx.get(Session.class).getData().operation(sessionData -> {
SessionKey<String> key = SessionKey.of(keyName, String.class);
sessionData.set(key, value);
});
}
private static Promise<String> getSessionValue(Context ctx, String keyName) {
return ctx.get(Session.class).getData().map(sessionData -> {
SessionKey<String> key = SessionKey.of(keyName, String.class);
return sessionData.get(key).orElse(null);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment