Skip to content

Commit

Permalink
Pass createSession to native getSession call
Browse files Browse the repository at this point in the history
* Pass createSession variable
* Use Optional.of
* Use final everywhere for params
* Make getNativeSession return Optional
  • Loading branch information
amaechler committed Apr 22, 2024
1 parent 7fca3ff commit 99c501a
Showing 1 changed file with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,77 +20,79 @@ public class ServletSessionStore implements SessionStore {

public static final ServletSessionStore INSTANCE = new ServletSessionStore();

protected HttpSession httpSession;
protected ServletSessionStore() {}

protected ServletSessionStore() {
}

protected ServletSessionStore(final HttpSession httpSession) {
this.httpSession = httpSession;
}

public HttpSession getHttpSession(WebContext context) {
public Optional<HttpSession> getNativeSession(final WebContext context, final boolean createSession) {
assert context instanceof ServletJaxRsContext;
return ((ServletJaxRsContext) context).getRequest().getSession();

final HttpSession nativeSession = ((ServletJaxRsContext) context).getRequest().getSession(createSession);
return Optional.ofNullable(nativeSession);
}

@Override
public Optional<Object> get(WebContext context, String key) {
return Optional.ofNullable(getHttpSession(context).getAttribute(key));
public Optional<Object> get(final WebContext context, final String key) {
return getNativeSession(context, false)
.map(it -> it.getAttribute(key));
}

@Override
public void set(WebContext context, String key, Object value) {
if (value == null) {
getHttpSession(context).removeAttribute(key);
} else {
getHttpSession(context).setAttribute(key, value);
}
public void set(final WebContext context, final String key, final Object value) {
getNativeSession(context, value != null)
.ifPresent(it -> {
if (value == null) {
it.removeAttribute(key);
} else {
it.setAttribute(key, value);
}
});
}

@Override
public boolean destroySession(WebContext context) {
final HttpSession session = getHttpSession(context);

session.invalidate();

return true;
public boolean destroySession(final WebContext context) {
return getNativeSession(context, false)
.map(it -> {
it.invalidate();
return true;
})
.orElse(false);
}

@Override
public Optional<Object> getTrackableSession(WebContext context) {
return Optional.ofNullable(getHttpSession(context));
public Optional<Object> getTrackableSession(final WebContext context) {
return Optional.ofNullable(getNativeSession(context, false));
}

@Override
public boolean renewSession(WebContext context) {
final HttpSession session = getHttpSession(context);
final Map<String, Object> attributes = new HashMap<>();
Collections.list(session.getAttributeNames()).forEach(k -> attributes.put(k, session.getAttribute(k)));

session.invalidate();

// let's recreate the session from zero, the previous becomes
// generally unusable depending on the servlet implementation
final HttpSession newSession = getHttpSession(context);
attributes.forEach(newSession::setAttribute);

return true;
public boolean renewSession(final WebContext context) {
return getNativeSession(context, false)
.map(it -> {
final Map<String, Object> attributes = new HashMap<>();
Collections.list(it.getAttributeNames()).forEach(k -> attributes.put(k, it.getAttribute(k)));

it.invalidate();

// let's recreate the session from zero, the previous becomes
// generally unusable depending on the servlet implementation
getNativeSession(context, true)
.ifPresent(newSession -> attributes.forEach(newSession::setAttribute));

return true;
})
.orElse(false);
}

@Override
public Optional<SessionStore> buildFromTrackableSession(WebContext context, Object trackableSession) {
return Optional.ofNullable(new ServletSessionStore() {
public Optional<SessionStore> buildFromTrackableSession(final WebContext context, final Object trackableSession) {
return Optional.of(new ServletSessionStore() {
@Override
public HttpSession getHttpSession(WebContext context) {
return (HttpSession) trackableSession;
public Optional<HttpSession> getNativeSession(WebContext context, boolean createSession) {
return Optional.of((HttpSession) trackableSession);
}
});
}

@Override
public Optional<String> getSessionId(WebContext context, boolean createSession) {
HttpSession session = getHttpSession(context);
return (session != null) ? Optional.of(session.getId()) : Optional.empty();
public Optional<String> getSessionId(final WebContext context, final boolean createSession) {
return getNativeSession(context, createSession).map(HttpSession::getId);
}
}

0 comments on commit 99c501a

Please sign in to comment.