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:com.facetime.communication.activemq.AmqConsumer.java

/**
 * Helper method to get the client for the current session, lazily creating
 * a client if there is none currently/*from w w w .j ava2  s  .c  o  m*/
 * 
 * @param request
 *            is the current HTTP request
 * @return the current client or a newly creates
 */
public static AmqConsumer getWebClient(HttpServletRequest request) {
    HttpSession session = request.getSession(true);
    AmqConsumer client = getWebClient(session);
    if (client == null || client.isClosed()) {
        client = AmqConsumer.createWebClient(request);
        session.setAttribute(WEB_CLIENT_ATTRIBUTE, client);
    }

    return client;
}

From source file:org.sventon.model.UserRepositoryContext.java

/**
 * Gets the UserContext instance from the user's HTTPSession.
 * If session does not exist, it will be created. If the attribute
 * <code>userContext</code> does not exists, a new instance will be
 * created and added to the session.//  w ww. j  a  va2s  .  c  om
 *
 * @param request        The HTTP request.
 * @param repositoryName Instance name.
 * @return The UserContext instance.
 * @see UserContext
 */
public static UserRepositoryContext getContext(final HttpServletRequest request,
        final RepositoryName repositoryName) {
    final HttpSession session = request.getSession(true);
    final String userName = ServletRequestUtils.getStringParameter(request, "userName", "");
    final String userPassword = ServletRequestUtils.getStringParameter(request, "userPassword", "");

    final UserContext userContext = (UserContext) WebUtils.getOrCreateSessionAttribute(session, "userContext",
            UserContext.class);

    UserRepositoryContext userRepositoryContext = userContext.getUserRepositoryContext(repositoryName);
    if (userRepositoryContext == null) {
        userRepositoryContext = new UserRepositoryContext();
        userContext.add(repositoryName, userRepositoryContext);
    }

    if (!StringUtils.isEmpty(userName) && !StringUtils.isEmpty(userPassword)) {
        userRepositoryContext.setCredentials(new Credentials(userName, userPassword));
    }
    return userRepositoryContext;
}

From source file:com.liusoft.dlog4j.UserLoginManager.java

/**
 * ?/*from www.j  a  v a2s .co  m*/
 * 
 * @param req
 * @param ubean
 */
public static void updateLoginUser(HttpServletRequest req, UserBean ubean) {
    HttpSession ssn = req.getSession(true);
    if (ssn != null && ubean != null) {
        ssn.setAttribute(SESSION_USER_KEY, SessionUserObject.copyFrom(ubean));
    }
}

From source file:com.jdon.strutsutil.FormBeanUtil.java

/**
 * lookup ActionForm in/*from ww w  .  j a  v  a 2 s .  co m*/
 * 
 * @param request
 * @return
 */
public static ActionForm lookupActionForm(HttpServletRequest request, String formName) {
    ActionForm actionForm = null;
    actionForm = (ActionForm) request.getAttribute(formName);
    if (actionForm == null && request.getSession(false) != null) {
        HttpSession session = request.getSession(false);
        actionForm = (ActionForm) session.getAttribute(formName);
    }
    return actionForm;
}

From source file:de.zib.gndms.kit.monitor.GroovyMoniServlet.java

private static HttpSession getSessionOrFail(@NotNull HttpServletRequest servletRequest) {
    HttpSession session = servletRequest.getSession(false);
    if (session == null)
        throw preCondFailed("No session found");
    return session;
}

From source file:org.artifactory.webapp.servlet.RequestUtils.java

public static boolean setAuthentication(HttpServletRequest request, Authentication authentication,
        boolean createSession) {
    HttpSession session = request.getSession(createSession);
    if (session == null) {
        return false;
    }// w  ww.  j a va2  s.  com
    session.setAttribute(LAST_USER_KEY, authentication);
    return true;
}

From source file:de.zib.gndms.kit.monitor.GroovyMoniServlet.java

private static void createNewSession(@NotNull HttpServletRequest request,
        @NotNull HttpServletResponse response) {
    request.getSession(true);
    response.setStatus(HttpServletResponse.SC_OK);
}

From source file:org.carewebframework.security.spring.DesktopSecurityContextRepository.java

/**
 * Given a servlet request, returns Spring security context object.
 * //from  w ww.  j  a  va  2  s.c  o m
 * @param request HttpServletRequest
 * @return SecurityContext The Spring security context. First looks for the desktop-based
 *         security context. If not found, then looks for a session-based security context. This
 *         call will convert a session-based security context to desktop-based if a desktop
 *         identifier is found in the request object, a desktop-based security context does not
 *         exist, and a session-based security context does exist.
 * @throws IllegalStateException if session is invalidated
 */
public static SecurityContext getSecurityContext(HttpServletRequest request) {
    final HttpSession session = request.getSession(false);
    boolean ignore = "rmDesktop".equals(request.getParameter("cmd_0"));
    return ignore || session == null ? SecurityContextHolder.createEmptyContext()
            : getSecurityContext(session, request.getParameter("dtid"));
}

From source file:com.liusoft.dlog4j.UserLoginManager.java

/**
 * //from   w  ww.  j a va 2 s .c  o  m
 * 
 * @param req
 * @param res
 * @param ubean
 * @param keepDays
 * @see com.liusoft.dlog4j.action.UserAction#doLogin(ActionMapping,
 *      ActionForm, HttpServletRequest, HttpServletResponse)
 */
public static SessionUserObject loginUser(HttpServletRequest req, HttpServletResponse res, UserBean ubean,
        int keepDays) {
    HttpSession ssn = req.getSession(false);
    if (ssn != null) {
        SessionUserObject rub = (SessionUserObject) ssn.getAttribute(SESSION_USER_KEY);
        if (rub != null && rub.getId() == ubean.getId()) {
            return rub;
        }
    }

    // ??,??
    ubean.setLastAddr(req.getRemoteAddr());
    ubean.setLastTime(new Timestamp(System.currentTimeMillis()));
    ubean.setKeepDays(keepDays);
    ubean.setOnlineStatus(1);
    DLOGUserManager.update(ubean);

    // ?cookie,?session?
    UUID uuid = new UUID();
    uuid.uid = ubean.getId();
    uuid.pwdCode = ubean.getPassword().hashCode();
    uuid.host = req.getRemoteAddr();

    String value = uuid.toString();
    RequestUtils.setCookie(req, res, COOKIE_UUID_KEY, value, (keepDays > 0) ? keepDays * 86400 : -1);
    RequestUtils.setCookie(req, res, COOKIE_LASTLOGIN_KEY, ubean.getLastTime().toString(), -1);

    // ?Session
    if (ssn == null)
        ssn = req.getSession(true);
    if (ssn != null && ubean != null) {
        ssn.setAttribute(SESSION_USER_KEY, SessionUserObject.copyFrom(ubean));
    }
    return ubean;
}

From source file:info.magnolia.cms.util.Resource.java

/**
 * Check for preview mode./*from w  ww  . j  ava  2s .com*/
 * @param req HttpServletRequest as received in JSP or servlet
 * @return boolean , true if preview is enabled
 */
public static boolean showPreview(HttpServletRequest req) {
    // first check if its set in request scope
    if (req.getParameter(MGNL_PREVIEW_ATTRIBUTE) != null) {
        return BooleanUtils.toBoolean(req.getParameter(MGNL_PREVIEW_ATTRIBUTE));
    } else {
        HttpSession httpsession = req.getSession(false);
        if (httpsession != null) {
            return BooleanUtils.toBoolean((Boolean) httpsession.getAttribute(MGNL_PREVIEW_ATTRIBUTE));
        }
    }
    return false;
}