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 5cab6af [ISSUE #3037] Fix Bug The Rewrite Plugin should support
{PathVariable… (#3038)
5cab6af is described below
commit 5cab6af1d209ce70219cee0a53813c607baf8942
Author: Lisandro <[email protected]>
AuthorDate: Mon Mar 14 17:40:41 2022 +0800
[ISSUE #3037] Fix Bug The Rewrite Plugin should support {PathVariable…
(#3038)
* [ISSUE #3037] Fix Bug The Rewrite Plugin should support {PathVariable}
request
* [ISSUE #3037] Fix CI
* [ISSUE #3037] Fix CI
* [ISSUE #3037] Fix CI
* [ISSUE #3037] Fix CI
* [ISSUE #3037] Fix CI
* [ISSUE #3037] Fix CI
Co-authored-by: lishuo <[email protected]>
---
.../org/apache/shenyu/common/utils/PathMatchUtils.java | 15 +++++++++++++++
.../apache/shenyu/common/utils/PathMatchUtilsTest.java | 15 +++++++++++++++
.../org/apache/shenyu/plugin/rewrite/RewritePlugin.java | 6 +++++-
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/PathMatchUtils.java
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/PathMatchUtils.java
index 9a3fdfe..29f72b7 100644
---
a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/PathMatchUtils.java
+++
b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/PathMatchUtils.java
@@ -20,6 +20,9 @@ package org.apache.shenyu.common.utils;
import com.google.common.base.Splitter;
import org.springframework.util.AntPathMatcher;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
/**
* The type Path match utils.
*/
@@ -28,6 +31,18 @@ public class PathMatchUtils {
private static final AntPathMatcher MATCHER = new AntPathMatcher();
/**
+ * replace url {id} to real param.
+ *
+ * @param path the total path
+ * @param regex the regex content
+ * @param replacement the replacement content
+ * @return the string
+ */
+ public static String replaceAll(final String path, final String regex,
final String replacement) {
+ return path.replaceAll(Pattern.quote(regex),
Matcher.quoteReplacement(replacement));
+ }
+
+ /**
* Match boolean.
*
* @param matchUrls to ignore urls
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathMatchUtilsTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathMatchUtilsTest.java
index 51c98dc..e9ef7c3 100644
---
a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathMatchUtilsTest.java
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/PathMatchUtilsTest.java
@@ -19,6 +19,8 @@ package org.apache.shenyu.common.utils;
import org.junit.jupiter.api.Test;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -44,4 +46,17 @@ public final class PathMatchUtilsTest {
assertTrue(PathMatchUtils.match("/**", "/testing/testing"));
assertTrue(PathMatchUtils.match("/test/**", "/test/test"));
}
+
+ @Test
+ public void testPathVariableHandle() {
+ //test filter PathVariable
+ assertTrue(PathMatchUtils.match("{id}/{name}",
"/demo/order/path/{id}/{name}".substring("/demo/order/path/{id}/{name}".indexOf("{"))));
+ //test filter original param
+ assertTrue(PathMatchUtils.match("1/godfje@",
"/demo/order/path/1/godfje@".substring("demo/order/path/{id}/{name}".indexOf("{")
+ 1)));
+ //test replaceAll result
+ final String realPath =
PathMatchUtils.replaceAll("demo/order/path/{id}/{name}",
+
"/demo/order/path/{id}/{name}".substring("/demo/order/path/{id}/{name}".indexOf("{")),
+
"/demo/order/path/1/godfje@".substring("demo/order/path/{id}/{name}".indexOf("{")
+ 1));
+ assertThat(realPath, is("demo/order/path/1/godfje@"));
+ }
}
diff --git
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
index c2cdd8b..4a43b6f 100644
---
a/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
+++
b/shenyu-plugin/shenyu-plugin-rewrite/src/main/java/org/apache/shenyu/plugin/rewrite/RewritePlugin.java
@@ -24,6 +24,7 @@ import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.dto.convert.rule.RewriteHandle;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.utils.PathMatchUtils;
import org.apache.shenyu.plugin.api.ShenyuPluginChain;
import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
@@ -52,7 +53,10 @@ public class RewritePlugin extends AbstractShenyuPlugin {
}
String rewriteUri = exchange.getRequest().getURI().getPath();
if (StringUtils.isNoneBlank(rewriteHandle.getRegex(),
rewriteHandle.getReplace())) {
- rewriteUri = rewriteUri.replaceAll(rewriteHandle.getRegex(),
rewriteHandle.getReplace());
+ rewriteUri = rewriteHandle.getReplace().contains("{")
+ ? PathMatchUtils.replaceAll(rewriteHandle.getReplace(),
rewriteHandle.getRegex().substring(rewriteHandle.getRegex().indexOf("{")),
+
rewriteUri.substring(rewriteHandle.getRegex().indexOf("{") + 1))
+ : rewriteUri.replaceAll(rewriteHandle.getRegex(),
rewriteHandle.getReplace());
exchange.getAttributes().put(Constants.REWRITE_URI, rewriteUri);
}
return chain.execute(exchange);