[ISSUE #10734] Implemented the parameter validation utility class (#10737)

* 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 <sunrisea@B-Q6UQMD6R-0222.local>
This commit is contained in:
Sunrisea 2023-07-07 11:06:00 +08:00 committed by GitHub
parent a70483563c
commit ba8e0e2dff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 1201 additions and 264 deletions

View File

@ -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;

View File

@ -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.

View File

@ -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 <tt>dataId</tt>.
*
* @return property value of dataId
*/
public String getDataId() {
return dataId;
}
/**
* Setter method for property <tt>dataId</tt>.
*
* @param dataId value to be assigned to property dataId
*/
public void setDataId(String dataId) {
this.dataId = dataId;
}
/**
* Getter method for property <tt>group</tt>.
*
* @return property value of group
*/
public String getGroup() {
return group;
}
/**
* Setter method for property <tt>group</tt>.
*
* @param group value to be assigned to property group
*/
public void setGroup(String group) {
this.group = group;
}
/**
* Getter method for property <tt>content</tt>.
*
@ -163,21 +121,4 @@ public class ConfigPublishRequest extends AbstractConfigRequest {
this.additionMap = additionMap;
}
/**
* Getter method for property <tt>tenant</tt>.
*
* @return property value of tenant
*/
public String getTenant() {
return tenant;
}
/**
* Setter method for property <tt>tenant</tt>.
*
* @param tenant value to be assigned to property tenant
*/
public void setTenant(String tenant) {
this.tenant = tenant;
}
}

View File

@ -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 <tt>dataId</tt>.
*
* @return property value of dataId
*/
public String getDataId() {
return dataId;
}
/**
* Setter method for property <tt>dataId</tt>.
*
* @param dataId value to be assigned to property dataId
*/
public void setDataId(String dataId) {
this.dataId = dataId;
}
/**
* Getter method for property <tt>group</tt>.
*
* @return property value of group
*/
public String getGroup() {
return group;
}
/**
* Setter method for property <tt>group</tt>.
*
* @param group value to be assigned to property group
*/
public void setGroup(String group) {
this.group = group;
}
/**
* Getter method for property <tt>tenant</tt>.
*
* @return property value of tenant
*/
public String getTenant() {
return tenant;
}
/**
* Setter method for property <tt>tenant</tt>.
*
* @param tenant value to be assigned to property tenant
*/
public void setTenant(String tenant) {
this.tenant = tenant;
}
/**
* Getter method for property <tt>tag</tt>.
*

View File

@ -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 <tt>dataId</tt>.
*
* @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 <tt>dataId</tt>.
*
* @param dataId value to be assigned to property dataId
*/
public void setDataId(String dataId) {
this.dataId = dataId;
}
/**
* Getter method for property <tt>group</tt>.
*
* @return property value of group
*/
public String getGroup() {
return group;
}
/**
* Setter method for property <tt>group</tt>.
*
* @param group value to be assigned to property group
*/
public void setGroup(String group) {
this.group = group;
}
/**
* Getter method for property <tt>tenant</tt>.
*
* @return property value of tenant
*/
public String getTenant() {
return tenant;
}
/**
* Setter method for property <tt>tenant</tt>.
*
* @param tenant value to be assigned to property tenant
*/
public void setTenant(String tenant) {
this.tenant = tenant;
}
}

View File

