org.rx.net.http.cookie.PersistentCookieStorage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
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();
}
}