use of java.net.HttpCookie in project robovm by robovm.
the class CookiesTest method testQuotedAttributeValues.
public void testQuotedAttributeValues() throws Exception {
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(cookieManager);
MockWebServer server = new MockWebServer();
try {
// RoboVM note: Modified to call server.shutdown() after test finishes.
server.play();
server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=\"android\"; " + "Comment=\"this cookie is delicious\"; " + "CommentURL=\"http://google.com/\"; " + "Discard; " + "Domain=\"" + server.getCookieDomain() + "\"; " + "Max-Age=\"60\"; " + "Path=\"/path\"; " + "Port=\"80,443," + server.getPort() + "\"; " + "Secure; " + "Version=\"1\""));
get(server, "/path/foo");
List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
assertEquals("a", cookie.getName());
assertEquals("android", cookie.getValue());
assertEquals("this cookie is delicious", cookie.getComment());
assertEquals("http://google.com/", cookie.getCommentURL());
assertEquals(true, cookie.getDiscard());
assertEquals(server.getCookieDomain(), cookie.getDomain());
assertEquals(60, cookie.getMaxAge());
assertEquals("/path", cookie.getPath());
assertEquals("80,443," + server.getPort(), cookie.getPortlist());
assertEquals(true, cookie.getSecure());
assertEquals(1, cookie.getVersion());
} finally {
server.shutdown();
}
}
use of java.net.HttpCookie in project jdk8u_jdk by JetBrains.
the class EmptyInputStream method filterHeaderField.
/**
* Returns a filtered version of the given headers value.
*
* Note: The implementation currently only filters out HttpOnly cookies
* from Set-Cookie and Set-Cookie2 headers.
*/
private String filterHeaderField(String name, String value) {
if (value == null)
return null;
if (SET_COOKIE.equalsIgnoreCase(name) || SET_COOKIE2.equalsIgnoreCase(name)) {
// cookie handler will store/retrieve the HttpOnly cookies]
if (cookieHandler == null || value.length() == 0)
return value;
sun.misc.JavaNetHttpCookieAccess access = sun.misc.SharedSecrets.getJavaNetHttpCookieAccess();
StringBuilder retValue = new StringBuilder();
List<HttpCookie> cookies = access.parse(value);
boolean multipleCookies = false;
for (HttpCookie cookie : cookies) {
// skip HttpOnly cookies
if (cookie.isHttpOnly())
continue;
if (multipleCookies)
// RFC 2965, comma separated
retValue.append(',');
retValue.append(access.header(cookie));
multipleCookies = true;
}
return retValue.length() == 0 ? "" : retValue.toString();
}
return value;
}
use of java.net.HttpCookie in project jdk8u_jdk by JetBrains.
the class InMemoryCookieStore method getInternal2.
// @param cookies [OUT] contains the found cookies
// @param cookieIndex the index
// @param comparator the prediction to decide whether or not
// a cookie in index should be returned
private <T> void getInternal2(List<HttpCookie> cookies, Map<T, List<HttpCookie>> cookieIndex, Comparable<T> comparator, boolean secureLink) {
for (T index : cookieIndex.keySet()) {
if (comparator.compareTo(index) == 0) {
List<HttpCookie> indexedCookies = cookieIndex.get(index);
// check the list of cookies associated with this domain
if (indexedCookies != null) {
Iterator<HttpCookie> it = indexedCookies.iterator();
while (it.hasNext()) {
HttpCookie ck = it.next();
if (cookieJar.indexOf(ck) != -1) {
// the cookie still in main cookie store
if (!ck.hasExpired()) {
// don't add twice
if ((secureLink || !ck.getSecure()) && !cookies.contains(ck))
cookies.add(ck);
} else {
it.remove();
cookieJar.remove(ck);
}
} else {
// the cookie has beed removed from main store,
// so also remove it from domain indexed store
it.remove();
}
}
}
// end of indexedCookies != null
}
// end of comparator.compareTo(index) == 0
}
// end of cookieIndex iteration
}
use of java.net.HttpCookie in project hive by apache.
the class DruidKerberosUtil method getAuthCookie.
public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
if (cookieStore == null) {
return null;
}
boolean isSSL = uri.getScheme().equals("https");
List<HttpCookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
// 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.
HttpCookie c = cookies.get(i);
if (c.getSecure() && !isSSL) {
continue;
}
if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
return c;
}
}
return null;
}
use of java.net.HttpCookie in project Payara by payara.
the class ClientCookieStore method store.
/**
* Store the cookies in the CookieStore to the provided location.
* This method will overwrite the contents of the target file.
*/
public void store() throws IOException {
PrintWriter out = null;
// Create the directory if it doesn't exist.
if (!cookieStoreFile.getParentFile().exists() && !cookieStoreFile.getParentFile().mkdirs()) {
throw new IOException("Unable to create directory: " + cookieStoreFile.toString());
}
out = new PrintWriter(new BufferedWriter(new FileWriter(cookieStoreFile)));
// Write comment at top of cache file.
out.println(CACHE_COMMENT);
for (URI uri : this.getURIs()) {
for (HttpCookie cookie : this.get(uri)) {
// Expire the cookie immediately if Discard is true
if (cookie.getMaxAge() < 1 || cookie.getDiscard())
continue;
StringBuilder sb = new StringBuilder();
sb.append(cookie.getName()).append("=").append(cookie.getValue());
if (cookie.getPath() != null) {
sb.append("; Path=").append(cookie.getPath());
}
if (cookie.getDomain() != null) {
sb.append("; Domain=").append(cookie.getDomain());
}
if (cookie.getComment() != null) {
sb.append("; Comment=").append(cookie.getComment());
}
if (cookie.getCommentURL() != null) {
sb.append("; CommentURL=\"").append(cookie.getCommentURL()).append("\"");
}
sb.append("; Max-Age=").append(cookie.getMaxAge());
if (cookie.getPortlist() != null) {
sb.append("; Port=\"").append(cookie.getPortlist()).append("\"");
}
// XXX: Can we safely ignore Secure attr?
sb.append("; Version=").append(cookie.getVersion());
out.println(sb);
}
}
out.close();
}
Aggregations