Utility method to transform a Cookie into a Set-Cookie Header.

@param cookie
the {@link Cookie} to format

@return the value of the Set-Cookie header
173394
public static String encodeCookie(Cookie cookie) {
        int maxAge = -1;
        if (cookie.getExpiryDate() != null) {
            maxAge = (int) ((cookie.getExpiryDate().getTime() - System.currentTimeMillis()) / ONE_SECOND);
            // According to Cookie class specification, a negative value
            // would be considered as no value. That is not what we want!
            if (maxAge < 0) {
                maxAge = 0;
            }
        }

        String cookieName = cookie.getName();
        String cookieValue = cookie.getValue();

        StringBuilder buffer = new StringBuilder();

        // Copied from org.apache.http.impl.cookie.BrowserCompatSpec.formatCookies(List<Cookie>)
        if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
            buffer.append(BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(null,
                    new BasicHeaderElement(cookieName, cookieValue), false).toString());
        } else {
            // Netscape style cookies do not support quoted values
            buffer.append(cookieName);
            buffer.append("=");
            if (cookieValue != null) {
                buffer.append(cookieValue);
            }
        }
        // End copy

        appendAttribute(buffer, "Comment", cookie.getComment());
        appendAttribute(buffer, "Domain", cookie.getDomain());
        appendAttribute(buffer, "Max-Age", maxAge);
        appendAttribute(buffer, "Path", cookie.getPath());
        if (cookie.isSecure()) {
            appendAttribute(buffer, "Secure");
        }
        if (((BasicClientCookie) cookie).containsAttribute(HTTP_ONLY_ATTR)) {
            appendAttribute(buffer, "HttpOnly");
        }
        appendAttribute(buffer, "Version", cookie.getVersion());

        return (buffer.toString());
    }
1346856
private static String convertV0SetCookie(HttpCookie cookie, boolean isRFC1123DateEnabled) {

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "Cookie Name: " + cookie.getName());
            Tr.debug(tc, "Cookie MaxAge: " + cookie.getMaxAge());
            Tr.debug(tc, "RFC1123 Date Enabled (4 digit year): " + isRFC1123DateEnabled);
        }

        StringBuilder buffer = new StringBuilder(40);

        // Append name=value
        buffer.append(cookie.getName());
        String value = cookie.getValue();
        if (null != value && 0 != value.length()) {
            buffer.append('=');
            buffer.append(value);
        } else {
            // PK48196 - send an empty value string
            buffer.append("=\"\"");
        }

        // check for optional comment
        String comment = cookie.getComment();
        if (null != comment && 0 != comment.length()) {
            buffer.append("; Comment=");
            maybeQuote(buffer, comment);
        }

        // check for optional max-age/expires
        int expires = cookie.getMaxAge();
        if (-1 < expires) {
            if (0 == expires) {
                if (!isRFC1123DateEnabled) {
                    buffer.append(longAgo);
                } else {
                    buffer.append(longAgoRFC1123);
                }
            } else {
                buffer.append("; Expires=");
                if (!isRFC1123DateEnabled) {
                    buffer.append(HttpDispatcher.getDateFormatter().getRFC2109Time(new Date(HttpDispatcher.getApproxTime() + (expires * 1000L))));
                } else {
                    buffer.append(HttpDispatcher.getDateFormatter().getRFC1123Time(new Date(HttpDispatcher.getApproxTime() + (expires * 1000L))));
                }
            }
        } else if (cookie.isDiscard()) {
            // convert discard to immediate expiration
            if (!isRFC1123DateEnabled) {
                buffer.append(longAgo);
            } else {
                buffer.append(longAgoRFC1123);
            }
        }

        // check for optional path
        value = cookie.getPath();
        if (null != value && 0 != value.length()) {
            buffer.append("; Path=");
            buffer.append(value);
        }

        // check for optional domain
        value = cookie.getDomain();
        if (null != value && 0 != value.length()) {
            buffer.append("; Domain=");
            buffer.append(value);
        }

        if (cookie.isSecure()) {
            buffer.append("; Secure");
        }

        if (cookie.isHttpOnly()) {
            buffer.append("; HttpOnly");
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "Created v0 Set-Cookie: [" + GenericUtils.nullOutPasswords(buffer.toString(), (byte) '&') + "]");
        }
        return buffer.toString();
    }
492795
@Override
    public boolean setCookie(HttpCookie cookie, HttpHeaderKeys header) {
        if (null == cookie || null == header) {
            return false;
        }

        // only cookie versions 0 and 1 are supported
        if (1 < cookie.getVersion()) {
            throw new IllegalArgumentException("Cookie version is invalid: " + cookie.getVersion());
        }

        if (header.equals(HttpHeaderKeys.HDR_SET_COOKIE) || header.equals(HttpHeaderKeys.HDR_SET_COOKIE2)) {
            return addCookie(cookie, header);
        }
        return false;
    }
