Java Code Examples for org.apache.http.impl.cookie.BasicClientCookie#setAttribute()

The following examples show how to use org.apache.http.impl.cookie.BasicClientCookie#setAttribute() . 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: Cookie.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new cookie with the specified name and value which applies to the specified domain,
 * the specified path, and expires on the specified date.
 * @param domain the domain to which this cookie applies
 * @param name the cookie name
 * @param value the cookie name
 * @param path the path to which this cookie applies
 * @param expires the date on which this cookie expires
 * @param secure whether or not this cookie is secure (i.e. HTTPS vs HTTP)
 * @param httpOnly whether or not this cookie should be only used for HTTP(S) headers
 */
public Cookie(final String domain, final String name, final String value, final String path, final Date expires,
    final boolean secure, final boolean httpOnly) {
    if (domain == null) {
        throw new IllegalArgumentException("Cookie domain must be specified");
    }

    final BasicClientCookie cookie = new BasicClientCookie(name, value != null ? value : "");
    cookie.setDomain(domain);
    cookie.setPath(path);
    cookie.setExpiryDate(expires);
    cookie.setSecure(secure);
    if (httpOnly) {
        cookie.setAttribute("httponly", "true");
    }
    httpClientCookie_ = cookie;
}
 
Example 2
Source File: JLineupHttpClient.java    From jlineup with Apache License 2.0 6 votes vote down vote up
private void addCookiesToStore(List<Cookie> cookies, CookieStore cookieStore, String domain) {
    for (Cookie cookie : cookies) {
        BasicClientCookie apacheCookie = new BasicClientCookie(cookie.name, cookie.value);
        apacheCookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
        if (cookie.domain != null) {
            apacheCookie.setDomain(cookie.domain);
        } else {
            apacheCookie.setDomain(domain);
        }
        if (cookie.expiry != null) {
            apacheCookie.setExpiryDate(cookie.expiry);
        }
        if (cookie.path != null) {
            apacheCookie.setPath(cookie.path);
        }
        apacheCookie.setSecure(cookie.secure);
        cookieStore.addCookie(apacheCookie);
    }
}
 
Example 3
Source File: CookieConverter.java    From hsac-fitnesse-fixtures with Apache License 2.0 6 votes vote down vote up
/**
 * Converts Selenium cookie to Apache http client.
 * @param browserCookie selenium cookie.
 * @return http client format.
 */
protected ClientCookie convertCookie(Cookie browserCookie) {
    BasicClientCookie cookie = new BasicClientCookie(browserCookie.getName(), browserCookie.getValue());
    String domain = browserCookie.getDomain();
    if (domain != null && domain.startsWith(".")) {
        // http client does not like domains starting with '.', it always removes it when it receives them
        domain = domain.substring(1);
    }
    cookie.setDomain(domain);
    cookie.setPath(browserCookie.getPath());
    cookie.setExpiryDate(browserCookie.getExpiry());
    cookie.setSecure(browserCookie.isSecure());
    if (browserCookie.isHttpOnly()) {
        cookie.setAttribute("httponly", "");
    }
    return cookie;
}
 
Example 4
Source File: CookieManager.java    From vividus with Apache License 2.0 5 votes vote down vote up
private static org.apache.http.cookie.Cookie createHttpClientCookie(Cookie seleniumCookie)
{
    BasicClientCookie httpClientCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue());
    httpClientCookie.setDomain(seleniumCookie.getDomain());
    httpClientCookie.setPath(seleniumCookie.getPath());
    httpClientCookie.setExpiryDate(seleniumCookie.getExpiry());
    httpClientCookie.setSecure(seleniumCookie.isSecure());
    httpClientCookie.setAttribute(ClientCookie.DOMAIN_ATTR, seleniumCookie.getDomain());
    httpClientCookie.setAttribute(ClientCookie.PATH_ATTR, seleniumCookie.getPath());
    return httpClientCookie;
}
 
Example 5
Source File: ApacheCloudStackClient.java    From apache-cloudstack-java-client with Apache License 2.0 5 votes vote down vote up
/**
 *  This method will create a {@link BasicClientCookie} with the given {@link HeaderElement}.
 *  It sill set the cookie's name and value according to the {@link HeaderElement#getName()} and {@link HeaderElement#getValue()} methods.
 *  Moreover, it will transport every {@link HeaderElement} parameter to the cookie using the {@link BasicClientCookie#setAttribute(String, String)}.
 *  Additionally, it configures the cookie path ({@link BasicClientCookie#setPath(String)}) to value '/client/api' and the cookie domain using {@link #configureDomainForCookie(BasicClientCookie)} method.
 */
protected BasicClientCookie createCookieForHeaderElement(HeaderElement element) {
    BasicClientCookie cookie = new BasicClientCookie(element.getName(), element.getValue());
    for (NameValuePair parameter : element.getParameters()) {
        cookie.setAttribute(parameter.getName(), parameter.getValue());
    }
    cookie.setPath("/client/api");
    configureDomainForCookie(cookie);
    return cookie;
}
 
Example 6
Source File: DefaultCookieManager.java    From esigate with Apache License 2.0 4 votes vote down vote up
protected static Cookie rewriteForBrowser(Cookie cookie, DriverRequest request) {
    String name = cookie.getName();
    // Rewrite name if JSESSIONID because it will interfere with current
    // server session
    if ("JSESSIONID".equalsIgnoreCase(name)) {
        name = "_" + name;
    }

    // Rewrite domain
    String domain =
            rewriteDomain(cookie.getDomain(), request.getBaseUrl().getHost(),
                    UriUtils.extractHostName(request.getOriginalRequest().getRequestLine().getUri()));

    // Rewrite path
    String originalPath = cookie.getPath();
    String requestPath = UriUtils.getPath(request.getOriginalRequest().getRequestLine().getUri());
    String path = originalPath;
    if (requestPath == null || !requestPath.startsWith(originalPath)) {
        path = "/";
    }

    // Rewrite secure
    boolean secure =
            (cookie.isSecure() && request.getOriginalRequest().getRequestLine().getUri().startsWith("https"));

    BasicClientCookie cookieToForward = new BasicClientCookie(name, cookie.getValue());
    if (domain != null) {
        cookieToForward.setDomain(domain);
    }
    cookieToForward.setPath(path);
    cookieToForward.setSecure(secure);
    cookieToForward.setComment(cookie.getComment());
    cookieToForward.setVersion(cookie.getVersion());
    cookieToForward.setExpiryDate(cookie.getExpiryDate());

    if (((BasicClientCookie) cookie).containsAttribute(CookieUtil.HTTP_ONLY_ATTR)) {
        cookieToForward.setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");
    }

    if (LOG.isDebugEnabled()) {
        // Ensure .toString is only called if debug enabled.
        LOG.debug("Forwarding cookie {} -> {}", cookie.toString(), cookieToForward.toString());
    }
    return cookieToForward;
}