me.geso.avans.session.DefaultWebSessionManager Maven / Gradle / Ivy
package me.geso.avans.session;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import lombok.NonNull;
import me.geso.webscrew.response.WebResponse;
public class DefaultWebSessionManager implements
WebSessionManager {
@NonNull
private final HttpServletRequest request;
@NonNull
private final WebSessionStore sessionStore;
@NonNull
private final SessionCookieFactory sessionCookieFactory;
@NonNull
private final SessionIDGenerator sessionIDGenerator;
@NonNull
private final XSRFTokenCookieFactory xsrfTokenCookieFactory;
public DefaultWebSessionManager(HttpServletRequest request,
WebSessionStore sessionStore,
SessionIDGenerator sessionIDGenerator,
SessionCookieFactory sessionCookieFactory,
XSRFTokenCookieFactory xsrfTokenCookieFactory
) {
this.request = request;
this.sessionStore = sessionStore;
this.sessionIDGenerator = sessionIDGenerator;
this.sessionCookieFactory = sessionCookieFactory;
this.xsrfTokenCookieFactory = xsrfTokenCookieFactory;
}
private SessionData sessionData;
private boolean expired = false;
private SessionData getSessionData() {
if (this.expired) {
throw new IllegalStateException(
"You can't call this method for expired session.");
}
if (this.sessionData == null) {
final Optional loadSession = this.loadSession();
if (loadSession.isPresent()) {
return loadSession.get();
}
final String sessionId = this.generateSessionId();
this.sessionData = SessionData.startNewSession(sessionId);
return this.sessionData;
} else {
return this.sessionData;
}
}
private Optional loadSession() {
final Cookie[] cookies = this.request.getCookies();
if (cookies != null) {
for (final Cookie cookie : cookies) {
if (this.sessionCookieFactory.getName()
.equals(cookie.getName())) {
final String sessionId = cookie.getValue();
final Optional