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 cookies(WebTestClient.RequestBodySpec request, WebRequest webRequest) { String cookieHeaderValue = header("Cookie", webRequest); 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(); request.cookie(cookieName, cookieValue); } } Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(webRequest.getUrl()); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) { request.cookie(cookie.getName(), cookie.getValue()); } }
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; }
/** * 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; }
/** * 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; }
/** * {@inheritDoc} */ @Override public int hashCode() { final String path = getPath() == null ? "/" : getPath(); return new HashCodeBuilder().append(getName()).append(getDomain()).append(path).toHashCode(); }
private void addCookiesToRequest(InProcRequest httpTester) { Set<Cookie> cookies = cookieManager.getCookies(); if (!cookies.isEmpty()) { List<String> cookieStrings = new ArrayList<>(); for (Cookie cookie : cookies) { cookieStrings.add(cookie.getName() + "=" + cookie.getValue()); } String cookieHeaderValue = StringUtils.join(cookieStrings, "; "); httpTester.addHeader("Cookie", cookieHeaderValue); } }
/** * {@inheritDoc} */ @Override public int hashCode() { final String path = getPath() == null ? "/" : getPath(); return new HashCodeBuilder().append(getName()).append(getDomain()).append(path).toHashCode(); }
private void addCookiesToRequest(InProcRequest httpTester) { Set<Cookie> cookies = cookieManager.getCookies(); if (!cookies.isEmpty()) { List<String> cookieStrings = new ArrayList<String>(); for (Cookie cookie : cookies) { cookieStrings.add(cookie.getName() + "=" + cookie.getValue()); } String cookieHeaderValue = StringUtils.join(cookieStrings, "; "); httpTester.addHeader("Cookie", cookieHeaderValue); } }
/** * {@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 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 public List<HttpCookie> get(final URI uri) { final List<HttpCookie> cookies = new ArrayList<>(); try { final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://"); final java.net.URL url = new java.net.URL(urlString); for (final Cookie cookie : webClient_.getCookies(url)) { final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue()); httpCookie.setVersion(0); cookies.add(httpCookie); } } catch (final Exception e) { throw new RuntimeException(e); } return cookies; }
/** * {@inheritDoc} */ @Override public List<HttpCookie> get(final URI uri) { final List<HttpCookie> cookies = new ArrayList<>(); try { final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://"); final java.net.URL url = new java.net.URL(urlString); for (final Cookie cookie : webClient_.getCookies(url)) { final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue()); httpCookie.setVersion(0); cookies.add(httpCookie); } } catch (final Exception e) { throw new RuntimeException(e); } return cookies; }
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); } }
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); } }
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()); } /*
@Override public void deleteCookieNamed(String name) { CookieManager cookieManager = getWebClient().getCookieManager(); URL url = getRawUrl(); Set<com.gargoylesoftware.htmlunit.util.Cookie> rawCookies = getWebClient().getCookies(url); for (com.gargoylesoftware.htmlunit.util.Cookie cookie : rawCookies) { if (name.equals(cookie.getName())) { cookieManager.removeCookie(cookie); } } }
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" : ""); }
/** * {@inheritDoc} */ @Override public String toString() { return getName() + "=" + getValue() + (getDomain() != null ? ";domain=" + getDomain() : "") + (getPath() != null ? ";path=" + getPath() : "") + (getExpires() != null ? ";expires=" + getExpires() : "") + (isSecure() ? ";secure" : "") + (isHttpOnly() ? ";httpOnly" : ""); }