use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributes method getSession.
/**
* Exposes the {@link HttpSession} that we're wrapping.
* @param allowCreate whether to allow creation of a new session if none exists yet
*/
@Nullable
protected final HttpSession getSession(boolean allowCreate) {
if (isRequestActive()) {
HttpSession session = this.request.getSession(allowCreate);
this.session = session;
return session;
} else {
// Access through stored session reference, if any...
HttpSession session = this.session;
if (session == null) {
if (allowCreate) {
throw new IllegalStateException("No session found and request already completed - cannot create new session!");
} else {
session = this.request.getSession(false);
this.session = session;
}
}
return session;
}
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributes method registerSessionDestructionCallback.
/**
* Register the given callback as to be executed after session termination.
* <p>Note: The callback object should be serializable in order to survive
* web app restarts.
* @param name the name of the attribute to register the callback for
* @param callback the callback to be executed for destruction
*/
protected void registerSessionDestructionCallback(String name, Runnable callback) {
HttpSession session = obtainSession();
session.setAttribute(DESTRUCTION_CALLBACK_NAME_PREFIX + name, new DestructionCallbackBindingListener(callback));
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributes method setAttribute.
@Override
public void setAttribute(String name, Object value, int scope) {
if (scope == SCOPE_REQUEST) {
if (!isRequestActive()) {
throw new IllegalStateException("Cannot set request attribute - request is not active anymore!");
}
this.request.setAttribute(name, value);
} else {
HttpSession session = obtainSession();
this.sessionAttributesToUpdate.remove(name);
session.setAttribute(name, value);
}
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method buildRequestSessionWithExistingSession.
@Test
public void buildRequestSessionWithExistingSession() throws Exception {
String sessionId = "session-id";
webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
HttpSession session = actualRequest.getSession();
assertThat(session.getId()).isEqualTo(sessionId);
assertSingleSessionCookie("JSESSIONID=" + session.getId() + "; Path=/test; Domain=example.com");
requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getSession()).isEqualTo(session);
webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId + "NEW");
actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getSession()).isNotEqualTo(session);
assertSingleSessionCookie("JSESSIONID=" + actualRequest.getSession().getId() + "; Path=/test; Domain=example.com");
}
use of jakarta.servlet.http.HttpSession in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method buildRequestSession.
@Test
public void buildRequestSession() throws Exception {
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
HttpSession newSession = actualRequest.getSession();
assertThat(newSession).isNotNull();
assertSingleSessionCookie("JSESSIONID=" + newSession.getId() + "; Path=/test; Domain=example.com");
webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + newSession.getId());
requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getSession()).isSameAs(newSession);
}
Aggregations