This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 91ff0dd [ISSUE #2627] [type:refactor] part 2 (#2999)
91ff0dd is described below
commit 91ff0dd5bb5da720f0894129795f515319cfdfc2
Author: dragon-zhang <[email protected]>
AuthorDate: Mon Mar 7 22:09:20 2022 +0800
[ISSUE #2627] [type:refactor] part 2 (#2999)
* [ISSUE #2627] [type:refactor] move buildDomain from DividePlugin to
Upstream
* [ISSUE #2627] [type:refactor] refactor URIPlugin
* add doc
* remove unused
---
.../shenyu/loadbalancer/entity/Upstream.java | 15 ++++++
.../shenyu/plugin/api/utils/RequestUrlUtils.java} | 53 ++++++++--------------
.../apache/shenyu/plugin/divide/DividePlugin.java | 11 +----
.../org/apache/shenyu/plugin/uri/URIPlugin.java | 24 ++--------
4 files changed, 40 insertions(+), 63 deletions(-)
diff --git
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
index 8f646b1..6a5827e 100644
---
a/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
+++
b/shenyu-loadbalancer/src/main/java/org/apache/shenyu/loadbalancer/entity/Upstream.java
@@ -17,6 +17,8 @@
package org.apache.shenyu.loadbalancer.entity;
+import org.apache.commons.lang3.StringUtils;
+
import java.util.Objects;
/**
@@ -253,6 +255,19 @@ public final class Upstream {
}
/**
+ * build request domain.
+ *
+ * @return domain
+ */
+ public String buildDomain() {
+ String protocol = this.getProtocol();
+ if (StringUtils.isBlank(protocol)) {
+ protocol = "http://";
+ }
+ return protocol + this.getUrl().trim();
+ }
+
+ /**
* class builder.
*
* @return Builder builder
diff --git
a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
similarity index 54%
copy from
shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
copy to
shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
index 4248e6b..662ddb7 100644
---
a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
+++
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/RequestUrlUtils.java
@@ -15,37 +15,38 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.uri;
+package org.apache.shenyu.plugin.api.utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.constant.Constants;
-import org.apache.shenyu.common.enums.PluginEnum;
-import org.apache.shenyu.plugin.api.ShenyuPlugin;
-import org.apache.shenyu.plugin.api.ShenyuPluginChain;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
-import org.apache.shenyu.plugin.api.utils.RequestQueryCodecUtil;
import org.springframework.web.server.ServerWebExchange;
-import reactor.core.publisher.Mono;
import java.net.URI;
/**
- * The type Uri plugin.
+ * RequestUrlUtils.
*/
-public class URIPlugin implements ShenyuPlugin {
+public final class RequestUrlUtils {
- @Override
- public Mono<Void> execute(final ServerWebExchange exchange, final
ShenyuPluginChain chain) {
- ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
- assert shenyuContext != null;
- String path = exchange.getAttribute(Constants.HTTP_DOMAIN);
- if (StringUtils.isBlank(path)) {
- return chain.execute(exchange);
- }
- String rewriteUri = (String)
exchange.getAttributes().get(Constants.REWRITE_URI);
+ private RequestUrlUtils() {
+ }
+
+ /**
+ * Build the final request uri.
+ *
+ * @param exchange the exchange
+ * @param domain the domain
+ * @return request uri
+ */
+ public static URI buildRequestUri(final ServerWebExchange exchange, final
String domain) {
+ String path = domain;
+ final String rewriteUri = (String)
exchange.getAttributes().get(Constants.REWRITE_URI);
if (StringUtils.isNoneBlank(rewriteUri)) {
path = path + rewriteUri;
} else {
+ ShenyuContext shenyuContext =
exchange.getAttribute(Constants.CONTEXT);
+ assert shenyuContext != null;
String realUrl = shenyuContext.getRealUrl();
if (StringUtils.isNoneBlank(realUrl)) {
path = path + realUrl;
@@ -54,22 +55,6 @@ public class URIPlugin implements ShenyuPlugin {
if
(StringUtils.isNoneBlank(exchange.getRequest().getURI().getQuery())) {
path = String.join("?", path,
RequestQueryCodecUtil.getCodecQuery(exchange));
}
- exchange.getAttributes().put(Constants.HTTP_URI, URI.create(path));
- return chain.execute(exchange);
- }
-
- @Override
- public int getOrder() {
- return PluginEnum.URI.getCode();
- }
-
- @Override
- public String named() {
- return PluginEnum.URI.getName();
- }
-
- @Override
- public boolean skip(final ServerWebExchange exchange) {
- return skipExceptHttpLike(exchange);
+ return URI.create(path);
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java
b/shenyu-plugin/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java
index 4f5e420..4e28af6 100644
---
a/shenyu-plugin/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java
+++
b/shenyu-plugin/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/DividePlugin.java
@@ -18,7 +18,6 @@
package org.apache.shenyu.plugin.divide;
import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.dto.SelectorData;
@@ -87,7 +86,7 @@ public class DividePlugin extends AbstractShenyuPlugin {
return WebFluxResultUtils.result(exchange, error);
}
// set the http url
- String domain = buildDomain(upstream);
+ String domain = upstream.buildDomain();
exchange.getAttributes().put(Constants.HTTP_DOMAIN, domain);
// set the http timeout
exchange.getAttributes().put(Constants.HTTP_TIME_OUT,
ruleHandle.getTimeout());
@@ -119,12 +118,4 @@ public class DividePlugin extends AbstractShenyuPlugin {
protected Mono<Void> handleRuleIfNull(final String pluginName, final
ServerWebExchange exchange, final ShenyuPluginChain chain) {
return WebFluxResultUtils.noRuleResult(pluginName, exchange);
}
-
- private String buildDomain(final Upstream upstream) {
- String protocol = upstream.getProtocol();
- if (StringUtils.isBlank(protocol)) {
- protocol = "http://";
- }
- return protocol + upstream.getUrl().trim();
- }
}
diff --git
a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
b/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
index 4248e6b..897872c 100644
---
a/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
+++
b/shenyu-plugin/shenyu-plugin-uri/src/main/java/org/apache/shenyu/plugin/uri/URIPlugin.java
@@ -22,8 +22,7 @@ import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
-import org.apache.shenyu.plugin.api.context.ShenyuContext;
-import org.apache.shenyu.plugin.api.utils.RequestQueryCodecUtil;
+import org.apache.shenyu.plugin.api.utils.RequestUrlUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@@ -36,25 +35,12 @@ public class URIPlugin implements ShenyuPlugin {
@Override
public Mono<Void> execute(final ServerWebExchange exchange, final
ShenyuPluginChain chain) {
- ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
- assert shenyuContext != null;
- String path = exchange.getAttribute(Constants.HTTP_DOMAIN);
- if (StringUtils.isBlank(path)) {
+ String domain = exchange.getAttribute(Constants.HTTP_DOMAIN);
+ if (StringUtils.isBlank(domain)) {
return chain.execute(exchange);
}
- String rewriteUri = (String)
exchange.getAttributes().get(Constants.REWRITE_URI);
- if (StringUtils.isNoneBlank(rewriteUri)) {
- path = path + rewriteUri;
- } else {
- String realUrl = shenyuContext.getRealUrl();
- if (StringUtils.isNoneBlank(realUrl)) {
- path = path + realUrl;
- }
- }
- if
(StringUtils.isNoneBlank(exchange.getRequest().getURI().getQuery())) {
- path = String.join("?", path,
RequestQueryCodecUtil.getCodecQuery(exchange));
- }
- exchange.getAttributes().put(Constants.HTTP_URI, URI.create(path));
+ final URI uri = RequestUrlUtils.buildRequestUri(exchange, domain);
+ exchange.getAttributes().put(Constants.HTTP_URI, uri);
return chain.execute(exchange);
}