475926
public function setCookie(Cookie $cookie) : Response
    {
        $this->cookies[] = $cookie;
        $this->addHeader('Set-Cookie', (string) $cookie);

        return $this;
    }
419584
protected function setCookie($cookie)
    {
        $this->cookie = trim(base64_encode(mcrypt_encrypt(
            MCRYPT_RIJNDAEL_256, '7e2d20j23a21db9f', $cookie, MCRYPT_MODE_ECB, $this->config->getSecret()
        )));

        setcookie($this->config->getCookieName(), $this->cookie, (time() + 60 * 5), null, null, null, true);

        return $this;
    }
843937
public Cookie put(CharSequence name, Cookie cookie) {
        return cookies.put(name, cookie);
    }
557589
public String encode(Collection<? extends Cookie> cookies) {
        if (checkNotNull(cookies, "cookies").isEmpty()) {
            return null;
        }

        StringBuilder buf = stringBuilder();
        if (strict) {
            if (cookies.size() == 1) {
                encode(buf, cookies.iterator().next());
            } else {
                Cookie[] cookiesSorted = cookies.toArray(new Cookie[0]);
                Arrays.sort(cookiesSorted, COOKIE_COMPARATOR);
                for (Cookie c : cookiesSorted) {
                    encode(buf, c);
                }
            }
        } else {
            for (Cookie c : cookies) {
                encode(buf, c);
            }
        }
        return stripTrailingSeparatorOrNull(buf);
    }
728686
public function setCookie(Cookie $cookie) : self
    {
        $this->cookies[$cookie->getName()] = $cookie;

        return $this;
    }
269894
public Response removeCookie(String name) {
        Cookie cookie = new Cookie(name, "");
        cookie.setSecure(true);
        cookie.setMaxAge(0);
        addCookie(cookie);

        return this;
    }
1093515
public String cookie(String name) {
        Cookie[] cookies = servletRequest.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
416945
public Response andCookie(Cookie cookie) {
        Objects.requireNonNull(cookie, Required.COOKIE.toString());
        this.cookies.add(cookie);

        return this;
    }
1269024
public void setCookie(Cookie cookie) {
        String domain = cookie.getDomain();
        Date expiry = cookie.getExpiry();
        String name = cookie.getName();
        String path = cookie.getPath();
        String value = cookie.getValue();

        String action = "Setting up cookie with attributes:<div><table><tbody><tr><td>Domain</td><td>" + domain +
                "</tr><tr><td>Expiration</td><td>" + expiry.toString() + "</tr><tr><td>Name</td><td>" + name +
                "</tr><tr><td>Path</td><td>" + path + "</tr><tr><td>Value</td><td>" + value +
                "</tr></tbody></table></div><br/>";
        String expected = "Cookie is added";
        try {
            driver.manage().addCookie(cookie);
        } catch (Exception e) {
            reporter.fail(action, expected, "Unable to add cookie. " + e.getMessage());
            log.warn(e);
            return;
        }
        reporter.pass(action, expected, expected);
    }
696600
@Override
    public Cookie toCookie() {
        Cookie cookie = new Cookie(name, value);
        cookie.setComment(comment);
        if (domain != null) {
            cookie.setDomain(domain);
        }
        cookie.setMaxAge(getMaxAge());
        cookie.setPath(path);
        cookie.setSecure(secure);
        cookie.setVersion(version);
        return cookie;
    }
1252782
public String getCookie(String name) {
		Cookie c = requestCookies.get().get(name);
		return c == null ? null : c.getValue();
	}
248010
public static Cookie createCookie(String cookieName, @Sensitive String cookieValue, HttpServletRequest req) {
        return createCookie(cookieName, cookieValue, -1, req);
    }
1006873
protected String cookieValue(String name){
        Cookie cookie = cookie(name);
        return cookie != null ? cookie.getValue() : null;
    }
107685
public void setCookies(List<HttpCookie> cookies) {
        if (cookies.isEmpty()) {
            setHeader(HttpHeader.COOKIE, null);
        }

        StringBuilder sbData = new StringBuilder();

        for (HttpCookie c : cookies) {
            sbData.append(c.getName());
            sbData.append('=');
            sbData.append(c.getValue());
            sbData.append("; ");
        }

        if (sbData.length() <= 3) {
            setHeader(HttpHeader.COOKIE, null);
            return;
        }

        final String data = sbData.substring(0, sbData.length() - 2);
        setHeader(HttpHeader.COOKIE, data);
    }