Search in sources :

Example 56 with HttpCookie

use of java.net.HttpCookie in project xUtils3 by wyouflf.

the class CookieEntity method toHttpCookie.

public HttpCookie toHttpCookie() {
    HttpCookie cookie = new HttpCookie(name, value);
    cookie.setComment(comment);
    cookie.setCommentURL(commentURL);
    cookie.setDiscard(discard);
    cookie.setDomain(domain);
    if (expiry == -1L) {
        cookie.setMaxAge(-1L);
    } else {
        cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
    }
    cookie.setPath(path);
    cookie.setPortlist(portList);
    cookie.setSecure(secure);
    cookie.setVersion(version);
    return cookie;
}
Also used : HttpCookie(java.net.HttpCookie)

Example 57 with HttpCookie

use of java.net.HttpCookie in project zaproxy by zaproxy.

the class HttpRequestHeader method setCookies.

/**
     * Construct new "Cookie:" line in request header based on HttpCookies.
     *
     * @param cookies the new cookies
     */
public void setCookies(List<HttpCookie> cookies) {
    if (cookies.isEmpty()) {
        setHeader(HttpHeader.COOKIE, null);
    }
    StringBuilder sbData = new StringBuilder();
    for (HttpCookie c : cookies) {
        sbData.append(c.getName());
        sbData.append('=');
        sbData.append(c.getValue());
        sbData.append("; ");
    }
    if (sbData.length() <= 3) {
        setHeader(HttpHeader.COOKIE, null);
        return;
    }
    final String data = sbData.substring(0, sbData.length() - 2);
    setHeader(HttpHeader.COOKIE, data);
}
Also used : HttpCookie(java.net.HttpCookie)

Example 58 with HttpCookie

use of java.net.HttpCookie in project jdk8u_jdk by JetBrains.

the class NullUriCookieTest method checkCookieNullUri.

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

Example 59 with HttpCookie

use of java.net.HttpCookie in project jdk8u_jdk by JetBrains.

the class TestHttpCookie method misc.

static void misc() {
    header("Test equals()");
    // test equals()
    HttpCookie c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
    c1.setDomain(".coyote.org");
    c1.setPath("/acme");
    HttpCookie c2 = (HttpCookie) c1.clone();
    eq(c1, c2, true);
    // test equals() when domain and path are null
    c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
    c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
    eq(c1, c2, true);
    // path is case-sensitive
    c1 = new HttpCookie("Customer", "WILE_E_COYOTE");
    c2 = new HttpCookie("CUSTOMER", "WILE_E_COYOTE");
    c1.setPath("/acme");
    c2.setPath("/ACME");
    eq(c1, c2, false);
    header("Test domainMatches()");
    dm(".foo.com", "y.x.foo.com", false);
    dm(".foo.com", "x.foo.com", true);
    dm(".com", "whatever.com", false);
    dm(".com.", "whatever.com", false);
    dm(".ajax.com", "ajax.com", true);
    dm(".local", "example.local", true);
    dm("example.local", "example", true);
    // bug 6277808
    testCount++;
    try {
        c1 = new HttpCookie("", "whatever");
    } catch (IllegalArgumentException ignored) {
    // expected exception; no-op
    }
    // CR 6692802: HttpOnly flag
    test("set-cookie: CUSTOMER=WILE_E_COYOTE;HttpOnly").httpOnly(true);
    test("set-cookie: CUSTOMER=WILE_E_COYOTE").httpOnly(false);
    // space disallowed in name (both Netscape and RFC2965)
    test("set-cookie: CUST OMER=WILE_E_COYOTE").nil();
}
Also used : HttpCookie(java.net.HttpCookie)

Example 60 with HttpCookie

use of java.net.HttpCookie in project 2017-01-HUDI-MAC-CHAR by NHNNEXT.

the class PersistentCookieStore method loadAllFromPersistence.

private void loadAllFromPersistence() {
    allCookies = new HashMap<URI, Set<HttpCookie>>();
    Map<String, ?> allPairs = sharedPreferences.getAll();
    for (Map.Entry<String, ?> entry : allPairs.entrySet()) {
        String[] uriAndName = entry.getKey().split(SP_KEY_DELIMITER_REGEX, 2);
        try {
            URI uri = new URI(uriAndName[0]);
            String encodedCookie = (String) entry.getValue();
            HttpCookie cookie = new SerializableHttpCookie().decode(encodedCookie);
            Set<HttpCookie> targetCookies = allCookies.get(uri);
            if (targetCookies == null) {
                targetCookies = new HashSet<HttpCookie>();
                allCookies.put(uri, targetCookies);
            }
            // Repeated cookies cannot exist in persistence
            // targetCookies.remove(cookie)
            targetCookies.add(cookie);
        } catch (URISyntaxException e) {
            Log.w(TAG, e);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) HttpCookie(java.net.HttpCookie)

Aggregations

HttpCookie (java.net.HttpCookie)148 CookieManager (java.net.CookieManager)49 CookieStore (java.net.CookieStore)33 URI (java.net.URI)31 Test (org.junit.Test)31 IOException (java.io.IOException)14 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 URL (java.net.URL)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