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:com.icesoft.faces.webapp.xmlhttp.PersistentFacesState.java

private void testSession() throws IllegalStateException {
    Object o = view.getFacesContext().getExternalContext().getSession(false);
    if (o != null) {
        if (o instanceof HttpSession) {
            HttpSession session = (HttpSession) o;
            session.getAttributeNames();
        } else if (o instanceof PortletSession) {
            PortletSession ps = (PortletSession) o;
            ps.getAttributeNames();//w w  w.j  a  v  a2  s  .  c o m
        }
    }
}

From source file:org.sakaiproject.portal.render.portlet.PortletToolRenderService.java

public RenderResult render(Portal portal, ToolConfiguration toolConfiguration, final HttpServletRequest request,
        final HttpServletResponse response, ServletContext context) throws IOException {

    getPortletDD(toolConfiguration);// w w  w.j a  v a2 s  .c om
    final SakaiPortletWindow window = isIn168TestMode(request) ? createPortletWindow(toolConfiguration.getId())
            : registry.getOrCreatePortletWindow(toolConfiguration);

    PortletState state = PortletStateAccess.getPortletState(request, window.getId().getStringId());

    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieved PortletState from request cache.  Applying to window.");
    }

    if (portalService.isResetRequested(request)) {
        if (state != null) {
            String statePrefix = "javax.portlet.p." + state.getId();

            HttpSession session = request.getSession(true);
            for (Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                if (key != null && key.startsWith(statePrefix)) {
                    session.removeAttribute(key);
                }
            }
            state = null; // Remove the remaining evidence of prior
            // existence
        }
    }

    if (state == null) {
        state = new PortletState(window.getId().getStringId());
        PortletStateAccess.setPortletState(request, state);
    }

    window.setState(state);

    try {
        final HttpServletRequest req = new SakaiServletRequest(request, state);
        final PortletContainer portletContainer = getPortletContainer(context);

        // Derive the Edit and Help URLs
        String editUrl = null;
        String helpUrl = null;
        RequiredContainerServices rs = portletContainer.getRequiredContainerServices();
        PortalCallbackService pcs = rs.getPortalCallbackService();
        PortletURLProvider pup = null;

        if (isPortletModeAllowed(toolConfiguration, "edit")) {
            pup = pcs.getPortletURLProvider(request, window);
            // System.out.println("pup = "+pup);
            pup.setPortletMode(new PortletMode("edit"));
            // System.out.println("pup edit="+pup.toString());
            editUrl = pup.toString();
        }

        if (isPortletModeAllowed(toolConfiguration, "help")) {
            pup = pcs.getPortletURLProvider(request, window);
            pup.setPortletMode(new PortletMode("help"));
            // System.out.println("pup help="+pup.toString());
            helpUrl = pup.toString();
        }

        return new Sakai168RenderResult(req, response, portletContainer, window, helpUrl, editUrl);
    } catch (PortletContainerException e) {
        throw new ToolRenderException(e.getMessage(), e);
    }
}

From source file:nl.strohalm.cyclos.utils.LoginHelper.java

@SuppressWarnings("rawtypes")
private HttpSession createNewSessionForlogin(final HttpServletRequest request) {
    HttpSession session = request.getSession();

    // retrieve the current attributes
    final Map<String, Object> attrMap = new HashMap<String, Object>();
    for (final Enumeration e = session.getAttributeNames(); e.hasMoreElements();) {
        final String attrName = (String) e.nextElement();
        attrMap.put(attrName, session.getAttribute(attrName));
    }//from   w w  w .j av  a  2  s .c  o  m

    // invalidates and creates a new session
    session.invalidate();
    session = request.getSession();

    // copy the previous attributes to the new session
    for (final Map.Entry<String, Object> entry : attrMap.entrySet()) {
        session.setAttribute(entry.getKey(), entry.getValue());
    }

    return session;
}

From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java

