private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) { Date expires = null; if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); }
String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue());
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { NameValuePair attrib = attribs[j]; String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
private BasicClientCookie duplicateCookie(Cookie seleniumCookie) { BasicClientCookie duplicateCookie = new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue()); duplicateCookie.setDomain(seleniumCookie.getDomain()); duplicateCookie.setAttribute(BasicClientCookie.DOMAIN_ATTR, seleniumCookie.getDomain()); duplicateCookie.setSecure(seleniumCookie.isSecure()); duplicateCookie.setExpiryDate(seleniumCookie.getExpiry()); duplicateCookie.setPath(seleniumCookie.getPath()); return duplicateCookie; } }
@Override public void parse(final SetCookie cookie, final String value) throws MalformedCookieException { ((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true"); }
@Override public void parse(final SetCookie cookie, final String value) throws MalformedCookieException { ((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true"); }
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); ((BasicClientCookie) cookie).setAttribute(CookieUtil.HTTP_ONLY_ATTR, ""); }
BasicClientCookie cookie = new BasicClientCookie("cookieName", "cookieValue"); cookie.setDomain(".my.domain.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true"); CookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie);
private static com.gargoylesoftware.htmlunit.util.Cookie createCookie(javax.servlet.http.Cookie cookie) { Date expires = null; if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); }
static com.gargoylesoftware.htmlunit.util.Cookie createCookie(Cookie cookie) { Date expires = null; if (cookie.getMaxAge() > -1) { expires = new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000); } BasicClientCookie result = new BasicClientCookie(cookie.getName(), cookie.getValue()); result.setDomain(cookie.getDomain()); result.setComment(cookie.getComment()); result.setExpiryDate(expires); result.setPath(cookie.getPath()); result.setSecure(cookie.getSecure()); if (cookie.isHttpOnly()) { result.setAttribute("httponly", "true"); } return new com.gargoylesoftware.htmlunit.util.Cookie(result); } }
CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "dfafdffddasfad32423423_!ad"); cookie.setDomain("sso.comp.com"); cookie.setPath("/"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true"); cookieStore.addCookie(cookie);
/** * 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; }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { final List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (final HeaderElement headerelement : elems) { final String name = headerelement.getName(); final String value = headerelement.getValue(); if (name == null || name.isEmpty()) { continue; } final BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters final NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { final NameValuePair attrib = attribs[j]; final String s = attrib.getName().toLowerCase(Locale.ROOT); cookie.setAttribute(s, attrib.getValue()); final CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { NameValuePair attrib = attribs[j]; String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { NameValuePair attrib = attribs[j]; String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
private <T> void contributeCookies(final DefaultHttpClient httpClient, final HttpClientRequest<T> httpClientRequest) { final List<Cookie> cookies = httpClientRequest.getCookies(); if (CollectionUtils.isNotEmpty(cookies)) { final CookieStore cookieStore = new BasicCookieStore(); for (final Cookie cookie : cookies) { final BasicClientCookie httpCookie = new BasicClientCookie(cookie.getName(), cookie.getValue()); final int maxAge = cookie.getMaxAge(); if (maxAge > 0) { final Date expire = new Date(System.currentTimeMillis() + maxAge * 1000L); httpCookie.setExpiryDate(expire); httpCookie.setAttribute(ClientCookie.MAX_AGE_ATTR, Integer.toString(maxAge)); } httpCookie.setVersion(1); httpCookie.setPath(cookie.getPath()); httpCookie.setDomain(cookie.getDomain()); httpCookie.setSecure(cookie.getSecure()); LOG.debug("Adding cookie to the request: '%s'", httpCookie); cookieStore.addCookie(httpCookie); } httpClient.setCookieStore(cookieStore); } else { LOG.debug("No cookies found."); httpClient.setCookieStore(null); } }
/** * 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; }
/** * Create an HttpClient cookie from a JMeter cookie */ private org.apache.http.cookie.Cookie makeCookie(Cookie jmc) { long exp = jmc.getExpiresMillis(); BasicClientCookie ret = new BasicClientCookie(jmc.getName(), jmc.getValue()); ret.setDomain(jmc.getDomain()); ret.setPath(jmc.getPath()); ret.setExpiryDate(exp > 0 ? new Date(exp) : null); // use null for no expiry ret.setSecure(jmc.getSecure()); ret.setVersion(jmc.getVersion()); if(jmc.isDomainSpecified()) { ret.setAttribute(ClientCookie.DOMAIN_ATTR, jmc.getDomain()); } if(jmc.isPathSpecified()) { ret.setAttribute(ClientCookie.PATH_ATTR, jmc.getPath()); } return ret; }
/** * 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; } }
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); } } }