Search in sources :

Example 1 with SessionInternal

use of io.vertx.ext.web.sstore.impl.SessionInternal in project vertx-web by vert-x3.

the class SessionHandlerImpl method flush.

private SessionHandler flush(RoutingContext context, boolean skipCrc, boolean ignoreStatus, Handler<AsyncResult<Void>> handler) {
    final boolean sessionUsed = context.isSessionAccessed();
    final Session session = context.session();
    if (session == null) {
        // No session in context
        handler.handle(Future.succeededFuture());
        return this;
    }
    if (!session.isDestroyed()) {
        final int currentStatusCode = context.response().getStatusCode();
        // Store the session (only and only if there was no error)
        if (ignoreStatus || (currentStatusCode >= 200 && currentStatusCode < 400)) {
            // store the current user into the session
            Boolean storeUser = context.get(SESSION_STOREUSER_KEY);
            if (storeUser != null && storeUser) {
                // during the request the user might have been removed
                if (context.user() != null) {
                    session.put(SESSION_USER_HOLDER_KEY, new UserHolder(context));
                }
            }
            if (session.isRegenerated()) {
                if (cookieless) {
                    // restore defaults
                    session.setAccessed();
                } else {
                    // the session cookie needs to be updated to the new id
                    final Cookie cookie = sessionCookie(context, session);
                    // restore defaults
                    session.setAccessed();
                    cookie.setValue(session.value());
                    setCookieProperties(cookie, false);
                }
                // we must invalidate the old id
                sessionStore.delete(session.oldId(), delete -> {
                    if (delete.failed()) {
                        handler.handle(Future.failedFuture(delete.cause()));
                    } else {
                        // we must wait for the result of the previous call in order to save the new one
                        sessionStore.put(session, put -> {
                            if (put.failed()) {
                                handler.handle(Future.failedFuture(put.cause()));
                            } else {
                                context.put(SESSION_FLUSHED_KEY, true);
                                if (session instanceof SessionInternal) {
                                    ((SessionInternal) session).flushed(skipCrc);
                                }
                                handler.handle(Future.succeededFuture());
                            }
                        });
                    }
                });
            } else if (!lazySession || sessionUsed) {
                if (!cookieless) {
                    // if lazy mode activated, no need to store the session nor to create the session cookie if not used.
                    sessionCookie(context, session);
                }
                session.setAccessed();
                sessionStore.put(session, put -> {
                    if (put.failed()) {
                        handler.handle(Future.failedFuture(put.cause()));
                    } else {
                        context.put(SESSION_FLUSHED_KEY, true);
                        if (session instanceof SessionInternal) {
                            ((SessionInternal) session).flushed(skipCrc);
                        }
                        handler.handle(Future.succeededFuture());
                    }
                });
            } else {
                // No-Op, just accept that the store skipped
                handler.handle(Future.succeededFuture());
            }
        } else {
            // No-Op, just accept that the store skipped
            handler.handle(Future.succeededFuture());
        }
    } else {
        if (!cookieless) {
            // invalidate the cookie as the session has been destroyed
            final Cookie expiredCookie = context.response().removeCookie(sessionCookieName);
            if (expiredCookie != null) {
                setCookieProperties(expiredCookie, true);
            }
        }
        // the old id must also be removed
        if (session.isRegenerated()) {
            sessionStore.delete(session.oldId(), delete -> {
                if (delete.failed()) {
                    handler.handle(Future.failedFuture(delete.cause()));
                } else {
                    // delete from the storage
                    sessionStore.delete(session.id(), delete2 -> {
                        if (delete2.failed()) {
                            handler.handle(Future.failedFuture(delete2.cause()));
                        } else {
                            context.put(SESSION_FLUSHED_KEY, true);
                            handler.handle(Future.succeededFuture());
                        }
                    });
                }
            });
        } else {
            // delete from the storage
            sessionStore.delete(session.id(), delete -> {
                if (delete.failed()) {
                    handler.handle(Future.failedFuture(delete.cause()));
                } else {
                    context.put(SESSION_FLUSHED_KEY, true);
                    handler.handle(Future.succeededFuture());
                }
            });
        }
    }
    return this;
}
Also used : Cookie(io.vertx.core.http.Cookie) Logger(io.vertx.core.impl.logging.Logger) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Session(io.vertx.ext.web.Session) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) RoutingContext(io.vertx.ext.web.RoutingContext) Future(io.vertx.core.Future) SessionInternal(io.vertx.ext.web.sstore.impl.SessionInternal) User(io.vertx.ext.auth.User) SessionStore(io.vertx.ext.web.sstore.SessionStore) CookieSameSite(io.vertx.core.http.CookieSameSite) SessionHandler(io.vertx.ext.web.handler.SessionHandler) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Cookie(io.vertx.core.http.Cookie) SessionInternal(io.vertx.ext.web.sstore.impl.SessionInternal) Session(io.vertx.ext.web.Session)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Promise (io.vertx.core.Promise)1 Vertx (io.vertx.core.Vertx)1 Cookie (io.vertx.core.http.Cookie)1 CookieSameSite (io.vertx.core.http.CookieSameSite)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 Logger (io.vertx.core.impl.logging.Logger)1 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)1 User (io.vertx.ext.auth.User)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 Session (io.vertx.ext.web.Session)1 SessionHandler (io.vertx.ext.web.handler.SessionHandler)1 SessionStore (io.vertx.ext.web.sstore.SessionStore)1 SessionInternal (io.vertx.ext.web.sstore.impl.SessionInternal)1