Example usage for org.springframework.web.context.request RequestAttributes getSessionId

List of usage examples for org.springframework.web.context.request RequestAttributes getSessionId

Introduction

In this page you can find the example usage for org.springframework.web.context.request RequestAttributes getSessionId.

Prototype

String getSessionId();

Source Link

Document

Return an id for the current underlying session.

Usage

From source file:de.metas.ui.web.session.UserSession.java

public UserSession() {
    super();/*w  w w.j av a  2 s .c om*/
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    sessionId = requestAttributes.getSessionId();

    userPreference = new UserPreference();
    loggedIn = false;

    //
    // Set initial language
    try {
        final Locale locale = LocaleContextHolder.getLocale();
        final Language language = Language.getLanguage(locale);
        setLanguage(language);
    } catch (final Exception e) {
        logger.warn("Failed setting the language, but moving on", e);
    }

    logger.trace("User session created: {}", this);
}

From source file:de.metas.ui.web.session.WebRestApiContextProvider.java

private final Properties getActualContext() {
    ////from  w  w w.  ja v a  2 s  .  c  om
    // IMPORTANT: this method will be called very often, so please make sure it's FAST!
    //

    //
    // If there is currently a temporary context active, return it first
    final Properties temporaryCtx = temporaryCtxHolder.get();
    if (temporaryCtx != null) {
        logger.trace("Returning temporary context: {}", temporaryCtx);
        return temporaryCtx;
    }

    //
    // Get the context from current session
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null) {
        Properties userSessionCtx = (Properties) requestAttributes.getAttribute(ATTRIBUTE_UserSessionCtx,
                RequestAttributes.SCOPE_SESSION);
        if (userSessionCtx == null) {
            // Create user session context
            userSessionCtx = new Properties();
            Env.setContext(userSessionCtx, CTXNAME_IsServerContext, false);

            requestAttributes.setAttribute(ATTRIBUTE_UserSessionCtx, userSessionCtx,
                    RequestAttributes.SCOPE_SESSION);

            if (logger.isDebugEnabled()) {
                logger.debug("Created user session context: sessionId={}, context={}",
                        requestAttributes.getSessionId(), userSessionCtx);
            }
        }

        logger.trace("Returning user session context: {}", temporaryCtx);
        return userSessionCtx;
    }

    //
    // If there was no current session it means we are running on server side, so return the server context
    logger.trace("Returning server context: {}", temporaryCtx);
    return serverCtx;
}