* fix #3973

* 重复代码抽取到一个方法

* 删除私有方法的注释

* 处理namespace参数的方法提出到一个工具类中

* 修改注释

* 添加licences

* 增加 TenantUtil 的测试

* TenantUtil 改名为 NamespaceUtil
This commit is contained in:
邪影oO 2020-10-13 15:48:46 +08:00 committed by GitHub
parent 49ff149dfb
commit 13599fd83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 20 deletions

View File

@ -43,6 +43,7 @@ import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
import com.alibaba.nacos.config.server.utils.MD5Util;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.RequestUtil;
import com.alibaba.nacos.config.server.utils.NamespaceUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.config.server.utils.ZipUtils;
import com.alibaba.nacos.sys.utils.InetUtils;
@ -90,8 +91,6 @@ public class ConfigController {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);
private static final String NAMESPACE_PUBLIC_KEY = "public";
private static final String EXPORT_CONFIG_FILE_NAME = "nacos_config_export_";
private static final String EXPORT_CONFIG_FILE_NAME_EXT = ".zip";
@ -196,7 +195,7 @@ public class ConfigController {
throws IOException, ServletException, NacosException {
// check tenant
ParamUtils.checkTenant(tenant);
tenant = processTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
// check params
ParamUtils.checkParam(dataId, group, "datumId", "content");
ParamUtils.checkParam(tag);
@ -469,7 +468,7 @@ public class ConfigController {
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "ids", required = false) List<Long> ids) {
ids.removeAll(Collections.singleton(null));
tenant = processTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = persistService.findAllConfigInfo4Export(dataId, group, tenant, appName, ids);
List<ZipUtils.ZipItem> zipItemList = new ArrayList<>();
StringBuilder metaData = null;
@ -527,12 +526,12 @@ public class ConfigController {
return ResultBuilder.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
}
if (StringUtils.isNotBlank(namespace)) {
if (persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
List<ConfigAllInfo> configInfoList = null;
try {
ZipUtils.UnZipResult unziped = ZipUtils.unzip(file.getBytes());
@ -629,9 +628,8 @@ public class ConfigController {
}
configBeansList.removeAll(Collections.singleton(null));
if (NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(namespace)) {
namespace = "";
} else if (persistService.tenantInfoCountByTenantId(namespace) <= 0) {
namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
@ -690,11 +688,4 @@ public class ConfigController {
return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult);
}
private String processTenant(String tenant) {
if (StringUtils.isEmpty(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant)) {
return "";
}
return tenant;
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 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.config.server.utils;
import org.apache.commons.lang3.StringUtils;
/**
* namespace(tenant) util.
* Because config and naming treat namespace(tenant) differently,
* this tool class can only be used by the config module.
* @author klw(213539@qq.com)
* @date 2020/10/12 17:56
*/
public class NamespaceUtil {
private static final String NAMESPACE_PUBLIC_KEY = "public";
private static final String NAMESPACE_NULL_KEY = "null";
/**
* Treat the namespace(tenant) parameters with values of "public" and "null" as an empty string.
* @param tenant namespace(tenant) id
* @return java.lang.String A namespace(tenant) string processed
*/
public static String processNamespaceParameter(String tenant) {
if (StringUtils.isBlank(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant) || NAMESPACE_NULL_KEY
.equalsIgnoreCase(tenant)) {
return "";
}
return tenant.trim();
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 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.config.server.utils;
import org.junit.Assert;
import org.junit.Test;
/**
* test NamespaceUtil.
*
* @author klw(213539 @ qq.com)
* @date 2020/10/13 9:46
*/
public class NamespaceUtilTest {
@Test
public void testProcessTenantParameter() {
String strPublic = "public";
String strNull = "null";
String strEmpty = "";
String strAbc = "abc";
String strdef123 = "def123";
String strAbcHasSpace = " abc ";
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strPublic));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strNull));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strEmpty));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(null));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbc));
Assert.assertEquals(strdef123, NamespaceUtil.processNamespaceParameter(strdef123));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbcHasSpace));
}
}