@ -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 <tt>dataId</tt>.
*
* @return property value of dataId
*/
public String getDataId() {
return dataId;
}
/**
* Setter method for property <tt>dataId</tt>.
*
* @param dataId value to be assigned to property dataId
*/
public void setDataId(String dataId) {
this.dataId = dataId;
}
/**
* Getter method for property <tt>group</tt>.
*
* @return property value of group
*/
public String getGroup() {
return group;
}
/**
* Setter method for property <tt>group</tt>.
*
* @param group value to be assigned to property group
*/
public void setGroup(String group) {
this.group = group;
}
/**
* Getter method for property <tt>tenant</tt>.
*
* @return property value of tenant
*/
public String getTenant() {
return tenant;
}
/**
* Setter method for property <tt>tenant</tt>.
*
* @param tenant value to be assigned to property tenant
*/
public void setTenant(String tenant) {
this.tenant = tenant;
}
/**
* Getter method for property <tt>tag</tt>.
*

View File

@ -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;
}
}

View File

@ -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.

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
@ -61,7 +61,7 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<resources>

View File

@ -0,0 +1,59 @@
/*
* 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.
* 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.alibaba.nacos.common.paramcheck;
/**
* Param check rules.
*
* @author zhuoguang
*/
public class ParamCheckRules {
public static final int MAX_NAMESPACE_SHOW_NAME_LENGTH = 256;
public static final int MAX_NAMESPACE_ID_LENGTH = 64;
public static final String NAMESPACE_ID_PATTERN_STRING = "^[^\\u4E00-\\u9FA5]+$";
public static final int MAX_DATA_ID_LENGTH = 512;
public static final String DATA_ID_PATTERN_STRING = "^((?!@@)[^\\u4E00-\\u9FA5])*$";
public static final int MAX_SERVICE_NAME_LENGTH = 512;
public static final String SERVICE_NAME_PATTERN_STRING = "^((?!@@)[^\\u4E00-\\u9FA5])*$";
public static final int MAX_GROUP_LENGTH = 64;
public static final String GROUP_PATTERN_STRING = "^((?!@@)[^\\u4E00-\\u9FA5])*$";
public static final int MAX_CLUSTER_LENGTH = 64;
public static final String CLUSTER_PATTERN_STRING = "^[^\\u4E00-\\u9FA5,]*$";
public static final int MAX_IP_LENGTH = 128;
public static final String IP_PATTERN_STRING = "^[^\\u4E00-\\u9FA5]*$";
public static final int MAX_PORT = 65535;
public static final int MIN_PORT = 0;
public static final int MAX_METADATA_LENGTH = 1024;
}

View File

