All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.http.cookie.PersistentCookieStorage Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.http.cookie;

import okhttp3.Cookie;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PersistentCookieStorage {
    static String createCookieKey(Cookie cookie) {
        return (cookie.secure() ? "https" : "http") + "://" + cookie.domain() + cookie.path() + "|" + cookie.name();
    }

    final Map store = new ConcurrentHashMap<>();

    public List loadAll() {
        return new ArrayList<>(store.values());
    }

    /**
     * Persist all cookies, existing cookies will be overwritten.
     *
     * @param cookies cookies persist
     */
    public void saveAll(Collection cookies) {
        for (Cookie cookie : cookies) {
            store.put(createCookieKey(cookie), cookie);
        }
    }

    /**
     * Removes indicated cookies from persistence.
     *
     * @param cookies cookies to remove from persistence
     */
    public void removeAll(Collection cookies) {
        for (Cookie cookie : cookies) {
            store.remove(createCookieKey(cookie));
        }
    }

    public void clear() {
        store.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy