java.net.CookieStore Java Examples

The following examples show how to use java.net.CookieStore. 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: AuthCookie.java    From utexas-utilities with Apache License 2.0 6 votes vote down vote up
protected void setAuthCookieVal(String authCookie, String domain) {
    this.authCookie = authCookie;
    settings.edit().putString(prefKey, authCookie).apply();

    /*
    this is technically unnecessary if OkHttp handled the authentication, because it will
    have already set the cookies in the CookieHandler. It doesn't seem to cause any issues
    just to re-add the cookies, though
     */
    HttpCookie httpCookie = new HttpCookie(authCookieKey, authCookie);
    httpCookie.setDomain(domain);
    try {
        CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
        cookies.add(URI.create(domain), httpCookie);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    cookieHasBeenSet = true;
    android.webkit.CookieManager.getInstance().setCookie(domain, httpCookie.getName() + "=" + httpCookie.getValue());
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        android.webkit.CookieManager.getInstance().flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
}
 
Example #2
Source File: AuthCookie.java    From utexas-utilities with Apache License 2.0 6 votes vote down vote up
private void resetCookie() {
    settings.edit().remove(prefKey).apply();
    authCookie = "";
    try {
        /*
        This step is *required* for PNA, and nicety for other services. PNA won't let you
        log in if you're still holding on to a valid authcookie, so we clear them out.
         */
        URI loginURI = URI.create(url.getHost());
        CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
        for (HttpCookie cookie : cookies.get(loginURI)) {
            cookies.remove(loginURI, cookie);
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    cookieHasBeenSet = false;
}
 
Example #3
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testCookieStoreNullUris() {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookieA = createCookie("a", "android", ".android.com", "/source");
    HttpCookie cookieB = createCookie("b", "banana", "code.google.com", "/p/android");

    cookieStore.add(null, cookieA);
    assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
    cookieStore.add(null, cookieB);
    assertEquals(Arrays.asList(cookieA, cookieB), cookieStore.getCookies());

    try {
        cookieStore.get(null);
        fail();
    } catch (NullPointerException expected) {
    }

    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertTrue(cookieStore.remove(null, cookieA));
    assertEquals(Arrays.asList(cookieB), cookieStore.getCookies());

    assertTrue(cookieStore.removeAll());
    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
}
 
Example #4
Source File: NullUriCookieTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #5
Source File: UTilitiesApplication.java    From utexas-utilities with Apache License 2.0 5 votes vote down vote up
public void logoutAll() {
    for (AuthCookie authCookie : authCookies.values()) {
        authCookie.logout();
    }
    CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
    cookies.removeAll();
}
 
Example #6
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreEviction() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie themeCookie = createCookie("theme", "light", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), themeCookie);

    HttpCookie sidCookie = createCookie("sid", "mysid", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), sidCookie);

    HttpCookie replacementThemeCookie = createCookie("theme", "dark", "a.com", "/");
    cookieStore.add(new URI("http://a.com/"), replacementThemeCookie);

    // toString() is used below to avoid confusion with assertEquals():
    // HttpCookie.equals() is implemented so that it only checks name, path and domain
    // attributes but we also want to check the value.
    assertEquals(
            "[sid=\"mysid\";$Path=\"/\";$Domain=\"a.com\", "
                    + "theme=\"dark\";$Path=\"/\";$Domain=\"a.com\"]",
            cookieStore.get(new URI("http://a.com/")).toString());

    HttpCookie replacementSidCookie = createCookie("sid", "mynewsid", "A.cOm", "/");
    cookieStore.add(new URI("http://a.com/"), replacementSidCookie);

    assertEquals(
            "[theme=\"dark\";$Path=\"/\";$Domain=\"a.com\", "
                    + "sid=\"mynewsid\";$Path=\"/\";$Domain=\"a.com\"]",
            cookieStore.get(new URI("http://a.com/")).toString());
}
 
