Skip to content

Commit

Permalink
[ISSUE #3907] when publishConfig, if type is absent, set default valu…
Browse files Browse the repository at this point in the history
…e '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
  • Loading branch information
horizonzy authored Nov 9, 2020
1 parent 48d0c68 commit 39a2f20
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
12 changes: 12 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ String getConfigAndSignListener(String dataId, String group, long timeoutMs, Lis
*/
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.
*
Expand Down
24 changes: 24 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/config/ConfigType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.api.config;

import com.alibaba.nacos.api.utils.StringUtils;

/**
* Config data type.
*
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -112,7 +113,12 @@ public void addListener(String dataId, String group, Listener listener) throws N

@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
Expand Down Expand Up @@ -211,7 +217,7 @@ private boolean removeConfigInner(String tenant, String dataId, String group, St
}

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

Expand All @@ -228,6 +234,7 @@ private boolean publishConfigInner(String tenant, String dataId, String group, S
params.put("dataId", dataId);
params.put("group", group);
params.put("content", content);
params.put("type", type);
if (StringUtils.isNotEmpty(tenant)) {
params.put("tenant", tenant);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,10 +33,10 @@
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;
Expand Down Expand Up @@ -134,6 +135,10 @@ public Boolean publishConfig(HttpServletRequest request, HttpServletResponse res
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);
Expand Down

0 comments on commit 39a2f20

Please sign in to comment.