private void parent(MockHttpServletRequest result, RequestBuilder parent) {
    if (parent == null) {
        return;/* ww  w.j  av a  2 s .c  om*/
    }
    MockHttpServletRequest parentRequest = parent.buildRequest(result.getServletContext());

    // session
    HttpSession parentSession = parentRequest.getSession(false);
    if (parentSession != null) {
        Enumeration<String> attrNames = parentSession.getAttributeNames();
        while (attrNames.hasMoreElements()) {
            String attrName = attrNames.nextElement();
            Object attrValue = parentSession.getAttribute(attrName);
            result.getSession().setAttribute(attrName, attrValue);
        }
    }

    // header
    Enumeration<String> headerNames = parentRequest.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String attrName = headerNames.nextElement();
        Enumeration<String> attrValues = parentRequest.getHeaders(attrName);
        while (attrValues.hasMoreElements()) {
            String attrValue = attrValues.nextElement();
            result.addHeader(attrName, attrValue);
        }
    }

    // parameter
    Map<String, String[]> parentParams = parentRequest.getParameterMap();
    for (Map.Entry<String, String[]> parentParam : parentParams.entrySet()) {
        String paramName = parentParam.getKey();
        String[] paramValues = parentParam.getValue();
        result.addParameter(paramName, paramValues);
    }

    // cookie
    Cookie[] parentCookies = parentRequest.getCookies();
    if (parentCookies != null) {
        result.setCookies(parentCookies);
    }

    // request attribute
    Enumeration<String> parentAttrNames = parentRequest.getAttributeNames();
    while (parentAttrNames.hasMoreElements()) {
        String parentAttrName = parentAttrNames.nextElement();
        result.setAttribute(parentAttrName, parentRequest.getAttribute(parentAttrName));
    }
}

From source file:com.shenit.commons.utils.HttpUtils.java

/**
 * sessionnames?session/*from  w  ww.  j a v  a  2s .  c  o m*/
 * 
 * @param req
 * @param names
 */
public static void purgeSessions(HttpServletRequest req, String... names) {
    HttpSession session = req.getSession(false);
    if (session == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("[purgeSessions] No sessions to purge");
        return;
    }
    if (ValidationUtils.isEmpty(names)) {
        // ?session
        Enumeration<String> namesEnum = session.getAttributeNames();
        for (; namesEnum.hasMoreElements();) {
            session.removeAttribute(namesEnum.nextElement());
        }
        return;
    }
    // ???
    for (String name : names) {
        session.removeAttribute(name);
    }
}

From source file:org.apache.ofbiz.base.util.UtilHttp.java

/**
 * Create a map from a HttpSession object
 * @return The resulting Map// www . j av  a 2 s  .co  m
 */
public static Map<String, Object> getSessionMap(HttpServletRequest request, Set<? extends String> namesToSkip) {
    Map<String, Object> sessionMap = new HashMap<String, Object>();
    HttpSession session = request.getSession();

    // look at all the session attributes
    Enumeration<String> sessionAttrNames = UtilGenerics.cast(session.getAttributeNames());
    while (sessionAttrNames.hasMoreElements()) {
        String attrName = sessionAttrNames.nextElement();
        if (namesToSkip != null && namesToSkip.contains(attrName))
            continue;

        Object attrValue = session.getAttribute(attrName);
        sessionMap.put(attrName, attrValue);
    }

    if (Debug.verboseOn()) {
        Debug.logVerbose("Made Session Attribute Map with [" + sessionMap.size() + "] Entries", module);
        Debug.logVerbose("Session Attribute Map Entries: " + System.getProperty("line.separator")
                + UtilMisc.printMap(sessionMap), module);
    }

    return sessionMap;
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession();

    Integer count = (Integer) session.getAttribute("tracker.count");
    if (count == null)
        count = new Integer(1);
    else/*  ww w .j ava  2 s  .co  m*/
        count = new Integer(count.intValue() + 1);
    session.setAttribute("tracker.count", count);

    out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
    out.println("<BODY><H1>Session Tracking Demo</H1>");

    out.println("You've visited this page " + count + ((count.intValue() == 1) ? " time." : " times."));

    out.println("<P>");

    out.println("<H2>Here is your session data:</H2>");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println(name + ": " + session.getAttribute(name) + "<BR>");
    }
    out.println("</BODY></HTML>");
}