Example #7
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreGetWithSecure() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookie = createCookie("theme", "light", "a.com", "/path");
    cookie.setSecure(true);
    cookieStore.add(new URI("https://a.com/path"), cookie);

    // CookieStoreImpl on Android ignores the "Secure" attribute. The RI implements the secure
    // check in CookieStoreImpl. For safety / app compatibility, if this is changed Android
    // should probably implement it in both places.
    assertEquals(1, cookieStore.get(new URI("http://a.com/path")).size());
    assertEquals(1, cookieStore.get(new URI("https://a.com/path")).size());
}
 
Example #8
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Regression test for http://b/25682357 /
 * https://code.google.com/p/android/issues/detail?id=193475
 * CookieStoreImpl.get(URI) not handling ports properly in the absence of an explicit cookie
 * Domain.
 */
public void testCookieStoreGetWithPort() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookie = new HttpCookie("theme", "light");
    // Deliberately not setting the cookie domain or path.
    cookieStore.add(new URI("http://a.com:12345"), cookie);

    // CookieStoreImpl must ignore the port during retrieval when domain is not set.
    assertEquals(1, cookieStore.get(new URI("http://a.com:12345/path1")).size());
    assertEquals(1, cookieStore.get(new URI("http://a.com/path1")).size());
}
 
Example #9
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreGet() throws Exception {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookiePort1 = createCookie("a1", "android", "a.com", "/path1");
    HttpCookie cookiePort2 = createCookie("a2", "android", "a.com", "/path2");
    HttpCookie secureCookie = createCookie("a3", "android", "a.com", "/path3");
    secureCookie.setSecure(true);
    HttpCookie notSecureCookie = createCookie("a4", "android", "a.com", "/path4");

    HttpCookie bCookie = createCookie("b1", "android", "b.com", "/path5");

    cookieStore.add(new URI("http://a.com:443/path1"), cookiePort1);
    cookieStore.add(new URI("http://a.com:8080/path2"), cookiePort2);
    cookieStore.add(new URI("https://a.com:443/path3"), secureCookie);
    cookieStore.add(new URI("https://a.com:443/path4"), notSecureCookie);
    cookieStore.add(new URI("https://b.com:8080/path5"), bCookie);

    List<HttpCookie> expectedStoreCookies = new ArrayList<>();
    expectedStoreCookies.add(cookiePort1);
    expectedStoreCookies.add(cookiePort2);
    expectedStoreCookies.add(secureCookie);
    expectedStoreCookies.add(notSecureCookie);

    // The default CookieStore implementation on Android is currently responsible for matching
    // the host/domain but not handling other cookie rules: it ignores the scheme (e.g. "secure"
    // checks), port and path.
    // The tests below fail on the RI. It looks like in the RI it is CookieStoreImpl that is
    // enforcing "secure" checks.
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:443/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:8080/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("https://a.com/anypath")));
    assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com/anypath")));
}
 
Example #10
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreRemoveRequiresUri() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieStore.add(new URI("http://android.com/source/"), cookieA);
    assertFalse("Expected remove() to take the cookie URI into account.", // RI6 fails this
            cookieStore.remove(new URI("http://code.google.com/"), cookieA));
    assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
}
 
Example #11
Source File: AbstractCookiesTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCookieStoreRemoveAll() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
    cookieStore.add(new URI("http://code.google.com/"), new HttpCookie("a", "android"));
    assertTrue(cookieStore.removeAll());
    assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
    assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
    assertFalse("Expected removeAll() to return false when the call doesn't mutate the store",
            cookieStore.removeAll());  // RI6 fails this
}
 
Example #12
Source File: Wasp.java    From wasp with Apache License 2.0 5 votes vote down vote up
public Builder enableCookies(CookieStore cookieStore, CookiePolicy cookiePolicy) {
  if (cookiePolicy == null) {
    throw new NullPointerException("CookiePolicy may not be null");
  }
  this.cookieHandler = new CookieManager(cookieStore, cookiePolicy);
  return this;
}
 
