Skip to content

Commit dd45968

Browse files
committed
!69 feat: 后台清理过期 Cookie
* feat: 后台清理过期 Cookie * fix: Cookie 值 URLEncode 处理 * feat: Cookie 存储机制 * feat: Cookie 自动化存取 * feat: Cookie 自动化存取 * feat: Cookie 自动化存取 * feat: Cookie 自动化存取 * feat: Cookie 存储机制 * feat: 增强 Cookie 相关 API 接口 * refactor: parse ForestCookie * refactor: parse ForestCookie * refactor: ForestCookie 不在依赖 OkHttp * refactor: ForestCookie 不在依赖 OkHttp * refactor: parse ForestCookie
1 parent 324117e commit dd45968

File tree

17 files changed

+1292
-139
lines changed

17 files changed

+1292
-139
lines changed

forest-core/src/main/java/com/dtflys/forest/Forest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dtflys.forest;
22

33
import com.dtflys.forest.config.ForestConfiguration;
4+
import com.dtflys.forest.http.ForestCookie;
45
import com.dtflys.forest.http.ForestFuture;
56
import com.dtflys.forest.http.ForestRequest;
67
import com.dtflys.forest.http.ForestResponse;
@@ -320,4 +321,17 @@ public static void await(Collection<ForestFuture> futures, Consumer<ForestRespon
320321
}
321322
}
322323

324+
/**
325+
* 通过Cookie名和Cookie值创建一个Cookie
326+
*
327+
* @param name Cookie名
328+
* @param value Cookie值
329+
* @return Forest Cookie
330+
* @since 1.7.0
331+
*/
332+
public static ForestCookie cookie(String name, String value) {
333+
return ForestCookie.nameValue(name, value);
334+
}
335+
336+
323337
}

forest-core/src/main/java/com/dtflys/forest/backend/okhttp3/executor/OkHttp3Executor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ protected void prepareHeaders(Request.Builder builder) {
164164
} else if (ForestHeader.CONTENT_ENCODING.equalsIgnoreCase(name)) {
165165
contentEncodingHeaderName = name;
166166
} else {
167-
builder.addHeader(name, MappingTemplate.getParameterValue(jsonConverter, nameValue.getValue()));
167+
String headerValue = MappingTemplate.getParameterValue(jsonConverter, nameValue.getValue());
168+
if (headerValue != null) {
169+
builder.addHeader(name, headerValue);
170+
}
168171
}
169172
}
170173
}

forest-core/src/main/java/com/dtflys/forest/config/ForestConfiguration.java

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import com.dtflys.forest.http.ForestAsyncMode;
5454
import com.dtflys.forest.http.ForestRequest;
5555
import com.dtflys.forest.http.ForestRequestType;
56+
import com.dtflys.forest.http.cookie.ForestCookieStorage;
57+
import com.dtflys.forest.http.cookie.MemoryCookieStorage;
5658
import com.dtflys.forest.interceptor.DefaultInterceptorFactory;
5759
import com.dtflys.forest.interceptor.Interceptor;
5860
import com.dtflys.forest.interceptor.InterceptorFactory;
@@ -180,7 +182,6 @@ public class ForestConfiguration implements VariableScope, Serializable {
180182
*/
181183
private Integer readTimeout;
182184

183-
184185
/**
185186
* 全局的请求失败重试策略类
186187
*/
@@ -361,6 +362,21 @@ public class ForestConfiguration implements VariableScope, Serializable {
361362
*/
362363
private List<AfterExecuteResultTypeHandler<?>> afterExecuteResultTypeHandlers = new LinkedList<>();
363364

365+
/**
366+
* 是否允许 Cookie 自动存取
367+
*/
368+
private boolean autoCookieSaveAndLoadEnabled = false;
369+
370+
/**
371+
* Cookie 的最大自动存储大小
372+
*/
373+
private int cookiesStorageMaxSize = 2048;
374+
375+
/**
376+
* 全局 Cookie 存储器
377+
*/
378+
private volatile ForestCookieStorage cookieStorage;
379+
364380
/**
365381
* Forest请求池
366382
*/
@@ -492,7 +508,6 @@ public HttpBackendSelector getBackendSelector() {
492508
public ForestConfiguration setBackend(HttpBackend backend) {
493509
if (backend != null) {
494510
backend.init(this);
495-
log.info("[Forest] Http Backend: " + backend.getName());
496511
}
497512
this.backend = backend;
498513
return this;
@@ -1771,6 +1786,65 @@ public ForestConfiguration setSslKeyStores(Map<String, SSLKeyStore> sslKeyStores
17711786
return this;
17721787
}
17731788

1789+
/**
1790+
* 是否允许 Cookie 自动存取
1791+
*
1792+
* @return {@code true}: 允许自动存取, {@code false}: 不允许
1793+
* @since 1.7.0
1794+
*/
1795+
public boolean isAutoCookieSaveAndLoadEnabled() {
1796+
return autoCookieSaveAndLoadEnabled;
1797+
}
1798+
1799+
/**
1800+
* 设置是否允许 Cookie 自动存取
1801+
*
1802+
* @param autoCookieSaveAndLoadEnabled {@code true}: 允许自动存取, {@code false}: 不允许
1803+
* @return 当前ForestConfiguration实例
1804+
* @since 1.7.0
1805+
*/
1806+
public ForestConfiguration setAutoCookieSaveAndLoadEnabled(boolean autoCookieSaveAndLoadEnabled) {
1807+
this.autoCookieSaveAndLoadEnabled = autoCookieSaveAndLoadEnabled;
1808+
return this;
1809+
}
1810+
1811+
/**
1812+
* 获取 Cookie 的最大自动存储大小
1813+
*
1814+
* @return Cookie 的最大自动存储大小
1815+
* @since 1.7.0
1816+
*/
1817+
public int getCookiesStorageMaxSize() {
1818+
return cookiesStorageMaxSize;
1819+
}
1820+
1821+
/**
1822+
* 设置 Cookie 的最大自动存储大小
1823+
*
1824+
* @param cookiesStorageMaxSize Cookie 的最大自动存储大小
1825+
* @since 1.7.0
1826+
*/
1827+
public void setCookiesStorageMaxSize(int cookiesStorageMaxSize) {
1828+
this.cookiesStorageMaxSize = cookiesStorageMaxSize;
1829+
}
1830+
1831+
/**
1832+
* 获取全局 Cookie 存储器
1833+
*
1834+
* @return Cookie 存储器对象
1835+
* @since 1.7.0
1836+
*/
1837+
public ForestCookieStorage getCookieStorage() {
1838+
if (cookieStorage == null) {
1839+
synchronized (this) {
1840+
if (cookieStorage == null) {
1841+
cookieStorage = new MemoryCookieStorage(cookiesStorageMaxSize);
1842+
}
1843+
}
1844+
}
1845+
return cookieStorage;
1846+
}
1847+
17741848
/**
17751849
* 获取全局请求池
17761850
*

0 commit comments

Comments
 (0)