org.apache.http.cookie.ClientCookie Java Examples

The following examples show how to use org.apache.http.cookie.ClientCookie. 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: CookieManagerTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void testGetCookiesAsHttpCookieStore()
{
    configureMockedWebDriver();
    Cookie seleniumCookie = createSeleniumCookie();
    mockGetCookies(seleniumCookie);
    CookieStore cookieStore = cookieManager.getCookiesAsHttpCookieStore();
    List<org.apache.http.cookie.Cookie> resultCookies = cookieStore.getCookies();
    assertEquals(1, resultCookies.size());
    org.apache.http.cookie.Cookie httpCookie = resultCookies.get(0);
    assertThat(httpCookie, instanceOf(BasicClientCookie.class));
    BasicClientCookie clientCookie = (BasicClientCookie) httpCookie;
    assertAll(
        () -> assertEquals(seleniumCookie.getDomain(), clientCookie.getDomain()),
        () -> assertEquals(seleniumCookie.getExpiry(), clientCookie.getExpiryDate()),
        () -> assertEquals(seleniumCookie.getName(), clientCookie.getName()),
        () -> assertEquals(seleniumCookie.getPath(), clientCookie.getPath()),
        () -> assertEquals(seleniumCookie.getValue(), clientCookie.getValue()),
        () -> assertEquals(seleniumCookie.isSecure(), clientCookie.isSecure()),
        () -> assertEquals(seleniumCookie.getDomain(), clientCookie.getAttribute(ClientCookie.DOMAIN_ATTR)),
        () -> assertEquals(seleniumCookie.getPath(), clientCookie.getAttribute(ClientCookie.PATH_ATTR))
    );
}
 
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: Cookie.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the specified array of HttpClient cookies into a list of cookies.
 * @param cookies the cookies to be converted
 * @return the specified HttpClient cookies, as cookies
 */
public static List<Cookie> fromHttpClient(final List<org.apache.http.cookie.Cookie> cookies) {
    final List<Cookie> list = new ArrayList<>(cookies.size());
    for (final org.apache.http.cookie.Cookie c : cookies) {
        list.add(new Cookie((ClientCookie) c));
    }
    return list;
}
 
Example #6
Source File: CookieConverter.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Converts Selenium cookies to Apache http client ones.
 * @param browserCookies cookies in Selenium format.
 * @param cookieStore store to place coverted cookies in.
 */
public void copySeleniumCookies(Set<Cookie> browserCookies, CookieStore cookieStore) {
    for (Cookie browserCookie : browserCookies) {
        ClientCookie cookie = convertCookie(browserCookie);
        cookieStore.addCookie(cookie);
    }
}
 
Example #7
Source File: HtmlUnitVersionAttributeHandler.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public String getAttributeName() {
    return ClientCookie.VERSION_ATTR;
}
 
Example #8
Source File: HtmlUnitCookieStore.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized void addCookie(final Cookie cookie) {
    manager_.addCookie(new com.gargoylesoftware.htmlunit.util.Cookie((ClientCookie) cookie));
}
 
Example #9
Source File: SolrPortAwareCookieSpecFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public PortAwareCookieSpec(String patterns[]) {
  super(patterns);
  super.registerAttribHandler(ClientCookie.DOMAIN_ATTR, new PortAwareDomainHandler());
}
 
Example #10
Source File: Cookie.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new HtmlUnit cookie from the HttpClient cookie provided.
 * @param clientCookie the HttpClient cookie
 */
public Cookie(final ClientCookie clientCookie) {
    httpClientCookie_ = clientCookie;
}