@ -0,0 +1,247 @@
/*
* 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.
* 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.alibaba.nacos.common.paramcheck;
import com.alibaba.nacos.common.utils.StringUtils;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Param Check Utils.
*
* @author zhuoguang
*/
public class ParamCheckUtils {
private static final Pattern NAMESPACE_ID_PATTERN = Pattern.compile(ParamCheckRules.NAMESPACE_ID_PATTERN_STRING);
private static final Pattern DATA_ID_PATTERN = Pattern.compile(ParamCheckRules.DATA_ID_PATTERN_STRING);
private static final Pattern SERVICE_NAME_PATTERN = Pattern.compile(ParamCheckRules.SERVICE_NAME_PATTERN_STRING);
private static final Pattern GROUP_PATTERN = Pattern.compile(ParamCheckRules.GROUP_PATTERN_STRING);
private static final Pattern CLUSTER_PATTERN = Pattern.compile(ParamCheckRules.CLUSTER_PATTERN_STRING);
private static final Pattern IP_PATTERN = Pattern.compile(ParamCheckRules.IP_PATTERN_STRING);
/**
* Check param info format.
*
* @param paramInfo the param info
*/
public static void checkParamInfoFormat(ParamInfo paramInfo) {
if (paramInfo == null) {
return;
}
checkNamespaceShowNameFormat(paramInfo.getNamespaceShowName());
checkNamespaceIdFormat(paramInfo.getNamespaceId());
checkDataIdFormat(paramInfo.getDataId());
checkServiceNameFormat(paramInfo.getServiceName());
checkGroupFormat(paramInfo.getGroup());
checkClusterFormat(paramInfo.getCluster());
checkIpFormat(paramInfo.getIp());
checkPortFormat(paramInfo.getPort());
checkMetadataFormat(paramInfo.getMetadata());
}
/**
* Check namespace show name format.
*
* @param namespaceShowName the namespace show name
*/
public static void checkNamespaceShowNameFormat(String namespaceShowName) {
if (StringUtils.isBlank(namespaceShowName)) {
return;
}
if (namespaceShowName.length() > ParamCheckRules.MAX_NAMESPACE_SHOW_NAME_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'namespaceShowName' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_NAMESPACE_SHOW_NAME_LENGTH));
}
}
/**
* Check namespace id format.
*
* @param namespaceId the namespace id
*/
public static void checkNamespaceIdFormat(String namespaceId) {
if (StringUtils.isBlank(namespaceId)) {
return;
}
if (namespaceId.length() > ParamCheckRules.MAX_NAMESPACE_ID_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'namespaceId/tenant' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_NAMESPACE_ID_LENGTH));
}
if (!NAMESPACE_ID_PATTERN.matcher(namespaceId).matches()) {
throw new IllegalArgumentException(
"Param 'namespaceId/tenant' is illegal, Chinese characters should not appear in the param.");
}
}
/**
* Check data id format.
*
* @param dataId the data id
*/
public static void checkDataIdFormat(String dataId) {
if (StringUtils.isBlank(dataId)) {
return;
}
if (dataId.length() > ParamCheckRules.MAX_DATA_ID_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'dataId' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_NAMESPACE_ID_LENGTH));
}
if (!DATA_ID_PATTERN.matcher(dataId).matches()) {
throw new IllegalArgumentException(
"Param 'dataId' is illegal, Chinese characters and '@@' should not appear in the param.");
}
}
/**
* Check service name format.
*
* @param serviceName the service name
*/
public static void checkServiceNameFormat(String serviceName) {
if (StringUtils.isBlank(serviceName)) {
return;
}
if (serviceName.length() > ParamCheckRules.MAX_SERVICE_NAME_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'serviceName' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_SERVICE_NAME_LENGTH));
}
if (!SERVICE_NAME_PATTERN.matcher(serviceName).matches()) {
throw new IllegalArgumentException(
"Param 'serviceName' is illegal, Chinese characters and '@@' should not appear in the param.");
}
}
/**
* Check group format.
*
* @param group the group
*/
public static void checkGroupFormat(String group) {
if (StringUtils.isBlank(group)) {
return;
}
if (group.length() > ParamCheckRules.MAX_GROUP_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'group' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_GROUP_LENGTH));
}
if (!GROUP_PATTERN.matcher(group).matches()) {
throw new IllegalArgumentException(
"Param 'group' is illegal, Chinese characters and '@@' should not appear in the param");
}
}
/**
* Check cluster format.
*
* @param cluster the cluster
*/
public static void checkClusterFormat(String cluster) {
if (StringUtils.isBlank(cluster)) {
return;
}
if (cluster.length() > ParamCheckRules.MAX_CLUSTER_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'cluster' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_CLUSTER_LENGTH));
}
if (!CLUSTER_PATTERN.matcher(cluster).matches()) {
throw new IllegalArgumentException(
"Param 'cluster' is illegal, Chinese characters and ',' should not appear in the param");
}
}
/**
* Check ip format.
*
* @param ip the ip
*/
public static void checkIpFormat(String ip) {
if (StringUtils.isBlank(ip)) {
return;
}
if (ip.length() > ParamCheckRules.MAX_IP_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'ip' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_IP_LENGTH));
}
if (!IP_PATTERN.matcher(ip).matches()) {
throw new IllegalArgumentException(
"Param 'ip' is illegal, Chinese characters should not appear in the param");
}
}
/**
* Check port format.
*
* @param port the port
*/
public static void checkPortFormat(String port) {
if (StringUtils.isBlank(port)) {
return;
}
int portInt = 0;
try {
portInt = Integer.parseInt(port);
} catch (Exception e) {
throw new IllegalArgumentException(
String.format("Param 'port' is illegal, the value should be between %d and %d",
ParamCheckRules.MIN_PORT, ParamCheckRules.MAX_PORT));
}
if (portInt > ParamCheckRules.MAX_PORT || portInt < ParamCheckRules.MIN_PORT) {
throw new IllegalArgumentException(
String.format("Param 'port' is illegal, the value should be between %d and %d",
ParamCheckRules.MIN_PORT, ParamCheckRules.MAX_PORT));
}
}
/**
* Check metadata format.
*
* @param metadata the metadata
*/
public static void checkMetadataFormat(Map<String, String> metadata) {
if (metadata == null || metadata.isEmpty()) {
return;
}
int totalLength = 0;
for (Map.Entry<String, String> entry : metadata.entrySet()) {
if (StringUtils.isNotBlank(entry.getKey())) {
totalLength = totalLength + entry.getKey().length();
}
if (StringUtils.isNotBlank(entry.getValue())) {
totalLength = totalLength + entry.getValue().length();
}
}
if (totalLength > ParamCheckRules.MAX_METADATA_LENGTH) {
throw new IllegalArgumentException(
String.format("Param 'Metadata' is illegal, the param length should not exceed %d.",
ParamCheckRules.MAX_METADATA_LENGTH));
}
}
}

