Search in sources :

Example 1 with SessionManager

use of cn.taketoday.web.session.SessionManager in project today-infrastructure by TAKETODAY.

the class AbstractTemplateView method exposeSessionAttributes.

private void exposeSessionAttributes(Map<String, Object> model, RequestContext context) {
    SessionManager sessionManager = RequestContextUtils.getSessionManager(context);
    if (sessionManager != null) {
        WebSession session = sessionManager.getSession(context, false);
        if (session != null) {
            Map<String, Object> exposed = null;
            String[] attributeNames = session.getAttributeNames();
            for (String attribute : attributeNames) {
                if (model.containsKey(attribute) && !allowSessionOverride) {
                    throw new ViewRenderingException("Cannot expose session attribute '" + attribute + "' because of an existing model object of the same name");
                }
                Object attributeValue = session.getAttribute(attribute);
                if (log.isDebugEnabled()) {
                    exposed = exposed != null ? exposed : new LinkedHashMap<>();
                    exposed.put(attribute, attributeValue);
                }
                model.put(attribute, attributeValue);
            }
            if (log.isTraceEnabled() && exposed != null) {
                log.trace("Exposed session attributes to model: {}", exposed);
            }
        }
    }
}
Also used : WebSession(cn.taketoday.web.session.WebSession) SessionManager(cn.taketoday.web.session.SessionManager) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with SessionManager

use of cn.taketoday.web.session.SessionManager in project today-infrastructure by TAKETODAY.

the class WebApplicationContextUtils method registerWebApplicationScopes.

/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebServletApplicationContext.
 *
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableBeanFactory beanFactory, @Nullable ServletContext sc) {
    SessionManager sessionManager = BeanFactoryUtils.find(beanFactory, SessionManager.BEAN_NAME, SessionManager.class);
    if (sessionManager == null) {
        sessionManager = new DefaultSessionManager(new MemSessionRepository(new SessionEventDispatcher(), new SecureRandomSessionIdGenerator()), null);
    }
    beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
    beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(sessionManager));
    if (sc != null) {
        ServletContextScope appScope = new ServletContextScope(sc);
        beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
        // Register as ServletContext attribute, for ContextCleanupListener to detect it.
        sc.setAttribute(ServletContextScope.class.getName(), appScope);
    }
    beanFactory.registerDependency(HttpSession.class, new SessionObjectSupplier());
    beanFactory.registerDependency(ServletRequest.class, new RequestObjectSupplier());
    beanFactory.registerDependency(ServletResponse.class, new ResponseObjectSupplier());
    beanFactory.registerDependency(ServletContext.class, sc);
}
Also used : SessionEventDispatcher(cn.taketoday.web.session.SessionEventDispatcher) SecureRandomSessionIdGenerator(cn.taketoday.web.session.SecureRandomSessionIdGenerator) SessionManager(cn.taketoday.web.session.SessionManager) DefaultSessionManager(cn.taketoday.web.session.DefaultSessionManager) DefaultSessionManager(cn.taketoday.web.session.DefaultSessionManager) MemSessionRepository(cn.taketoday.web.session.MemSessionRepository)

Example 3 with SessionManager

use of cn.taketoday.web.session.SessionManager in project today-infrastructure by TAKETODAY.

the class SessionLocaleResolver method setSessionAttribute.

private void setSessionAttribute(RequestContext request, String attributeName, @Nullable Object attribute) {
    SessionManager sessionManager = getSessionManager(request);
    if (sessionManager != null) {
        WebSession session = sessionManager.getSession(request);
        session.setAttribute(attributeName, attribute);
    }
}
Also used : WebSession(cn.taketoday.web.session.WebSession) SessionManager(cn.taketoday.web.session.SessionManager)

Example 4 with SessionManager

use of cn.taketoday.web.session.SessionManager in project today-framework by TAKETODAY.

the class WebApplicationContextUtils method registerWebApplicationScopes.

/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebServletApplicationContext.
 *
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableBeanFactory beanFactory, @Nullable ServletContext sc) {
    SessionManager sessionManager = BeanFactoryUtils.find(beanFactory, SessionManager.BEAN_NAME, SessionManager.class);
    if (sessionManager == null) {
        sessionManager = new DefaultSessionManager(new InMemorySessionRepository(new SessionEventDispatcher(), new SecureRandomSessionIdGenerator()), null);
    }
    beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
    beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(sessionManager));
    if (sc != null) {
        ServletContextScope appScope = new ServletContextScope(sc);
        beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
        // Register as ServletContext attribute, for ContextCleanupListener to detect it.
        sc.setAttribute(ServletContextScope.class.getName(), appScope);
    }
    beanFactory.registerDependency(HttpSession.class, new SessionObjectSupplier());
    beanFactory.registerDependency(ServletRequest.class, new RequestObjectSupplier());
    beanFactory.registerDependency(ServletResponse.class, new ResponseObjectSupplier());
    beanFactory.registerDependency(ServletContext.class, sc);
}
Also used : SessionEventDispatcher(cn.taketoday.web.session.SessionEventDispatcher) SecureRandomSessionIdGenerator(cn.taketoday.web.session.SecureRandomSessionIdGenerator) SessionManager(cn.taketoday.web.session.SessionManager) DefaultSessionManager(cn.taketoday.web.session.DefaultSessionManager) DefaultSessionManager(cn.taketoday.web.session.DefaultSessionManager) InMemorySessionRepository(cn.taketoday.web.session.InMemorySessionRepository)

Example 5 with SessionManager

use of cn.taketoday.web.session.SessionManager in project today-framework by TAKETODAY.

the class AbstractTemplateView method exposeSessionAttributes.

private void exposeSessionAttributes(Map<String, Object> model, RequestContext context) {
    SessionManager sessionManager = RequestContextUtils.getSessionManager(context);
    if (sessionManager != null) {
        WebSession session = sessionManager.getSession(context, false);
        if (session != null) {
            Map<String, Object> exposed = null;
            String[] attributeNames = session.getAttributeNames();
            for (String attribute : attributeNames) {
                if (model.containsKey(attribute) && !allowSessionOverride) {
                    throw new ViewRenderingException("Cannot expose session attribute '" + attribute + "' because of an existing model object of the same name");
                }
                Object attributeValue = session.getAttribute(attribute);
                if (log.isDebugEnabled()) {
                    exposed = exposed != null ? exposed : new LinkedHashMap<>();
                    exposed.put(attribute, attributeValue);
                }
                model.put(attribute, attributeValue);
            }
            if (log.isTraceEnabled() && exposed != null) {
                log.trace("Exposed session attributes to model: {}", exposed);
            }
        }
    }
}
Also used : WebSession(cn.taketoday.web.session.WebSession) SessionManager(cn.taketoday.web.session.SessionManager) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SessionManager (cn.taketoday.web.session.SessionManager)6 WebSession (cn.taketoday.web.session.WebSession)4 DefaultSessionManager (cn.taketoday.web.session.DefaultSessionManager)2 SecureRandomSessionIdGenerator (cn.taketoday.web.session.SecureRandomSessionIdGenerator)2 SessionEventDispatcher (cn.taketoday.web.session.SessionEventDispatcher)2 LinkedHashMap (java.util.LinkedHashMap)2 InMemorySessionRepository (cn.taketoday.web.session.InMemorySessionRepository)1 MemSessionRepository (cn.taketoday.web.session.MemSessionRepository)1