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:org.sakaiproject.sdata.tool.SnoopHandler.java

/**
 * @param request// w w  w.  j  av  a 2 s .c  o  m
 */
private void snoopRequest(HttpServletRequest request) {
    StringBuilder sb = new StringBuilder("SData Request :").append(request);
    sb.append("\n\tRequest Path :").append(request.getPathInfo());
    sb.append("\n\tMethod :").append(request.getMethod());
    for (Enumeration<?> hnames = request.getHeaderNames(); hnames.hasMoreElements();) {
        String name = (String) hnames.nextElement();
        sb.append("\n\tHeader :").append(name).append("=[").append(request.getHeader(name)).append("]");
    }
    for (Enumeration<?> hnames = request.getParameterNames(); hnames.hasMoreElements();) {
        String name = (String) hnames.nextElement();
        sb.append("\n\tParameter :").append(name).append("=[").append(request.getParameter(name)).append("]");
    }
    if (request.getCookies() != null) {
        for (Cookie c : request.getCookies()) {
            sb.append("\n\tCookie:");
            sb.append("name[").append(c.getName());
            sb.append("]path[").append(c.getPath());
            sb.append("]value[").append(c.getValue());
        }
    }
    sb.append("]");
    for (Enumeration<?> hnames = request.getAttributeNames(); hnames.hasMoreElements();) {
        String name = (String) hnames.nextElement();
        sb.append("\n\tAttribute :").append(name).append("=[").append(request.getAttribute(name)).append("]");
    }
    HttpSession session = request.getSession();
    sb.append("\n\tUser :").append(request.getRemoteUser());
    if (session != null) {
        sb.append("\n\tSession ID :").append(session.getId());
        for (Enumeration<?> hnames = session.getAttributeNames(); hnames.hasMoreElements();) {
            String name = (String) hnames.nextElement();
            sb.append("\n\tSession Attribute :").append(name).append("=[").append(session.getAttribute(name))
                    .append("]");
        }

    } else {
        sb.append("\n\tNo Session");
    }

    LOG.info(sb.toString());
}

From source file:jp.terasoluna.fw.service.thin.BLogicMapper.java

/**
 * ZbVwv?peBL?[li[?B// ww w. ja  va2  s. c  o m
 *
 * @param value ?ol
 * @param propName v?peB
 * @param request HTTPNGXg
 * @param response HTTPX|X
 */
@SuppressWarnings("unchecked")
@Override
public void setValueToSession(Object value, String propName, HttpServletRequest request,
        HttpServletResponse response) {

    if (propName == null || "".equals(propName)) {
        log.error("illegal argument:propName = [" + propName + "]");
        throw new IllegalArgumentException();
    }

    // ZbV
    HttpSession session = request.getSession(true);

    if (log.isDebugEnabled()) {
        Enumeration<String> enumeration = session.getAttributeNames();
        int existFlag = 0;
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            if (key.equals(propName)) {
                existFlag = 1;
                break;
            }
        }
        if (existFlag != 1) {
            log.debug("Session's key does not exist:key =[" + propName + "]");
        }
    }
    // ZbVli[
    session.setAttribute(propName, value);
}

From source file:com.adito.core.actions.AuthenticatedDispatchAction.java

/**
 * @param mapping//from   w ww  .  jav  a  2  s .  co  m
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 * @throws Exception
 */
public ActionForward cleanUpAndReturnToReferer(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession();
    String toRemove = null;
    for (Enumeration e = session.getAttributeNames(); toRemove == null && e.hasMoreElements();) {
        String n = (String) e.nextElement();
        if (session.getAttribute(n) == form) {
            toRemove = n;
        }
    }
    if (toRemove != null) {
        request.getSession().removeAttribute(toRemove);
    }
    request.getSession().removeAttribute(Constants.EDITING_ITEM);

    // First look for a 'done' forward in the current mapping. If there is
    // none, then use the referer in the form, otherwise redirect to home
    ActionForward fwd = mapping.findForward("done");
    if (fwd != null) {
        return new RedirectWithMessages(fwd, request);
    }
    if (((CoreForm) form).getReferer() == null) {
        log.warn("Original referer was null, forwarding to home");
        return mapping.findForward("home");
    } else {
        return new RedirectWithMessages(((CoreForm) form).getReferer(), request);
    }
}

From source file:com.skilrock.lms.web.roleMgmt.common.PrivsInterceptor.java

public void refreshSession(HttpSession session) {
    ServletContext sc = ServletActionContext.getServletContext();
    String sesVariables = (String) sc.getAttribute("SESSION_VARIABLES");
    List sesVar = Arrays.asList(sesVariables.split(","));

    Enumeration sesEnum = session.getAttributeNames();
    while (sesEnum.hasMoreElements()) {
        Object variable = sesEnum.nextElement();
        if (!sesVar.contains(variable.toString())) {
            session.removeAttribute(variable.toString());
        }/*from  ww w .ja v  a 2  s . c o  m*/
    }
}

From source file:com.boundlessgeo.geoserver.api.controllers.SessionsController.java

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody JSONArr list() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd'T'HH:mm:ssZ");

    JSONArr arr = new JSONArr();
    for (HttpSession session : AppSessionDebugger.list()) {
        JSONObj obj = arr.addObject();/*  w  w w . jav  a 2s  . c om*/
        obj.put("id", session.getId()).put("created", dateFormat.format(new Date(session.getCreationTime())))
                .put("updated", dateFormat.format(new Date(session.getLastAccessedTime())))
                .put("timeout", session.getMaxInactiveInterval());

        @SuppressWarnings("unchecked")
        Enumeration<String> attNames = session.getAttributeNames();
        JSONObj atts = obj.putObject("attributes");
        while (attNames.hasMoreElements()) {
            String attName = attNames.nextElement();
            atts.put(attName, session.getAttribute(attName).toString());
        }
    }

    return arr;
}

