From ba8e0e2dffd5c6cd8bbacd0c62f0a09eb52bb41b Mon Sep 17 00:00:00 2001 From: Sunrisea <49605583+Sunrisea@users.noreply.github.com> Date: Fri, 7 Jul 2023 11:06:00 +0800 Subject: [PATCH] [ISSUE #10734] Implemented the parameter validation utility class (#10737) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * For #10734,Refactor the AbstractConfigRequest ,move common properties from its subclasses to the parent class,to reduce the number of corresponding parameter extractors by . * For #10734,add ParamCheckRules,ParamCheckUtils, add ParamExtractor interface and the abstract classes HttpParamExtractor and RpcParamExtractor, implement the corresponding Manager. * For #10734,fix codestyle * For #10734,fix codestyle , move extractor and extractorManager to core directory * For #10734,fix codestyle * For #10734,fix dependency of common module * For #10734,fix codestyle and copyright * For #10734,fix pom codestyle and copyright * For #10734,fix pom codestyle and copyright * For #10734,fix copyright * For #10734,fix copyright * For #10734,fix bug caused by refactor of AbstractConfigRequest ,add ut test of ParamExtractorManager * For #10734,fix bug caused by refactor of AbstractConfigRequest --------- Co-authored-by: zhuoguang --- .../remote/request/AbstractConfigRequest.java | 35 ++- .../request/ConfigBatchListenRequest.java | 2 +- .../remote/request/ConfigPublishRequest.java | 75 +---- .../remote/request/ConfigQueryRequest.java | 62 +--- .../remote/request/ConfigRemoveRequest.java | 67 +--- .../ConfigChangeClusterSyncRequest.java | 62 +--- .../parser/grpc/ConfigGrpcResourceParser.java | 25 +- .../parser/grpc/NamingGrpcResourceParser.java | 2 +- common/pom.xml | 4 +- .../common/paramcheck/ParamCheckRules.java | 59 ++++ .../common/paramcheck/ParamCheckUtils.java | 247 +++++++++++++++ .../nacos/common/paramcheck/ParamInfo.java | 116 +++++++ .../paramcheck/ParamCheckUtilsTest.java | 290 ++++++++++++++++++ .../AbstractHttpParamExtractor.java | 81 +++++ .../paramcheck/AbstractRpcParamExtractor.java | 60 ++++ .../paramcheck/HttpParamExtractorManager.java | 72 +++++ .../nacos/core/paramcheck/ParamExtractor.java | 43 +++ .../paramcheck/RpcParamExtractorManager.java | 70 +++++ .../HttpParamExtractorManagerTest.java | 48 +++ .../RpcParamExtractorManagerTest.java | 45 +++ 20 files changed, 1201 insertions(+), 264 deletions(-) create mode 100644 common/src/main/java/com/alibaba/nacos/common/paramcheck/ParamCheckRules.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/paramcheck/ParamCheckUtils.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/paramcheck/ParamInfo.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/paramcheck/ParamCheckUtilsTest.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/AbstractHttpParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/AbstractRpcParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/HttpParamExtractorManager.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/ParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManager.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/HttpParamExtractorManagerTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManagerTest.java diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/AbstractConfigRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/AbstractConfigRequest.java index 0d3ae1fc5..c821aa598 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/AbstractConfigRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/AbstractConfigRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,11 +21,42 @@ import com.alibaba.nacos.api.remote.request.Request; /** * abstract request of config module request,all config module request should extends this class. + * * @author liuzunfei * @version $Id: ConfigCommonRequest.java, v 0.1 2020年07月13日 9:05 PM liuzunfei Exp $ */ public abstract class AbstractConfigRequest extends Request { - + + private String dataId; + + private String group; + + private String tenant; + + public String getDataId() { + return dataId; + } + + public void setDataId(String dataId) { + this.dataId = dataId; + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public String getTenant() { + return tenant; + } + + public void setTenant(String tenant) { + this.tenant = tenant; + } + @Override public String getModule() { return Constants.Config.CONFIG_MODULE; diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java index f01e3b320..1a5c22bac 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigBatchListenRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigPublishRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigPublishRequest.java index f72beb815..f7104add5 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigPublishRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigPublishRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,12 +27,6 @@ import java.util.Map; */ public class ConfigPublishRequest extends AbstractConfigRequest { - String dataId; - - String group; - - String tenant; - String content; String casMd5; @@ -43,6 +37,13 @@ public class ConfigPublishRequest extends AbstractConfigRequest { } + public ConfigPublishRequest(String dataId, String group, String tenant, String content) { + this.content = content; + super.setGroup(group); + super.setTenant(tenant); + super.setDataId(dataId); + } + /** * get additional param. * @@ -66,49 +67,6 @@ public class ConfigPublishRequest extends AbstractConfigRequest { additionMap.put(key, value); } - public ConfigPublishRequest(String dataId, String group, String tenant, String content) { - this.content = content; - this.dataId = dataId; - this.group = group; - this.tenant = tenant; - } - - /** - * Getter method for property dataId. - * - * @return property value of dataId - */ - public String getDataId() { - return dataId; - } - - /** - * Setter method for property dataId. - * - * @param dataId value to be assigned to property dataId - */ - public void setDataId(String dataId) { - this.dataId = dataId; - } - - /** - * Getter method for property group. - * - * @return property value of group - */ - public String getGroup() { - return group; - } - - /** - * Setter method for property group. - * - * @param group value to be assigned to property group - */ - public void setGroup(String group) { - this.group = group; - } - /** * Getter method for property content. * @@ -163,21 +121,4 @@ public class ConfigPublishRequest extends AbstractConfigRequest { this.additionMap = additionMap; } - /** - * Getter method for property tenant. - * - * @return property value of tenant - */ - public String getTenant() { - return tenant; - } - - /** - * Setter method for property tenant. - * - * @param tenant value to be assigned to property tenant - */ - public void setTenant(String tenant) { - this.tenant = tenant; - } } diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigQueryRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigQueryRequest.java index 74f3f328d..31b349215 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigQueryRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigQueryRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2018 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,6 @@ import com.alibaba.nacos.api.common.Constants; */ public class ConfigQueryRequest extends AbstractConfigRequest { - private String dataId; - - private String group; - - private String tenant; - private String tag; /** @@ -50,60 +44,6 @@ public class ConfigQueryRequest extends AbstractConfigRequest { return request; } - /** - * Getter method for property dataId. - * - * @return property value of dataId - */ - public String getDataId() { - return dataId; - } - - /** - * Setter method for property dataId. - * - * @param dataId value to be assigned to property dataId - */ - public void setDataId(String dataId) { - this.dataId = dataId; - } - - /** - * Getter method for property group. - * - * @return property value of group - */ - public String getGroup() { - return group; - } - - /** - * Setter method for property group. - * - * @param group value to be assigned to property group - */ - public void setGroup(String group) { - this.group = group; - } - - /** - * Getter method for property tenant. - * - * @return property value of tenant - */ - public String getTenant() { - return tenant; - } - - /** - * Setter method for property tenant. - * - * @param tenant value to be assigned to property tenant - */ - public void setTenant(String tenant) { - this.tenant = tenant; - } - /** * Getter method for property tag. * diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigRemoveRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigRemoveRequest.java index 535475f80..7afb849c6 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigRemoveRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/ConfigRemoveRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,12 +24,6 @@ package com.alibaba.nacos.api.config.remote.request; */ public class ConfigRemoveRequest extends AbstractConfigRequest { - String dataId; - - String group; - - String tenant; - String tag; public ConfigRemoveRequest() { @@ -37,19 +31,10 @@ public class ConfigRemoveRequest extends AbstractConfigRequest { } public ConfigRemoveRequest(String dataId, String group, String tenant, String tag) { - this.dataId = dataId; - this.group = group; + super.setDataId(dataId); + super.setGroup(group); + super.setTenant(tenant); this.tag = tag; - this.tenant = tenant; - } - - /** - * Getter method for property dataId. - * - * @return property value of dataId - */ - public String getDataId() { - return dataId; } /** @@ -70,48 +55,4 @@ public class ConfigRemoveRequest extends AbstractConfigRequest { this.tag = tag; } - /** - * Setter method for property dataId. - * - * @param dataId value to be assigned to property dataId - */ - public void setDataId(String dataId) { - this.dataId = dataId; - } - - /** - * Getter method for property group. - * - * @return property value of group - */ - public String getGroup() { - return group; - } - - /** - * Setter method for property group. - * - * @param group value to be assigned to property group - */ - public void setGroup(String group) { - this.group = group; - } - - /** - * Getter method for property tenant. - * - * @return property value of tenant - */ - public String getTenant() { - return tenant; - } - - /** - * Setter method for property tenant. - * - * @param tenant value to be assigned to property tenant - */ - public void setTenant(String tenant) { - this.tenant = tenant; - } } diff --git a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java index e2b45ef04..c9dab7288 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/remote/request/cluster/ConfigChangeClusterSyncRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,12 +26,6 @@ import com.alibaba.nacos.api.config.remote.request.AbstractConfigRequest; */ public class ConfigChangeClusterSyncRequest extends AbstractConfigRequest { - String dataId; - - String group; - - String tenant; - String tag; long lastModified; @@ -56,60 +50,6 @@ public class ConfigChangeClusterSyncRequest extends AbstractConfigRequest { isBatch = batch; } - /** - * Getter method for property dataId. - * - * @return property value of dataId - */ - public String getDataId() { - return dataId; - } - - /** - * Setter method for property dataId. - * - * @param dataId value to be assigned to property dataId - */ - public void setDataId(String dataId) { - this.dataId = dataId; - } - - /** - * Getter method for property group. - * - * @return property value of group - */ - public String getGroup() { - return group; - } - - /** - * Setter method for property group. - * - * @param group value to be assigned to property group - */ - public void setGroup(String group) { - this.group = group; - } - - /** - * Getter method for property tenant. - * - * @return property value of tenant - */ - public String getTenant() { - return tenant; - } - - /** - * Setter method for property tenant. - * - * @param tenant value to be assigned to property tenant - */ - public void setTenant(String tenant) { - this.tenant = tenant; - } - /** * Getter method for property tag. * diff --git a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java index 5f1ec8138..78fd66e77 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2021 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.alibaba.nacos.auth.parser.grpc; +import com.alibaba.nacos.api.config.remote.request.AbstractConfigRequest; import com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest; import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.common.utils.ReflectUtils; @@ -39,23 +40,35 @@ public class ConfigGrpcResourceParser extends AbstractGrpcResourceParser { if (!configListenContexts.isEmpty()) { namespaceId = ((ConfigBatchListenRequest) request).getConfigListenContexts().get(0).getTenant(); } + } else if (request instanceof AbstractConfigRequest) { + namespaceId = ((AbstractConfigRequest) request).getTenant(); } else { namespaceId = (String) ReflectUtils.getFieldValue(request, "tenant", StringUtils.EMPTY); } - return namespaceId; + return StringUtils.isBlank(namespaceId) ? StringUtils.EMPTY : namespaceId; } @Override protected String getGroup(Request request) { - String groupName = (String) ReflectUtils - .getFieldValue(request, com.alibaba.nacos.api.common.Constants.GROUP, StringUtils.EMPTY); + String groupName; + if (request instanceof AbstractConfigRequest) { + groupName = ((AbstractConfigRequest) request).getGroup(); + } else { + groupName = (String) ReflectUtils + .getFieldValue(request, com.alibaba.nacos.api.common.Constants.GROUP, StringUtils.EMPTY); + } return StringUtils.isBlank(groupName) ? StringUtils.EMPTY : groupName; } @Override protected String getResourceName(Request request) { - String dataId = (String) ReflectUtils - .getFieldValue(request, com.alibaba.nacos.api.common.Constants.DATAID, StringUtils.EMPTY); + String dataId; + if (request instanceof AbstractConfigRequest) { + dataId = ((AbstractConfigRequest) request).getDataId(); + } else { + dataId = (String) ReflectUtils + .getFieldValue(request, com.alibaba.nacos.api.common.Constants.DATAID, StringUtils.EMPTY); + } return StringUtils.isBlank(dataId) ? StringUtils.EMPTY : dataId; } } diff --git a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java index 86c248b8d..fe755f19c 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2021 Alibaba Group Holding Ltd. + * Copyright 1999-2023 Alibaba Group Holding Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/common/pom.xml b/common/pom.xml index 63b29a1f7..142794e9a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -1,6 +1,6 @@