Search in sources :

Example 46 with CookieManager

use of java.net.CookieManager in project jabref by JabRef.

the class URLDownload method getCookieFromUrl.

public List<HttpCookie> getCookieFromUrl() throws IOException {
    CookieManager cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager);
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    URLConnection con = this.openConnection();
    // must be read to store the cookie
    con.getHeaderFields();
    try {
        return cookieManager.getCookieStore().get(this.source.toURI());
    } catch (URISyntaxException e) {
        LOGGER.error("Unable to convert download URL to URI", e);
        return Collections.emptyList();
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) CookieManager(java.net.CookieManager) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 47 with CookieManager

use of java.net.CookieManager in project camel by apache.

the class ExchangeCookieHandler method getCookieManager.

@Override
protected CookieManager getCookieManager(Exchange exchange) {
    Object handlerObj = exchange.getProperty(Exchange.COOKIE_HANDLER);
    if (handlerObj instanceof java.net.CookieManager) {
        return (CookieManager) handlerObj;
    } else {
        CookieManager handler = new CookieManager();
        exchange.setProperty(Exchange.COOKIE_HANDLER, handler);
        return handler;
    }
}
Also used : CookieManager(java.net.CookieManager)

Example 48 with CookieManager

use of java.net.CookieManager in project android-oss by kickstarter.

the class RefTagUtilsTest method testFindRefTagCookieForProject_WhenCookieExists.

@Test
public void testFindRefTagCookieForProject_WhenCookieExists() {
    final CookieManager cookieManager = new CookieManager();
    final CookieStore cookieStore = cookieManager.getCookieStore();
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.recommended();
    // set and retrieve the cookie
    cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
    final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);
    assertNotNull(cookie);
    assertEquals(RefTagUtils.cookieNameForProject(project), cookie.getName());
    assertEquals(RefTagUtils.cookieValueForRefTag(refTag), cookie.getValue());
}
Also used : CookieStore(java.net.CookieStore) Project(com.kickstarter.models.Project) RefTag(com.kickstarter.libs.RefTag) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 49 with CookieManager

use of java.net.CookieManager in project ion by koush.

the class CookieMiddleware method reinit.

public void reinit() {
    manager = new CookieManager(null, null);
    preferences = ion.getContext().getSharedPreferences(ion.getName() + "-cookies", Context.MODE_PRIVATE);
    Map<String, ?> allPrefs = preferences.getAll();
    for (String key : allPrefs.keySet()) {
        try {
            String value = preferences.getString(key, null);
            Headers headers = new Headers();
            String[] lines = value.split("\n");
            boolean first = true;
            for (String line : lines) {
                if (first) {
                    first = false;
                } else if (!TextUtils.isEmpty(line)) {
                    headers.addLine(line);
                }
            }
            manager.put(URI.create(key), headers.getMultiMap());
        } catch (Exception e) {
            Log.e("Ion", "unable to load cookies", e);
        }
    }
}
Also used : Headers(com.koushikdutta.async.http.Headers) CookieManager(java.net.CookieManager)

Example 50 with CookieManager

use of java.net.CookieManager in project ion by koush.

the class CookieTests method testReinit.

public void testReinit() throws Exception {
    CookieMiddleware middleware = Ion.getDefault(getContext()).getCookieMiddleware();
    Ion ion = Ion.getDefault(getContext());
    ion.getCookieMiddleware().clear();
    Headers headers = new Headers();
    headers.set("Set-Cookie", "foo=bar");
    URI uri = URI.create("http://example.com");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "poop=scoop");
    middleware.put(uri, headers);
    headers.set("Set-Cookie", "foo=goop");
    middleware.put(uri, headers);
    middleware.reinit();
    CookieManager manager = middleware.getCookieManager();
    Headers newHeaders = new Headers();
    Map<String, List<String>> cookies = manager.get(uri, newHeaders.getMultiMap());
    manager.get(uri, cookies);
    CookieMiddleware.addCookies(cookies, newHeaders);
    assertTrue(newHeaders.get("Cookie").contains("foo=goop"));
    assertTrue(newHeaders.get("Cookie").contains("poop=scoop"));
    assertFalse(newHeaders.get("Cookie").contains("bar"));
}
Also used : CookieMiddleware(com.koushikdutta.ion.cookie.CookieMiddleware) Headers(com.koushikdutta.async.http.Headers) Ion(com.koushikdutta.ion.Ion) List(java.util.List) URI(java.net.URI) CookieManager(java.net.CookieManager)

Aggregations

CookieManager (java.net.CookieManager)94 HttpCookie (java.net.HttpCookie)53 URI (java.net.URI)50 CookieStore (java.net.CookieStore)28 Test (org.junit.Test)16 MockResponse (com.google.mockwebserver.MockResponse)14 MockWebServer (com.google.mockwebserver.MockWebServer)14 List (java.util.List)9 MockResponse (okhttp3.mockwebserver.MockResponse)9 ArrayList (java.util.ArrayList)7 MockWebServer (okhttp3.mockwebserver.MockWebServer)7 RecordedRequest (com.google.mockwebserver.RecordedRequest)6 IOException (java.io.IOException)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Project (com.kickstarter.models.Project)3 Headers (com.koushikdutta.async.http.Headers)3 OkHttpClient (okhttp3.OkHttpClient)3 RefTag (com.kickstarter.libs.RefTag)2