Skip to content

Commit

Permalink
Fix#3973 (alibaba#3974)
Browse files Browse the repository at this point in the history
* fix alibaba#3973

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

* 删除私有方法的注释

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

* 修改注释

* 添加licences

* 增加 TenantUtil 的测试

* TenantUtil 改名为 NamespaceUtil
  • Loading branch information
KeRan213539 authored and WesleyOne committed Oct 20, 2020
1 parent af29a2e commit 59d6ad5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -196,7 +195,7 @@ public void getConfig(HttpServletRequest request, HttpServletResponse response,
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);
Expand Down Expand Up @@ -469,7 +468,7 @@ public ResponseEntity<byte[]> exportConfig(@RequestParam(value = "dataId", requi
@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;
Expand Down Expand Up @@ -527,12 +526,12 @@ public RestResult<Map<String, Object>> importAndPublishConfig(HttpServletRequest
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());
Expand Down Expand Up @@ -628,10 +627,9 @@ public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request,
return ResultBuilder.buildResult(ResultCodeEnum.NO_SELECTED_CONFIG, failedData);
}
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);
}
Expand Down Expand Up @@ -690,11 +688,4 @@ public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request,
return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult);
}

private String processTenant(String tenant) {
if (StringUtils.isEmpty(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant)) {
return "";
}
return tenant;
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 59d6ad5

Please sign in to comment.