use of javax.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
*/
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 javax.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 = getSession(true);
session.setAttribute(DESTRUCTION_CALLBACK_NAME_PREFIX + name, new DestructionCallbackBindingListener(callback));
}
use of javax.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 = getSession(true);
this.sessionAttributesToUpdate.remove(name);
session.setAttribute(name, value);
}
}
use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.
the class WebUtils method getSessionAttribute.
/**
* Check the given request for a session attribute of the given name.
* Returns null if there is no session or if the session has no such attribute.
* Does not create a new session if none has existed before!
* @param request current HTTP request
* @param name the name of the session attribute
* @return the value of the session attribute, or {@code null} if not found
*/
public static Object getSessionAttribute(HttpServletRequest request, String name) {
Assert.notNull(request, "Request must not be null");
HttpSession session = request.getSession(false);
return (session != null ? session.getAttribute(name) : null);
}
use of javax.servlet.http.HttpSession in project spring-framework by spring-projects.
the class ServletRequestAttributesTests method updateAccessedAttributes.
@Test
public void updateAccessedAttributes() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpSession session = mock(HttpSession.class);
given(request.getSession(anyBoolean())).willReturn(session);
given(session.getAttribute(KEY)).willReturn(VALUE);
ServletRequestAttributes attrs = new ServletRequestAttributes(request);
assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION));
attrs.requestCompleted();
verify(session, times(2)).getAttribute(KEY);
verify(session).setAttribute(KEY, VALUE);
verifyNoMoreInteractions(session);
}
Aggregations