Example usage for javax.servlet.http HttpSession setAttribute

List of usage examples for javax.servlet.http HttpSession setAttribute

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession setAttribute.

Prototype

public void setAttribute(String name, Object value);

Source Link

Document

Binds an object to this session, using the name specified.

Usage

From source file:org.openmrs.module.logmanager.web.util.WebUtils.java

/**
 * Gets an int parameter from the request that is being "remembered" in the session
 * @param request the http request/*from w ww .  java 2s  .  c  o  m*/
 * @param name the name of the parameter
 * @param def the default value of the parameter
 * @param sessionPrefix the prefix to generate session attribute from parameter name
 * @return the parameter value
 */
public static int getSessionedIntParameter(HttpServletRequest request, String name, int def,
        String sessionPrefix) {
    HttpSession session = request.getSession();
    int val = def;

    // If specified in request, read that and store in session
    if (request.getParameter(name) != null) {
        val = ServletRequestUtils.getIntParameter(request, name, def);
        session.setAttribute(sessionPrefix + name, val);
    }
    // Otherwise look for a matching attribute in the session
    else {
        Integer sessionVal = (Integer) session.getAttribute(sessionPrefix + name);
        if (sessionVal != null)
            val = sessionVal;
    }

    return val;
}

From source file:in.mycp.utils.Commons.java

public static void setSessionAttribute(String key, String val) {
    try {// w ww .j av a2  s  .  c  o m
        HttpSession session = WebContextFactory.get().getSession();
        session.setAttribute(key, val);
    } catch (Exception e) {
        log.error(e.getMessage());
    }
}

From source file:in.mycp.utils.Commons.java

public static void setSessionMsg(String msg) {
    try {//w  ww.j av a 2s.  c o m
        HttpSession session = WebContextFactory.get().getSession();
        session.setAttribute("session_msg", msg);
    } catch (Exception e) {
        log.error(e.getMessage());
    }
}

From source file:cn.vlabs.umt.ui.UMTContext.java

public static void saveUser(HttpSession session, LoginInfo info) {
    info = killNull(info);// w  w  w. j  av  a 2s.c o m
    if (info.getLoginNameInfo() == null || info.getUser() == null) {
        LOGGER.error("can't set null logininfo to session!");
        return;
    }
    session.setAttribute(Attributes.LOGIN_INFO, info);
}

From source file:com.netspective.sparx.security.EncryptedParametersFilter.java

public static final String getUserKey(final ServletRequest request) {
    HttpSession session = ((HttpServletRequest) request).getSession();
    String result = (String) session.getAttribute(SESSATTRNAME_ENCRYPTION_KEY);
    if (result == null) {
        try {//w ww  .j a va  2  s.c o  m
            result = GloballyUniqueIdentifier.getRandomGUID(true);
            session.setAttribute(SESSATTRNAME_ENCRYPTION_KEY, result);
        } catch (Exception e) {
            log.error("Error creating key", e);
            result = "really bad key (unsecure and not random)";
        }
    }
    return result;
}

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:org.runway.utils.AuthenticationUtils.java

public static void autoLogin(User user, HttpServletRequest request,
        AuthenticationManager authenticationManager) {

    //           GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
    //             user.getAuthority()) };

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
            user.getPassword(), user.getAuthorities());

    // generate session if one doesn't exist
    HttpSession session = request.getSession();

    token.setDetails(new WebAuthenticationDetails(request));
    Authentication authenticatedUser = authenticationManager.authenticate(token);

    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    // setting role to the session
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());

}

From source file:com.daimler.spm.storefront.util.CSRFTokenManager.java

/**
 * Returns the CSRF token for the provided httpSession.
 *
 * @param httpSession//from w  ww . j  av a2 s  . c  o  m
 * @return the CSRF token
 */
public static String getTokenForSession(final HttpSession httpSession) {
    String sessionCsrfToken = null;
    synchronized (httpSession) {
        sessionCsrfToken = (String) httpSession.getAttribute(CSRF_TOKEN_SESSION_ATTRIBUTE);
        if (StringUtils.isBlank(sessionCsrfToken)) {
            sessionCsrfToken = generateToken();
            httpSession.setAttribute(CSRF_TOKEN_SESSION_ATTRIBUTE, sessionCsrfToken);
        }
    }
    return sessionCsrfToken;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.authenticate.LogoutRedirector.java

private static Set<String> getRestrictedPageUris(HttpServletRequest request) {
    HttpSession session = request.getSession();

    @SuppressWarnings("unchecked")
    Set<String> restrictedPageUris = (Set<String>) session.getAttribute(ATTRIBUTE_RESTRICTED_PAGE_URIS);

    if (restrictedPageUris == null) {
        restrictedPageUris = new HashSet<String>();
        session.setAttribute(ATTRIBUTE_RESTRICTED_PAGE_URIS, restrictedPageUris);
    }//from w w  w  .j a  v a2s .  c o  m

    return restrictedPageUris;
}

From source file:be.fedict.eid.applet.service.impl.AuthenticationChallenge.java

/**
 * Generates a challenge and stores it in the given HTTP session for later
 * consumption./*from ww  w .j a  v  a  2s. c om*/
 * 
 * @param session
 * @return the challenge.
 */
public static byte[] generateChallenge(HttpSession session) {
    AuthenticationChallenge authenticationChallenge = new AuthenticationChallenge();
    if (null != session.getAttribute(AUTHN_CHALLENGE_SESSION_ATTRIBUTE)) {
        LOG.warn("overwriting a previous authentication challenge");
    }
    session.setAttribute(AUTHN_CHALLENGE_SESSION_ATTRIBUTE, authenticationChallenge);
    byte[] challenge = authenticationChallenge.getChallenge();
    return challenge;
}