From source file:SessionSnoop.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession();

    Integer count = (Integer) session.getAttribute("count");
    if (count == null)
        count = new Integer(1);
    else/*from   w w  w  .  j av a 2  s . com*/
        count = new Integer(count.intValue() + 1);
    session.setAttribute("count", count);

    out.println("<HTML><HEAD><TITLE>Session Count</TITLE></HEAD>");
    out.println("<BODY><H1>Session Count</H1>");

    out.println("You've visited this page " + count + ((count == 1) ? " time." : " times."));

    out.println("<P>");

    out.println("<H3>Here is your saved session data:</H3>");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println(name + ": " + session.getAttribute(name) + "<BR>");
    }

    out.println("<H3>Here are some vital stats on your session:</H3>");
    out.println("Session id: " + session.getId() + " <I>(keep it secret)</I><BR>");
    out.println("New session: " + session.isNew() + "<BR>");
    out.println("Timeout: " + session.getMaxInactiveInterval());
    out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I><BR>");
    out.println("Creation time: " + session.getCreationTime());
    out.println("<I>(" + new Date(session.getCreationTime()) + ")</I><BR>");
    out.println("Last access time: " + session.getLastAccessedTime());
    out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I><BR>");

    out.println("Requested session ID from cookie: " + req.isRequestedSessionIdFromCookie() + "<BR>");
    out.println("Requested session ID from URL: " + req.isRequestedSessionIdFromURL() + "<BR>");
    out.println("Requested session ID valid: " + req.isRequestedSessionIdValid() + "<BR>");

    out.println("<H3>Test URL Rewriting</H3>");
    out.println("Click <A HREF=\"" + res.encodeURL(req.getRequestURI()) + "\">here</A>");
    out.println("to test that session tracking works via URL");
    out.println("rewriting even when cookies aren't supported.");

    out.println("</BODY></HTML>");
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession();

    Integer count = (Integer) session.getAttribute("snoop.count");
    if (count == null)
        count = new Integer(1);
    else//from   w ww  .  j  a v  a  2 s. c o  m
        count = new Integer(count.intValue() + 1);
    session.setAttribute("snoop.count", count);

    out.println("<HTML><HEAD><TITLE>SessionSnoop</TITLE></HEAD>");
    out.println("<BODY><H1>Session Snoop</H1>");

    out.println("You've visited this page " + count + ((count.intValue() == 1) ? " time." : " times."));

    out.println("<P>");

    out.println("<H3>Here is your saved session data:</H3>");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println(name + ": " + session.getAttribute(name) + "<BR>");
    }

    out.println("<H3>Here are some vital stats on your session:</H3>");
    out.println("Session id: " + session.getId() + " <I>(keep it secret)</I><BR>");
    out.println("New session: " + session.isNew() + "<BR>");
    out.println("Timeout: " + session.getMaxInactiveInterval());
    out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I><BR>");
    out.println("Creation time: " + session.getCreationTime());
    out.println("<I>(" + new Date(session.getCreationTime()) + ")</I><BR>");
    out.println("Last access time: " + session.getLastAccessedTime());
    out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I><BR>");

    out.println("Requested session ID from cookie: " + req.isRequestedSessionIdFromCookie() + "<BR>");
    out.println("Requested session ID from URL: " + req.isRequestedSessionIdFromURL() + "<BR>");
    out.println("Requested session ID valid: " + req.isRequestedSessionIdValid() + "<BR>");

    out.println("<H3>Test URL Rewriting</H3>");
    out.println("Click <A HREF=\"" + res.encodeURL(req.getRequestURI()) + "\">here</A>");
    out.println("to test that session tracking works via URL");
    out.println("rewriting even when cookies aren't supported.");

    out.println("</BODY></HTML>");
}

From source file:architecture.ee.web.community.struts2.action.SocialConnectAction.java

public String execute() throws Exception {
    HttpSession session = request.getSession(true);
    if (StringUtils.isNotEmpty(domainName)) {
        String domainNameInSession = (String) session.getAttribute(DOMAIN_NAME_KEY);
        log.debug("domainName: " + domainName);
        log.debug("domainNameInSession: " + domainNameInSession);
        log.debug(StringUtils.equals(domainName, domainNameInSession));
        if (!StringUtils.equals(domainName, domainNameInSession)) {
            session.setAttribute(DOMAIN_NAME_KEY, domainName);
        }//from   ww  w. j av  a  2 s .  c  om
    }
    Enumeration names = session.getAttributeNames();
    while (names.hasMoreElements()) {
        String key = (String) names.nextElement();
        Object value = session.getAttribute(key);
        log.debug(key + "=" + value);
    }
    return success();
}