Search in sources :

Example 91 with HttpSession

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;
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession)

Example 92 with HttpSession

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));
}
Also used : HttpSession(javax.servlet.http.HttpSession)

Example 93 with HttpSession

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);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession)

Example 94 with HttpSession

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);
}
Also used : HttpSession(javax.servlet.http.HttpSession)

Example 95 with HttpSession

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Aggregations

HttpSession (javax.servlet.http.HttpSession)730 HttpServletRequest (javax.servlet.http.HttpServletRequest)151 Test (org.junit.Test)110 IOException (java.io.IOException)80 HttpServletResponse (javax.servlet.http.HttpServletResponse)80 ServletException (javax.servlet.ServletException)75 ArrayList (java.util.ArrayList)65 RequestDispatcher (javax.servlet.RequestDispatcher)59 HashMap (java.util.HashMap)48 Map (java.util.Map)44 Locale (java.util.Locale)39 Properties (java.util.Properties)39 PrintWriter (java.io.PrintWriter)38 Cookie (javax.servlet.http.Cookie)27 List (java.util.List)24 SQLException (java.sql.SQLException)23 WebUser (org.compiere.util.WebUser)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 ModelAndView (org.springframework.web.servlet.ModelAndView)20