@Override public void start() throws Exception { Router router = Router.router(vertx); router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); router.route().handler(routingContext -> { Session session = routingContext.session(); Integer cnt = session.get("hitcount"); cnt = (cnt == null ? 0 : cnt) + 1; session.put("hitcount", cnt); routingContext.response().putHeader("content-type", "text/html") .end("<html><body><h1>Hitcount: " + cnt + "</h1></body></html>"); }); vertx.createHttpServer().requestHandler(router).listen(8080); } }
private void stuffSession(Session session) { session.put("somelong", 123456L); session.put("someint", 1234); session.put("someshort", (short) 123); session.put("somebyte", (byte) 12); session.put("somedouble", 123.456d); session.put("somefloat", 123.456f); session.put("somechar", 'X'); session.put("somebooleantrue", true); session.put("somebooleanfalse", false); session.put("somestring", "wibble"); session.put("somebytes", bytes); session.put("somebuffer", buffer); session.put("someclusterserializable", new JsonObject().put("foo", "bar")); }
@Override public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { Session session = context.session(); if (session != null) { // Now redirect to the login url - we'll get redirected back here after successful login session.put(returnURLParam, context.request().uri()); handler.handle(Future.failedFuture(new HttpStatusException(302, loginRedirectURL))); } else { handler.handle(Future.failedFuture("No session - did you forget to include a SessionHandler?")); } } }
/** * Put some data in a session * @param key the key for the data * @param obj the data * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.Session put(String key, Object obj) { delegate.put(key, obj); return this; }
/** * Put some data in a session * @param key the key for the data * @param obj the data * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.Session put(String key, Object obj) { delegate.put(key, obj); return this; }
@Override public void set(String key, Object value) { vertxSession.put(key, value); }
@Test public void testLastAccessed2() throws Exception { router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(store)); AtomicReference<Session> rid = new AtomicReference<>(); router.route().handler(rc -> { rid.set(rc.session()); rc.session().put("foo", "bar"); vertx.setTimer(1000, tid -> rc.response().end()); }); testRequest(HttpMethod.GET, "/", 200, "OK"); // accessed() is called after request too assertTrue(rid.get().lastAccessed() - System.currentTimeMillis() < 500); }
@Test public void testIssue172_setnull() throws Exception { router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(store)); AtomicReference<Session> rid = new AtomicReference<>(); router.route().handler(rc -> { rid.set(rc.session()); rc.session().put("foo", null); vertx.setTimer(1000, tid -> rc.response().end()); }); testRequest(HttpMethod.GET, "/", 200, "OK"); }
session.put(SESSION_USER_HOLDER_KEY, new UserHolder(routingContext));
switch (requestCount.get()) { case 0: sess.put("foo", "bar"); break; case 1:
rc.session().put("foo", "foo_value"); rc.response().end(); });
case 0: rid.set(sess.id()); sess.put("foo", "bar"); sess.destroy(); break;
/** * Set the current user as property into the Context session * * @param user * @param context */ public static final void setCurrentUser(IAuthenticatable user, RoutingContext context) { context.session().put(IAuthenticatable.CURRENT_USER_PROPERTY, user); }
@Override public void set(VertxWebContext context, String key, Object value) { context.getVertxSession().put(key, value); }
public static io.vertx.ext.web.Session put(io.vertx.ext.web.Session j_receiver, java.lang.String key, java.lang.Object obj) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.put(key, io.vertx.core.impl.ConversionHelper.toObject(obj))); return j_receiver; } public static <T>java.lang.Object get(io.vertx.ext.web.Session j_receiver, java.lang.String key) {
@Override public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { Session session = context.session(); if (session != null) { // Now redirect to the login url - we'll get redirected back here after successful login session.put(returnURLParam, context.request().uri()); handler.handle(Future.failedFuture(new HttpStatusException(302, loginRedirectURL))); } else { handler.handle(Future.failedFuture("No session - did you forget to include a SessionHandler?")); } } }
@Test public void testLastAccessed2() throws Exception { router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(store)); AtomicReference<Session> rid = new AtomicReference<>(); router.route().handler(rc -> { rid.set(rc.session()); rc.session().put("foo", "bar"); vertx.setTimer(1000, tid -> rc.response().end()); }); testRequest(HttpMethod.GET, "/", 200, "OK"); // accessed() is called after request too assertTrue(rid.get().lastAccessed() - System.currentTimeMillis() < 500); }
@Test public void testIssue172_setnull() throws Exception { router.route().handler(CookieHandler.create()); router.route().handler(SessionHandler.create(store)); AtomicReference<Session> rid = new AtomicReference<>(); router.route().handler(rc -> { rid.set(rc.session()); rc.session().put("foo", null); vertx.setTimer(1000, tid -> rc.response().end()); }); testRequest(HttpMethod.GET, "/", 200, "OK"); }