Java Code Examples for java.net.CookieManager#put()

The following examples show how to use java.net.CookieManager#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testClearingWithMaxAge0() throws Exception {
    CookieManager cm = new CookieManager(createCookieStore(), null);

    URI uri = URI.create("https://test.com");
    Map<String, List<String>> header = new HashMap<>();
    List<String> value = new ArrayList<>();

    value.add("cookie=1234567890test; domain=.test.com; path=/; " +
              "expires=Fri, 31 Dec 9999 04:01:25 GMT-0000");
    header.put("Set-Cookie", value);
    cm.put(uri, header);

    List<HttpCookie> cookies = cm.getCookieStore().getCookies();
    assertEquals(1, cookies.size());

    value.clear();
    header.clear();
    value.add("cookie=1234567890test; domain=.test.com; path=/; " +
              "max-age=0");
    header.put("Set-Cookie", value);
    cm.put(uri, header);

    cookies = cm.getCookieStore().getCookies();
    assertEquals(0, cookies.size());
}
 
Example 2
Source File: CookieFilter.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public HttpRequestImpl response(Response r) throws IOException {
    HttpHeaders hdrs = r.headers();
    HttpRequestImpl request = r.request();
    Exchange<?> e = r.exchange;
    Log.logTrace("Response: processing cookies for {0}", request.uri());
    Optional<CookieManager> cookieManOpt = e.client().cookieManager();
    if (cookieManOpt.isPresent()) {
        CookieManager cookieMan = cookieManOpt.get();
        Log.logTrace("Response: parsing cookies from {0}", hdrs.map());
        cookieMan.put(request.uri(), hdrs.map());
    } else {
        Log.logTrace("Response: No cookie manager found for {0}",
                     request.uri());
    }
    return null;
}
 
Example 3
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieManagerGet_pathChecks() throws Exception {
    CookieManager cookieManager = new CookieManager(createCookieStore(), null);

    cookieManager.put(new URI("http://a.com/"), cookieHeaders("a1=android"));
    cookieManager.put(new URI("http://a.com/path1"),
            cookieHeaders("a2=android; Path=\"/path1\""));
    cookieManager.put(new URI("http://a.com/path2"),
            cookieHeaders("a3=android; Path=\"/path2\""));

    assertManagerCookiesMatch(cookieManager, "http://a.com/notpath", "a1=android");
    assertManagerCookiesMatch(cookieManager, "http://a.com/path1", "a1=android; a2=android");
}
 
Example 4
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMatchingDomainsAccepted() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://www.android.com/"),
            cookieHeaders("a=android;domain=.android.com"));
    assertEquals(".android.com", cookieStore.getCookie("a").getDomain());
}
 
Example 5
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNonMatchingDomainsRejected() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com/"),
            cookieHeaders("a=android;domain=google.com"));
    assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.cookies);
}
 
Example 6
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private CookieManager store(String[][] cookies,
                                   Map<String, List<String>> responseHeaders, CookiePolicy policy)
        throws IOException, URISyntaxException {
    CookieManager manager = new CookieManager(createCookieStore(), policy);
    // Put all cookies into manager
    for (int i = 0; i < cookies.length; i++) {
        for (int j = 2; j < cookies[i].length; j += 2) {
            URI uri = new URI(cookies[i][j]);
            manager.put(uri, responseHeaders);
        }
    }
    return manager;
}
 
Example 7
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testResponseWithMultipleCookieHeaderLines() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com"), cookieHeaders("a=android", "b=banana"));
    List<HttpCookie> cookies = sortedCopy(cookieStore.cookies);
    assertEquals(2, cookies.size());
    HttpCookie cookieA = cookies.get(0);
    assertEquals("a", cookieA.getName());
    assertEquals("android", cookieA.getValue());
    HttpCookie cookieB = cookies.get(1);
    assertEquals("b", cookieB.getName());
    assertEquals("banana", cookieB.getValue());
}
 
Example 8
Source File: CookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookiesWithLeadingPeriod() throws Exception {
    CookieManager cm = new CookieManager(createCookieStore(), null);
    Map<String, List<String>> responseHeaders = Collections.singletonMap("Set-Cookie",
            Collections.singletonList("foo=bar"));

    URI uri = new URI("http://chargepoint.com");
    cm.put(uri, responseHeaders);

    Map<String, List<String>> cookies = cm.get(
            new URI("https://webservices.chargepoint.com/backend.php/mobileapi/"),
            responseHeaders);

    List<String> cookieList = cookies.values().iterator().next();
    assertEquals(Collections.singletonList("foo=bar"), cookieList);
}
 