View File

@ -0,0 +1,116 @@
/*
* 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.
* 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.alibaba.nacos.common.paramcheck;
import java.util.Map;
/**
* Param info.
*
* @author zhuoguang
*/
public class ParamInfo {
private String namespaceShowName;
private String namespaceId;
private String dataId;
private String serviceName;
private String group;
private String cluster;
private String ip;
private String port;
private Map<String, String> metadata;
public String getNamespaceShowName() {
return namespaceShowName;
}
public void setNamespaceShowName(String namespaceShowName) {
this.namespaceShowName = namespaceShowName;
}
public String getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(String namespaceId) {
this.namespaceId = namespaceId;
}
public String getDataId() {
return dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getCluster() {
return cluster;
}
public void setCluster(String cluster) {
this.cluster = cluster;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public Map<String, String> getMetadata() {
return metadata;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
}

View File

@ -0,0 +1,290 @@
/*
* 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.
* 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.alibaba.nacos.common.paramcheck;
import junit.framework.TestCase;
import java.util.HashMap;
import java.util.Map;
/**
* The type Param check utils test.
*/
public class ParamCheckUtilsTest extends TestCase {
/**
* Test check param info format.
*/
public void testCheckParamInfoFormat() {
ParamInfo paramInfo = new ParamInfo();
ParamCheckUtils.checkParamInfoFormat(paramInfo);
}
/**
* Test check namespace show name format.
*/
public void testCheckNamespaceShowNameFormat() {
StringBuilder builder = new StringBuilder();
ParamCheckUtils.checkNamespaceShowNameFormat(builder.toString());
for (int i = 0; i < 256; i++) {
builder.append("a");
}
ParamCheckUtils.checkNamespaceShowNameFormat(builder.toString());
builder.append("a");
try {
ParamCheckUtils.checkNamespaceShowNameFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check namespace id format.
*/
public void testCheckNamespaceIdFormat() {
StringBuilder namespaceid = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_NAMESPACE_ID_LENGTH; i++) {
namespaceid.append("@");
}
ParamCheckUtils.checkNamespaceIdFormat(namespaceid.toString());
namespaceid.append("@");
try {
ParamCheckUtils.checkNamespaceIdFormat(namespaceid.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String namespaceId = "";
try {
ParamCheckUtils.checkNamespaceIdFormat(namespaceId);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check data id format.
*/
public void testCheckDataIdFormat() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_DATA_ID_LENGTH; i++) {
builder.append("a");
}
ParamCheckUtils.checkDataIdFormat(builder.toString());
try {
builder.append("a");
ParamCheckUtils.checkDataIdFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String dataid = "@##@";
ParamCheckUtils.checkDataIdFormat(dataid);
dataid = "你好";
try {
ParamCheckUtils.checkDataIdFormat(dataid);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
dataid = "@@aaaaa";
try {
ParamCheckUtils.checkDataIdFormat(dataid);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check service name format.
*/
public void testCheckServiceNameFormat() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_SERVICE_NAME_LENGTH; i++) {
builder.append("a");
}
ParamCheckUtils.checkServiceNameFormat(builder.toString());
try {
builder.append("a");
ParamCheckUtils.checkServiceNameFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String servicename = "@##@";
ParamCheckUtils.checkServiceNameFormat(servicename);
servicename = "你好";
try {
ParamCheckUtils.checkServiceNameFormat(servicename);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
servicename = "@@aaaaa";
try {
ParamCheckUtils.checkServiceNameFormat(servicename);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check group format.
*/
public void testCheckGroupFormat() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_GROUP_LENGTH; i++) {
builder.append("a");
}
ParamCheckUtils.checkGroupFormat(builder.toString());
builder.append("a");
try {
ParamCheckUtils.checkGroupFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String group = "@@aa";
try {
ParamCheckUtils.checkGroupFormat(group);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
group = "你好";
try {
ParamCheckUtils.checkGroupFormat(group);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check cluster format.
*/
public void testCheckClusterFormat() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_CLUSTER_LENGTH; i++) {
builder.append("@");
}
ParamCheckUtils.checkClusterFormat(builder.toString());
builder.append("@");
try {
ParamCheckUtils.checkClusterFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String cluster = "你好a";
try {
ParamCheckUtils.checkClusterFormat(cluster);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
cluster = ",a";
try {
ParamCheckUtils.checkClusterFormat(cluster);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check ip format.
*/
public void testCheckIpFormat() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < ParamCheckRules.MAX_IP_LENGTH; i++) {
builder.append("0");
}
ParamCheckUtils.checkIpFormat(builder.toString());
builder.append("1");
try {
ParamCheckUtils.checkIpFormat(builder.toString());
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
String ip = "你好";
try {
ParamCheckUtils.checkIpFormat(ip);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
System.out.println(e.getMessage());
assertTrue(e instanceof IllegalArgumentException);
}
}
/**
* Test check port format.
*/
public void testCheckPortFormat() {
String port = "-10";
try {
ParamCheckUtils.checkPortFormat(port);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
port = "65536";
try {
ParamCheckUtils.checkPortFormat(port);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
port = "aaa";
try {
ParamCheckUtils.checkPortFormat(port);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
port = "0";
ParamCheckUtils.checkPortFormat(port);
port = "65535";
ParamCheckUtils.checkPortFormat(port);
}
/**
* Test check metadata format.
*/
public void testCheckMetadataFormat() {
Map<String, String> metadata = new HashMap<>();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 511; i++) {
builder.append("a");
}
metadata.put("a", builder.toString());
metadata.put("b", builder.toString());
ParamCheckUtils.checkMetadataFormat(metadata);
metadata.put("c", "");
try {
ParamCheckUtils.checkMetadataFormat(metadata);
fail("expected a illegalArgumentException to be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
}
}

View File

@ -0,0 +1,81 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* Extract param from http-request.
*
* @author zhuoguang
*/
public abstract class AbstractHttpParamExtractor implements ParamExtractor<HttpServletRequest> {
private static final String SPLITTER = "@@";
private static final String NACOS_SERVER_CONTEXT = "/nacos";
private final List<String> targetRequestList;
/**
* Instantiates a new Abstract http param extractor.
*/
public AbstractHttpParamExtractor() {
targetRequestList = new ArrayList<>();
init();
}
/**
* Init,add target request to the target request list.
*/
public abstract void init();
@Override
public List<String> getTargetRequestList() {
return targetRequestList;
}
/**
* extract param and check.
*
* @param request the request
* @throws Exception exception
*/
@Override
public abstract void extractParamAndCheck(HttpServletRequest request) throws Exception;
/**
* Add target request.
*
* @param uri the uri
* @param method the method
*/
public void addTargetRequest(String uri, String method) {
targetRequestList.add(NACOS_SERVER_CONTEXT + uri + SPLITTER + method);
}
/**
* Add default target request.
*
* @param module the module
*/
public void addDefaultTargetRequest(String module) {
targetRequestList.add("default" + SPLITTER + module);
}
}

View File

@ -0,0 +1,60 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import com.alibaba.nacos.api.remote.request.Request;
import java.util.ArrayList;
import java.util.List;
/**
* Abstract ParamExtractor class for rpc request.
*
* @author zhuoguang
*/
public abstract class AbstractRpcParamExtractor implements ParamExtractor<Request> {
private final List<String> targetrequestlist;
public AbstractRpcParamExtractor() {
targetrequestlist = new ArrayList<>();
init();
}
/**
* Init, add target request to the target request list.
*/
public abstract void init();
@Override
public List<String> getTargetRequestList() {
return targetrequestlist;
}
/**
* extract param and check.
*
* @param request the request
* @throws Exception exception
*/
@Override
public abstract void extractParamAndCheck(Request request) throws Exception;
public void addTargetRequest(String type) {
targetrequestlist.add(type);
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* HttpParamExtractor Manager.
*
* @author zhuoguang
*/
public class HttpParamExtractorManager {
private static final String SPLITTER = "@@";
private static final HttpParamExtractorManager INSTANCE = new HttpParamExtractorManager();
private static final AbstractHttpParamExtractor DEFAULT_EXTRACTOR = new AbstractHttpParamExtractor() {
@Override
public void init() {
}
@Override
public void extractParamAndCheck(HttpServletRequest request) throws Exception {
}
};
private final Map<String, AbstractHttpParamExtractor> extractorMap = new ConcurrentHashMap<>(32);
private HttpParamExtractorManager() {
Collection<AbstractHttpParamExtractor> extractors = NacosServiceLoader.load(AbstractHttpParamExtractor.class);
for (AbstractHttpParamExtractor extractor : extractors) {
List<String> targetrequestlist = extractor.getTargetRequestList();
for (String targetrequest : targetrequestlist) {
extractorMap.put(targetrequest, extractor);
}
}
}
public static HttpParamExtractorManager getInstance() {
return INSTANCE;
}
public AbstractHttpParamExtractor getExtractor(String uri, String method, String module) {
AbstractHttpParamExtractor extractor = extractorMap.get(uri + SPLITTER + method);
if (extractor == null) {
extractor = extractorMap.get("default" + SPLITTER + module);
}
return extractor == null ? DEFAULT_EXTRACTOR : extractor;
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import java.util.List;
/**
* ParamExtractor interface.
*
* @param <T> the type parameter
* @author zhuoguang
*/
public interface ParamExtractor<T> {
/**
* Gets target request list.
*
* @return the target request list
*/
List<String> getTargetRequestList();
/**
* Extract param and check.
*
* @param params the params
* @throws Exception the exception
*/
void extractParamAndCheck(T params) throws Exception;
}

View File

@ -0,0 +1,70 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import com.alibaba.nacos.api.remote.request.Request;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* HttpParamExtractor Manager.
*
* @author zhuoguang
*/
public class RpcParamExtractorManager {
private static final RpcParamExtractorManager INSTANCE = new RpcParamExtractorManager();
private static final AbstractRpcParamExtractor DEFAULT_EXTRACTOR = new AbstractRpcParamExtractor() {
@Override
public void init() {
}
@Override
public void extractParamAndCheck(Request params) throws Exception {
}
};
private final Map<String, AbstractRpcParamExtractor> extractorMap = new ConcurrentHashMap<>(32);
private RpcParamExtractorManager() {
Collection<AbstractRpcParamExtractor> extractors = NacosServiceLoader.load(AbstractRpcParamExtractor.class);
for (AbstractRpcParamExtractor extractor : extractors) {
List<String> targetrequestlist = extractor.getTargetRequestList();
for (String targetRequest : targetrequestlist) {
extractorMap.put(targetRequest, extractor);
}
}
}
public static RpcParamExtractorManager getInstance() {
return INSTANCE;
}
public AbstractRpcParamExtractor getExtractor(String type) {
AbstractRpcParamExtractor extractor = extractorMap.get(type);
if (extractor == null) {
extractor = DEFAULT_EXTRACTOR;
}
return extractor;
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import com.alibaba.nacos.common.utils.HttpMethod;
import junit.framework.TestCase;
import org.springframework.mock.web.MockHttpServletRequest;
/**
* The type Http param extractor manager test.
*/
public class HttpParamExtractorManagerTest extends TestCase {
/**
* Test get instance.
*/
public void testGetInstance() {
HttpParamExtractorManager paramExtractorManager = HttpParamExtractorManager.getInstance();
}
/**
* Test get extractor.
*
* @throws Exception the exception
*/
public void testGetExtractor() throws Exception {
HttpParamExtractorManager paramExtractorManager = HttpParamExtractorManager.getInstance();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/nacos/v1/ns/instance");
request.setMethod(HttpMethod.POST);
AbstractHttpParamExtractor extractor = paramExtractorManager.getExtractor(request.getRequestURI(), request.getMethod(), "naming");
extractor.extractParamAndCheck(request);
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.
* 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.alibaba.nacos.core.paramcheck;
import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest;
import junit.framework.TestCase;
/**
* The type Rpc param extractor manager test.
*/
public class RpcParamExtractorManagerTest extends TestCase {
/**
* Test get instance.
*/
public void testGetInstance() {
RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance();
}
/**
* Test get extractor.
*
* @throws Exception the exception
*/
public void testGetExtractor() throws Exception {
RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance();
ConfigQueryRequest request = new ConfigQueryRequest();
AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(request.getClass().getSimpleName());
extractor.extractParamAndCheck(request);
}
}