From source file:com.sammyun.controller.shop.LoginController.java

/**
 * <??>?? <??>/*from  www.j  a  va2s.  c  o  m*/
 * 
 * @param username
 * @param request
 * @param response
 * @param session
 * @param member
 * @see [?#?#?]
 */
protected void syncCart(HttpServletRequest request, HttpServletResponse response, HttpSession session,
        Member member) {

    Map<String, Object> attributes = new HashMap<String, Object>();
    Enumeration<?> keys = session.getAttributeNames();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        attributes.put(key, session.getAttribute(key));
    }
    session.invalidate();
    session = request.getSession();
    for (Entry<String, Object> entry : attributes.entrySet()) {
        session.setAttribute(entry.getKey(), entry.getValue());
    }

    session.setAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME, new Principal(member.getId(), member.getUsername()));
    WebUtils.addCookie(request, response, Member.USERNAME_COOKIE_NAME, member.getUsername());
}

From source file:org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy.java

@SuppressWarnings("unchecked")
private HashMap<String, Object> createMigratedAttributeMap(HttpSession session) {
    HashMap<String, Object> attributesToMigrate = null;

    if (migrateSessionAttributes || retainedAttributes == null) {
        attributesToMigrate = new HashMap<String, Object>();

        Enumeration enumer = session.getAttributeNames();

        while (enumer.hasMoreElements()) {
            String key = (String) enumer.nextElement();
            if (!migrateSessionAttributes && !key.startsWith("SPRING_SECURITY_")) {
                // Only retain Spring Security attributes
                continue;
            }/*from ww  w  .ja v a  2  s . co m*/
            attributesToMigrate.put(key, session.getAttribute(key));
        }
    } else {
        // Only retain the attributes which have been specified in the retainAttributes list
        if (!retainedAttributes.isEmpty()) {
            attributesToMigrate = new HashMap<String, Object>();
            for (String name : retainedAttributes) {
                Object value = session.getAttribute(name);

                if (value != null) {
                    attributesToMigrate.put(name, value);
                }
            }
        }
    }
    return attributesToMigrate;
}

From source file:org.codehaus.groovy.grails.web.pages.ext.jsp.GroovyPagesPageContext.java

@SuppressWarnings("rawtypes")
@Override/* w  w w  . ja  v a  2s .  com*/
public Enumeration getAttributeNamesInScope(int scope) {
    switch (scope) {
    case PAGE_SCOPE:
        return new IteratorEnumeration(pageScope.getVariables().keySet().iterator());
    case REQUEST_SCOPE:
        return request.getAttributeNames();
    case SESSION_SCOPE:
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            return httpSession.getAttributeNames();
        }
        return EMPTY_ENUMERATION;
    case APPLICATION_SCOPE:
        return servletContext.getAttributeNames();
    }
    return EMPTY_ENUMERATION;
}

From source file:com.huateng.ebank.framework.session.SessionManager.java

public boolean destroySessionData(HttpServletRequest req) {
    if (logger.isDebugEnabled()) {
        logger.debug("destroySessionData(HttpServletRequest) - start"); //$NON-NLS-1$
    }//from  www . j a va  2  s  . c o m

    HttpSession session = req.getSession(false);
    if (null == session)
        return false;
    String strAttrName = null;
    Enumeration names = session.getAttributeNames();
    while (names.hasMoreElements()) {
        strAttrName = (String) names.nextElement();
        session.removeAttribute(strAttrName);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("destroySessionData(HttpServletRequest) - end"); //$NON-NLS-1$
    }
    return true;
}

From source file:org.terasoluna.gfw.web.token.transaction.HttpSessionTransactionTokenStore.java

/**
 * Creates a new Token key and reserve it in the HttpSession<br>
 * removes oldeset token if token size is greater than or equals {@link #transactionTokensPerTokenName} in the same
 * namespace.//w w w . j a  va  2  s. c  o  m
 * @see org.terasoluna.gfw.web.token.transaction.TransactionTokenStore#createAndReserveTokenKey(java.lang.String)
 */
@Override
public String createAndReserveTokenKey(String tokenName) {
    String tokenNamePrefix = TOKEN_HOLDER_SESSION_ATTRIBUTE_PREFIX + tokenName;
    Set<String> sessionAttributeNames = new HashSet<String>();
    HttpSession session = getSession();
    Object mutex = getMutex(session);
    String tokenKey = null;
    synchronized (mutex) {
        Enumeration<String> tokenNameEnumeration = session.getAttributeNames();
        while (tokenNameEnumeration.hasMoreElements()) {
            String name = tokenNameEnumeration.nextElement();
            // fetch the sessionKeyPrefix (session key with only Token prefix and namespace name) and compare
            if (tokenNamePrefix.equals(name.split(TransactionToken.TOKEN_STRING_SEPARATOR)[0])) {
                sessionAttributeNames.add(name);
            }
        }

        for (int i = 0, max = sessionAttributeNames.size(); i < max; i++) {
            // do not use while loop to avoid infinite loop
            if (sessionAttributeNames.size() >= transactionTokensPerTokenName) {
                removeOldTokenName(sessionAttributeNames, session);
            } else {
                break;
            }
        }

        for (int i = 0; i < retryCreateTokenName; i++) {
            String str = generator.generate(session.getId());
            String name = tokenNamePrefix + TransactionToken.TOKEN_STRING_SEPARATOR + str;
            if (!sessionAttributeNames.contains(name)) {
                tokenKey = str;
                break;
            }
        }
    }
    if (tokenKey == null) {
        throw new IllegalStateException(
                "token key generation failed within retry count " + retryCreateTokenName);
    }

    return tokenKey;
}