Example usage for javax.servlet.http Cookie getValue

List of usage examples for javax.servlet.http Cookie getValue

Introduction

In this page you can find the example usage for javax.servlet.http Cookie getValue.

Prototype

public String getValue() 

Source Link

Document

Gets the current value of this Cookie.

Usage

From source file:nl.tue.gale.ae.GaleContext.java

public static String getCookie(Resource resource, String name) {
    Cookie[] cookies = req(resource).getCookies();
    if (cookies == null)
        return null;
    for (Cookie cookie : cookies)
        if (cookie.getName().equalsIgnoreCase(name))
            return decodeURL(cookie.getValue());
    return null;//from   www.  jav  a  2 s. co  m
}

From source file:cn.vlabs.umt.ui.servlet.login.LoginMethod.java

/**
 * cookiecookie?/*w  w  w .  ja v a 2s  .  co m*/
 */
public static String getSsoCookieValue(HttpServletRequest request) {
    PCookie pcookie = (PCookie) ServiceFactory.getBean(request, "PCookie");
    Cookie cookie = getCookieByName(request, Attributes.COOKIE_NAME);
    if (cookie == null) {
        return null;
    }
    return pcookie.decrypt(cookie.getValue());
}

From source file:com.anjz.util.CookieUtils.java

private static void getCookieHeaderValue(final Cookie cookie, final StringBuffer buf, final boolean httpOnly) {
    final int version = cookie.getVersion();

    // this part is the same for all cookies

    String name = cookie.getName(); // Avoid NPE on malformed cookies
    if (name == null) {
        name = "";
    }//w  w  w. j a  v  a2 s.  c  o m
    String value = cookie.getValue();
    if (value == null) {
        value = "";
    }

    buf.append(name);
    buf.append("=");

    maybeQuote(version, buf, value);

    // add version 1 specific information
    if (version == 1) {
        // Version=1 ... required
        buf.append("; Version=1");

        // Comment=comment
        if (cookie.getComment() != null) {
            buf.append("; Comment=");
            maybeQuote(version, buf, cookie.getComment());
        }
    }

    // add domain information, if present

    if (cookie.getDomain() != null) {
        buf.append("; Domain=");
        maybeQuote(version, buf, cookie.getDomain());
    }

    // Max-Age=secs/Discard ... or use old "Expires" format
    if (cookie.getMaxAge() >= 0) {
        if (version == 0) {
            buf.append("; Expires=");
            SimpleDateFormat dateFormat = new SimpleDateFormat(OLD_COOKIE_PATTERN, LOCALE_US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); //GMT?
            if (cookie.getMaxAge() == 0) {
                dateFormat.format(new Date(10000), buf, new FieldPosition(0));
            } else {
                dateFormat.format(new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L), buf,
                        new FieldPosition(0));
            }
        } else {
            buf.append("; Max-Age=");
            buf.append(cookie.getMaxAge());
        }
    } else if (version == 1) {
        buf.append("; Discard");
    }

    // Path=path
    if (cookie.getPath() != null) {
        buf.append("; Path=");
        maybeQuote(version, buf, cookie.getPath());
    }

    // Secure
    if (cookie.getSecure()) {
        buf.append("; Secure");
    }

    // HttpOnly
    if (httpOnly) {
        buf.append("; HttpOnly");
    }
}

From source file:edu.ucsb.nceas.metacat.util.RequestUtil.java

/**
 * Get the session data from a request. The Scenarios we can run across
 * here: //from  w  w w . j a  v a 2  s  .co m
 * -- the session id parameter was set in the request parameters 
 * -- request.getSession returns a new session. There is a chance that the
 *    session id was set in a cookie. Check for a JSESSIONID cookie and use
 *    that id if provided. 
 * -- request.getSession returns a session that is a)
 *    preexisting or b) new but without a JSESSIONID cookie. Use the session id
 *    from this session
 * 
 * @param request
 *            the request from which to get the session data
 * @return the session data object representing the active session for this
 *         request. If there is no active session, the public session data
 *         is returned
 */
