From 9e4bc5ccdc403c331b7335d536f854b85f5413ca Mon Sep 17 00:00:00 2001 From: provenceee <83857838+provenceee@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:01:59 +0800 Subject: [PATCH] *fix an issue where inputstream is sometimes lost. Signed-off-by: provenceee <83857838+provenceee@users.noreply.github.com> --- .../integration/entity/LaneTestEntity.java | 4 +- .../io/sermant/integration/lane/LaneTest.java | 4 +- .../integration/registry/ConsumerTest.java | 4 +- .../registry/SpringAndDubboTest.java | 4 +- .../integration/router/RouterTest.java | 4 +- .../io/sermant/integration/lane/LaneTest.java | 4 +- .../integration/router/TagRouterTest.java | 4 +- .../interceptor/UrlInterceptor.java | 4 +- .../declarer/ClientFactoryDeclarer.java | 4 +- .../DispatcherServletInterceptor.java | 2 +- ...DiscoveryRegistryDirectoryInterceptor.java | 2 +- .../io/sermant/router/spring/entity/Keys.java | 50 +++++++++ .../spring/handler/AbstractHandler.java | 18 ++- .../handler/AbstractMappingHandler.java | 55 ---------- .../handler/AbstractRequestTagHandler.java | 73 ------------- ...neMappingHandler.java => LaneHandler.java} | 14 ++- .../spring/handler/LaneRequestTagHandler.java | 91 ---------------- .../handler/RouteRequestTagHandler.java | 51 --------- ...uteMappingHandler.java => TagHandler.java} | 12 +- .../AbstractHandlerMappingInterceptor.java | 36 ++++-- .../DispatcherServletInterceptor.java | 45 ++++++-- .../router/spring/service/LaneService.java | 14 +-- .../spring/utils/SpringRouterUtils.java | 35 +++++- ...gHandlerTest.java => LaneHandlerTest.java} | 33 ++++-- .../handler/LaneRequestTagHandlerTest.java | 103 ------------------ .../handler/RouteRequestTagHandlerTest.java | 66 ----------- ...ngHandlerTest.java => TagHandlerTest.java} | 20 ++-- .../spring/handler/TestLaneService.java | 8 +- .../spring/utils/SpringRouterUtilsTest.java | 63 +++++++++++ .../spring/service/LaneServiceImpl.java | 10 +- .../router/spring/utils/RouteUtils.java | 48 +------- .../spring/service/LaneServiceTest.java | 23 +--- 32 files changed, 292 insertions(+), 616 deletions(-) create mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/entity/Keys.java delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractMappingHandler.java delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractRequestTagHandler.java rename sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/{LaneMappingHandler.java => LaneHandler.java} (88%) delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneRequestTagHandler.java delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteRequestTagHandler.java rename sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/{RouteMappingHandler.java => TagHandler.java} (79%) rename sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/{LaneMappingHandlerTest.java => LaneHandlerTest.java} (76%) delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneRequestTagHandlerTest.java delete mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteRequestTagHandlerTest.java rename sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/{RouteMappingHandlerTest.java => TagHandlerTest.java} (82%) create mode 100644 sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/utils/SpringRouterUtilsTest.java diff --git a/sermant-integration-tests/dubbo-test/dubbo-integration-api/src/main/java/io/sermant/integration/entity/LaneTestEntity.java b/sermant-integration-tests/dubbo-test/dubbo-integration-api/src/main/java/io/sermant/integration/entity/LaneTestEntity.java index 20bcea6628..9e4eb32285 100644 --- a/sermant-integration-tests/dubbo-test/dubbo-integration-api/src/main/java/io/sermant/integration/entity/LaneTestEntity.java +++ b/sermant-integration-tests/dubbo-test/dubbo-integration-api/src/main/java/io/sermant/integration/entity/LaneTestEntity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ public class LaneTestEntity implements Serializable { private final boolean enabled; /** - * 构造方法 + * constructor * * @param id id * @param enabled enabled diff --git a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java index bca4fdc950..3dc7ebe037 100644 --- a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java +++ b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ public class LaneTest { private final boolean isExecuteSpringTest; /** - * 构造方法 + * constructor */ public LaneTest() { baseUrl = "http://127.0.0.1:" + System.getProperty("controller.port", "28019") + "/controller/getLaneBy"; diff --git a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/ConsumerTest.java b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/ConsumerTest.java index 8d976fceee..66e289617c 100644 --- a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/ConsumerTest.java +++ b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/ConsumerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,7 +41,7 @@ public class ConsumerTest { private final String dubboVersion; /** - * 构造方法 + * constructor */ public ConsumerTest() { dubboVersion = System.getProperty("dubbo.running.version"); diff --git a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/SpringAndDubboTest.java b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/SpringAndDubboTest.java index 9d7058e477..579b233cff 100644 --- a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/SpringAndDubboTest.java +++ b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/registry/SpringAndDubboTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ public class SpringAndDubboTest { private final String baseUrl; /** - * 构造方法 + * constructor */ public SpringAndDubboTest() { baseUrl = "http://127.0.0.1:" + System.getProperty("controller.port", "28020") + "/consumer/"; diff --git a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/router/RouterTest.java b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/router/RouterTest.java index 616074174e..21cbc3af47 100644 --- a/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/router/RouterTest.java +++ b/sermant-integration-tests/dubbo-test/dubbo-integration-test/src/test/java/io/sermant/integration/router/RouterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,7 +60,7 @@ public class RouterTest { private final boolean isExecuteSpringTest; /** - * 构造方法 + * constructor */ public RouterTest() throws InterruptedException { testTagRouterBaseUrl = diff --git a/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java b/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java index 40fb609f6f..609238bdff 100644 --- a/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java +++ b/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/lane/LaneTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,7 +90,7 @@ public class LaneTest { private final boolean shouldTestWebclient; /** - * 构造方法 + * constructor */ public LaneTest() { springCloudVersion = Optional.ofNullable(System.getenv("SPRING_CLOUD_VERSION")).orElse("Hoxton.RELEASE"); diff --git a/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/router/TagRouterTest.java b/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/router/TagRouterTest.java index 071ad79188..4950d2a4b9 100644 --- a/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/router/TagRouterTest.java +++ b/sermant-integration-tests/spring-test/spring-integration-test/src/test/java/io/sermant/integration/router/TagRouterTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,7 +92,7 @@ public class TagRouterTest { private final String springCloudVersion; /** - * 构造方法 + * constructor */ public TagRouterTest() throws InterruptedException { springCloudVersion = Optional.ofNullable(System.getenv("SPRING_CLOUD_VERSION")).orElse("Hoxton.RELEASE"); diff --git a/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/interceptor/UrlInterceptor.java b/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/interceptor/UrlInterceptor.java index 56c8925306..515db64b54 100644 --- a/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/interceptor/UrlInterceptor.java +++ b/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/interceptor/UrlInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,7 +66,7 @@ public class UrlInterceptor extends AbstractInterceptor { private Set supportRules; /** - * 构造方法 + * constructor */ public UrlInterceptor() { config = PluginConfigManager.getPluginConfig(LoadbalancerConfig.class); diff --git a/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/declarer/ClientFactoryDeclarer.java b/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/declarer/ClientFactoryDeclarer.java index df985a63c4..b0e20d0bad 100644 --- a/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/declarer/ClientFactoryDeclarer.java +++ b/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/io/sermant/loadbalancer/declarer/ClientFactoryDeclarer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ public class ClientFactoryDeclarer extends AbstractDeclarer { private static final String METHOD_NAME = "getInstance"; /** - * 构造方法 + * constructor */ public ClientFactoryDeclarer() { super(ENHANCE_CLASS, INTERCEPT_CLASS, METHOD_NAME); diff --git a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/io/sermant/monitor/interceptor/DispatcherServletInterceptor.java b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/io/sermant/monitor/interceptor/DispatcherServletInterceptor.java index df59332d75..941be643db 100644 --- a/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/io/sermant/monitor/interceptor/DispatcherServletInterceptor.java +++ b/sermant-plugins/sermant-monitor/monitor-plugin/src/main/java/io/sermant/monitor/interceptor/DispatcherServletInterceptor.java @@ -40,7 +40,7 @@ public class DispatcherServletInterceptor extends AbstractInterceptor { private Function getRequestUri; /** - * 构造方法 + * constructor */ public DispatcherServletInterceptor() { initFunction(); diff --git a/sermant-plugins/sermant-router/dubbo3-router-plugin/src/main/java/io/sermant/router/dubbo/interceptor/ServiceDiscoveryRegistryDirectoryInterceptor.java b/sermant-plugins/sermant-router/dubbo3-router-plugin/src/main/java/io/sermant/router/dubbo/interceptor/ServiceDiscoveryRegistryDirectoryInterceptor.java index 8ca3102282..94d2285c5b 100644 --- a/sermant-plugins/sermant-router/dubbo3-router-plugin/src/main/java/io/sermant/router/dubbo/interceptor/ServiceDiscoveryRegistryDirectoryInterceptor.java +++ b/sermant-plugins/sermant-router/dubbo3-router-plugin/src/main/java/io/sermant/router/dubbo/interceptor/ServiceDiscoveryRegistryDirectoryInterceptor.java @@ -39,7 +39,7 @@ public class ServiceDiscoveryRegistryDirectoryInterceptor extends AbstractInterc private final AtomicBoolean isInitialized = new AtomicBoolean(); /** - * 构造方法 + * constructor */ public ServiceDiscoveryRegistryDirectoryInterceptor() { invokerRuleStrategyService = PluginServiceManager.getPluginService(InvokerRuleStrategyService.class); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/entity/Keys.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/entity/Keys.java new file mode 100644 index 0000000000..fa594bc406 --- /dev/null +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/entity/Keys.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sermant.router.spring.entity; + +import java.util.Set; + +/** + * Transparent transmission marks the key entity + * + * @author provenceee + * @since 2023-02-21 + */ +public class Keys { + private final Set matchedKeys; + + private final Set injectedTags; + + /** + * Constructor + * + * @param matchedKeys Label routing transparent transmission markers + * @param injectedTags Swim lane transparent markers + */ + public Keys(Set matchedKeys, Set injectedTags) { + this.matchedKeys = matchedKeys; + this.injectedTags = injectedTags; + } + + public Set getMatchedKeys() { + return matchedKeys; + } + + public Set getInjectedTags() { + return injectedTags; + } +} diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractHandler.java index 3ed1132c8c..bf3dcd6e08 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import io.sermant.router.common.handler.Handler; import io.sermant.router.common.utils.CollectionUtils; +import io.sermant.router.spring.entity.Keys; import java.util.Collections; import java.util.HashMap; @@ -51,4 +52,17 @@ protected Map> getRequestTag(Map> head } return map; } -} \ No newline at end of file + + /** + * Obtain transparent tags + * + * @param path The path of the request + * @param methodName http method + * @param headers HTTP request headers + * @param parameters URL parameter + * @param keys The key of the tag to be obtained + * @return Marks for transparent transmission + */ + public abstract Map> getRequestTag(String path, String methodName, + Map> headers, Map> parameters, Keys keys); +} diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractMappingHandler.java deleted file mode 100644 index 707d1e4823..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractMappingHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import io.sermant.core.plugin.service.PluginServiceManager; -import io.sermant.router.spring.service.SpringConfigService; - -import java.util.List; -import java.util.Map; - -/** - * AbstractHandlerMapping handler - * - * @author provenceee - * @since 2023-02-21 - */ -public abstract class AbstractMappingHandler extends AbstractHandler { - /** - * Configuration service - */ - protected final SpringConfigService configService; - - /** - * Constructor - */ - public AbstractMappingHandler() { - configService = PluginServiceManager.getPluginService(SpringConfigService.class); - } - - /** - * Obtain transparent tags - * - * @param path The path of the request - * @param methodName http method - * @param headers HTTP request headers - * @param parameters URL parameter - * @return Marks for transparent transmission - */ - public abstract Map> getRequestTag(String path, String methodName, - Map> headers, Map> parameters); -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractRequestTagHandler.java deleted file mode 100644 index 9119d9c16f..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/AbstractRequestTagHandler.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Web Blocker handler - * - * @author provenceee - * @since 2023-02-21 - */ -public abstract class AbstractRequestTagHandler extends AbstractHandler { - /** - * Obtain transparent tags - * - * @param path The path of the request - * @param methodName HTTP method - * @param headers HTTP request headers - * @param parameters URL parameter - * @param keys Transparent transmission is marked with a key - * @return Marks for transparent transmission - */ - public abstract Map> getRequestTag(String path, String methodName, - Map> headers, Map parameters, Keys keys); - - /** - * Transparent transmission marks the key entity - * - * @author provenceee - * @since 2023-02-21 - */ - public static class Keys { - private final Set matchKeys; - - private final Set injectTags; - - /** - * Constructor - * - * @param matchKeys Label routing transparent transmission markers - * @param injectTags Swim lane transparent markers - */ - public Keys(Set matchKeys, Set injectTags) { - this.matchKeys = matchKeys; - this.injectTags = injectTags; - } - - public Set getMatchKeys() { - return matchKeys; - } - - public Set getInjectTags() { - return injectTags; - } - } -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneHandler.java similarity index 88% rename from sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneMappingHandler.java rename to sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneHandler.java index 1b645da086..8c3711dc88 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneMappingHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import io.sermant.core.plugin.service.PluginServiceManager; import io.sermant.router.common.constants.RouterConstant; import io.sermant.router.common.utils.CollectionUtils; +import io.sermant.router.spring.entity.Keys; import io.sermant.router.spring.service.LaneService; import java.util.Collections; @@ -30,12 +31,12 @@ import java.util.logging.Logger; /** - * AbstractHandlerMapping handler + * lane handler * * @author provenceee * @since 2023-02-21 */ -public class LaneMappingHandler extends AbstractMappingHandler { +public class LaneHandler extends AbstractHandler { private static final Logger LOGGER = LoggerFactory.getLogger(); private final LaneService laneService; @@ -43,7 +44,7 @@ public class LaneMappingHandler extends AbstractMappingHandler { /** * Constructor */ - public LaneMappingHandler() { + public LaneHandler() { laneService = PluginServiceManager.getPluginService(LaneService.class); } @@ -54,12 +55,13 @@ public LaneMappingHandler() { * @param methodName http method * @param headers HTTP request headers * @param parameters URL parameter + * @param keys The key of the tag to be obtained * @return Marks for transparent transmission */ @Override public Map> getRequestTag(String path, String methodName, Map> headers, - Map> parameters) { - Set injectTags = configService.getInjectTags(); + Map> parameters, Keys keys) { + Set injectTags = keys.getInjectedTags(); if (CollectionUtils.isEmpty(injectTags)) { // The staining mark is empty, which means that there are no staining rules, and it is returned directly LOGGER.fine("Lane tags are empty."); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneRequestTagHandler.java deleted file mode 100644 index 077caef946..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/LaneRequestTagHandler.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import io.sermant.core.common.LoggerFactory; -import io.sermant.core.plugin.service.PluginServiceManager; -import io.sermant.router.common.constants.RouterConstant; -import io.sermant.router.common.utils.CollectionUtils; -import io.sermant.router.spring.service.LaneService; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Stain web blocker handler - * - * @author provenceee - * @since 2023-02-21 - */ -public class LaneRequestTagHandler extends AbstractRequestTagHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - private final LaneService laneService; - - /** - * Constructor - */ - public LaneRequestTagHandler() { - laneService = PluginServiceManager.getPluginService(LaneService.class); - } - - /** - * Obtain transparent tags - * - * @param path The path of the request - * @param methodName The name of the method - * @param headers HTTP request headers - * @return Marks for transparent transmission - */ - @Override - public Map> getRequestTag(String path, String methodName, Map> headers, - Map parameters, Keys keys) { - Set injectTags = keys.getInjectTags(); - if (CollectionUtils.isEmpty(injectTags)) { - // The staining mark is empty, which means that there are no staining rules, and it is returned directly - LOGGER.fine("Lane tags are empty."); - return Collections.emptyMap(); - } - - // Markers for upstream transparent transmissions - Map> tags = getRequestTag(headers, injectTags); - - // This staining marker - Map> laneTag = laneService.getLaneByParameterArray(path, methodName, headers, parameters); - if (CollectionUtils.isEmpty(laneTag)) { - LOGGER.fine("Lane is empty."); - return tags; - } - - // If there is a marker in the upstream transmission that is the same as the one in this staining, - // the upstream transmission shall prevail - laneTag.forEach(tags::putIfAbsent); - if (LOGGER.isLoggable(Level.FINE)) { - LOGGER.fine("Lane is " + tags); - } - return tags; - } - - @Override - public int getOrder() { - return RouterConstant.LANE_HANDLER_ORDER; - } -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteRequestTagHandler.java deleted file mode 100644 index d9a5c7f50f..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteRequestTagHandler.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import io.sermant.router.common.constants.RouterConstant; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Routing web interceptor handler - * - * @author provenceee - * @since 2023-02-21 - */ -public class RouteRequestTagHandler extends AbstractRequestTagHandler { - /** - * Obtain transparent tags - * - * @param path The path of the request - * @param methodName The name of the method - * @param headers HTTP request headers - * @return Marks for transparent transmission - */ - @Override - public Map> getRequestTag(String path, String methodName, Map> headers, - Map parameters, Keys keys) { - Set matchKeys = keys.getMatchKeys(); - return getRequestTag(headers, matchKeys); - } - - @Override - public int getOrder() { - return RouterConstant.ROUTER_HANDLER_ORDER; - } -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/TagHandler.java similarity index 79% rename from sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteMappingHandler.java rename to sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/TagHandler.java index e3a1b3df5c..485ab06e1e 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/RouteMappingHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/handler/TagHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,18 +17,19 @@ package io.sermant.router.spring.handler; import io.sermant.router.common.constants.RouterConstant; +import io.sermant.router.spring.entity.Keys; import java.util.List; import java.util.Map; import java.util.Set; /** - * AbstractHandlerMapping handler + * tag handler * * @author provenceee * @since 2023-02-21 */ -public class RouteMappingHandler extends AbstractMappingHandler { +public class TagHandler extends AbstractHandler { /** * Obtain transparent tags * @@ -36,12 +37,13 @@ public class RouteMappingHandler extends AbstractMappingHandler { * @param methodName http method * @param headers HTTP request headers * @param parameters URL parameter + * @param keys The key of the tag to be obtained * @return Marks for transparent transmission */ @Override public Map> getRequestTag(String path, String methodName, Map> headers, - Map> parameters) { - Set matchKeys = configService.getMatchKeys(); + Map> parameters, Keys keys) { + Set matchKeys = keys.getMatchedKeys(); return getRequestTag(headers, matchKeys); } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java index 1a81fe4911..a6efaa0d6a 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2022-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,16 @@ import io.sermant.core.plugin.agent.entity.ExecuteContext; import io.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import io.sermant.core.plugin.service.PluginServiceManager; import io.sermant.router.common.handler.Handler; +import io.sermant.router.common.utils.CollectionUtils; import io.sermant.router.common.utils.ThreadLocalUtils; -import io.sermant.router.spring.handler.AbstractMappingHandler; -import io.sermant.router.spring.handler.LaneMappingHandler; -import io.sermant.router.spring.handler.RouteMappingHandler; +import io.sermant.router.spring.entity.Keys; +import io.sermant.router.spring.handler.AbstractHandler; +import io.sermant.router.spring.handler.LaneHandler; +import io.sermant.router.spring.handler.TagHandler; +import io.sermant.router.spring.service.SpringConfigService; +import io.sermant.router.spring.utils.SpringRouterUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -32,6 +37,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Set; /** * Webflux obtains the header interception point @@ -43,15 +49,18 @@ public class AbstractHandlerMappingInterceptor extends AbstractInterceptor { private static final String EXCEPT_CLASS_NAME = "org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping"; - private final List handlers; + private final SpringConfigService configService; + + private final List handlers; /** * Constructor */ public AbstractHandlerMappingInterceptor() { + configService = PluginServiceManager.getPluginService(SpringConfigService.class); handlers = new ArrayList<>(); - handlers.add(new LaneMappingHandler()); - handlers.add(new RouteMappingHandler()); + handlers.add(new LaneHandler()); + handlers.add(new TagHandler()); handlers.sort(Comparator.comparingInt(Handler::getOrder)); } @@ -59,14 +68,21 @@ public AbstractHandlerMappingInterceptor() { public ExecuteContext before(ExecuteContext context) { if (shouldHandle(context)) { ThreadLocalUtils.removeRequestTag(); + Set matchKeys = configService.getMatchKeys(); + Set injectTags = configService.getInjectTags(); + if (CollectionUtils.isEmpty(matchKeys) && CollectionUtils.isEmpty(injectTags)) { + // The staining mark is empty, which means that there are no staining rules, and it is returned directly + return context; + } ServerWebExchange exchange = (ServerWebExchange) context.getArguments()[0]; ServerHttpRequest request = exchange.getRequest(); HttpHeaders headers = request.getHeaders(); String path = request.getURI().getPath(); String methodName = request.getMethod().name(); - Map> queryParams = request.getQueryParams(); - handlers.forEach(handler -> ThreadLocalUtils - .addRequestTag(handler.getRequestTag(path, methodName, headers, queryParams))); + String query = request.getURI().getQuery(); + Map> queryParams = SpringRouterUtils.getParametersByQuery(query); + handlers.forEach(handler -> ThreadLocalUtils.addRequestTag( + handler.getRequestTag(path, methodName, headers, queryParams, new Keys(matchKeys, injectTags)))); } return context; } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/DispatcherServletInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/DispatcherServletInterceptor.java index 7138857fb9..ee7a6bcdbf 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/DispatcherServletInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/interceptor/DispatcherServletInterceptor.java @@ -16,19 +16,24 @@ package io.sermant.router.spring.interceptor; +import io.sermant.core.common.LoggerFactory; import io.sermant.core.plugin.agent.entity.ExecuteContext; import io.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import io.sermant.core.plugin.service.PluginServiceManager; +import io.sermant.core.utils.StringUtils; import io.sermant.router.common.handler.Handler; import io.sermant.router.common.utils.CollectionUtils; import io.sermant.router.common.utils.ThreadLocalUtils; -import io.sermant.router.spring.handler.AbstractRequestTagHandler; -import io.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; -import io.sermant.router.spring.handler.LaneRequestTagHandler; -import io.sermant.router.spring.handler.RouteRequestTagHandler; +import io.sermant.router.spring.entity.Keys; +import io.sermant.router.spring.handler.AbstractHandler; +import io.sermant.router.spring.handler.LaneHandler; +import io.sermant.router.spring.handler.TagHandler; import io.sermant.router.spring.service.SpringConfigService; import io.sermant.router.spring.utils.SpringRouterUtils; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -36,9 +41,12 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; @@ -49,11 +57,13 @@ * @since 2022-07-12 */ public class DispatcherServletInterceptor extends AbstractInterceptor { - private final List handlers; + private static final Logger LOGGER = LoggerFactory.getLogger(); + + private final List handlers; private final SpringConfigService configService; - private Function> getParameterMap; + private Function getQueryString; private Function getRequestUri; @@ -69,8 +79,8 @@ public class DispatcherServletInterceptor extends AbstractInterceptor { public DispatcherServletInterceptor() { configService = PluginServiceManager.getPluginService(SpringConfigService.class); handlers = new ArrayList<>(); - handlers.add(new LaneRequestTagHandler()); - handlers.add(new RouteRequestTagHandler()); + handlers.add(new LaneHandler()); + handlers.add(new TagHandler()); handlers.sort(Comparator.comparingInt(Handler::getOrder)); initFunction(); } @@ -85,11 +95,13 @@ public ExecuteContext before(ExecuteContext context) { } Object request = context.getArguments()[0]; Map> headers = getHeaders(request); - Map parameterMap = getParameterMap.apply(request); + String queryString = getQueryString.apply(request); + String decode = Optional.ofNullable(queryString).map(this::decode).orElse(StringUtils.EMPTY); + Map> queryParams = SpringRouterUtils.getParametersByQuery(decode); String path = getRequestUri.apply(request); String method = getMethod.apply(request); handlers.forEach(handler -> ThreadLocalUtils.addRequestTag( - handler.getRequestTag(path, method, headers, parameterMap, new Keys(matchKeys, injectTags)))); + handler.getRequestTag(path, method, headers, queryParams, new Keys(matchKeys, injectTags)))); return context; } @@ -107,6 +119,15 @@ public ExecuteContext onThrow(ExecuteContext context) { return context; } + private String decode(String str) { + try { + return URLDecoder.decode(str, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException ex) { + LOGGER.log(Level.SEVERE, "Cannot decode the string[{0}], ex is {1}.", new Object[]{str, ex.getMessage()}); + return StringUtils.EMPTY; + } + } + private Map> getHeaders(Object request) { Map> headers = new HashMap<>(); Enumeration enumeration = getHeaderNames.apply(request); @@ -131,13 +152,13 @@ private List enumeration2List(Enumeration enumeration) { private void initFunction() { boolean canLoadLowVersion = canLoadLowVersion(); if (canLoadLowVersion) { - getParameterMap = obj -> ((HttpServletRequest) obj).getParameterMap(); + getQueryString = obj -> ((HttpServletRequest) obj).getQueryString(); getRequestUri = obj -> ((HttpServletRequest) obj).getRequestURI(); getMethod = obj -> ((HttpServletRequest) obj).getMethod(); getHeaderNames = obj -> ((HttpServletRequest) obj).getHeaderNames(); getHeaders = (obj, key) -> ((HttpServletRequest) obj).getHeaders(key); } else { - getParameterMap = SpringRouterUtils::getParameterMap; + getQueryString = SpringRouterUtils::getQueryString; getRequestUri = SpringRouterUtils::getRequestUri; getMethod = SpringRouterUtils::getMethod; getHeaderNames = SpringRouterUtils::getHeaderNames; diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/service/LaneService.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/service/LaneService.java index 170318a457..e3e248a9bd 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/service/LaneService.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/service/LaneService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,18 +28,6 @@ * @since 2023-02-20 */ public interface LaneService extends PluginService { - /** - * Get swimlane markers - * - * @param path The path of the request - * @param methodName http method - * @param headers HTTP request headers - * @param parameters URL parameter - * @return Swimlane markers - */ - Map> getLaneByParameterArray(String path, String methodName, Map> headers, - Map parameters); - /** * Get swimlane markers * diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/utils/SpringRouterUtils.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/utils/SpringRouterUtils.java index 96b913591f..f873be355a 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/utils/SpringRouterUtils.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/io/sermant/router/spring/utils/SpringRouterUtils.java @@ -29,7 +29,9 @@ import org.springframework.cloud.client.DefaultServiceInstance; import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -46,19 +48,44 @@ public class SpringRouterUtils { private static final String ZONE_KEY = "zone"; + private static final String QUERY_SEPARATOR = "&"; + + private static final String KV_SEPARATOR = "="; + + private static final int KV_SPLIT_LENGTH = 2; + private static RouterConfig routerConfig = PluginConfigManager.getPluginConfig(RouterConfig.class); private SpringRouterUtils() { } /** - * get http parameter + * get parameters from query sting + * + * @param query query sting + * @return parameters + */ + public static Map> getParametersByQuery(String query) { + if (StringUtils.isBlank(query)) { + return Collections.emptyMap(); + } + String[] queryArr = query.split(QUERY_SEPARATOR); + Map> parameters = new HashMap<>(); + for (String kv : queryArr) { + String[] kvArr = kv.split(KV_SEPARATOR, KV_SPLIT_LENGTH); + parameters.computeIfAbsent(kvArr[0], value -> new ArrayList<>()).add(kvArr[1]); + } + return Collections.unmodifiableMap(parameters); + } + + /** + * get http query * * @param obj HttpServletRequest - * @return parameter + * @return query */ - public static Map getParameterMap(Object obj) { - return (Map) ReflectUtils.invokeWithNoneParameter(obj, "getParameterMap"); + public static String getQueryString(Object obj) { + return ReflectUtils.invokeWithNoneParameterAndReturnString(obj, "getQueryString"); } /** diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneMappingHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneHandlerTest.java similarity index 76% rename from sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneMappingHandlerTest.java rename to sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneHandlerTest.java index 6fb9d4e6c9..c21cd5682a 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneMappingHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneHandlerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,12 @@ package io.sermant.router.spring.handler; +import io.sermant.core.plugin.config.PluginConfigManager; import io.sermant.core.service.ServiceManager; +import io.sermant.router.common.config.RouterConfig; import io.sermant.router.spring.TestSpringConfigService; +import io.sermant.router.spring.entity.Keys; import io.sermant.router.spring.service.LaneService; -import io.sermant.router.spring.service.SpringConfigService; import org.junit.AfterClass; import org.junit.Assert; @@ -39,14 +41,16 @@ * @author provenceee * @since 2023-02-28 */ -public class LaneMappingHandlerTest { +public class LaneHandlerTest { private static MockedStatic mockServiceManager; + private static MockedStatic mockPluginConfigManager; + private static TestLaneService laneService; private static TestSpringConfigService configService; - private final LaneMappingHandler handler; + private final LaneHandler handler; /** * Perform mock before the UT is executed @@ -58,8 +62,9 @@ public static void before() { mockServiceManager.when(() -> ServiceManager.getService(LaneService.class)) .thenReturn(laneService); configService = new TestSpringConfigService(); - mockServiceManager.when(() -> ServiceManager.getService(SpringConfigService.class)) - .thenReturn(configService); + mockPluginConfigManager = Mockito.mockStatic(PluginConfigManager.class); + mockPluginConfigManager.when(() -> PluginConfigManager.getPluginConfig(RouterConfig.class)). + thenReturn(new RouterConfig()); } /** @@ -68,10 +73,11 @@ public static void before() { @AfterClass public static void after() { mockServiceManager.close(); + mockPluginConfigManager.close(); } - public LaneMappingHandlerTest() { - handler = new LaneMappingHandler(); + public LaneHandlerTest() { + handler = new LaneHandler(); } /** @@ -81,7 +87,8 @@ public LaneMappingHandlerTest() { public void testGetRequestTag() { // Test matchTags as null configService.setReturnEmptyWhenGetMatchTags(true); - Map> requestTag = handler.getRequestTag("", "", null, null); + Map> requestTag = handler.getRequestTag("", "", null, null, + new Keys(configService.getMatchKeys(), configService.getInjectTags())); Assert.assertEquals(requestTag, Collections.emptyMap()); // Test getLane returns null @@ -90,7 +97,8 @@ public void testGetRequestTag() { Map> headers = new HashMap<>(); headers.put("bar", Collections.singletonList("bar1")); headers.put("foo", Collections.singletonList("foo1")); - requestTag = handler.getRequestTag("", "", headers, null); + requestTag = handler.getRequestTag("", "", headers, null, + new Keys(configService.getMatchKeys(), configService.getInjectTags())); Assert.assertEquals(2, requestTag.size()); Assert.assertEquals("bar1", requestTag.get("bar").get(0)); Assert.assertEquals("foo1", requestTag.get("foo").get(0)); @@ -98,10 +106,11 @@ public void testGetRequestTag() { // Test getLane is not empty configService.setReturnEmptyWhenGetMatchTags(false); laneService.setReturnEmpty(false); - requestTag = handler.getRequestTag("", "", headers, null); + requestTag = handler.getRequestTag("", "", headers, null, + new Keys(configService.getMatchKeys(), configService.getInjectTags())); Assert.assertEquals(3, requestTag.size()); Assert.assertEquals("bar1", requestTag.get("bar").get(0)); Assert.assertEquals("foo1", requestTag.get("foo").get(0)); Assert.assertEquals("flag1", requestTag.get("sermant-flag").get(0)); } -} \ No newline at end of file +} diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneRequestTagHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneRequestTagHandlerTest.java deleted file mode 100644 index 22c8933aef..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/LaneRequestTagHandlerTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import io.sermant.core.service.ServiceManager; -import io.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; -import io.sermant.router.spring.service.LaneService; - -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.MockedStatic; -import org.mockito.Mockito; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Test LaneInterceptorHandler - * - * @author provenceee - * @since 2023-02-28 - */ -public class LaneRequestTagHandlerTest { - private static MockedStatic mockServiceManager; - - private static TestLaneService laneService; - - private final LaneRequestTagHandler handler; - - /** - * Perform mock before the UT is executed - */ - @BeforeClass - public static void before() { - mockServiceManager = Mockito.mockStatic(ServiceManager.class); - laneService = new TestLaneService(); - mockServiceManager.when(() -> ServiceManager.getService(LaneService.class)) - .thenReturn(laneService); - } - - /** - * Release the mock object after the UT is executed - */ - @AfterClass - public static void after() { - mockServiceManager.close(); - } - - public LaneRequestTagHandlerTest() { - handler = new LaneRequestTagHandler(); - } - - /** - * Test the getRequestTag method - */ - @Test - public void testGetRequestTag() { - // Test matchTags as null - Map> requestTag = handler.getRequestTag("", "", null, null, new Keys(null, null)); - Assert.assertEquals(requestTag, Collections.emptyMap()); - - // Test getLane returns null - laneService.setReturnEmpty(true); - Map> headers = new HashMap<>(); - headers.put("bar", Collections.singletonList("bar1")); - headers.put("foo", Collections.singletonList("foo1")); - Set matchTags = new HashSet<>(); - matchTags.add("bar"); - matchTags.add("foo"); - requestTag = handler.getRequestTag("", "", headers, null, new Keys(null, matchTags)); - Assert.assertEquals(2, requestTag.size()); - Assert.assertEquals("bar1", requestTag.get("bar").get(0)); - Assert.assertEquals("foo1", requestTag.get("foo").get(0)); - - // Test getLane is not empty - laneService.setReturnEmpty(false); - requestTag = handler.getRequestTag("", "", headers, null, new Keys(null, matchTags)); - Assert.assertEquals(3, requestTag.size()); - Assert.assertEquals("bar1", requestTag.get("bar").get(0)); - Assert.assertEquals("foo1", requestTag.get("foo").get(0)); - Assert.assertEquals("flag1", requestTag.get("sermant-flag").get(0)); - } -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteRequestTagHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteRequestTagHandlerTest.java deleted file mode 100644 index ca9f8614cd..0000000000 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteRequestTagHandlerTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.sermant.router.spring.handler; - -import io.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * Test RouteInterceptorHandler - * - * @author provenceee - * @since 2023-02-28 - */ -public class RouteRequestTagHandlerTest { - private final RouteRequestTagHandler handler; - - public RouteRequestTagHandlerTest() { - handler = new RouteRequestTagHandler(); - } - - /** - * Test the getRequestTag method - */ - @Test - public void testGetRequestTag() { - // Normal - Map> headers = new HashMap<>(); - Set matchKeys = new HashSet<>(); - matchKeys.add("bar"); - matchKeys.add("foo"); - headers.put("bar", Collections.singletonList("bar1")); - headers.put("foo", Collections.singletonList("foo1")); - Map> requestTag = handler.getRequestTag("", "", headers, null, new Keys(matchKeys, null)); - Assert.assertNotNull(requestTag); - Assert.assertEquals(2, requestTag.size()); - Assert.assertEquals("bar1", requestTag.get("bar").get(0)); - Assert.assertEquals("foo1", requestTag.get("foo").get(0)); - - // Test matchKeys as empty - requestTag = handler.getRequestTag("", "", null, null, new Keys(null, null)); - Assert.assertEquals(Collections.emptyMap(), requestTag); - } -} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteMappingHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TagHandlerTest.java similarity index 82% rename from sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteMappingHandlerTest.java rename to sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TagHandlerTest.java index eee68f9108..9212c238d8 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/RouteMappingHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TagHandlerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ import io.sermant.core.service.ServiceManager; import io.sermant.router.spring.TestSpringConfigService; -import io.sermant.router.spring.service.SpringConfigService; +import io.sermant.router.spring.entity.Keys; import org.junit.AfterClass; import org.junit.Assert; @@ -38,12 +38,12 @@ * @author provenceee * @since 2023-02-28 */ -public class RouteMappingHandlerTest { +public class TagHandlerTest { private static MockedStatic mockServiceManager; private static TestSpringConfigService configService; - private final RouteMappingHandler handler; + private final TagHandler handler; /** * Perform mock before the UT is executed @@ -52,8 +52,6 @@ public class RouteMappingHandlerTest { public static void before() { mockServiceManager = Mockito.mockStatic(ServiceManager.class); configService = new TestSpringConfigService(); - mockServiceManager.when(() -> ServiceManager.getService(SpringConfigService.class)) - .thenReturn(configService); } /** @@ -64,8 +62,8 @@ public static void after() { mockServiceManager.close(); } - public RouteMappingHandlerTest() { - handler = new RouteMappingHandler(); + public TagHandlerTest() { + handler = new TagHandler(); } /** @@ -78,7 +76,8 @@ public void testGetRequestTag() { Map> headers = new HashMap<>(); headers.put("bar", Collections.singletonList("bar1")); headers.put("foo", Collections.singletonList("foo1")); - Map> requestTag = handler.getRequestTag("", "", headers, null); + Map> requestTag = handler.getRequestTag("", "", headers, null, + new Keys(configService.getMatchKeys(), configService.getInjectTags())); Assert.assertNotNull(requestTag); Assert.assertEquals(2, requestTag.size()); Assert.assertEquals("bar1", requestTag.get("bar").get(0)); @@ -86,7 +85,8 @@ public void testGetRequestTag() { // Test getMatchKeys returns null configService.setReturnEmptyWhenGetMatchKeys(true); - requestTag = handler.getRequestTag("", "", null, null); + requestTag = handler.getRequestTag("", "", null, null, + new Keys(configService.getMatchKeys(), configService.getInjectTags())); Assert.assertEquals(Collections.emptyMap(), requestTag); } } \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TestLaneService.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TestLaneService.java index 563471836f..576846b0f3 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TestLaneService.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/handler/TestLaneService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,12 +32,6 @@ public class TestLaneService implements LaneService { private boolean returnEmpty; - @Override - public Map> getLaneByParameterArray(String path, String methodName, - Map> headers, Map parameters) { - return getLane(); - } - @Override public Map> getLaneByParameterList(String path, String methodName, Map> headers, Map> parameters) { diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/utils/SpringRouterUtilsTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/utils/SpringRouterUtilsTest.java new file mode 100644 index 0000000000..eef39d3d2c --- /dev/null +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/io/sermant/router/spring/utils/SpringRouterUtilsTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sermant.router.spring.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * SpringRouterUtils test + * + * @author provenceee + * @since 2024-11-20 + */ +public class SpringRouterUtilsTest { + /** + * test getParametersByQuery + */ + @Test + public void testGetParametersByQuery() { + String query = "a=b"; + Map> parametersByQuery = SpringRouterUtils.getParametersByQuery(query); + List list = parametersByQuery.get("a"); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("b", list.get(0)); + + query = "a=b&a=c"; + parametersByQuery = SpringRouterUtils.getParametersByQuery(query); + list = parametersByQuery.get("a"); + Assert.assertEquals(2, list.size()); + Assert.assertEquals("b", list.get(0)); + Assert.assertEquals("c", list.get(1)); + + query = "a=b&b=&a=c"; + parametersByQuery = SpringRouterUtils.getParametersByQuery(query); + list = parametersByQuery.get("a"); + Assert.assertEquals(2, list.size()); + Assert.assertEquals("b", list.get(0)); + Assert.assertEquals("c", list.get(1)); + list = parametersByQuery.get("b"); + Assert.assertEquals(1, list.size()); + Assert.assertEquals("", list.get(0)); + + Assert.assertEquals(Collections.emptyMap(), SpringRouterUtils.getParametersByQuery(null)); + } +} diff --git a/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/service/LaneServiceImpl.java b/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/service/LaneServiceImpl.java index 98f4e5d5e9..76c7d29dcc 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/service/LaneServiceImpl.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/service/LaneServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,14 +37,6 @@ * @since 2023-02-20 */ public class LaneServiceImpl implements LaneService { - @Override - public Map> getLaneByParameterArray(String path, String methodName, - Map> headers, Map parameters) { - List rules = getRules(path, methodName); - List routes = RouteUtils.getLaneRoutesByParameterArray(rules, headers, parameters); - return RuleUtils.getTargetLaneTags(routes); - } - @Override public Map> getLaneByParameterList(String path, String methodName, Map> headers, Map> parameters) { diff --git a/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/utils/RouteUtils.java b/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/utils/RouteUtils.java index a038ea827d..11a41fa696 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/utils/RouteUtils.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/main/java/io/sermant/router/spring/utils/RouteUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2021-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,29 +39,6 @@ public class RouteUtils { private RouteUtils() { } - /** - * Get matching swimlanes - * - * @param list Valid rules - * @param headers header - * @param parameters parameters - * @return Matching swimlane markers - */ - public static List getLaneRoutesByParameterArray(List list, Map> headers, - Map parameters) { - for (Rule rule : list) { - Match match = rule.getMatch(); - if (match == null) { - return rule.getRoute(); - } - if (isMatchByHeaders(match.getHeaders(), headers) && isMatchByParameterArray(match.getParameters(), - parameters)) { - return rule.getRoute(); - } - } - return Collections.emptyList(); - } - /** * Get matching swimlanes * @@ -108,29 +85,6 @@ private static boolean isMatchByHeaders(Map> matchHeader return true; } - private static boolean isMatchByParameterArray(Map> matchParameters, - Map parameters) { - if (CollectionUtils.isEmpty(matchParameters)) { - return true; - } - for (Entry> entry : matchParameters.entrySet()) { - String key = entry.getKey(); - List matchRuleList = entry.getValue(); - for (MatchRule matchRule : matchRuleList) { - ValueMatch valueMatch = matchRule.getValueMatch(); - List values = valueMatch.getValues(); - MatchStrategy matchStrategy = valueMatch.getMatchStrategy(); - String[] arr = parameters.get(key); - String arg = (arr == null || arr.length == 0) ? null : arr[0]; - if (!matchStrategy.isMatch(values, arg, matchRule.isCaseInsensitive())) { - // As long as one of them doesn't match, it's a mismatch - return false; - } - } - } - return true; - } - private static boolean isMatchByParameterList(Map> matchParameters, Map> parameters) { if (CollectionUtils.isEmpty(matchParameters)) { diff --git a/sermant-plugins/sermant-router/spring-router-service/src/test/java/io/sermant/router/spring/service/LaneServiceTest.java b/sermant-plugins/sermant-router/spring-router-service/src/test/java/io/sermant/router/spring/service/LaneServiceTest.java index ab3b4e1adc..12c9c53bf7 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/test/java/io/sermant/router/spring/service/LaneServiceTest.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/test/java/io/sermant/router/spring/service/LaneServiceTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (C) 2023-2024 Huawei Technologies Co., Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,10 +63,7 @@ public void clear() { */ @Test public void testWithInvalidConfiguration() { - Map> lane = service.getLaneByParameterArray("", "", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); - - lane = service.getLaneByParameterList("", "", Collections.emptyMap(), null); + Map> lane = service.getLaneByParameterList("", "", Collections.emptyMap(), null); Assert.assertEquals(Collections.emptyMap(), lane); } @@ -76,10 +73,7 @@ public void testWithInvalidConfiguration() { @Test public void testWithEmptyRules() { initRules(); - Map> lane = service.getLaneByParameterArray("", "", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); - - lane = service.getLaneByParameterList("", "", Collections.emptyMap(), null); + Map> lane = service.getLaneByParameterList("", "", Collections.emptyMap(), null); Assert.assertEquals(Collections.emptyMap(), lane); } @@ -90,10 +84,6 @@ public void testWithEmptyRules() { public void testWithEmptyRoutes() { initRules(); Map> lane = service - .getLaneByParameterArray("/foo/test", "get", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); - - lane = service .getLaneByParameterList("/foo/test", "get", Collections.emptyMap(), null); Assert.assertEquals(Collections.emptyMap(), lane); } @@ -105,13 +95,6 @@ public void testWithEmptyRoutes() { public void testGetLane() { initRules(); Map> lane = service - .getLaneByParameterArray("/foo/test", "get", - Collections.singletonMap("bar", Collections.singletonList("bar1")), - Collections.singletonMap("foo", new String[]{"foo1"})); - Assert.assertEquals(1, lane.size()); - Assert.assertEquals("flag1", lane.get("sermant-flag").get(0)); - - lane = service .getLaneByParameterList("/foo/test", "get", Collections.singletonMap("bar", Collections.singletonList("bar1")), Collections.singletonMap("foo", Collections.singletonList("foo1")));