From 21527576b1b42756ea7bdf0ac41bd73c9a7f6834 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Mon, 15 May 2023 18:04:24 +0800 Subject: [PATCH] Support path parameter such as /user/{userId} #432 --- .../com/fizzgate/filter/AggregateFilter.java | 1 + .../java/com/fizzgate/fizz/ConfigLoader.java | 43 +++++++++++++++++++ .../main/java/com/fizzgate/fizz/Pipeline.java | 1 + 3 files changed, 45 insertions(+) diff --git a/fizz-core/src/main/java/com/fizzgate/filter/AggregateFilter.java b/fizz-core/src/main/java/com/fizzgate/filter/AggregateFilter.java index 96a73fc..60dbc55 100644 --- a/fizz-core/src/main/java/com/fizzgate/filter/AggregateFilter.java +++ b/fizz-core/src/main/java/com/fizzgate/filter/AggregateFilter.java @@ -160,6 +160,7 @@ public class AggregateFilter implements WebFilter { clientInput.put("headers", headers); clientInput.put("params", MapUtil.toHashMap(request.getQueryParams())); clientInput.put("contentType", request.getHeaders().getFirst(CommonConstants.HEADER_CONTENT_TYPE)); + clientInput.put("pathParams", com.fizzgate.util.ThreadContext.get("pathParams")); Mono result = null; MediaType contentType = request.getHeaders().getContentType(); diff --git a/fizz-core/src/main/java/com/fizzgate/fizz/ConfigLoader.java b/fizz-core/src/main/java/com/fizzgate/fizz/ConfigLoader.java index 54565a1..7cf7d1a 100644 --- a/fizz-core/src/main/java/com/fizzgate/fizz/ConfigLoader.java +++ b/fizz-core/src/main/java/com/fizzgate/fizz/ConfigLoader.java @@ -27,6 +27,7 @@ import com.fizzgate.fizz.input.InputType; import com.fizzgate.util.Consts; import com.fizzgate.util.ReactorUtils; +import com.fizzgate.util.UrlTransformUtils; import org.apache.commons.io.FileUtils; import org.apache.logging.log4j.ThreadContext; import org.noear.snack.ONode; @@ -50,6 +51,7 @@ import java.lang.ref.SoftReference; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; import static com.fizzgate.config.AggregateRedisConfig.AGGREGATE_REACTIVE_REDIS_TEMPLATE; import static com.fizzgate.util.Consts.S.FORWARD_SLASH; @@ -388,6 +390,47 @@ public class ConfigLoader { ClientInputConfig cfg = (ClientInputConfig) input.getConfig(); return new AggregateResource(pipeline, input); } + } else { + + String aggrMethodPath = null; + try { + for (Map.Entry entry : aggregateResources.entrySet()) { + aggrMethodPath = entry.getKey(); + boolean match = UrlTransformUtils.ANT_PATH_MATCHER.match(aggrMethodPath, key); + if (match) { + String configStr = aggregateResources.get(aggrMethodPath); + Input input = createInput(configStr); + Pipeline pipeline = createPipeline(configStr); + if (pipeline != null && input != null) { + Map pathVariables = UrlTransformUtils.ANT_PATH_MATCHER.extractUriTemplateVariables(aggrMethodPath, key); + Map map = Collections.emptyMap(); + if (!CollectionUtils.isEmpty(pathVariables)) { + map = pathVariables.entrySet().stream().filter( + e -> { + return e.getKey().indexOf('$') == -1; + } + ) + .collect( + Collectors.toMap( + Map.Entry::getKey, + e -> { + return (Object) e.getValue(); + } + ) + ); + } + com.fizzgate.util.ThreadContext.set("pathParams", map); + return new AggregateResource(pipeline, input); + } else { + LOGGER.warn("request {} match {}, input {} pipeline {}", key, aggrMethodPath, input, pipeline); + return null; + } + } + } + } catch (IOException e) { + LOGGER.warn("request {} match {}, create input or pipeline error", key, aggrMethodPath, e); + return null; + } } return null; } diff --git a/fizz-core/src/main/java/com/fizzgate/fizz/Pipeline.java b/fizz-core/src/main/java/com/fizzgate/fizz/Pipeline.java index db5e4c9..b7bf643 100644 --- a/fizz-core/src/main/java/com/fizzgate/fizz/Pipeline.java +++ b/fizz-core/src/main/java/com/fizzgate/fizz/Pipeline.java @@ -242,6 +242,7 @@ public class Pipeline { inputRequest.put("method", clientInput.get("method")); inputRequest.put("headers", clientInput.get("headers")); inputRequest.put("params", clientInput.get("params")); + inputRequest.put("pathParams", clientInput.get("pathParams")); stepContext.addFilePartMap((Map) clientInput.get("filePartMap")); if (CONTENT_TYPE_XML.equals(config.getContentType()) || (StringUtils.isEmpty(config.getContentType())