private com.gargoylesoftware.htmlunit.util.Cookie createCookie(MockHttpServletRequest request, String sessionid) { return new com.gargoylesoftware.htmlunit.util.Cookie(request.getServerName(), "JSESSIONID", sessionid, request.getContextPath() + "/", null, request.isSecure(), true); }
private void cookies(MockHttpServletRequest request) { List<Cookie> cookies = new ArrayList<>(); String cookieHeaderValue = header("Cookie"); if (cookieHeaderValue != null) { StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;"); while (tokens.hasMoreTokens()) { String cookieName = tokens.nextToken().trim(); Assert.isTrue(tokens.hasMoreTokens(), () -> "Expected value for cookie name '" + cookieName + "': full cookie header was [" + cookieHeaderValue + "]"); String cookieValue = tokens.nextToken().trim(); processCookie(request, cookies, new Cookie(cookieName, cookieValue)); } } Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(this.webRequest.getUrl()); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) { processCookie(request, cookies, new Cookie(cookie.getName(), cookie.getValue())); } Cookie[] parentCookies = request.getCookies(); if (parentCookies != null) { for (Cookie cookie : parentCookies) { cookies.add(cookie); } } if (!ObjectUtils.isEmpty(cookies)) { request.setCookies(cookies.toArray(new Cookie[0])); } }
private void storeCookies(WebRequest webRequest, javax.servlet.http.Cookie[] cookies) { Date now = new Date(); CookieManager cookieManager = this.webClient.getCookieManager(); for (javax.servlet.http.Cookie cookie : cookies) { if (cookie.getDomain() == null) { cookie.setDomain(webRequest.getUrl().getHost()); } Cookie toManage = createCookie(cookie); Date expires = toManage.getExpires(); if (expires == null || expires.after(now)) { cookieManager.addCookie(toManage); } else { cookieManager.removeCookie(toManage); } } }
public WebResponse getResponse( WebRequest request ) throws IOException { Cookie jsCookie = webClient.getCookieManager().getCookie("JSESSIONID"); if (jsCookie != null && (!jsCookie.getValue().startsWith("\"") && !jsCookie.getValue().endsWith("\""))) { Cookie newCookie = new Cookie(jsCookie.getDomain(), jsCookie.getName(), "\"" + jsCookie.getValue() + "\"", jsCookie.getPath(), jsCookie.getExpires(), jsCookie.isSecure()); webClient.getCookieManager().removeCookie(jsCookie); webClient.getCookieManager().addCookie(newCookie); } return super.getResponse(request); } };
/** * {@inheritDoc} */ @Override public String toString() { return getName() + "=" + getValue() + (getDomain() != null ? ";domain=" + getDomain() : "") + (getPath() != null ? ";path=" + getPath() : "") + (getExpires() != null ? ";expires=" + getExpires() : "") + (isSecure() ? ";secure" : "") + (isHttpOnly() ? ";httpOnly" : ""); }
@Override public List<javax.servlet.http.Cookie> getCookies() { List<javax.servlet.http.Cookie> result = new LinkedList<javax.servlet.http.Cookie>(); Set<Cookie> cookies = wc.getCookieManager().getCookies(); for (Cookie cookie : cookies) { javax.servlet.http.Cookie c = new javax.servlet.http.Cookie( cookie.getName(), cookie.getValue()); c.setComment(cookie.toHttpClient().getComment()); c.setDomain(cookie.getDomain()); Date expire = cookie.toHttpClient().getExpiryDate(); if (expire == null) { c.setMaxAge(-1); } else { Date now = Calendar.getInstance().getTime(); // Convert milli-second to second Long second = Long.valueOf((expire.getTime() - now.getTime()) / 1000); c.setMaxAge(second.intValue()); } c.setPath(cookie.getPath()); c.setSecure(cookie.toHttpClient().isSecure()); c.setVersion(cookie.toHttpClient().getVersion()); result.add(c); } return result; }
int found = 0; for (Cookie c : cookies) { String name = c.getName(); String value = c.getValue(); Cookie existing = cm.getCookie(name); if (existing != null) { found++; String eval = existing.getValue(); if (eval.equals(value)) { LOG.info("Same Value as Existing: " + existing + " value=" + value); updated++; LOG.info("Different Value as Existing: " + existing + " value=" + value); Cookie nc = new Cookie(existing.getDomain(), name, value); cm.addCookie(nc);
/** * {@inheritDoc} */ @Override public boolean equals(final Object o) { if (!(o instanceof Cookie)) { return false; } final Cookie other = (Cookie) o; final String path = getPath() == null ? "/" : getPath(); final String otherPath = other.getPath() == null ? "/" : other.getPath(); return new EqualsBuilder().append(getName(), other.getName()).append(getDomain(), other.getDomain()) .append(path, otherPath).isEquals(); }
String inspectCookies(Collection<Cookie> col) { String out = ""; out += "Size:" + col.size(); ArrayList<Cookie> sorted = new ArrayList<>(); sorted.addAll(col); Collections.sort(sorted, (c1, c2) -> c1.getName().compareTo(c2.getName())); for (Cookie c : sorted) { out += c.getName() + "=" + c.getValue() + " [" + c.getDomain() + "]\n"; } return out; }
/** * {@inheritDoc} */ @Override @JsxGetter public String getCookie() { final HtmlPage page = getPage(); final URL url = page.getUrl(); final StringBuilder builder = new StringBuilder(); final Set<Cookie> cookies = page.getWebClient().getCookies(url); for (final Cookie cookie : cookies) { if (cookie.isHttpOnly()) { continue; } if (builder.length() != 0) { builder.append("; "); } if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) { builder.append(cookie.getName()); builder.append("="); } builder.append(cookie.getValue()); } return builder.toString(); }
private void storeCookie(String hostName, String rawCookie) { Cookie cookie = cookieParser.parseCookie(hostName, rawCookie); Date now = new Date(); if (cookie.getExpires() != null && now.after(cookie.getExpires())) { removeCookie(this.cookieManager, cookie.getName()); } else { this.cookieManager.addCookie(cookie); } }
/** * Returns the currently configured cookie with the specified name, or {@code null} if one does not exist. * If disabled, this returns null. * @param name the name of the cookie to return * @return the currently configured cookie with the specified name, or {@code null} if one does not exist */ public synchronized Cookie getCookie(final String name) { if (!isCookiesEnabled()) { return null; } for (Cookie cookie : cookies_) { if (StringUtils.equals(cookie.getName(), name)) { return cookie; } } return null; }
private void assertSingleSessionCookie(String expected) { com.gargoylesoftware.htmlunit.util.Cookie jsessionidCookie = webClient.getCookieManager().getCookie("JSESSIONID"); if (expected == null || expected.contains("Expires=Thu, 01-Jan-1970 00:00:01 GMT")) { assertThat(jsessionidCookie, nullValue()); return; } String actual = jsessionidCookie.getValue(); assertThat("JSESSIONID=" + actual + "; Path=/test; Domain=example.com", equalTo(expected)); }
/** * {@inheritDoc} */ @Override public String toString() { return getName() + "=" + getValue() + (getDomain() != null ? ";domain=" + getDomain() : "") + (getPath() != null ? ";path=" + getPath() : "") + (getExpires() != null ? ";expires=" + getExpires() : "") + (isSecure() ? ";secure" : "") + (isHttpOnly() ? ";httpOnly" : ""); }
public void saveCookies() throws IOException { CookieManager cm = getWebClient().getCookieManager(); ArrayList<BasicClientCookie> list = new ArrayList<>(); for (Cookie c : cm.getCookies()) { BasicClientCookie bc = new BasicClientCookie(c.getName(), c.getValue()); bc.setDomain(c.getDomain()); bc.setPath(c.getPath()); list.add(bc); } File cookiesFile = Directories.META.getDir(cookiesFileName); if (cookiesFile.exists()) { cookiesFile.delete(); } String o = new Gson().toJson(list); FileUtils.writeByteArrayToFile(cookiesFile, o.getBytes()); } /*
/** * {@inheritDoc} */ @Override public boolean equals(final Object o) { if (!(o instanceof Cookie)) { return false; } final Cookie other = (Cookie) o; final String path = getPath() == null ? "/" : getPath(); final String otherPath = other.getPath() == null ? "/" : other.getPath(); return new EqualsBuilder().append(getName(), other.getName()).append(getDomain(), other.getDomain()) .append(path, otherPath).isEquals(); }
/** * {@inheritDoc} */ @Override @JsxGetter public String getCookie() { final HtmlPage page = getPage(); final URL url = page.getUrl(); final StringBuilder builder = new StringBuilder(); final Set<Cookie> cookies = page.getWebClient().getCookies(url); for (final Cookie cookie : cookies) { if (cookie.isHttpOnly()) { continue; } if (builder.length() != 0) { builder.append("; "); } if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) { builder.append(cookie.getName()); builder.append('='); } builder.append(cookie.getValue()); } return builder.toString(); }
private void storeCookie(String hostName, String rawCookie) { Cookie cookie = cookieParser.parseCookie(hostName, rawCookie); Date now = new Date(); if (cookie.getExpires() != null && now.after(cookie.getExpires())) { removeCookie(this.cookieManager, cookie.getName()); } else { this.cookieManager.addCookie(cookie); } }
/** * Returns the currently configured cookie with the specified name, or {@code null} if one does not exist. * If disabled, this returns null. * @param name the name of the cookie to return * @return the currently configured cookie with the specified name, or {@code null} if one does not exist */ public synchronized Cookie getCookie(final String name) { if (!isCookiesEnabled()) { return null; } for (Cookie cookie : cookies_) { if (StringUtils.equals(cookie.getName(), name)) { return cookie; } } return null; }
assertNotNull(cid); assertFalse(cid.isEmpty()); String jsessionid = client.getCookieManager().getCookie(JSESSIONID).getValue(); assertNotNull(jsessionid); assertFalse(jsessionid.isEmpty());