Example usage for javax.servlet.http HttpSession getId

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

Introduction

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

Prototype

public String getId();

Source Link

Document

Returns a string containing the unique identifier assigned to this session.

Usage

From source file:com.utest.webservice.util.SessionUtil.java

public static String extractSession(MessageContext context, boolean insertToResponze) {
    HttpServletRequest req = context.getHttpServletRequest();
    javax.servlet.http.Cookie[] available = req.getCookies();
    String sessionId = null;//from  w  w w .  java  2 s  .c  o  m
    if (available != null) {
        for (javax.servlet.http.Cookie ck : available) {
            if (SESSION_NAME.equalsIgnoreCase(ck.getName())) {
                sessionId = ck.getValue();
            }
        }
    }
    if (sessionId == null) {
        HttpSession session = req.getSession();
        sessionId = session.getId();
        if (insertToResponze) {
            javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(SESSION_NAME, sessionId);
            context.getHttpServletResponse().addCookie(cookie);
        }
    }
    return sessionId;
}

From source file:com.sonicle.webtop.core.app.servlet.ServletHelper.java

/**
 * Retrieve the ID of the passed http session.
 * @param session The http session//from  www  .jav  a2  s .  co m
 * @return Session's ID
 */
public static String getSessionID(HttpSession session) {
    return (session != null) ? session.getId() : "";
}

From source file:cn.vlabs.duckling.vwb.VWBFilter.java

public static void removeGlobalCookie(HttpServletRequest request, HttpServletResponse response,
        HttpSession session) {
    Cookie oldCookie = new Cookie(COOKIE_NAME, session.getId());
    oldCookie.setPath(request.getContextPath());
    oldCookie.setMaxAge(0);/*from w ww  .j  a  v a2 s  .  c o  m*/
    response.addCookie(oldCookie);
}

From source file:org.carewebframework.ui.util.RequestUtil.java

/**
 * Get current request's session id or null if session has not yet been created.
 * //from  www  .j  a  v  a 2  s  .  c o m
 * @throws IllegalStateException if called outside scope of an Execution/ServletRequest
 * @return String representing session id or null if session has not yet been created
 */
public static String getSessionId() {
    HttpSession session = assertRequest().getSession(false);
    return session == null ? null : session.getId();
}

From source file:cn.bc.web.util.DebugUtils.java

public static StringBuffer getDebugInfo(HttpServletRequest request, HttpServletResponse response) {
    @SuppressWarnings("rawtypes")
    Enumeration e;/*  w w  w  .j av  a2 s . c om*/
    String name;
    StringBuffer html = new StringBuffer();

    //session
    HttpSession session = request.getSession();
    html.append("<div><b>session:</b></div><ul>");
    html.append(createLI("Id", session.getId()));
    html.append(createLI("CreationTime", new Date(session.getCreationTime()).toString()));
    html.append(createLI("LastAccessedTime", new Date(session.getLastAccessedTime()).toString()));

    //session:attributes
    e = session.getAttributeNames();
    html.append("<li>attributes:<ul>\r\n");
    while (e.hasMoreElements()) {
        name = (String) e.nextElement();
        html.append(createLI(name, String.valueOf(session.getAttribute(name))));
    }
    html.append("</ul></li>\r\n");
    html.append("</ul>\r\n");

    //request
    html.append("<div><b>request:</b></div><ul>");
    html.append(createLI("URL", request.getRequestURL().toString()));
    html.append(createLI("QueryString", request.getQueryString()));
    html.append(createLI("Method", request.getMethod()));
    html.append(createLI("CharacterEncoding", request.getCharacterEncoding()));
    html.append(createLI("ContentType", request.getContentType()));
    html.append(createLI("Protocol", request.getProtocol()));
    html.append(createLI("RemoteAddr", request.getRemoteAddr()));
    html.append(createLI("RemoteHost", request.getRemoteHost()));
    html.append(createLI("RemotePort", request.getRemotePort() + ""));
    html.append(createLI("RemoteUser", request.getRemoteUser()));
    html.append(createLI("ServerName", request.getServerName()));
    html.append(createLI("ServletPath", request.getServletPath()));
    html.append(createLI("ServerPort", request.getServerPort() + ""));
    html.append(createLI("Scheme", request.getScheme()));
    html.append(createLI("LocalAddr", request.getLocalAddr()));
    html.append(createLI("LocalName", request.getLocalName()));
    html.append(createLI("LocalPort", request.getLocalPort() + ""));
    html.append(createLI("Locale", request.getLocale().toString()));

    //request:headers
    e = request.getHeaderNames();
    html.append("<li>Headers:<ul>\r\n");
    while (e.hasMoreElements()) {
        name = (String) e.nextElement();
        html.append(createLI(name, request.getHeader(name)));
    }
    html.append("</ul></li>\r\n");

    //request:parameters
    e = request.getParameterNames();
    html.append("<li>Parameters:<ul>\r\n");
    while (e.hasMoreElements()) {
        name = (String) e.nextElement();
        html.append(createLI(name, request.getParameter(name)));
    }
    html.append("</ul></li>\r\n");

    html.append("</ul>\r\n");

    //response
    html.append("<div><b>response:</b></div><ul>");
    html.append(createLI("CharacterEncoding", response.getCharacterEncoding()));
    html.append(createLI("ContentType", response.getContentType()));
    html.append(createLI("BufferSize", response.getBufferSize() + ""));
    html.append(createLI("Locale", response.getLocale().toString()));
    html.append("<ul>\r\n");
    return html;
}