Example #13
Source File: NullUriCookieTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #14
Source File: Main.java    From Java-Coding-Problems with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

        /*
        HttpClient client = HttpClient.newBuilder()
                .cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
                .build();

        System.out.println(client.cookieHandler().orElseThrow());
         */
        
        CookieManager cm = new CookieManager();
        cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        
        HttpClient client = HttpClient.newBuilder()
                .cookieHandler(cm)
                .build();

        HttpRequest request = HttpRequest.newBuilder()
                .header("Authorization", "Bearer Q3ku4mp_hCQGvAFeYKa0ktFCDKS3VpSA1nwf")                
                .uri(URI.create("https://gorest.co.in/public-api/users/1"))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println("Status code: " + response.statusCode());
        System.out.println("\n Body: " + response.body());       
        System.out.println("\nset-cookie header: " + response.headers().firstValue("set-cookie"));
        
        CookieStore cookieStore = cm.getCookieStore();
        
        System.out.println("\nCookies: " + cookieStore.getCookies());
    }
 
Example #15
Source File: NullUriCookieTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #16
Source File: NullUriCookieTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #17
Source File: NullUriCookieTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #18
Source File: NullUriCookieTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #19
Source File: NullUriCookieTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #20
Source File: NullUriCookieTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #21
Source File: NullUriCookieTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #22
Source File: NullUriCookieTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #23
Source File: NullUriCookieTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #24
Source File: NullUriCookieTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #25
Source File: RawHttpCliClient.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
private static CookieHandler cookieManagerFor(@Nullable File cookieJar) {
    CookieStore cookieStore;
    try {
        cookieStore = cookieJar == null ? null : new FileCookieJar(cookieJar);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
}
 
Example #26
Source File: OnWriteFlushStrategy.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
@Override
public void onUpdate(CookieStore cookieStore) {
    super.onUpdate(cookieStore);
    int count = totalWrites.incrementAndGet();
    if (count % writeCount == 0) {
        try {
            flush.call();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #27
Source File: PeriodicFlushStrategy.java    From rawhttp with Apache License 2.0 5 votes vote down vote up
@Override
public void onUpdate(CookieStore cookieStore) {
    super.onUpdate(cookieStore);
    executorService.submit(() -> {
        requiresFlush = true;
    });
}
 
Example #28
Source File: NullUriCookieTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static void checkCookieNullUri() throws Exception {
    //get a cookie store implementation and add a cookie to the store with null URI
    CookieStore cookieStore = (new CookieManager()).getCookieStore();
    //Check if removeAll() retrurns false on an empty CookieStore
    if (cookieStore.removeAll()) {
        fail = true;
    }
    checkFail("removeAll on empty store should return false");
    HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
    cookie.setDomain("foo.com");
    cookieStore.add(null, cookie);

    //Retrieve added cookie
    URI uri = new URI("http://foo.com");
    List<HttpCookie> addedCookieList = cookieStore.get(uri);

    //Verify CookieStore behaves well
    if (addedCookieList.size() != 1) {
       fail = true;
    }
    checkFail("Abnormal size of cookie jar");

    for (HttpCookie chip : addedCookieList) {
        if (!chip.equals(cookie)) {
             fail = true;
        }
    }
    checkFail("Cookie not retrieved from Cookie Jar");
    boolean ret = cookieStore.remove(null,cookie);
    if (!ret) {
        fail = true;
    }
    checkFail("Abnormal removal behaviour from Cookie Jar");
}
 
Example #29
Source File: GingerbreadUtil.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the cookies in the given cookie handler. Cookies can only be cleared if the
 * cookieHandler is a CookieManager with a non-null CookieStore.
 *
 * @param cookieHandler the cookie handler where cookies should be cleared
 * @return true if cookies were cleared; false otherwise
 */
public static boolean clearCookies(CookieHandler cookieHandler) {
  if (cookieHandler instanceof CookieManager) {
    CookieManager cookieManager = (CookieManager) cookieHandler;
    CookieStore cookieStore = cookieManager.getCookieStore();
    if (cookieStore != null) {
      cookieStore.removeAll();
      return true;
    }
  }
  return false;
}
 
Example #30
Source File: Ok2Lite.java    From httplite with Apache License 2.0 4 votes vote down vote up
public Builder setCookieStore(CookieStore cookieStore, CookiePolicy policy){
    cookieHandler = new CookieManager(cookieStore, policy);
    return this;
}