Example 9
Source File: HttpOnly.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 10
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testNonMatchingPathsRejected() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com/foo/bar"),
            cookieHeaders("a=android;path=/baz/bar"));
    assertEquals("Expected to reject cookies whose path is not a prefix of the request path",
            Collections.<HttpCookie>emptyList(), cookieStore.cookies); // RI6 fails this
}
 
Example 11
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieManagerGet_hostChecks() throws Exception {
    CookieManager cookieManager = new CookieManager(createCookieStore(), null);

    cookieManager.put(new URI("http://a.com/"), cookieHeaders("a1=android"));
    cookieManager.put(new URI("http://b.com/"), cookieHeaders("b1=android"));

    assertManagerCookiesMatch(cookieManager, "http://a.com/", "a1=android");
    assertManagerCookiesMatch(cookieManager, "http://b.com/", "b1=android");
}
 
Example 12
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testPathDefaulting() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com/foo/bar"), cookieHeaders("a=android"));
    assertEquals("/foo/", cookieStore.getCookie("a").getPath());
    cookieManager.put(new URI("http://android.com/"), cookieHeaders("b=banana"));
    assertEquals("/", cookieStore.getCookie("b").getPath());
    cookieManager.put(new URI("http://android.com/foo/"), cookieHeaders("c=carrot"));
    assertEquals("/foo/", cookieStore.getCookie("c").getPath());
}
 
Example 13
Source File: HttpOnly.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 14
Source File: HttpOnly.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 15
Source File: HttpOnly.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 16
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieManagerGet_schemeChecks() throws Exception {
    CookieManager cookieManager = new CookieManager(createCookieStore(), null);

    cookieManager.put(new URI("http://a.com/"), cookieHeaders("a1=android"));
    cookieManager.put(new URI("https://a.com/"), cookieHeaders("a2=android"));
    cookieManager.put(new URI("https://a.com/"), cookieHeaders("a3=android; Secure"));

    assertManagerCookiesMatch(cookieManager, "http://a.com/", "a1=android; a2=android");
    assertManagerCookiesMatch(cookieManager, "https://a.com/",
            "a1=android; a2=android; a3=android");
}
 
Example 17
Source File: HttpOnly.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 18
Source File: HttpUtils.java    From commerce-gradle-plugin with Apache License 2.0 5 votes vote down vote up
public static HttpURLConnection connectAndUpdateCookies(HttpURLConnection connection, CookieManager cookies, Map<String, List<String>> headers) throws Exception {
    connection.connect();
    URI uri = connection.getURL().toURI();
    cookies.put(uri, connection.getHeaderFields());
    while (isRedirect(connection)) {
        connection = followRedirect(connection, cookies, headers);
        cookies.put(connection.getURL().toURI(), connection.getHeaderFields());
    }
    if (connection.getResponseCode() > 400) {
        throw new IllegalStateException("error connecting to " + connection.getURL() + "HTTP Status: " + connection.getResponseCode());
    }
    return connection;
}
 
Example 19
Source File: HttpOnly.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void populateCookieStore(URI uri)
        throws IOException {

    CookieManager cm = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    Map<String,List<String>> header = new HashMap<>();
    List<String> values = new ArrayList<>();
    values.add("JSESSIONID=" + SESSION_ID + "; version=1; Path="
               + URI_PATH +"; HttpOnly");
    values.add("CUSTOMER=WILE_E_COYOTE; version=1; Path=" + URI_PATH);
    header.put("Set-Cookie", values);
    cm.put(uri, header);
}
 
Example 20
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testMatchingPathsAccepted() throws Exception {
    TestCookieStore cookieStore = new TestCookieStore();
    CookieManager cookieManager = new CookieManager(cookieStore, ACCEPT_ORIGINAL_SERVER);
    cookieManager.put(new URI("http://android.com/foo/bar/"),
            cookieHeaders("a=android;path=/foo"));
    assertEquals("/foo", cookieStore.getCookie("a").getPath());
}