cn.hutool.http.cookie.ThreadLocalCookieStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.http.cookie;
import java.net.CookieManager;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
/**
* 线程隔离的Cookie存储。多线程环境下Cookie隔离使用,防止Cookie覆盖
*
* 见:https://stackoverflow.com/questions/16305486/cookiemanager-for-multiple-threads
*
* @author looly
* @since 4.1.18
*/
public class ThreadLocalCookieStore implements CookieStore {
private final static ThreadLocal STORES = new ThreadLocal() {
@Override
protected synchronized CookieStore initialValue() {
/* InMemoryCookieStore */
return (new CookieManager()).getCookieStore();
}
};
/**
* 获取本线程下的CookieStore
*
* @return CookieStore
*/
public CookieStore getCookieStore() {
return STORES.get();
}
/**
* 移除当前线程的Cookie
*
* @return this
*/
public ThreadLocalCookieStore removeCurrent() {
STORES.remove();
return this;
}
@Override
public void add(URI uri, HttpCookie cookie) {
getCookieStore().add(uri, cookie);
}
@Override
public List get(URI uri) {
return getCookieStore().get(uri);
}
@Override
public List getCookies() {
return getCookieStore().getCookies();
}
@Override
public List getURIs() {
return getCookieStore().getURIs();
}
@Override
public boolean remove(URI uri, HttpCookie cookie) {
return getCookieStore().remove(uri, cookie);
}
@Override
public boolean removeAll() {
return getCookieStore().removeAll();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy