public Cookie getCookieWithName(String name) { List<Cookie> cookies = getCookies(); // skip through cookies and return cookie you want for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { return cookie; } } return null; }
public int compare(final Cookie c1, final Cookie c2) { int res = c1.getName().compareTo(c2.getName()); if (res == 0) { // do not differentiate empty and null domains String d1 = c1.getDomain(); if (d1 == null) { d1 = ""; } String d2 = c2.getDomain(); if (d2 == null) { d2 = ""; } res = d1.compareToIgnoreCase(d2); } return res; }
while (iteratorCookie.hasNext()) { Cookie cookie = iteratorCookie.next(); cookieStr.append(cookie.getName() + "=" + cookie.getValue()); if (iteratorCookie.hasNext()) { cookieStr.append(";");
public static String getCookie(String name) { List<Cookie> cookies = cookieStore.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(name)) { return cookie.getValue(); } } return null; }
/** * Returns a string that uniquely identifies the cookie, The format The * format of the key is {@code "normalizedDomain;name;path"}. Adapted from * {@link CookieIdentityComparator#compare(Cookie, Cookie)}. */ protected String sortableKey(Cookie cookie) { String normalizedDomain = normalizeHost(cookie.getDomain()); // use ";" as delimiter since it is the delimiter in the cookie header, // so presumably can't appear in any of these values StringBuilder buf = new StringBuilder(normalizedDomain); buf.append(";").append(cookie.getName()); buf.append(";").append(cookie.getPath() != null ? cookie.getPath() : "/"); return buf.toString(); }
@Override public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } String name = cookie.getName(); if (name.indexOf(' ') != -1) { throw new MalformedCookieException("Cookie name may not contain blanks"); } if (name.startsWith("$")) { throw new MalformedCookieException("Cookie name may not start with $"); } super.validate(cookie, origin); }
/** * The function iterates through the list of cookies in the cookiestore and tries to * match them with the cookieName. If there is a match, the cookieStore already * has a valid cookie and the client need not send Credentials for validation purpose. * @param cookieStore The cookie Store * @param cookieName Name of the cookie which needs to be validated * @param isSSL Whether this is a http/https connection * @return true or false based on whether the client needs to send the credentials or * not to the server. */ static boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) { if (cookieName == null || cookieStore == null) { return true; } List<Cookie> cookies = cookieStore.getCookies(); for (Cookie c : cookies) { // If this is a secured cookie and the current connection is non-secured, // then, skip this cookie. We need to skip this cookie because, the cookie // replay will not be transmitted to the server. if (c.isSecure() && !isSSL) { continue; } if (c.getName().equals(cookieName)) { return false; } } return true; }
/** * Don't log the cookie's value; that's potentially sensitive information. */ private String cookieToString(Cookie cookie) { return cookie.getClass().getSimpleName() + "[version=" + cookie.getVersion() + ",name=" + cookie.getName() + ",domain=" + cookie.getDomain() + ",path=" + cookie.getPath() + ",expiry=" + cookie.getExpiryDate() + "]"; } // END android-added
public void addCookie(Cookie cookie) { if (isCookieCountMaxedForDomain(cookie.getDomain())) { logger.log( Level.FINEST, "Maximum number of cookies reached for domain " + cookie.getDomain() + ". Will not add new cookie " + cookie.getName() + " with value " + cookie.getValue()); return; } addCookieImpl(cookie); }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); buffer.append("="); String s = cookie.getValue(); if (s != null) { buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) { final CookieOrigin cookieOrigin = cookieOriginFromUri(requestSpec.getURI()); for (Cookie cookie : cookieStore.getCookies()) { if (cookieSpec.match(cookie, cookieOrigin) && !requestSpec.getCookies().hasCookieWithName(cookie.getName())) { requestSpec.cookie(cookie.getName(), cookie.getValue()); } } final Response response = ctx.next(requestSpec, responseSpec); List<Cookie> responseCookies = extractResponseCookies(response, cookieOrigin); cookieStore.addCookies(responseCookies.toArray(new Cookie[0])); return response; }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
public static HttpHeaders getHeaders(CookieStore cookies) { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML, MediaType.ALL)); for (org.apache.http.cookie.Cookie cookie : cookies.getCookies()) { headers.add("Cookie", cookie.getName() + "=" + cookie.getValue()); } return headers; }
line.append(cookie.getExpiryDate() != null ? cookie.getExpiryDate().getTime() / 1000 : -1); line.append(tab); line.append(cookie.getName()); line.append(tab); line.append(cookie.getValue() != null ? cookie.getValue() : "");
/** * Return a string suitable for sending in a <tt>"Cookie"</tt> header * as defined in RFC 2109 for backward compatibility with cookie version 0 * @param buffer The char array buffer to use for output * @param cookie The {@link Cookie} to be formatted as string * @param version The version to use. */ protected void formatCookieAsVer(final CharArrayBuffer buffer, final Cookie cookie, int version) { formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version); if (cookie.getPath() != null) { if (cookie instanceof ClientCookie && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) { buffer.append("; "); formatParamAsVer(buffer, "$Path", cookie.getPath(), version); } } if (cookie.getDomain() != null) { if (cookie instanceof ClientCookie && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) { buffer.append("; "); formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version); } } }
public TimelyEndpointConfig(Cookie sessionCookie) { if (null != sessionCookie) { this.sessionCookie = String.format(FORMAT, sessionCookie.getName(), sessionCookie.getValue()); } }
public static String getSessionIdValueFromState(CookieStore cookieStore) { String sessionID = null; for (Cookie cookie : cookieStore.getCookies()) { if ("JSESSIONID".equalsIgnoreCase(cookie.getName())) { sessionID = cookie.getValue(); break; } } return sessionID; }
private void printCookie(Cookie cookie) { htmlDocument.add("Name: " + cookie.getName() + "<br/>"); htmlDocument.add("Path: " + cookie.getPath() + "<br/>"); htmlDocument.add("Domain: " + cookie.getDomain() + "<br/>"); htmlDocument.add("Value: " + cookie.getValue() + "<br/>"); }
private static void setCookie(HttpRequest request, Cookie[] cookies) { if (cookies != null && cookies.length > 0) { List<String> cookieStr = new LinkedList<>(); for (Cookie cookie : cookies) { cookieStr.add(cookie.getName() + "=" + cookie.getValue()); } request.setHeader("Cookie", StringUtils.join(cookieStr, "; ")); } }
/** Get login token cookie or null if not found */ private Cookie getLoginCookie(HttpContext context, String loginTokenName) { for (Cookie cookie : HttpClientContext.adapt(context).getCookieStore().getCookies()) { if (cookie.getName().equalsIgnoreCase(loginTokenName) && !cookie.getValue().isEmpty()) { return cookie; } } return null; } }