From source file:com.liusoft.dlog4j.util.RequestUtils.java

/**
 * ?FCKUpload??ID/*from ww w .  jav a  2  s.c  o m*/
 * @return
 */
public static String getDlogSessionId(HttpServletRequest req) {
    //Cookie?ssn_id
    String ssn_id = null;
    Cookie cok = RequestUtils.getCookie(req, Globals.SESSION_ID_KEY_IN_COOKIE);
    if (cok != null) {
        ssn_id = cok.getValue();
    }
    if (StringUtils.isEmpty(ssn_id)) {
        //Cookie????
        HttpSession ssn = req.getSession(false);
        if (ssn != null)
            ssn_id = ssn.getId();
    }
    return ssn_id;
}

From source file:net.webpasswordsafe.server.ServerSessionUtil.java

public static void initCsrfSession() {
    HttpSession session = getRequest().getSession(false);
    if (session.isNew() || (session.getAttribute(Constants.CSRF_TOKEN_KEY) == null)) {
        // either new session or old session without csrf token set, so set it
        session.setAttribute(Constants.CSRF_TOKEN_KEY, session.getId());
        Cookie cookie = new Cookie(Constants.CSRF_TOKEN_KEY, session.getId());
        cookie.setPath("".equals(getRequest().getContextPath()) ? "/" : getRequest().getContextPath());
        getResponse().addCookie(cookie);
    }//from  w w w  .  j a  v  a2 s  . c  om
}

From source file:org.appverse.web.framework.backend.api.helpers.security.SecurityHelper.java

public static String createXSRFToken(final HttpServletRequest request) throws IOException {
    // getSession(false) as this method never creates a new session
    HttpSession session = request.getSession(false);
    String xsrfSessionToken = (String) session.getAttribute(XSRF_TOKEN_NAME);
    if (xsrfSessionToken == null) {
        Random r = new Random(System.currentTimeMillis());
        long value = System.currentTimeMillis() + r.nextLong();
        char ids[] = session.getId().toCharArray();
        for (int i = 0; i < ids.length; i++) {
            value += ids[i] * (i + 1);// w  ww  . j  a  va  2 s .  c  o  m
        }
        xsrfSessionToken = Long.toString(value);
        session.setAttribute(XSRF_TOKEN_NAME, xsrfSessionToken);
    }
    return xsrfSessionToken;
}

From source file:org.sloth.util.ControllerUtils.java

/**
 * De-Authorizes the {@code HttpSession}.
 * /* w  ww. j a  v  a 2 s .  c om*/
 * @param s
 *            the {@code HttpSession}
 */
public static void deAuth(HttpSession s) {
    User u = getUser(s);
    if (u != null) {
        logger.info("Unbinding User {} from Session {}", u.getId(), s.getId());
    }
    s.invalidate();
}

From source file:com.amalto.webapp.core.bean.Configuration.java

private static void storeInSession(Configuration configuration) {
    HttpSession session = SessionContextHolder.currentSession();
    if (session != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Registering configuration into session " + session.getId()); //$NON-NLS-1$
        }//from  ww  w . j a  v  a2s .co  m
        session.setAttribute(MDM_CONFIGURATION_ATTRIBUTE, configuration);
    }
}