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;
}
Aggregations