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:com.iterzp.momo.utils.WebUtils.java

/**
 * ?cookie//  w w 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 {
            name = URLEncoder.encode(name, "UTF-8");
            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.xidu.framework.common.util.CookieUtils.java

/**
 * get the value from cookie vai the name
 * //from   ww  w. j a  v  a 2  s. c  o m
 * @param req
 *            - HttpServletRequest's instance
 * @param res
 *            - HttpServletResponse's instance
 * @param name
 *            - Cookie's entry key
 * @return cookie's entry value
 */
public static String getCookie(HttpServletRequest req, HttpServletResponse res, String name) {
    if (StringUtils.isBlank(name)) {
        return null;
    }
    Cookie cookie = null;
    String value = null;
    Cookie[] cookies = req.getCookies();
    if (null != cookies) {
        for (int i = 0; i < cookies.length; i++) {
            cookie = cookies[i];
            logger.debug("Cookie name:" + cookie.getName() + " value:" + cookie.getValue());
            if (name.equals(cookie.getName())) {
                value = cookie.getValue();
                break;
            }
        }
    }
    logger.debug("Getting cookie value:" + value);
    return EnDecoderUtils.decode(value);
}

From source file:gov.nih.nci.cabig.caaers.web.utils.WebUtils.java

public static String getBuildInfoCookie(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (cookies == null)
        return null;
    for (Cookie c : cookies) {
        if (c.getName().equals(BUILD_INFO_COOKIE))
            return c.getValue();
    }//from w w  w . j  ava 2s.  co m
    return null;
}

From source file:com.persistent.cloudninja.controller.AuthFilterUtils.java

public static String getRoleFromCookie(Cookie cookie, String authotitiesPrefix) {
    String cookieValue = cookie.getValue();
    String roleArray = getFieldValueFromCookieString(authotitiesPrefix, cookieValue);
    String role = roleArray.substring(1, roleArray.length() - 1);
    return role;//  w w  w .  ja  va  2 s.c  o m
}

From source file:RequestUtil.java

/**
 * Encode a cookie as per RFC 2109. The resulting string can be used as the
 * value for a <code>Set-Cookie</code> header.
 * /*from   w w  w  .  j a  v a2 s .c  o m*/
 * @param cookie
 *            The cookie to encode.
 * @return A string following RFC 2109.
 */
public static String encodeCookie(Cookie cookie) {

    StringBuffer buf = new StringBuffer(cookie.getName());
    buf.append("=");
    buf.append(cookie.getValue());

    if (cookie.getComment() != null) {
        buf.append("; Comment=\"");
        buf.append(cookie.getComment());
        buf.append("\"");
    }

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

    if (cookie.getMaxAge() >= 0) {
        buf.append("; Max-Age=\"");
        buf.append(cookie.getMaxAge());
        buf.append("\"");
    }

    if (cookie.getPath() != null) {
        buf.append("; Path=\"");
        buf.append(cookie.getPath());
        buf.append("\"");
    }

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

    if (cookie.getVersion() > 0) {
        buf.append("; Version=\"");
        buf.append(cookie.getVersion());
        buf.append("\"");
    }

    return (buf.toString());
}

From source file:org.dd4t.core.util.HttpUtils.java

public static String getSessionPreviewToken(HttpServletRequest request) {
    if (request == null) {
        return null;
    }/*from   w w w .j ava  2 s . com*/

    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (TridionUtils.PREVIEW_SESSION_TOKEN.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
    }

    return null;
}

From source file:com.liferay.portal.util.CookieKeys.java

public static void addCookie(HttpServletRequest request, HttpServletResponse response, Cookie cookie,
        boolean secure) {

    if (!PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES || PropsValues.TCK_URL) {

        return;/*  w w w .  j a v a2s  . co m*/
    }

    // LEP-5175

    String name = cookie.getName();

    String originalValue = cookie.getValue();
    String encodedValue = originalValue;

    if (isEncodedCookie(name)) {
        encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));

        if (_log.isDebugEnabled()) {
            _log.debug("Add encoded cookie " + name);
            _log.debug("Original value " + originalValue);
            _log.debug("Hex encoded value " + encodedValue);
        }
    }

    cookie.setSecure(secure);
    cookie.setValue(encodedValue);
    cookie.setVersion(VERSION);

    // Setting a cookie will cause the TCK to lose its ability to track
    // sessions

    response.addCookie(cookie);
}

From source file:com.feilong.servlet.http.CookieUtil.java

/**
 * {@link Cookie} key value? map({@link TreeMap}).
 *
 * @param request/*ww w.j a  v  a  2s.  c om*/
 *            the request
 * @return  {@link HttpServletRequest#getCookies()}, {@link Collections#emptyMap()};<br>
 *         ?, loop cookies,? {@link Cookie#getName()}? {@link Cookie#getValue()} ?map 
 * @see HttpServletRequest#getCookies()
 * @see javax.servlet.http.Cookie#getName()
 * @see javax.servlet.http.Cookie#getValue()
 * @see javax.servlet.jsp.el.ImplicitObjectELResolver.ImplicitObjects#createCookieMap(javax.servlet.jsp.PageContext)
 */
public static Map<String, String> getCookieMap(HttpServletRequest request) {
    Cookie[] cookies = request.getCookies();
    if (isNullOrEmpty(cookies)) {
        return emptyMap();
    }

    Map<String, String> map = new TreeMap<>();
    for (Cookie cookie : cookies) {
        map.put(cookie.getName(), cookie.getValue());
    }
    return map;
}

From source file:edu.stanford.epad.epadws.xnat.XNATSessionOperations.java

public static String getJSessionIDFromRequest(HttpServletRequest servletRequest) {
    String jSessionID = null;/* w ww.  j  av a2s .com*/

    Cookie[] cookies = servletRequest.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) {
                jSessionID = cookie.getValue();
                break;
            }
        }
    }
    if (jSessionID == null)
        log.warning("No JSESESSIONID cookie present in request " + servletRequest.getRequestURL());

    return jSessionID;
}

From source file:com.feilong.servlet.http.CookieUtil.java

/**
 * ?Cookie.//w w  w  . j ava  2s .com
 * 
 * @param request
 *            HttpServletRequest
 * @param cookieName
 *            cookie??,{@link Cookie#getName()}
 * @return ??cookie, <code>null</code>;<br>
 *         ?, {@link Cookie#getValue()}
 * @see #getCookie(HttpServletRequest, String)
 * @see Cookie#getValue()
 */
public static String getCookieValue(HttpServletRequest request, String cookieName) {
    Cookie cookie = getCookie(request, cookieName);
    return null == cookie ? null : cookie.getValue();
}