Search in sources :

Example 66 with HttpCookie

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();
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) MockWebServer(com.google.mockwebserver.MockWebServer) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager)

Example 67 with HttpCookie

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;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 68 with HttpCookie

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
}
Also used : HttpCookie(java.net.HttpCookie)

Example 69 with HttpCookie

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;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 70 with HttpCookie

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();
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) URI(java.net.URI) HttpCookie(java.net.HttpCookie) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

HttpCookie (java.net.HttpCookie)151 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)32 Test (org.junit.Test)31 IOException (java.io.IOException)16 Test (org.testng.annotations.Test)13 MockResponse (com.google.mockwebserver.MockResponse)11 MockWebServer (com.google.mockwebserver.MockWebServer)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Project (com.kickstarter.models.Project)5 RestResponse (com.linkedin.r2.message.rest.RestResponse)5 Cookie (javax.servlet.http.Cookie)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 MockResponse (okhttp3.mockwebserver.MockResponse)5 MockWebServer (okhttp3.mockwebserver.MockWebServer)5 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 ByteString (com.linkedin.data.ByteString)4