public static SessionData getSessionData(HttpServletRequest request) {
    SessionData sessionData = null;
    String sessionId = null;

    Hashtable<String, String[]> params = getParameters(request);

    if (params.containsKey("sessionid")) {
        // the session id is specified in the request parameters
        sessionId = ((String[]) params.get("sessionid"))[0];
        logMetacat.debug("session ID provided in request properties: " + sessionId);
    } else {
        HttpSession session = request.getSession(true);
        if (session.isNew()) {
            // this is a new session
            Cookie sessionCookie = RequestUtil.getCookie(request, "JSESSIONID");
            if (sessionCookie != null) {
                // and there is a JSESSIONID cookie
                sessionId = sessionCookie.getValue();
                logMetacat.debug("session ID provided in request cookie: " + sessionId);
            }
        }
        if (sessionId == null) {
            // there is an existing session (session is old)
            sessionId = session.getId();
            logMetacat.debug("session ID retrieved from request: " + sessionId);
        }
    }

    // if the session id is registered in SessionService, get the
    // SessionData for it. Otherwise, use the public session.
    if (SessionService.isSessionRegistered(sessionId)) {
        logMetacat.debug("retrieving session data from session service " + "for session id " + sessionId);
        sessionData = SessionService.getRegisteredSession(sessionId);
    } else {
        logMetacat.debug("using public session.  Given session id is " + "registered: " + sessionId);
        sessionData = SessionService.getPublicSession();
    }

    return sessionData;
}

From source file:org.itracker.web.util.LoginUtilities.java

public static User setupSession(String login, HttpServletRequest request, HttpServletResponse response) {
    if (null == login) {
        logger.warn("setupSession: null login", (logger.isDebugEnabled() ? new RuntimeException() : null));
        throw new IllegalArgumentException("null login");
    }/*from w w w. jav  a  2  s  .  c o m*/
    UserService userService = ServletContextUtils.getItrackerServices().getUserService();
    User user = userService.getUserByLogin(login);
    if (user != null) {
        String encPassword = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (Constants.COOKIE_NAME.equals(cookie.getName())) {
                    int seperator = cookie.getValue().indexOf('~');
                    if (seperator > 0) {
                        encPassword = cookie.getValue().substring(seperator + 1);
                    }
                }
            }
        }

        return setupSession(user, encPassword, request, response);
    }
    return null;
}

From source file:com.tc.utils.XSPUtils.java

public static String getCookie(String name) {
    String value = "";
    for (Cookie cookie : getCookies()) {
        if (cookie.getName().equals(name)) {
            value = cookie.getValue();
            break;
        }//from w  ww.j  a v  a  2  s  .co  m
    }
    return value;
}

From source file:org.itracker.web.util.LoginUtilities.java

public static boolean checkAutoLogin(HttpServletRequest request, boolean allowSaveLogin) {
    boolean foundLogin = false;

    if (request != null) {
        int authType = getRequestAuthType(request);

        // Check for auto login in request
        if (authType == AuthenticationConstants.AUTH_TYPE_REQUEST) {
            String redirectURL = request.getRequestURI().substring(request.getContextPath().length())
                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
            request.setAttribute(Constants.AUTH_TYPE_KEY, AuthenticationConstants.AUTH_TYPE_REQUEST);
            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
            request.setAttribute("processLogin", "true");
            foundLogin = true;/* ww w.  ja  va2  s .c o  m*/

        }

        // Add in check for client certs

        // Check for auto login with cookies, this will only happen if users
        // are allowed to save
        // their logins to cookies
        if (allowSaveLogin && !foundLogin) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (Constants.COOKIE_NAME.equals(cookie.getName())) {
                        int seperator = cookie.getValue().indexOf('~');
                        final String login;
                        if (seperator > 0) {
                            login = cookie.getValue().substring(0, seperator);
                            if (logger.isDebugEnabled()) {
                                logger.debug("Attempting autologin for user " + login + ".");
                            }

                            String redirectURL = request.getRequestURI()
                                    .substring(request.getContextPath().length())
                                    + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
                            request.setAttribute(Constants.AUTH_LOGIN_KEY,
                                    cookie.getValue().substring(0, seperator));
                            request.setAttribute(Constants.AUTH_TYPE_KEY,
                                    AuthenticationConstants.AUTH_TYPE_PASSWORD_ENC);

                            request.setAttribute(Constants.AUTH_VALUE_KEY,
                                    cookie.getValue().substring(seperator + 1));
                            request.setAttribute(Constants.AUTH_REDIRECT_KEY, redirectURL);
                            request.setAttribute("processLogin", "true");
                            foundLogin = true;
                        }
                    }
                }
            }
        }

    }

    return foundLogin;
}

