From 39a2f20d5633871a91b6044488da554c983c0657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BB=B6?= <1060026287@qq.com> Date: Mon, 9 Nov 2020 11:36:50 +0800 Subject: [PATCH] [ISSUE #3907] when publishConfig, if type is absent, set default value 'text' (#3915) * when publishConfig, if type is absent, set default value 'text' * 1.add publishConfig api(add param type) 2.check type is valid in nacos server --- .../nacos/api/config/ConfigService.java | 12 ++++++++++ .../alibaba/nacos/api/config/ConfigType.java | 24 +++++++++++++++++++ .../client/config/NacosConfigService.java | 11 +++++++-- .../server/controller/ConfigController.java | 7 +++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java index 114fa69dc..e81ac9486 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java @@ -79,6 +79,18 @@ public interface ConfigService { */ boolean publishConfig(String dataId, String group, String content) throws NacosException; + /** + * Publish config. + * + * @param dataId dataId + * @param group group + * @param content content + * @param type config type {@link ConfigType} + * @return Whether publish + * @throws NacosException NacosException + */ + boolean publishConfig(String dataId, String group, String content, String type) throws NacosException; + /** * Remove config. * diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java index 01095e686..d70e2a98d 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.api.config; +import com.alibaba.nacos.api.utils.StringUtils; + /** * Config data type. * @@ -62,4 +64,26 @@ public enum ConfigType { public String getType() { return type; } + + public static ConfigType getDefaultType() { + return TEXT; + } + + /** + * check input type is valid. + * + * @param type config type + * @return it the type valid + */ + public static Boolean isValidType(String type) { + if (StringUtils.isBlank(type)) { + return false; + } + for (ConfigType value : values()) { + if (value.type.equals(type)) { + return true; + } + } + return false; + } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java index a0c614011..d38d5e0c8 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/NacosConfigService.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.client.config; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; @@ -112,7 +113,12 @@ public class NacosConfigService implements ConfigService { @Override public boolean publishConfig(String dataId, String group, String content) throws NacosException { - return publishConfigInner(namespace, dataId, group, null, null, null, content); + return publishConfig(dataId, group, content, ConfigType.getDefaultType().getType()); + } + + @Override + public boolean publishConfig(String dataId, String group, String content, String type) throws NacosException { + return publishConfigInner(namespace, dataId, group, null, null, null, content, type); } @Override @@ -211,7 +217,7 @@ public class NacosConfigService implements ConfigService { } private boolean publishConfigInner(String tenant, String dataId, String group, String tag, String appName, - String betaIps, String content) throws NacosException { + String betaIps, String content, String type) throws NacosException { group = null2defaultGroup(group); ParamUtils.checkParam(dataId, group, content); @@ -228,6 +234,7 @@ public class NacosConfigService implements ConfigService { params.put("dataId", dataId); params.put("group", group); params.put("content", content); + params.put("type", type); if (StringUtils.isNotEmpty(tenant)) { params.put("tenant", tenant); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java index 95c775d05..37848a4ec 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java @@ -16,6 +16,7 @@ package com.alibaba.nacos.config.server.controller; +import com.alibaba.nacos.api.config.ConfigType; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.auth.common.ActionTypes; @@ -32,10 +33,10 @@ import com.alibaba.nacos.config.server.model.GroupkeyListenserStatus; import com.alibaba.nacos.config.server.model.Page; import com.alibaba.nacos.config.server.model.SameConfigPolicy; import com.alibaba.nacos.config.server.model.SampleResult; +import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.result.ResultBuilder; import com.alibaba.nacos.config.server.result.code.ResultCodeEnum; import com.alibaba.nacos.config.server.service.AggrWhitelist; -import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent; import com.alibaba.nacos.config.server.service.ConfigChangePublisher; import com.alibaba.nacos.config.server.service.ConfigSubService; import com.alibaba.nacos.config.server.service.repository.PersistService; @@ -134,6 +135,10 @@ public class ConfigController { final String srcIp = RequestUtil.getRemoteIp(request); final String requestIpApp = RequestUtil.getAppName(request); srcUser = RequestUtil.getSrcUserName(request); + //check type + if (!ConfigType.isValidType(type)) { + type = ConfigType.getDefaultType().getType(); + } // check tenant ParamUtils.checkTenant(tenant); ParamUtils.checkParam(dataId, group, "datumId", content);