Example usage for javax.servlet.http HttpSession getAttributeNames

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

Introduction

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

Prototype

public Enumeration<String> getAttributeNames();

Source Link

Document

Returns an Enumeration of String objects containing the names of all the objects bound to this session.

Usage

From source file:gov.nih.nci.ncicb.cadsr.common.security.LogoutServlet.java

private Map copyAllsessionObjects(HttpSession session) {
    log.error("LogoutServlet.copyAllsessionObjects start:" + TimeUtils.getEasternTime());
    HashMap map = new HashMap();
    Enumeration keys = session.getAttributeNames();
    for (; keys.hasMoreElements();) {
        String key = (String) keys.nextElement();
        map.put(key, session.getAttribute(key));
    }/*from  w ww  .ja va 2 s  .  c o m*/
    log.error("LogoutServlet.copyAllsessionObjects end:" + TimeUtils.getEasternTime());
    return map;
}

From source file:org.apache.struts2.StrutsJUnit4TestCase.java

public void finishExecution() {
    HttpSession session = this.request.getSession();
    Enumeration attributeNames = session.getAttributeNames();

    MockHttpServletRequest nextRequest = new MockHttpServletRequest();

    while (attributeNames.hasMoreElements()) {
        String key = (String) attributeNames.nextElement();
        Object attribute = session.getAttribute(key);
        nextRequest.getSession().setAttribute(key, attribute);
    }/*  w  w w  . ja  v a  2s .co m*/

    this.response = new MockHttpServletResponse();
    this.request = nextRequest;
    this.pageContext = new MockPageContext(servletContext, request, response);
}

From source file:org.springframework.web.context.request.ServletRequestAttributes.java

public String[] getAttributeNames(int scope) {
    if (scope == SCOPE_REQUEST) {
        return StringUtils.toStringArray(this.request.getAttributeNames());
    } else {//w ww  .  jav a  2  s. co  m
        HttpSession session = getSession(false);
        if (session != null) {
            return StringUtils.toStringArray(session.getAttributeNames());
        } else {
            return new String[0];
        }
    }
}

From source file:org.regola.security.cas.SingleSignOutFilter.java

private void rigeneraSessione(HttpServletRequest request) {
    HttpSession oldSession = request.getSession(false);

    HashMap<String, Object> tmp = new HashMap<String, Object>();

    if (oldSession != null) {

        Enumeration enumer = oldSession.getAttributeNames();

        while (enumer.hasMoreElements()) {

            String s = (String) enumer.nextElement();

            tmp.put(s, oldSession.getAttribute(s));

        }//from  ww  w. j a v  a 2 s . com

        log.debug("Sessione " + oldSession.getId() + " valida? " + request.isRequestedSessionIdValid());
        oldSession.invalidate();

    }

    HttpSession newSession = request.getSession(true);

    log.debug("E adesso sessione " + newSession.getId() + " valida? " + request.isRequestedSessionIdValid());

    for (Map.Entry<String, Object> entry : tmp.entrySet()) {

        newSession.setAttribute(entry.getKey(), entry.getValue());

    }
}

From source file:org.openmrs.web.servlet.LoginServlet.java

/**
 * Regenerates session id after each login attempt.
 * @param request/*  www .  ja va  2  s .co  m*/
 */
private void regenerateSession(HttpServletRequest request) {

    HttpSession oldSession = request.getSession();

    Enumeration attrNames = oldSession.getAttributeNames();
    Properties props = new Properties();

    if (attrNames != null) {
        while (attrNames.hasMoreElements()) {
            String key = (String) attrNames.nextElement();
            props.put(key, oldSession.getAttribute(key));
        }

        //Invalidating previous session
        oldSession.invalidate();
        //Generate new session
        HttpSession newSession = request.getSession(true);
        attrNames = props.keys();

        while (attrNames.hasMoreElements()) {
            String key = (String) attrNames.nextElement();
            newSession.setAttribute(key, props.get(key));
        }
    }
}

From source file:org.molasdin.wbase.jsf.spring.web.SpringBeansRewireListener.java

@Override
public void sessionDidActivate(HttpSessionEvent httpSessionEvent) {
    HttpSession session = httpSessionEvent.getSession();
    WebApplicationContext ctx = WebApplicationContextUtils
            .getRequiredWebApplicationContext(session.getServletContext());
    AutowireCapableBeanFactory bf = ctx.getAutowireCapableBeanFactory();
    for (String entry : IteratorUtils.asIterable(IteratorUtils.asIterator(session.getAttributeNames()))) {
        if (bf.containsBean(entry)) {
            Object bean = session.getAttribute(entry);
            bean = bf.configureBean(bean, entry);
            session.setAttribute(entry, bean);
        }//  ww  w. java  2 s.  c  om
    }

}

From source file:jp.co.opentone.bsol.linkbinder.view.filter.LoginFilter.java

@SuppressWarnings("unchecked")
private Map<String, Object> collectSessionAttributes(HttpSession session) {
    Map<String, Object> attributes = new HashMap<String, Object>();
    if (session != null) {
        for (Enumeration<String> e = session.getAttributeNames(); e.hasMoreElements();) {
            String key = e.nextElement();
            attributes.put(key, session.getAttribute(key));
        }/*from   w  w  w  .j  a v  a  2s  .  c om*/
    }
    return attributes;
}

From source file:nl.strohalm.cyclos.controls.members.pending.ValidateRegistrationAction.java

