org.apache.http.cookie.SetCookie Java Examples

The following examples show how to use org.apache.http.cookie.SetCookie. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpCookieSteps.java    From vividus with Apache License 2.0 6 votes vote down vote up
/**
 * Change cookie value.
 * If several cookies with the same name exist in cookie store, the value will be changed for all of them,
 * @param cookieName     name of cookie
 * @param newCookieValue value to set
 */
@When("I change value of all HTTP cookies with name `$cookieName` to `$newCookieValue`")
public void changeHttpCookieValue(String cookieName, String newCookieValue)
{
    List<Cookie> cookies = findCookiesBy(FILTER_BY_NAME, cookieName);
    if (assertCookiesPresent(cookieName, cookies.size()))
    {
        cookies.forEach(cookie -> {
            if (cookie instanceof SetCookie)
            {
                ((SetCookie) cookie).setValue(newCookieValue);
            }
            else
            {
                throw new IllegalStateException(
                        String.format("Unable to change value of cookie with name '%s' of type '%s'", cookieName,
                                cookie.getClass().getName()));
            }
        });
    }
}
 
Example #2
Source File: HtmlUnitDomainHandler.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
@Override
public void parse(final SetCookie cookie, final String value)
        throws MalformedCookieException {
    Args.notNull(cookie, HttpHeader.COOKIE);
    if (TextUtils.isBlank(value)) {
        throw new MalformedCookieException("Blank or null value for domain attribute");
    }
    // Ignore domain attributes ending with '.' per RFC 6265, 4.1.2.3
    if (value.endsWith(".")) {
        return;
    }
    String domain = value;
    domain = domain.toLowerCase(Locale.ROOT);

    final int dotIndex = domain.indexOf('.');
    if (browserVersion_.hasFeature(HTTP_COOKIE_REMOVE_DOT_FROM_ROOT_DOMAINS)
            && dotIndex == 0 && domain.length() > 1 && domain.indexOf('.', 1) == -1) {
        domain = domain.toLowerCase(Locale.ROOT);
        domain = domain.substring(1);
    }
    if (dotIndex > 0) {
        domain = '.' + domain;
    }

    cookie.setDomain(domain);
}
 
Example #3
Source File: HtmlUnitVersionAttributeHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Parse cookie version attribute.
 */
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
    if (value == null) {
        throw new MalformedCookieException("Missing value for version attribute");
    }
    int version = 0;
    try {
        version = Integer.parseInt(value);
    }
    catch (final NumberFormatException e) {
        // ignore invalid versions
    }
    cookie.setVersion(version);
}
 
Example #4
Source File: HtmlUnitExpiresHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(final SetCookie cookie, String value) throws MalformedCookieException {
    if (value.startsWith("\"") && value.endsWith("\"")) {
        value = value.substring(1, value.length() - 1);
    }
    value = value.replaceAll("[ ,:-]+", " ");

    Date startDate = null;
    String[] datePatterns = DEFAULT_DATE_PATTERNS;

    if (null != browserVersion_) {
        if (browserVersion_.hasFeature(HTTP_COOKIE_START_DATE_1970)) {
            startDate = HtmlUnitBrowserCompatCookieSpec.DATE_1_1_1970;
        }

        if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1)) {
            datePatterns = EXTENDED_DATE_PATTERNS_1;
        }

        if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2)) {
            final Calendar calendar = Calendar.getInstance(Locale.ROOT);
            calendar.setTimeZone(DateUtils.GMT);
            calendar.set(1969, Calendar.JANUARY, 1, 0, 0, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            startDate = calendar.getTime();

            datePatterns = EXTENDED_DATE_PATTERNS_2;
        }
    }

    final Date expiry = DateUtils.parseDate(value, datePatterns, startDate);
    cookie.setExpiryDate(expiry);
}
 
Example #5
Source File: HtmlUnitHttpOnlyHandler.java    From htmlunit with Apache License 2.0 3 votes vote down vote up
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
    ((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true");
}
 
Example #6
Source File: HttpOnlyHandler.java    From esigate with Apache License 2.0 2 votes vote down vote up
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {

        Args.notNull(cookie, "Cookie");
        ((BasicClientCookie) cookie).setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");

    }