diff --git a/sermant-plugins/sermant-router/spring-router-plugin/pom.xml b/sermant-plugins/sermant-router/spring-router-plugin/pom.xml index cd96b96ac2..62f26ba137 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/pom.xml +++ b/sermant-plugins/sermant-router/spring-router-plugin/pom.xml @@ -73,12 +73,6 @@ ${spring.version} provided - - org.springframework - spring-webflux - ${spring.version} - provided - io.projectreactor.netty reactor-netty diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractMappingHandler.java index debb05fcb5..e7c51baa05 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractMappingHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractMappingHandler.java @@ -16,11 +16,9 @@ package com.huaweicloud.sermant.router.spring.handler; -import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; -import com.huaweicloud.sermant.router.spring.service.SpringConfigService; - import java.util.List; import java.util.Map; +import java.util.Set; /** * AbstractHandlerMapping处理器 @@ -29,18 +27,6 @@ * @since 2023-02-21 */ public abstract class AbstractMappingHandler extends AbstractHandler { - /** - * 配置服务 - */ - protected final SpringConfigService configService; - - /** - * 构造方法 - */ - public AbstractMappingHandler() { - configService = PluginServiceManager.getPluginService(SpringConfigService.class); - } - /** * 获取透传的标记 * @@ -48,8 +34,40 @@ public AbstractMappingHandler() { * @param methodName http方法 * @param headers http请求头 * @param parameters url参数 + * @param keys 透传标记key * @return 透传的标记 */ public abstract Map> getRequestTag(String path, String methodName, - Map> headers, Map> parameters); + Map> headers, Map> parameters, Keys keys); + + /** + * 透传标记key实体 + * + * @author provenceee + * @since 2023-02-21 + */ + public static class Keys { + private final Set matchKeys; + + private final Set injectTags; + + /** + * 构造方法 + * + * @param matchKeys 标签路由透传标记 + * @param injectTags 泳道透传标记 + */ + 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/com/huaweicloud/sermant/router/spring/handler/AbstractRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractRequestTagHandler.java index dfa37e2a71..f4c8516309 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractRequestTagHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/AbstractRequestTagHandler.java @@ -1,73 +1,73 @@ -/* - * 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 com.huaweicloud.sermant.router.spring.handler; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * web拦截器处理器 - * - * @author provenceee - * @since 2023-02-21 - */ -public abstract class AbstractRequestTagHandler extends AbstractHandler { - /** - * 获取透传的标记 - * - * @param path 请求路径 - * @param methodName http方法 - * @param headers http请求头 - * @param parameters url参数 - * @param keys 透传标记key - * @return 透传的标记 - */ - public abstract Map> getRequestTag(String path, String methodName, - Map> headers, Map parameters, Keys keys); - - /** - * 透传标记key实体 - * - * @author provenceee - * @since 2023-02-21 - */ - public static class Keys { - private final Set matchKeys; - - private final Set injectTags; - - /** - * 构造方法 - * - * @param matchKeys 标签路由透传标记 - * @param injectTags 泳道透传标记 - */ - 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 +///* +// * 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 com.huaweicloud.sermant.router.spring.handler; +// +//import java.util.List; +//import java.util.Map; +//import java.util.Set; +// +///** +// * web拦截器处理器 +// * +// * @author provenceee +// * @since 2023-02-21 +// */ +//public abstract class AbstractRequestTagHandler extends AbstractHandler { +// /** +// * 获取透传的标记 +// * +// * @param path 请求路径 +// * @param methodName http方法 +// * @param headers http请求头 +// * @param parameters url参数 +// * @param keys 透传标记key +// * @return 透传的标记 +// */ +// public abstract Map> getRequestTag(String path, String methodName, +// Map> headers, Map parameters, Keys keys); +// +// /** +// * 透传标记key实体 +// * +// * @author provenceee +// * @since 2023-02-21 +// */ +// public static class Keys { +// private final Set matchKeys; +// +// private final Set injectTags; +// +// /** +// * 构造方法 +// * +// * @param matchKeys 标签路由透传标记 +// * @param injectTags 泳道透传标记 +// */ +// 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/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandler.java index 984163e3f5..d27271e4a1 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandler.java @@ -58,8 +58,8 @@ public LaneMappingHandler() { */ @Override public Map> getRequestTag(String path, String methodName, Map> headers, - Map> parameters) { - Set injectTags = configService.getInjectTags(); + Map> parameters, Keys keys) { + Set injectTags = keys.getInjectTags(); if (CollectionUtils.isEmpty(injectTags)) { // 染色标记为空,代表没有染色规则,直接return LOGGER.fine("Lane tags are empty."); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandler.java index 6ee7215da8..e075f80b94 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandler.java @@ -1,90 +1,90 @@ -/* - * 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 com.huaweicloud.sermant.router.spring.handler; - -import com.huaweicloud.sermant.core.common.LoggerFactory; -import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; -import com.huaweicloud.sermant.router.common.constants.RouterConstant; -import com.huaweicloud.sermant.router.common.utils.CollectionUtils; -import com.huaweicloud.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; - -/** - * 染色web拦截器处理器 - * - * @author provenceee - * @since 2023-02-21 - */ -public class LaneRequestTagHandler extends AbstractRequestTagHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - private final LaneService laneService; - - /** - * 构造方法 - */ - public LaneRequestTagHandler() { - laneService = PluginServiceManager.getPluginService(LaneService.class); - } - - /** - * 获取透传的标记 - * - * @param path 请求路径 - * @param methodName 方法名称 - * @param headers http请求头 - * @return 透传的标记 - */ - @Override - public Map> getRequestTag(String path, String methodName, Map> headers, - Map parameters, Keys keys) { - Set injectTags = keys.getInjectTags(); - if (CollectionUtils.isEmpty(injectTags)) { - // 染色标记为空,代表没有染色规则,直接return - LOGGER.fine("Lane tags are empty."); - return Collections.emptyMap(); - } - - // 上游透传的标记 - Map> tags = getRequestTag(headers, injectTags); - - // 本次染色标记 - Map> laneTag = laneService.getLaneByParameterArray(path, methodName, headers, parameters); - if (CollectionUtils.isEmpty(laneTag)) { - LOGGER.fine("Lane is empty."); - return tags; - } - - // 如果上游传来的标记中,存在与本次染色相同的标记,以上游传递的为准 - 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 +///* +// * 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 com.huaweicloud.sermant.router.spring.handler; +// +//import com.huaweicloud.sermant.core.common.LoggerFactory; +//import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +//import com.huaweicloud.sermant.router.common.constants.RouterConstant; +//import com.huaweicloud.sermant.router.common.utils.CollectionUtils; +//import com.huaweicloud.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; +// +///** +// * 染色web拦截器处理器 +// * +// * @author provenceee +// * @since 2023-02-21 +// */ +//public class LaneRequestTagHandler extends AbstractRequestTagHandler { +// private static final Logger LOGGER = LoggerFactory.getLogger(); +// +// private final LaneService laneService; +// +// /** +// * 构造方法 +// */ +// public LaneRequestTagHandler() { +// laneService = PluginServiceManager.getPluginService(LaneService.class); +// } +// +// /** +// * 获取透传的标记 +// * +// * @param path 请求路径 +// * @param methodName 方法名称 +// * @param headers http请求头 +// * @return 透传的标记 +// */ +// @Override +// public Map> getRequestTag(String path, String methodName, Map> headers, +// Map parameters, Keys keys) { +// Set injectTags = keys.getInjectTags(); +// if (CollectionUtils.isEmpty(injectTags)) { +// // 染色标记为空,代表没有染色规则,直接return +// LOGGER.fine("Lane tags are empty."); +// return Collections.emptyMap(); +// } +// +// // 上游透传的标记 +// Map> tags = getRequestTag(headers, injectTags); +// +// // 本次染色标记 +// Map> laneTag = laneService.getLaneByParameterArray(path, methodName, headers, parameters); +// if (CollectionUtils.isEmpty(laneTag)) { +// LOGGER.fine("Lane is empty."); +// return tags; +// } +// +// // 如果上游传来的标记中,存在与本次染色相同的标记,以上游传递的为准 +// 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/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandler.java index 27d847f04b..145b94f051 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandler.java @@ -40,8 +40,8 @@ public class RouteMappingHandler extends AbstractMappingHandler { */ @Override public Map> getRequestTag(String path, String methodName, Map> headers, - Map> parameters) { - Set matchKeys = configService.getMatchKeys(); + Map> parameters, Keys keys) { + Set matchKeys = keys.getMatchKeys(); return getRequestTag(headers, matchKeys); } diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandler.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandler.java index fefae5ba89..a337a3e237 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandler.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandler.java @@ -1,51 +1,51 @@ -/* - * 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 com.huaweicloud.sermant.router.spring.handler; - -import com.huaweicloud.sermant.router.common.constants.RouterConstant; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * 路由web拦截器处理器 - * - * @author provenceee - * @since 2023-02-21 - */ -public class RouteRequestTagHandler extends AbstractRequestTagHandler { - /** - * 获取透传的标记 - * - * @param path 请求路径 - * @param methodName 方法名称 - * @param headers http请求头 - * @return 透传的标记 - */ - @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 +///* +// * 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 com.huaweicloud.sermant.router.spring.handler; +// +//import com.huaweicloud.sermant.router.common.constants.RouterConstant; +// +//import java.util.List; +//import java.util.Map; +//import java.util.Set; +// +///** +// * 路由web拦截器处理器 +// * +// * @author provenceee +// * @since 2023-02-21 +// */ +//public class RouteRequestTagHandler extends AbstractRequestTagHandler { +// /** +// * 获取透传的标记 +// * +// * @param path 请求路径 +// * @param methodName 方法名称 +// * @param headers http请求头 +// * @return 透传的标记 +// */ +// @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/com/huaweicloud/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java index 98283f90f8..1db0b96846 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/AbstractHandlerMappingInterceptor.java @@ -18,11 +18,16 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; import com.huaweicloud.sermant.router.common.handler.Handler; +import com.huaweicloud.sermant.router.common.utils.CollectionUtils; import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils; import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler; +import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler.Keys; import com.huaweicloud.sermant.router.spring.handler.LaneMappingHandler; import com.huaweicloud.sermant.router.spring.handler.RouteMappingHandler; +import com.huaweicloud.sermant.router.spring.service.SpringConfigService; +import com.huaweicloud.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获取header拦截点 @@ -43,12 +49,15 @@ public class AbstractHandlerMappingInterceptor extends AbstractInterceptor { private static final String EXCEPT_CLASS_NAME = "org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping"; + private final SpringConfigService configService; + private final List handlers; /** * 构造方法 */ public AbstractHandlerMappingInterceptor() { + configService = PluginServiceManager.getPluginService(SpringConfigService.class); handlers = new ArrayList<>(); handlers.add(new LaneMappingHandler()); handlers.add(new RouteMappingHandler()); @@ -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)) { + // 染色标记为空,代表没有染色规则,直接return + 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/com/huaweicloud/sermant/router/spring/interceptor/DispatcherServletInterceptor.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/DispatcherServletInterceptor.java index e844c40ed2..24bfdc7a48 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/DispatcherServletInterceptor.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/DispatcherServletInterceptor.java @@ -19,16 +19,20 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; +import com.huaweicloud.sermant.core.utils.StringUtils; import com.huaweicloud.sermant.router.common.handler.Handler; import com.huaweicloud.sermant.router.common.utils.CollectionUtils; import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils; -import com.huaweicloud.sermant.router.spring.handler.AbstractRequestTagHandler; -import com.huaweicloud.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; -import com.huaweicloud.sermant.router.spring.handler.LaneRequestTagHandler; -import com.huaweicloud.sermant.router.spring.handler.RouteRequestTagHandler; +import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler; +import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler.Keys; +import com.huaweicloud.sermant.router.spring.handler.LaneMappingHandler; +import com.huaweicloud.sermant.router.spring.handler.RouteMappingHandler; import com.huaweicloud.sermant.router.spring.service.SpringConfigService; import com.huaweicloud.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,6 +40,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; /** @@ -45,7 +50,7 @@ * @since 2022-07-12 */ public class DispatcherServletInterceptor extends AbstractInterceptor { - private final List handlers; + private final List handlers; private final SpringConfigService configService; @@ -55,8 +60,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 LaneMappingHandler()); + handlers.add(new RouteMappingHandler()); handlers.sort(Comparator.comparingInt(Handler::getOrder)); } @@ -70,11 +75,13 @@ public ExecuteContext before(ExecuteContext context) { } Object request = context.getArguments()[0]; Map> headers = getHeaders(request); - Map parameterMap = SpringRouterUtils.getParameterMap(request); + String queryString = SpringRouterUtils.getQueryString(request); + String decode = Optional.ofNullable(queryString).map(this::decode).orElse(StringUtils.EMPTY); + Map> queryParams = SpringRouterUtils.getParametersByQuery(decode); String path = SpringRouterUtils.getRequestUri(request); String method = SpringRouterUtils.getMethod(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; } @@ -92,6 +99,14 @@ public ExecuteContext onThrow(ExecuteContext context) { return context; } + private String decode(String str) { + try { + return URLDecoder.decode(str, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + return StringUtils.EMPTY; + } + } + private Map> getHeaders(Object request) { Map> headers = new HashMap<>(); Enumeration enumeration = SpringRouterUtils.getHeaderNames(request); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneService.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneService.java index 4f4040a124..d2d6be90ff 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneService.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneService.java @@ -28,17 +28,17 @@ * @since 2023-02-20 */ public interface LaneService extends PluginService { - /** - * 获取泳道标记 - * - * @param path 请求路径 - * @param methodName http方法 - * @param headers http请求头 - * @param parameters url参数 - * @return 泳道标记 - */ - Map> getLaneByParameterArray(String path, String methodName, Map> headers, - Map parameters); +// /** +// * 获取泳道标记 +// * +// * @param path 请求路径 +// * @param methodName http方法 +// * @param headers http请求头 +// * @param parameters url参数 +// * @return 泳道标记 +// */ +// Map> getLaneByParameterArray(String path, String methodName, Map> headers, +// Map parameters); /** * 获取泳道标记 diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtils.java b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtils.java index e5e1b75aac..f237e327e2 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtils.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtils.java @@ -22,7 +22,11 @@ import com.huaweicloud.sermant.router.common.utils.ReflectUtils; import com.huaweicloud.sermant.router.spring.cache.AppCache; +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; @@ -37,17 +41,42 @@ 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 SpringRouterUtils() { } /** - * 获取请求中的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); + } + + /** + * 获取请求中的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/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandlerTest.java index 7df0b6cfb4..401b9c3d38 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneMappingHandlerTest.java @@ -18,8 +18,8 @@ import com.huaweicloud.sermant.core.service.ServiceManager; import com.huaweicloud.sermant.router.spring.TestSpringConfigService; +import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler.Keys; import com.huaweicloud.sermant.router.spring.service.LaneService; -import com.huaweicloud.sermant.router.spring.service.SpringConfigService; import org.junit.AfterClass; import org.junit.Assert; @@ -58,8 +58,6 @@ public static void before() { mockServiceManager.when(() -> ServiceManager.getService(LaneService.class)) .thenReturn(laneService); configService = new TestSpringConfigService(); - mockServiceManager.when(() -> ServiceManager.getService(SpringConfigService.class)) - .thenReturn(configService); } /** @@ -81,7 +79,8 @@ public LaneMappingHandlerTest() { public void testGetRequestTag() { // 测试matchTags为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()); // 测试getLane返回空 @@ -90,7 +89,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,7 +98,8 @@ public void testGetRequestTag() { // 测试getLane不为空 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)); diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandlerTest.java index b0d380f3e9..58f3a9fa68 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/LaneRequestTagHandlerTest.java @@ -1,103 +1,103 @@ -/* - * 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 com.huaweicloud.sermant.router.spring.handler; - -import com.huaweicloud.sermant.core.service.ServiceManager; -import com.huaweicloud.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; -import com.huaweicloud.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; - -/** - * 测试LaneInterceptorHandler - * - * @author provenceee - * @since 2023-02-28 - */ -public class LaneRequestTagHandlerTest { - private static MockedStatic mockServiceManager; - - private static TestLaneService laneService; - - private final LaneRequestTagHandler handler; - - /** - * UT执行前进行mock - */ - @BeforeClass - public static void before() { - mockServiceManager = Mockito.mockStatic(ServiceManager.class); - laneService = new TestLaneService(); - mockServiceManager.when(() -> ServiceManager.getService(LaneService.class)) - .thenReturn(laneService); - } - - /** - * UT执行后释放mock对象 - */ - @AfterClass - public static void after() { - mockServiceManager.close(); - } - - public LaneRequestTagHandlerTest() { - handler = new LaneRequestTagHandler(); - } - - /** - * 测试getRequestTag方法 - */ - @Test - public void testGetRequestTag() { - // 测试matchTags为null - Map> requestTag = handler.getRequestTag("", "", null, null, new Keys(null, null)); - Assert.assertEquals(requestTag, Collections.emptyMap()); - - // 测试getLane返回空 - 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)); - - // 测试getLane不为空 - 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 +///* +// * 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 com.huaweicloud.sermant.router.spring.handler; +// +//import com.huaweicloud.sermant.core.service.ServiceManager; +//import com.huaweicloud.sermant.router.spring.handler.AbstractRequestTagHandler.Keys; +//import com.huaweicloud.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; +// +///** +// * 测试LaneInterceptorHandler +// * +// * @author provenceee +// * @since 2023-02-28 +// */ +//public class LaneRequestTagHandlerTest { +// private static MockedStatic mockServiceManager; +// +// private static TestLaneService laneService; +// +// private final LaneRequestTagHandler handler; +// +// /** +// * UT执行前进行mock +// */ +// @BeforeClass +// public static void before() { +// mockServiceManager = Mockito.mockStatic(ServiceManager.class); +// laneService = new TestLaneService(); +// mockServiceManager.when(() -> ServiceManager.getService(LaneService.class)) +// .thenReturn(laneService); +// } +// +// /** +// * UT执行后释放mock对象 +// */ +// @AfterClass +// public static void after() { +// mockServiceManager.close(); +// } +// +// public LaneRequestTagHandlerTest() { +// handler = new LaneRequestTagHandler(); +// } +// +// /** +// * 测试getRequestTag方法 +// */ +// @Test +// public void testGetRequestTag() { +// // 测试matchTags为null +// Map> requestTag = handler.getRequestTag("", "", null, null, new Keys(null, null)); +// Assert.assertEquals(requestTag, Collections.emptyMap()); +// +// // 测试getLane返回空 +// 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)); +// +// // 测试getLane不为空 +// 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/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandlerTest.java index 16de292679..68469bbd34 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteMappingHandlerTest.java @@ -18,7 +18,7 @@ import com.huaweicloud.sermant.core.service.ServiceManager; import com.huaweicloud.sermant.router.spring.TestSpringConfigService; -import com.huaweicloud.sermant.router.spring.service.SpringConfigService; +import com.huaweicloud.sermant.router.spring.handler.AbstractMappingHandler.Keys; import org.junit.AfterClass; import org.junit.Assert; @@ -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); } /** @@ -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() { // 测试getMatchKeys返回空 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/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandlerTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandlerTest.java index a733619fd4..3f021f48b9 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandlerTest.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/RouteRequestTagHandlerTest.java @@ -1,66 +1,66 @@ -/* - * 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 com.huaweicloud.sermant.router.spring.handler; - -import com.huaweicloud.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; - -/** - * 测试RouteInterceptorHandler - * - * @author provenceee - * @since 2023-02-28 - */ -public class RouteRequestTagHandlerTest { - private final RouteRequestTagHandler handler; - - public RouteRequestTagHandlerTest() { - handler = new RouteRequestTagHandler(); - } - - /** - * 测试getRequestTag方法 - */ - @Test - public void testGetRequestTag() { - // 正常情况 - 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)); - - // 测试matchKeys为空 - requestTag = handler.getRequestTag("", "", null, null, new Keys(null, null)); - Assert.assertEquals(Collections.emptyMap(), requestTag); - } -} \ No newline at end of file +///* +// * 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 com.huaweicloud.sermant.router.spring.handler; +// +//import com.huaweicloud.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; +// +///** +// * 测试RouteInterceptorHandler +// * +// * @author provenceee +// * @since 2023-02-28 +// */ +//public class RouteRequestTagHandlerTest { +// private final RouteRequestTagHandler handler; +// +// public RouteRequestTagHandlerTest() { +// handler = new RouteRequestTagHandler(); +// } +// +// /** +// * 测试getRequestTag方法 +// */ +// @Test +// public void testGetRequestTag() { +// // 正常情况 +// 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)); +// +// // 测试matchKeys为空 +// 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/com/huaweicloud/sermant/router/spring/handler/TestLaneService.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/TestLaneService.java index eef4b68f01..649a1b5872 100644 --- a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/TestLaneService.java +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/handler/TestLaneService.java @@ -32,11 +32,11 @@ 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> getLaneByParameterArray(String path, String methodName, +// Map> headers, Map parameters) { +// return getLane(); +// } @Override public Map> getLaneByParameterList(String path, String methodName, diff --git a/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtilsTest.java b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtilsTest.java new file mode 100644 index 0000000000..0355419f31 --- /dev/null +++ b/sermant-plugins/sermant-router/spring-router-plugin/src/test/java/com/huaweicloud/sermant/router/spring/utils/SpringRouterUtilsTest.java @@ -0,0 +1,62 @@ +/* + * 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 com.huaweicloud.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-14 + */ +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)); + } +} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneServiceImpl.java b/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneServiceImpl.java index 3c6368db90..8152ca8b9c 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneServiceImpl.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/service/LaneServiceImpl.java @@ -37,13 +37,13 @@ * @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> 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, diff --git a/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/utils/RouteUtils.java b/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/utils/RouteUtils.java index b20cfffd2e..dbd7f207ac 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/utils/RouteUtils.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/main/java/com/huaweicloud/sermant/router/spring/utils/RouteUtils.java @@ -47,20 +47,20 @@ private RouteUtils() { * @param parameters parameters * @return 匹配的泳道标记 */ - 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(); - } +// 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(); +// } /** * 获取匹配的泳道 @@ -108,28 +108,28 @@ 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())) { - // 只要一个匹配不上,那就是不匹配 - return false; - } - } - } - 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())) { +// // 只要一个匹配不上,那就是不匹配 +// return false; +// } +// } +// } +// return true; +// } private static boolean isMatchByParameterList(Map> matchParameters, Map> parameters) { diff --git a/sermant-plugins/sermant-router/spring-router-service/src/test/java/com/huaweicloud/sermant/router/spring/service/LaneServiceTest.java b/sermant-plugins/sermant-router/spring-router-service/src/test/java/com/huaweicloud/sermant/router/spring/service/LaneServiceTest.java index 1f32c9c048..ec93132513 100644 --- a/sermant-plugins/sermant-router/spring-router-service/src/test/java/com/huaweicloud/sermant/router/spring/service/LaneServiceTest.java +++ b/sermant-plugins/sermant-router/spring-router-service/src/test/java/com/huaweicloud/sermant/router/spring/service/LaneServiceTest.java @@ -63,10 +63,10 @@ public void clear() { */ @Test public void testWithInvalidConfiguration() { - Map> lane = service.getLaneByParameterArray("", "", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); +// 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 +76,10 @@ public void testWithInvalidConfiguration() { @Test public void testWithEmptyRules() { initRules(); - Map> lane = service.getLaneByParameterArray("", "", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); +// 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); } @@ -89,11 +89,11 @@ public void testWithEmptyRules() { @Test public void testWithEmptyRoutes() { initRules(); - Map> lane = service - .getLaneByParameterArray("/foo/test", "get", Collections.emptyMap(), null); - Assert.assertEquals(Collections.emptyMap(), lane); +// Map> lane = service +// .getLaneByParameterArray("/foo/test", "get", Collections.emptyMap(), null); +// Assert.assertEquals(Collections.emptyMap(), lane); - lane = service + Map> lane = service .getLaneByParameterList("/foo/test", "get", Collections.emptyMap(), null); Assert.assertEquals(Collections.emptyMap(), lane); } @@ -104,14 +104,14 @@ public void testWithEmptyRoutes() { @Test 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)); +// 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 + Map> lane = service .getLaneByParameterList("/foo/test", "get", Collections.singletonMap("bar", Collections.singletonList("bar1")), Collections.singletonMap("foo", Collections.singletonList("foo1"))); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/grace/declarers/DispatcherServletDeclarer.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/grace/declarers/DispatcherServletDeclarer.java index 38a395ea5b..46ba38c2a9 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/grace/declarers/DispatcherServletDeclarer.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/grace/declarers/DispatcherServletDeclarer.java @@ -49,7 +49,7 @@ public ClassMatcher getClassMatcher() { @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{ - InterceptDeclarer.build(MethodMatcher.nameEquals("applyPreHandle"), INTERCEPT_CLASS) + InterceptDeclarer.build(MethodMatcher.nameEquals("doService"), INTERCEPT_CLASS) }; } }