Example usage for javax.servlet.http HttpServletRequest getSession

List of usage examples for javax.servlet.http HttpServletRequest getSession

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getSession.

Prototype

public HttpSession getSession(boolean create);

Source Link

Document

Returns the current <code>HttpSession</code> associated with this request or, if there is no current session and <code>create</code> is true, returns a new session.

Usage

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

public static String createXSRFToken(final HttpServletRequest request) throws IOException {
    // getSession(false) as this method never creates a new session
    HttpSession session = request.getSession(false);
    String xsrfSessionToken = (String) session.getAttribute(XSRF_TOKEN_NAME);
    if (xsrfSessionToken == null) {
        Random r = new Random(System.currentTimeMillis());
        long value = System.currentTimeMillis() + r.nextLong();
        char ids[] = session.getId().toCharArray();
        for (int i = 0; i < ids.length; i++) {
            value += ids[i] * (i + 1);/*from   w  w w. ja va2 s .com*/
        }
        xsrfSessionToken = Long.toString(value);
        session.setAttribute(XSRF_TOKEN_NAME, xsrfSessionToken);
    }
    return xsrfSessionToken;
}

From source file:info.magnolia.cms.security.Authenticator.java

/**
 * Get JAAS authenticated subject/*from   w w  w. j av  a 2  s.  co  m*/
 * @param request
 * @return Authenticated JAAS subject
 */
public static Subject getSubject(HttpServletRequest request) {
    HttpSession httpsession = request.getSession(false);
    if (httpsession != null) {
        return (Subject) httpsession.getAttribute(ATTRIBUTE_JAAS_SUBJECT);
    }
    return null;
}

From source file:info.magnolia.cms.security.Authenticator.java

/**
 * checks user session for attribute "user node"
 * @param request current HttpServletRequest
 * @return <code>true</code> if the user is authenticated, <code>false</code> otherwise
 *///  w w  w.j  a  va2  s .c om
public static boolean isAuthenticated(HttpServletRequest request) {
    HttpSession httpsession = request.getSession(false);
    if (httpsession != null) {
        Object user = httpsession.getAttribute(ATTRIBUTE_JAAS_SUBJECT);
        return user != null;
    }
    return false;
}

From source file:net.sourceforge.vulcan.web.struts.actions.ProjectReportBaseAction.java

static PreferencesDto findPreferences(HttpServletRequest request) {
    final HttpSession session = request.getSession(false);

    if (session != null) {
        final PreferencesDto prefs = (PreferencesDto) session.getAttribute(Keys.PREFERENCES);
        if (prefs != null) {
            return prefs;
        }//from w  ww.  ja va 2 s  . c  o m
    }

    return (PreferencesDto) request.getAttribute(Keys.PREFERENCES);
}

From source file:io.lavagna.web.helper.UserSession.java

public static void lock(HttpServletRequest req, HttpServletResponse resp) {
    HttpSession session = req.getSession(true);
    session.setAttribute(AUTH_LOCKED, true);
}

From source file:org.carewebframework.ui.util.RequestUtil.java

/**
 * Return current HttpSession given request.
 * /* w ww .j  a  v a  2  s  .c o m*/
 * @param request Http servlet request object.
 * @return HttpSession, null when invoked outside the scope of an Execution/ServletRequest
 */
public static HttpSession getSession(final HttpServletRequest request) {
    return request == null ? null : request.getSession(false);
}

From source file:edu.cornell.mannlib.vedit.beans.LoginStatusBean.java

/**
 * Get the current user, or null if not logged in.
 *//*from w  w w  .j  av a2s. c om*/
public static UserAccount getCurrentUser(HttpServletRequest request) {
    if (request == null) {
        return null;
    }
    return getCurrentUser(request.getSession(false));
}

From source file:edu.cornell.mannlib.vedit.beans.LoginStatusBean.java

/**
 * Get the bean from this request, or a dummy bean if the user is not logged
 * in. Never returns null./*from  ww w .j ava  2  s  .  co  m*/
 */
public static LoginStatusBean getBean(HttpServletRequest request) {
    if (request == null) {
        return DUMMY_BEAN;
    }

    HttpSession session = request.getSession(false);
    if (session == null) {
        return DUMMY_BEAN;
    }

    return getBean(session);
}

From source file:com.openforevent.main.UserPosition.java

public static String sendVisitID(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    GenericValue visit = VisitHandler.getVisit(request.getSession());
    String visitId = visit.getString("visitId");
    session.setAttribute("visitId", visitId);
    return visitId;
}

From source file:info.magnolia.cms.security.Authenticator.java

/**
 * @param request current HttpServletRequest
 * @return String , current logged in user
 *///from   w ww .j a v  a 2 s. c  o  m
public static String getUserId(HttpServletRequest request) {
    String userId = null;

    HttpSession httpsession = request.getSession(false);
    if (httpsession != null) {
        userId = (String) httpsession.getAttribute(ATTRIBUTE_USER_ID);
    }

    if (userId == null) {
        String credentials = request.getHeader("Authorization");
        if (credentials != null) {
            try {
                userId = getDecodedCredentials(credentials.substring(6).trim());
                if (httpsession != null) {
                    httpsession.setAttribute(ATTRIBUTE_USER_ID, userId);
                }
            } catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
        }
    }

    return userId;
}