[FOR 5771 config common/filter/http] improve the code quality of nacos-client. (#5979)

* [code quality] [nacos-client] [common/filter/http] simple the judge logic, constant export, chinese javadoc translate

* [code quality] [nacos-client] [common/filter/http] remove unused import
This commit is contained in:
brotherlu-xcq 2021-06-08 09:53:45 +08:00 committed by GitHub
parent 4cbc6a4960
commit 23bd92b36f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 34 deletions

View File

@ -34,4 +34,6 @@ public class ConfigConstants {
public static final String CONFIG_TYPE = "configType";
public static final String ENCRYPTED_DATA_KEY = "encryptedDataKey";
public static final String TYPE = "type";
}

View File

@ -25,6 +25,16 @@ import com.alibaba.nacos.common.utils.StringUtils;
*/
public class GroupKey {
private static final char PLUS = '+';
private static final char PERCENT = '%';
private static final char TWO = '2';
private static final char B = 'B';
private static final char FIVE = '5';
public static String getKey(String dataId, String group) {
return getKey(dataId, group, "");
}
@ -46,10 +56,10 @@ public class GroupKey {
}
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
sb.append(PLUS);
urlEncode(group, sb);
if (StringUtils.isNotEmpty(datumStr)) {
sb.append('+');
sb.append(PLUS);
urlEncode(datumStr, sb);
}
@ -70,7 +80,7 @@ public class GroupKey {
for (int i = 0; i < groupKey.length(); ++i) {
char c = groupKey.charAt(i);
if ('+' == c) {
if (PLUS == c) {
if (null == dataId) {
dataId = sb.toString();
sb.setLength(0);
@ -80,13 +90,13 @@ public class GroupKey {
} else {
throw new IllegalArgumentException("invalid groupkey:" + groupKey);
}
} else if ('%' == c) {
} else if (PERCENT == c) {
char next = groupKey.charAt(++i);
char nextnext = groupKey.charAt(++i);
if ('2' == next && 'B' == nextnext) {
sb.append('+');
} else if ('2' == next && '5' == nextnext) {
sb.append('%');
if (TWO == next && B == nextnext) {
sb.append(PLUS);
} else if (TWO == next && FIVE == nextnext) {
sb.append(PERCENT);
} else {
throw new IllegalArgumentException("invalid groupkey:" + groupKey);
}
@ -116,9 +126,9 @@ public class GroupKey {
static void urlEncode(String str, StringBuilder sb) {
for (int idx = 0; idx < str.length(); ++idx) {
char c = str.charAt(idx);
if ('+' == c) {
if (PLUS == c) {
sb.append("%2B");
} else if ('%' == c) {
} else if (PERCENT == c) {
sb.append("%25");
} else {
sb.append(c);

View File

@ -51,7 +51,7 @@ public class ConfigFilterChainManager implements IConfigFilterChain {
* @return this
*/
public synchronized ConfigFilterChainManager addFilter(IConfigFilter filter) {
// 根据order大小顺序插入
// ordered by order value
int i = 0;
while (i < this.filters.size()) {
IConfigFilter currentValue = this.filters.get(i);

View File

@ -26,6 +26,7 @@ import static com.alibaba.nacos.client.config.common.ConfigConstants.CONTENT;
import static com.alibaba.nacos.client.config.common.ConfigConstants.DATA_ID;
import static com.alibaba.nacos.client.config.common.ConfigConstants.GROUP;
import static com.alibaba.nacos.client.config.common.ConfigConstants.TENANT;
import static com.alibaba.nacos.client.config.common.ConfigConstants.TYPE;
/**
* Config Request.
@ -71,11 +72,11 @@ public class ConfigRequest implements IConfigRequest {
}
public String getType() {
return (String) param.get("type");
return (String) param.get(TYPE);
}
public void setType(String type) {
param.put("type", type);
param.put(TYPE, type);
}
@Override

View File

@ -21,7 +21,6 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.common.http.HttpRestResult;
import io.prometheus.client.Histogram;
import java.io.IOException;
import java.util.Map;
/**
@ -31,6 +30,14 @@ import java.util.Map;
*/
public class MetricsHttpAgent implements HttpAgent {
private static final String GET = "GET";
private static final String POST = "POST";
private static final String DELETE = "DELETE";
private static final String DEFAULT_CODE = "NA";
private final HttpAgent httpAgent;
public MetricsHttpAgent(HttpAgent httpAgent) {
@ -45,12 +52,10 @@ public class MetricsHttpAgent implements HttpAgent {
@Override
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA");
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(GET, path, DEFAULT_CODE);
HttpRestResult<String> result;
try {
result = httpAgent.httpGet(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {
throw e;
} finally {
timer.observeDuration();
}
@ -61,12 +66,10 @@ public class MetricsHttpAgent implements HttpAgent {
@Override
public HttpRestResult<String> httpPost(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA");
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(POST, path, DEFAULT_CODE);
HttpRestResult<String> result;
try {
result = httpAgent.httpPost(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {
throw e;
} finally {
timer.observeDuration();
}
@ -77,12 +80,10 @@ public class MetricsHttpAgent implements HttpAgent {
@Override
public HttpRestResult<String> httpDelete(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA");
Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(DELETE, path, DEFAULT_CODE);
HttpRestResult<String> result;
try {
result = httpAgent.httpDelete(path, headers, paramValues, encode, readTimeoutMs);
} catch (IOException e) {
throw e;
} finally {
timer.observeDuration();
}

View File

@ -49,6 +49,16 @@ public class ServerHttpAgent implements HttpAgent {
private static final NacosRestTemplate NACOS_RESTTEMPLATE = ConfigHttpClientManager.getInstance()
.getNacosRestTemplate();
private String accessKey;
private String secretKey;
private String encode;
private int maxRetry = 3;
final ServerListManager serverListMgr;
@Override
public HttpRestResult<String> httpGet(String path, Map<String, String> headers, Map<String, String> paramValues,
String encode, long readTimeoutMs) throws Exception {
@ -272,15 +282,4 @@ public class ServerHttpAgent implements HttpAgent {
SpasAdapter.freeCredentialInstance();
LOGGER.info("{} do shutdown stop", className);
}
private String accessKey;
private String secretKey;
private String encode;
private int maxRetry = 3;
final ServerListManager serverListMgr;
}