@Override
protected ActionForward executeAction(final ActionMapping mapping, final ActionForm actionForm,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {

    // The only interesting session attribute for us is inContainerPage. All others will be cleared out
    final HttpSession session = request.getSession(false);
    @SuppressWarnings("unchecked")
    final Enumeration<String> attributeNames = session.getAttributeNames();
    final ArrayList<String> attributesToBeRemoved = new ArrayList<String>();
    // in order to prevent ConcurrentModificationException, first store the attribs to be removed
    while (attributeNames.hasMoreElements()) {
        final String name = attributeNames.nextElement();
        if (!"inContainerPage".equals(name)) {
            attributesToBeRemoved.add(name);
        }//w  w w. j a  v a 2  s .c  o  m
    }
    // and after that delete them
    for (final String name : attributesToBeRemoved) {
        session.removeAttribute(name);
    }

    final String validationKeyMessage = messageHelper.message("pendingMember.validationKey");
    final ValidateRegistrationForm form = (ValidateRegistrationForm) actionForm;
    final String key = form.getKey();

    // Load the pending member, in order to check the container url
    PendingMember pendingMember;
    try {
        pendingMember = elementService.loadPendingMemberByKey(key, PendingMember.Relationships.MEMBER_GROUP);
    } catch (final EntityNotFoundException e) {
        // The key is invalid
        return ActionHelper.sendError(mapping, request, response, "errors.invalid", validationKeyMessage);
    }

    // Check for the container url
    final String containerUrl = findContainerUrl(pendingMember);
    if (containerUrl != null) {
        final boolean inContainerPage = Boolean.TRUE.equals(session.getAttribute("inContainerPage"));
        if (!inContainerPage) {
            session.setAttribute("inContainerPage", true);
            session.setAttribute("instantRedirectTo",
                    request.getContextPath() + "/do" + mapping.getPath() + "?key=" + key);
            return new ActionForward(containerUrl, true);
        }
    }

    // Check for a non-empty key
    if (StringUtils.isEmpty(key)) {
        return ActionHelper.sendError(mapping, request, response, "errors.required", validationKeyMessage);
    }

    // Process the validation
    try {
        Member member;
        try {
            member = elementService.publicValidateRegistration(key);
        } catch (final EntityNotFoundException e) {
            // The key is invalid
            return ActionHelper.sendError(mapping, request, response, "errors.invalid", validationKeyMessage);
        } catch (final RegistrationAgreementNotAcceptedException e) {
            // He should accept the agreement first
            return ActionHelper.redirectWithParam(request, mapping.findForward("acceptAgreement"), "key", key);
        }

        // Set the correct session attribute for customized login page
        String loginParamName = null;
        Object loginParamValue = null;
        final MemberGroup group = member.getMemberGroup();
        if (StringUtils.isNotEmpty(group.getLoginPageName())) {
            loginParamName = "groupId";
            loginParamValue = group.getId();
        } else {
            // Try by group filter
            for (final GroupFilter filter : group.getGroupFilters()) {
                if (filter.getLoginPageName() != null) {
                    loginParamName = "groupFilterId";
                    loginParamValue = filter.getId();
                    break;
                }
            }
        }
        if (loginParamName != null) {
            session.setAttribute("loginParamName", loginParamName);
            session.setAttribute("loginParamValue", loginParamValue);
        }

        // We will send the flow to the error page not to showing an error, but the created message
        final boolean passwordGenerated = member.getMemberUser().isPasswordGenerated();
        String messageKey;
        if (!member.isActive()) {
            messageKey = "createMember.public.awaitingActivation";
        } else if (passwordGenerated) {
            messageKey = "createMember.public.awaitingPassword";
        } else {
            messageKey = "createMember.public.validated";
        }
        ActionHelper.sendError(mapping, request, response, messageKey, member.getUsername());
        return mapping.findForward("confirmation");
    } catch (final ValidationException e) {
        return ActionHelper.handleValidationException(mapping, request, response, e);
    } catch (final Exception e) {
        actionHelper.generateLog(request, getServlet().getServletContext(), e);
        return ActionHelper.sendError(mapping, request, response, null);
    }
}

From source file:sample.client.Application.java

Map<String, String> attributes(HttpSession session) {
    Map<String, String> sessionAttributes = new HashMap<String, String>();

    for (String attributeName : toIterable(session.getAttributeNames())) {
        sessionAttributes.put(attributeName, String.valueOf(session.getAttribute(attributeName)));
    }//from  ww  w.j av  a 2 s . c o  m

    return sessionAttributes;
}

From source file:org.talframework.tal.aspects.loggers.http.DefaultRequestLogger.java

/**
 * Helper to log out the session attributes at debug level
 * //  w  ww  .  ja  v a2  s .  c  o  m
 * @param req The request
 * @param logger The logger
 */
@SuppressWarnings("unchecked")
private void logSessionAttributes(HttpServletRequest req, Log logger) {
    HttpSession session = req.getSession(false);
    if (session == null)
        return;

    StringBuilder buf = new StringBuilder();
    buf.append("Session Contents: ");
    buf.append("\n\t**** Session Contents ****");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String k = (String) e.nextElement();
        buf.append("\n\t").append(k).append("=").append(LoggerHelper.getValue(session.getAttribute(k)));
    }
    buf.append("\n\t**** End Session Contents ****");
    logger.debug(buf.toString());
}