Skip to content

Commit 0131fb6

Browse files
committed
!70 feat: 深度变量引用
* feat: 深度变量引用 * refactor: 重构URL解析过程 * refactor: 重构变量作用域 * feat: 通过配置自定义异步线程池拒绝策略 * comment: ResponseResult 接口注释 * update: version 1.7.0 * fix: 去掉不需要 import 的 okhttp3 包 * fix: 修改接口中常量的命名,避免用户在其与只有大小写区别的方法之间产生混淆 * fix: 修改接口中常量的命名,避免用户在其与只有大小写区别的方法之间产生混淆
1 parent dd45968 commit 0131fb6

File tree

51 files changed

+833
-241
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+833
-241
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Forest有哪些特性?
8383
<dependency>
8484
<groupId>com.dtflys.forest</groupId>
8585
<artifactId>forest-spring-boot-starter</artifactId>
86-
<version>1.6.4</version>
86+
<version>1.7.0</version>
8787
</dependency>
8888
```
8989

forest-core/src/main/java/com/dtflys/forest/backend/httpclient/body/HttpclientBodyBuilder.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import com.dtflys.forest.http.body.ObjectRequestBody;
1010
import com.dtflys.forest.mapping.MappingTemplate;
1111
import com.dtflys.forest.multipart.ForestMultipart;
12-
import com.dtflys.forest.utils.RequestNameValue;
1312
import com.dtflys.forest.utils.StringUtils;
14-
import okhttp3.MediaType;
15-
import okhttp3.MultipartBody;
16-
import okhttp3.RequestBody;
1713
import org.apache.http.HttpEntity;
1814
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
1915
import org.apache.http.entity.ByteArrayEntity;

forest-core/src/main/java/com/dtflys/forest/callback/OnResponse.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
public interface OnResponse {
1818

1919
default ResponseSuccess success() {
20-
return ResponseResult.SUCCESS;
20+
return ResponseResult.RESPONSE_RESULT_SUCCESS;
2121
}
2222

2323
default ResponseError error() {
24-
return ResponseResult.ERROR;
24+
return ResponseResult.RESPONSE_RESULT_ERROR;
2525
}
2626

2727
default ResponseSuccess success(Object data) {
@@ -37,7 +37,7 @@ default ResponseError error(String message) {
3737
}
3838

3939
default ResponseProceed proceed() {
40-
return ResponseResult.PROCEED;
40+
return ResponseResult.RESPONSE_RESULT_PROCEED;
4141
}
4242

4343
/**

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.dtflys.forest.logging.DefaultLogHandler;
6262
import com.dtflys.forest.logging.ForestLogHandler;
6363
import com.dtflys.forest.logging.ForestLogger;
64+
import com.dtflys.forest.mapping.MappingTemplate;
6465
import com.dtflys.forest.pool.FixedRequestPool;
6566
import com.dtflys.forest.pool.ForestRequestPool;
6667
import com.dtflys.forest.proxy.ProxyFactory;
@@ -71,6 +72,7 @@
7172
import com.dtflys.forest.reflection.ForestObjectFactory;
7273
import com.dtflys.forest.reflection.ForestMethodVariable;
7374
import com.dtflys.forest.reflection.ForestVariable;
75+
import com.dtflys.forest.reflection.TemplateVariable;
7476
import com.dtflys.forest.result.AfterExecuteResultTypeHandler;
7577
import com.dtflys.forest.result.ForestRequestResultHandler;
7678
import com.dtflys.forest.result.ResultTypeHandler;
@@ -1634,8 +1636,7 @@ public <T> ProxyFactory<T> getProxyFactory(Class<T> clazz) {
16341636
*/
16351637
@Deprecated
16361638
public ForestConfiguration setVariableValue(String name, Object value) {
1637-
this.variables.put(name, new BasicVariable(value));
1638-
return this;
1639+
return setVariable(name, value);
16391640
}
16401641

16411642
/**
@@ -1646,7 +1647,15 @@ public ForestConfiguration setVariableValue(String name, Object value) {
16461647
* @return 当前ForestConfiguration实例
16471648
*/
16481649
public ForestConfiguration setVariable(String name, Object value) {
1649-
this.variables.put(name, new BasicVariable(value));
1650+
if (value instanceof ForestVariable) {
1651+
this.variables.put(name, (ForestVariable) value);
1652+
} else if (value instanceof CharSequence) {
1653+
final MappingTemplate mappingTemplate = MappingTemplate.create(this, String.valueOf(value));
1654+
final TemplateVariable templateVariable = new TemplateVariable(mappingTemplate);
1655+
variables.put(name, templateVariable);
1656+
} else {
1657+
this.variables.put(name, new BasicVariable(value));
1658+
}
16501659
return this;
16511660
}
16521661

@@ -1754,8 +1763,6 @@ public ForestConfiguration getConfiguration() {
17541763
return this;
17551764
}
17561765

1757-
1758-
17591766
/**
17601767
* 判断变量是否已定义
17611768
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ default <R> R variableValue(String name, Class<R> clazz) {
3232
ForestVariable getVariable(String name);
3333

3434
ForestConfiguration getConfiguration();
35+
3536
}

forest-core/src/main/java/com/dtflys/forest/http/ForestRequest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@
115115
import java.util.concurrent.ConcurrentHashMap;
116116
import java.util.concurrent.Future;
117117
import java.util.concurrent.TimeUnit;
118-
import java.util.concurrent.atomic.AtomicReference;
119-
import java.util.function.BiConsumer;
120118
import java.util.function.Consumer;
121119
import java.util.function.Function;
122120
import java.util.function.Supplier;
@@ -132,7 +130,7 @@
132130
* @author gongjun[dt_flys@hotmail.com]
133131
* @since 2016-03-24
134132
*/
135-
public class ForestRequest<T> extends AbstractVariableScope<ForestRequest<T>> implements HasURL, HasHeaders {
133+
public class ForestRequest<T> extends AbstractVariableScope<ForestRequest<T>> implements RequestVariableScope, HasURL, HasHeaders {
136134

137135
private final static Object[] EMPTY_RENDER_ARGS = new Object[0];
138136

@@ -801,7 +799,7 @@ public ForestRequest<T> setUrl(MappingURLTemplate urlTemplate, Object... args) {
801799
if (!this.query.isEmpty()) {
802800
this.query.clearQueriesFromUrl();
803801
}
804-
802+
805803
final ForestURL newUrl = urlTemplate.render(this, args, this.query);
806804
if (this.url == null) {
807805
this.url = newUrl;
@@ -5506,4 +5504,8 @@ public <R> R execute(TypeReference<R> typeReference) {
55065504
return execute(typeReference.getType());
55075505
}
55085506

5507+
@Override
5508+
public ForestRequest asRequest() {
5509+
return this;
5510+
}
55095511
}

forest-core/src/main/java/com/dtflys/forest/http/ForestURL.java

Lines changed: 121 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424

2525
package com.dtflys.forest.http;
2626

27+
import com.dtflys.forest.Forest;
28+
import com.dtflys.forest.config.ForestConfiguration;
29+
import com.dtflys.forest.config.VariableScope;
2730
import com.dtflys.forest.exceptions.ForestRuntimeException;
2831
import com.dtflys.forest.mapping.MappingURLTemplate;
2932
import com.dtflys.forest.utils.StringUtils;
3033
import com.dtflys.forest.utils.URLUtils;
3134
import org.slf4j.Logger;
3235
import org.slf4j.LoggerFactory;
3336

37+
import java.lang.annotation.ElementType;
3438
import java.net.MalformedURLException;
3539
import java.net.URI;
3640
import java.net.URISyntaxException;
@@ -47,10 +51,23 @@ public class ForestURL {
4751
private final static Logger log = LoggerFactory.getLogger(ForestURL.class);
4852

4953
/**
50-
* 原始URL
54+
* 变量作用域
55+
* @since 1.7.0
56+
*/
57+
private volatile VariableScope scope = Forest.config();
58+
59+
volatile ForestRequest request;
60+
61+
/**
62+
* URL字符串模板
63+
*/
64+
private MappingURLTemplate template;
65+
66+
/**
67+
* 解析后的URL
5168
* <p>即为整个完整的没被拆分的URL字符串
5269
*/
53-
private String originalUrl;
70+
private String parsedUrl;
5471

5572
private ForestAddress address;
5673

@@ -103,12 +120,70 @@ public class ForestURL {
103120
/**
104121
* 是否需要重新生成 URL
105122
*/
106-
private boolean needRegenerateUrl = false;
123+
private volatile boolean needUrlRegenerate = false;
124+
125+
126+
private volatile boolean variablesChanged = false;
127+
128+
private void needUrlRegenerate() {
129+
needUrlRegenerate = true;
130+
}
131+
132+
public static ForestURL create(final String url) {
133+
return create(Forest.config(), url);
134+
}
135+
136+
137+
public static ForestURL create(ForestRequest request, final MappingURLTemplate template) {
138+
final ForestURL forestURL = create(template);
139+
forestURL.request = request;
140+
return forestURL;
141+
}
142+
143+
144+
public static ForestURL create(final MappingURLTemplate template) {
145+
final ForestURL forestURL = new ForestURL();
146+
forestURL.template = template;
147+
forestURL.needUrlRegenerate = true;
148+
forestURL.variablesChanged = true;
149+
return forestURL;
150+
}
151+
107152

108-
private void needRegenerateUrl() {
109-
needRegenerateUrl = true;
153+
public static ForestURL create(final ForestConfiguration configuration, final String url) {
154+
final MappingURLTemplate urlTemplate = MappingURLTemplate.get(configuration, url);
155+
final ForestURL forestURL = new ForestURL();
156+
forestURL.parsedUrl = url;
157+
forestURL.template = urlTemplate;
158+
forestURL.needUrlRegenerate = true;
159+
forestURL.variablesChanged = true;
160+
return forestURL;
110161
}
111162

163+
public static ForestURL parse(final ForestRequest request, final String url) {
164+
final ForestConfiguration configuration = request.getConfiguration();
165+
final MappingURLTemplate urlTemplate = MappingURLTemplate.get(configuration, url);
166+
final ForestURL forestURL = new ForestURL();
167+
forestURL.parsedUrl = url;
168+
forestURL.template = urlTemplate;
169+
urlTemplate.render(forestURL, request, request.arguments(), request.getQuery());
170+
return forestURL;
171+
}
172+
173+
public static ForestURL parse(final String url) {
174+
return parse(Forest.config(), url);
175+
}
176+
177+
178+
public static ForestURL parse(final ForestConfiguration configuration, final String url) {
179+
final MappingURLTemplate urlTemplate = MappingURLTemplate.get(configuration, url);
180+
final ForestURL forestURL = new ForestURL();
181+
forestURL.parsedUrl = url;
182+
forestURL.template = urlTemplate;
183+
final ForestQueryMap queryMap = new ForestQueryMap(null);
184+
urlTemplate.render(forestURL, configuration, new Object[0], queryMap);
185+
return forestURL;
186+
}
112187

113188
public static ForestURL fromUrl(String url) {
114189
try {
@@ -118,6 +193,20 @@ public static ForestURL fromUrl(String url) {
118193
}
119194
}
120195

196+
public static ForestURL unparsedURL(final String url) {
197+
ForestURL forestURL = new ForestURL();
198+
forestURL.template = MappingURLTemplate.get(url);
199+
return forestURL;
200+
}
201+
202+
public static ForestURL emptyURL() {
203+
return new ForestURL();
204+
}
205+
206+
207+
private ForestURL() {
208+
}
209+
121210
public ForestURL(URL url) {
122211
if (url == null) {
123212
throw new ForestRuntimeException("[Forest] Request url cannot be null!");
@@ -142,7 +231,7 @@ public ForestURL(String scheme, String userInfo, String host, Integer port, Stri
142231
this.port = port;
143232
this.path = path;
144233
this.ref = ref;
145-
needRegenerateUrl();
234+
needUrlRegenerate();
146235
}
147236

148237
/**
@@ -151,11 +240,11 @@ public ForestURL(String scheme, String userInfo, String host, Integer port, Stri
151240
* @return 原始URL字符串
152241
*/
153242
public String getOriginalUrl() {
154-
if (originalUrl == null || needRegenerateUrl) {
155-
originalUrl = toURLString();
156-
needRegenerateUrl = false;
243+
if (parsedUrl == null || needUrlRegenerate) {
244+
parsedUrl = toURLString();
245+
needUrlRegenerate = false;
157246
}
158-
return originalUrl;
247+
return parsedUrl;
159248
}
160249

161250
/**
@@ -201,11 +290,12 @@ public ForestURL setScheme(String scheme) {
201290
}
202291
this.scheme = scheme.trim();
203292
refreshSSL();
204-
needRegenerateUrl();
293+
needUrlRegenerate();
205294
return this;
206295
}
207296

208297
public String getHost() {
298+
checkAndReparseUrl();
209299
if (StringUtils.isEmpty(host) && address != null) {
210300
return address.getHost();
211301
}
@@ -220,7 +310,7 @@ public ForestURL setHost(String host) {
220310
if (this.host.endsWith("/")) {
221311
this.host = this.host.substring(0, this.host.lastIndexOf("/"));
222312
}
223-
needRegenerateUrl();
313+
needUrlRegenerate();
224314
return this;
225315
}
226316

@@ -238,9 +328,9 @@ public int getPort() {
238328
return normalizePort(port, ssl);
239329
}
240330

241-
public ForestURL setPort(int port) {
331+
public ForestURL setPort(Integer port) {
242332
this.port = port;
243-
needRegenerateUrl();
333+
needUrlRegenerate();
244334
return this;
245335
}
246336

@@ -315,7 +405,7 @@ public ForestURL setBasePath(String basePath, boolean forced) {
315405
this.basePath = "/" + this.basePath;
316406
}
317407
}
318-
needRegenerateUrl();
408+
needUrlRegenerate();
319409
return this;
320410
}
321411

@@ -344,7 +434,7 @@ public ForestURL setPath(String path) {
344434
return this;
345435
}
346436
this.path = path.trim();
347-
needRegenerateUrl();
437+
needUrlRegenerate();
348438
return this;
349439
}
350440

@@ -357,7 +447,7 @@ public String getUserInfo() {
357447

358448
public ForestURL setUserInfo(String userInfo) {
359449
this.userInfo = userInfo;
360-
needRegenerateUrl();
450+
needUrlRegenerate();
361451
return this;
362452
}
363453

@@ -572,7 +662,7 @@ public ForestURL setBaseURL(ForestURL baseURL) {
572662
this.path = basePath + this.path;
573663
}
574664
}
575-
needRegenerateUrl();
665+
needUrlRegenerate();
576666
return this;
577667
}
578668

@@ -595,7 +685,7 @@ public ForestURL mergeAddress() {
595685
if (StringUtils.isEmpty(basePath)) {
596686
setBasePath(address.getBasePath(), false);
597687
}
598-
needRegenerateUrl();
688+
needUrlRegenerate();
599689
}
600690
return this;
601691
}
@@ -615,5 +705,17 @@ public ForestURL checkAndComplete() {
615705
}
616706
return this;
617707
}
708+
709+
private void checkAndReparseUrl() {
710+
if (variablesChanged && template != null) {
711+
final ForestQueryMap queryMap = new ForestQueryMap(null);
712+
if (request != null) {
713+
template.render(this, request, request.arguments(), request.getQuery());
714+
} else {
715+
template.render(this, Forest.config(), new Object[0], queryMap);
716+
}
717+
needUrlRegenerate();
718+
}
719+
}
618720

619721
}

0 commit comments

Comments
 (0)