From source file:com.xpn.xwiki.user.impl.xwiki.MyPersistentLoginManager.java

/**
 * Given an array of Cookies, a name, and a default value, this method tries to find the value of the cookie with
 * the given name. If there is no cookie matching the name in the array, then the default value is returned instead.
 * /*  w ww. j  a  v a2  s  .  c o  m*/
 * @param cookies The list of cookies to search.
 * @param cookieName The name of the cookie whose value should be returned.
 * @param defaultValue The default value that should be returned when no cookie with the given name was found.
 * @return The value of the cookie with the given name, or defaultValue if no such cookie was found.
 */
private static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
    String value = defaultValue;
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookieName.equals(cookie.getName())) {
                value = cookie.getValue();
            }
        }
    }
    return value;
}

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyController.java

private static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) {
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookieName.equals(cookie.getName())) {
                return (cookie.getValue());
            }//from  ww w  . j  a  v  a2s .  c o  m
        }
    }
    return defaultValue;
}

From source file:com.netease.channel.util.LoginUtil.java

/**
 * cookie???(,?)/* ww w . ja  va 2s  .com*/
 *
 * @param request
 * @return
 */
public static String getLoginStatus(final HttpServletRequest request) {
    Cookie ntesCookie = null;
    String username = null;
    String encodedUserName = null;
    Cookie[] cookies = request.getCookies();
    // ? cookies ? NULL  0 
    if (cookies != null && cookies.length > 0) {
        // ?? cookies
        for (Cookie cooky : cookies) {
            // cookies ??
            String cname = cooky.getName();
            //  cookies ?? NTES_SESS
            if ("NTES_SESS".equals(cname)) {
                ntesCookie = cooky;
                // cookies ???
                encodedUserName = ntesCookie.getValue();
                // ??
                ntescode ntes = new ntescode();
                // ?????
                int ret = ntes.validate_cookie(encodedUserName.getBytes(), 8, PERSISTENTTIME, true);
                // ?????
                if (ret >= 0) {
                    username = new String(ntes.ssn);
                    if (username.indexOf("@") <= 0) {
                        username += "@163.com";
                    }
                } else {
                    username = "";
                }
            }
        }
        // NTES_SESS???passport?
        // ?pinfo
        // 1cookie?pinfo??2
        String encodedPassport;
        if (StringUtils.isEmpty(username)) {
            try {
                for (Cookie cooky : cookies) {
                    if ("NTES_PASSPORT".equals(cooky.getName())) {
                        ntesCookie = cooky;
                        // cookies ???
                        encodedPassport = ntesCookie.getValue();
                        // ??
                        ntescode ntes = new ntescode();
                        // ?????,ret>0pinfo??
                        // 1,false??cookie
                        int ret = ntes.validate_persistent_cookie(encodedPassport.getBytes(), 8, PERSISTENTTIME,
                                true);
                        if (ret >= 0) {
                            username = new String(ntes.ssn);
                            if (username.indexOf("@") <= 0) {
                                username += "@163.com";
                            }
                        }
                    }
                }
            } catch (Exception ex) {
            }
        }
    }
    return username;
}