Example usage for javax.servlet.http HttpServletRequest getCookies

List of usage examples for javax.servlet.http HttpServletRequest getCookies

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getCookies.

Prototype

public Cookie[] getCookies();

Source Link

Document

Returns an array containing all of the Cookie objects the client sent with this request.

Usage

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

/**
 * cookie???(,?)//from   w w w.  j  a  v a 2 s .  c  om
 *
 * @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;
}

From source file:com.threewks.thundr.request.servlet.ServletSupport.java

/**
 * Gets the first cookie of the given name from the request
 * /*from  ww w .  j a  v a 2  s .  co  m*/
 * @param name
 * @param req
 * @return
 */
public static final javax.servlet.http.Cookie getCookie(String name, HttpServletRequest req) {
    if (req.getCookies() != null) {
        for (javax.servlet.http.Cookie cookie : req.getCookies()) {
            if (cookie.getName().equals(name)) {
                return cookie;
            }
        }
    }
    return null;
}

From source file:org.apache.accumulo.monitor.servlets.BasicServlet.java

public static final String getCookieValue(HttpServletRequest req, String name) {
    if (req.getCookies() != null)
        for (Cookie c : req.getCookies())
            if (c.getName().equals(name))
                return c.getValue();
    return null;//from  w w  w . java2s.com
}

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

/**
 * ?COOKIE//from   w w  w .  j a va  2  s  .co m
 * 
 * @param name
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null)
        return null;
    for (int i = 0; i < cookies.length; i++) {
        if (name.equals(cookies[i].getName())) {
            return cookies[i];
        }
    }
    return null;
}

From source file:net.shopxx.util.CookieUtils.java

/**
 * ?cookie//  ww  w. j  a v a2 s . c  o m
 * 
 * @param request
 *            HttpServletRequest
 * @param name
 *            cookie??
 * @return ?null
 */
public static String getCookie(HttpServletRequest request, String name) {
    Assert.notNull(request);
    Assert.hasText(name);
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        try {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return URLDecoder.decode(cookie.getValue(), "UTF-8");
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.ms.commons.cookie.parser.CookieParser.java

/**
 * <pre>/*  ww w .j  a v  a  2  s. c  om*/
 * ?questCookieCookie?{@link CookieNameEnum}Key,{@link CookieNameHelper}ValueMap
 * {@link CookieNameHelper}?{@link CookieNameEnum}{@link CookieKeyEnum}
 * @return Request CookieemptyMap
 * </pre>
 */
public static Map<CookieNameEnum, CookieNameHelper> loadCookie(HttpServletRequest request) {
    // Map
    Map<CookieNameEnum, CookieNameHelper> allValues = new HashMap<CookieNameEnum, CookieNameHelper>();
    // Cookie
    Map<String, String> cookieKV = CookieUtils.arrayToMap(request.getCookies());
    // ?CookieName??
    for (CookieNameEnum cookieName : CookieNameEnum.values()) {
        // ?CookieName??
        CookieNameConfig cookieNameConfig = CookieNamePolicyParser.getCookieNamePolicyMap().get(cookieName);
        boolean isCookieExisted = cookieKV.containsKey(cookieNameConfig.getCookieName());
        if (isCookieExisted) {
            String value = cookieKV.get(cookieName.getCookieName());
            CookieNameHelper cookieNameHelper = paserCookieValue(cookieNameConfig, value);
            // ?
            if (cookieNameHelper != null) {
                allValues.put(cookieName, cookieNameHelper);
            }
        }

    }
    return allValues;
}

From source file:org.craftercms.core.util.HttpServletUtils.java

public static Map<String, String> createCookiesMap(HttpServletRequest request) {
    Map<String, String> cookiesMap = new HashMap<String, String>();
    Cookie[] cookies = request.getCookies();

    if (ArrayUtils.isNotEmpty(cookies)) {
        for (Cookie cookie : request.getCookies()) {
            cookiesMap.put(cookie.getName(), cookie.getValue());
        }//from   w  ww  . ja va2s.c  o  m
    }

    return cookiesMap;
}

From source file:com.buession.cas.web.utils.CaptchaUtils.java

/**
 * ???/*from   w ww . ja va 2s  .  c om*/
 * 
 * @param request
 *        HttpServletRequest
 * @param captchaProducer
 *        ?????
 * @param validateCode
 *        ????
 * @return ???
 */
public static boolean validate(final HttpServletRequest request, final Config config,
        final String validateCode) {
    if (validateCode == null || validateCode.length() == 0) {
        return false;
    }

    Cookie cookies[] = request.getCookies();
    if (cookies == null || cookies.length == 0) {
        return false;
    }

    String captchaCookieName = CaptchaUtils.getCaptchaCookieName(config);
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];

        if (captchaCookieName.equals(cookie.getName()) == true) {
            mcrypt.setSalt(request.getSession().getId());
            return mcrypt.encode(validateCode).equalsIgnoreCase(cookie.getValue());
        }
    }

    return false;
}

From source file:com.rantop.web.util.web.ServletUtils.java

/**
 * Convenience method to get a cookie by name
 *
 * @param request the current request/*from  ww  w  .j a  v a  2 s  . c  om*/
 * @param name the name of the cookie to find
 *
 * @return the cookie (if found), null if not found
 */
public static Cookie getCookie(HttpServletRequest request, String name) {
    Cookie[] cookies = request.getCookies();
    Cookie returnCookie = null;

    if (cookies == null) {
        return returnCookie;
    }

    for (int i = 0; i < cookies.length; i++) {
        Cookie thisCookie = cookies[i];

        if (thisCookie.getName().equals(name)) {
            // cookies with no value do me no good!
            if (!thisCookie.getValue().equals("")) {
                returnCookie = thisCookie;

                break;
            }
        }
    }

    return returnCookie;
}

From source file:com.activecq.api.utils.CookieUtil.java

/**
 * Removes all cookies for the domain//  w  ww  .  j av  a2 s.c om
 *
 * @param request Request to get the Cookies to drop
 * @param response Response to expire the Cookies on
 */
public static int dropAllCookies(HttpServletRequest request, HttpServletResponse response) {
    int count = 0;
    Cookie[] cookies = request.getCookies();

    if (cookies == null) {
        return 0;
    }

    for (Cookie cookie : cookies) {
        cookie.setMaxAge(0);
        cookie.setPath(cookie.getPath());

        addCookie(cookie, response);
        count++;
    }

    return count;
}