From e01a44702bc7b6f33462b4debb9726475b01bdf3 Mon Sep 17 00:00:00 2001 From: slievrly Date: Wed, 19 Jun 2019 23:47:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?optimize=20charset=E3=80=81continue=20and?= =?UTF-8?q?=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: slievrly --- .../nacos/api/config/listener/AbstractListener.java | 2 +- .../config/filter/impl/ConfigFilterChainManager.java | 1 - .../nacos/client/config/impl/ClientWorker.java | 1 - .../nacos/client/config/impl/ServerListManager.java | 1 - .../alibaba/nacos/client/config/utils/ParamUtils.java | 2 -- .../config/server/service/ServerListService.java | 1 - .../server/service/notify/NotifySingleService.java | 1 - .../alibaba/nacos/config/server/utils/ParamUtils.java | 2 -- .../com/alibaba/nacos/core/utils/SystemUtils.java | 4 +++- .../java/com/alibaba/nacos/core/utils/WebUtils.java | 5 +++-- .../java/com/alibaba/nacos/naming/acl/AuthInfo.java | 4 +++- .../naming/cluster/transport/FastJsonSerializer.java | 11 ++++------- .../naming/consistency/persistent/raft/RaftCore.java | 5 +++-- .../naming/consistency/persistent/raft/RaftStore.java | 5 +++-- .../nacos/naming/controllers/DistroController.java | 6 ++++-- .../nacos/naming/controllers/RaftController.java | 3 ++- .../naming/healthcheck/NoneHealthCheckProcessor.java | 1 - .../java/com/alibaba/nacos/naming/misc/NetUtils.java | 8 ++++---- .../com/alibaba/nacos/naming/push/PushService.java | 3 ++- .../alibaba/nacos/test/config/ConfigAPI_ITCase.java | 2 +- 20 files changed, 33 insertions(+), 35 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/listener/AbstractListener.java b/api/src/main/java/com/alibaba/nacos/api/config/listener/AbstractListener.java index f0f874a2b..66f0d5ce0 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/listener/AbstractListener.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/listener/AbstractListener.java @@ -18,7 +18,7 @@ package com.alibaba.nacos.api.config.listener; import java.util.concurrent.Executor; /** - * Listner Adapter,use default notify thread + * Listener Adapter,use default notify thread * * @author water.lyl */ diff --git a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java index f06fde03f..8650a0d73 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java @@ -73,7 +73,6 @@ public class ConfigFilterChainManager implements IConfigFilterChain { @Override public void doFilter(final IConfigRequest request, final IConfigResponse response) throws NacosException { if (this.currentPosition == this.additionalFilters.size()) { - return; } else { this.currentPosition++; IConfigFilter nextFilter = this.additionalFilters.get(this.currentPosition - 1); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index da2b45882..f06d520fc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -305,7 +305,6 @@ public class ClientWorker { cacheData.setContent(content); LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}", agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); - return; } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index aa3a2fab1..f28c00dde 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -285,7 +285,6 @@ public class ServerListManager { List result = new ArrayList(lines.size()); for (String serverAddr : lines) { if (null == serverAddr || serverAddr.trim().isEmpty()) { - continue; } else { String[] ipPort = serverAddr.trim().split(":"); String ip = ipPort[0].trim(); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java b/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java index 0112fa180..55defb6d2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java @@ -44,9 +44,7 @@ public class ParamUtils { for (int i = 0; i < length; i++) { char ch = param.charAt(i); if (Character.isLetterOrDigit(ch)) { - continue; } else if (isValidChar(ch)) { - continue; } else { return false; } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java index 4d03c043c..cb60eebe1 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java @@ -262,7 +262,6 @@ public class ServerListService implements ApplicationListener ips = new ArrayList(lines.size()); for (String serverAddr : lines) { if (null == serverAddr || serverAddr.trim().isEmpty()) { - continue; } else { ips.add(getFormatServerAddr(serverAddr)); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifySingleService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifySingleService.java index 5fbdecadb..e09e5c3bf 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifySingleService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/notify/NotifySingleService.java @@ -123,7 +123,6 @@ public class NotifySingleService { if (null == executors.putIfAbsent(ip, executor)) { logger.warn("[notify-thread-pool] setup thread target ip {} ok.", ip); - continue; } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java index 77c168168..413ef404b 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java @@ -47,9 +47,7 @@ public class ParamUtils { for (int i = 0; i < length; i++) { char ch = param.charAt(i); if (Character.isLetterOrDigit(ch)) { - continue; } else if (isValidChar(ch)) { - continue; } else { return false; } diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java index 53ad39752..9435e1d4b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java @@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory; import java.io.*; import java.lang.management.ManagementFactory; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -127,7 +128,8 @@ public class SystemUtils { Reader reader = null; try { - reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), UTF_8); + reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), + StandardCharsets.UTF_8); List lines = IoUtils.readLines(reader); String comment = "#"; for (String line : lines) { diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java index f4210c1b2..4f5d2d2b6 100644 --- a/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java +++ b/core/src/main/java/com/alibaba/nacos/core/utils/WebUtils.java @@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * @author nkorange @@ -34,7 +35,7 @@ public class WebUtils { String encoding = req.getParameter("encoding"); if (!StringUtils.isEmpty(encoding)) { try { - value = new String(value.getBytes("UTF-8"), encoding); + value = new String(value.getBytes(StandardCharsets.UTF_8), encoding); } catch (UnsupportedEncodingException ignore) { } } @@ -57,7 +58,7 @@ public class WebUtils { String encoding = req.getParameter("encoding"); if (!StringUtils.isEmpty(encoding)) { try { - value = new String(value.getBytes("UTF-8"), encoding); + value = new String(value.getBytes(StandardCharsets.UTF_8), encoding); } catch (UnsupportedEncodingException ignore) { } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthInfo.java index 63b4ba9f4..d9d3710da 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/acl/AuthInfo.java @@ -15,6 +15,8 @@ */ package com.alibaba.nacos.naming.acl; +import java.nio.charset.StandardCharsets; + /** * @author nkorange */ @@ -50,7 +52,7 @@ public class AuthInfo { public String toString() { try { // very simple encryption is enough - byte[] authBytes = (operator + ":" + appKey).getBytes("UTF-8"); + byte[] authBytes = (operator + ":" + appKey).getBytes(StandardCharsets.UTF_8); StringBuilder authBuilder = new StringBuilder(); for (byte authByte : authBytes) { authBuilder.append((byte) (~((short) authByte))).append(","); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java b/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java index 26c869cc1..59bc56e3d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/cluster/transport/FastJsonSerializer.java @@ -24,6 +24,7 @@ import com.alibaba.nacos.naming.pojo.Record; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -45,17 +46,13 @@ public class FastJsonSerializer implements Serializer { @Override public T deserialize(byte[] data, Class clazz) { - try { - return JSON.parseObject(new String(data, UTF_8), clazz); - } catch (UnsupportedEncodingException e) { - return null; - } + return JSON.parseObject(new String(data, StandardCharsets.UTF_8), clazz); } @Override public T deserialize(byte[] data, TypeReference clazz) { try { - String dataString = new String(data, UTF_8); + String dataString = new String(data, StandardCharsets.UTF_8); return JSON.parseObject(dataString, clazz); } catch (Exception e) { Loggers.SRV_LOG.error("deserialize data failed.", e); @@ -66,7 +63,7 @@ public class FastJsonSerializer implements Serializer { @Override public Map> deserializeMap(byte[] data, Class clazz) { try { - String dataString = new String(data, UTF_8); + String dataString = new String(data, StandardCharsets.UTF_8); Map dataMap = JSON.parseObject(dataString, new TypeReference>() { }); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java index 6e773a39c..8d620c9b6 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java @@ -45,6 +45,7 @@ import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.Lock; @@ -525,11 +526,11 @@ public class RaftCore { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); - gzip.write(content.getBytes("UTF-8")); + gzip.write(content.getBytes(StandardCharsets.UTF_8)); gzip.close(); byte[] compressedBytes = out.toByteArray(); - String compressedContent = new String(compressedBytes, "UTF-8"); + String compressedContent = new String(compressedBytes, StandardCharsets.UTF_8); Loggers.RAFT.info("raw beat data size: {}, size of compressed data: {}", content.length(), compressedContent.length()); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java index cc391e09b..adcb13003 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java @@ -38,6 +38,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -121,7 +122,7 @@ public class RaftStore { buffer = ByteBuffer.allocate((int) file.length()); fc.read(buffer); - String json = new String(buffer.array(), "UTF-8"); + String json = new String(buffer.array(), StandardCharsets.UTF_8); if (StringUtils.isBlank(json)) { return null; } @@ -222,7 +223,7 @@ public class RaftStore { FileChannel fc = null; ByteBuffer data; - data = ByteBuffer.wrap(JSON.toJSONString(datum).getBytes("UTF-8")); + data = ByteBuffer.wrap(JSON.toJSONString(datum).getBytes(StandardCharsets.UTF_8)); try { fc = new FileOutputStream(cacheFile, false).getChannel(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java index 058dd273c..5567ed8db 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/DistroController.java @@ -39,6 +39,8 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -115,11 +117,11 @@ public class DistroController { for (String key : keys.split(keySplitter)) { datumMap.put(key, consistencyService.get(key)); } - response.getWriter().write(new String(serializer.serialize(datumMap), "UTF-8")); + response.getWriter().write(new String(serializer.serialize(datumMap), StandardCharsets.UTF_8)); } @RequestMapping(value = "/datums", method = RequestMethod.GET) public void getAllDatums(HttpServletRequest request, HttpServletResponse response) throws Exception { - response.getWriter().write(new String(serializer.serialize(dataStore.getDataMap()), "UTF-8")); + response.getWriter().write(new String(serializer.serialize(dataStore.getDataMap()), StandardCharsets.UTF_8)); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java index bf1e6c284..5bdac9415 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/RaftController.java @@ -45,6 +45,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -82,7 +83,7 @@ public class RaftController { @RequestMapping(value = "/beat", method = RequestMethod.POST) public JSONObject beat(HttpServletRequest request, HttpServletResponse response) throws Exception { - String entity = new String(IoUtils.tryDecompress(request.getInputStream()), "UTF-8"); + String entity = new String(IoUtils.tryDecompress(request.getInputStream()), StandardCharsets.UTF_8); String value = URLDecoder.decode(entity, "UTF-8"); value = URLDecoder.decode(value, "UTF-8"); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/NoneHealthCheckProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/NoneHealthCheckProcessor.java index a9ecb3338..c7bf4a84d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/NoneHealthCheckProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/healthcheck/NoneHealthCheckProcessor.java @@ -28,7 +28,6 @@ public class NoneHealthCheckProcessor implements HealthCheckProcessor { @Override public void process(HealthCheckTask task) { - return; } @Override diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java index 42886983b..22301dc6a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/NetUtils.java @@ -44,10 +44,10 @@ public class NetUtils { int[] b = new int[4]; String x = ""; - b[0] = (int) ((ip >> 24) & 0xff); - b[1] = (int) ((ip >> 16) & 0xff); - b[2] = (int) ((ip >> 8) & 0xff); - b[3] = (int) (ip & 0xff); + b[0] = (ip >> 24) & 0xff; + b[1] = (ip >> 16) & 0xff; + b[2] = (ip >> 8) & 0xff; + b[3] = ip & 0xff; x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]); return x; diff --git a/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java b/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java index 1d26d5a40..15a50c0b4 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/push/PushService.java @@ -33,6 +33,7 @@ import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; @@ -512,7 +513,7 @@ public class PushService { String dataStr = JSON.toJSONString(data); try { - byte[] dataBytes = dataStr.getBytes("UTF-8"); + byte[] dataBytes = dataStr.getBytes(StandardCharsets.UTF_8); dataBytes = compressIfNecessary(dataBytes); DatagramPacket packet = new DatagramPacket(dataBytes, dataBytes.length, client.socketAddr); diff --git a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java index 837a6cce9..fdf6c2edb 100644 --- a/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java +++ b/test/src/test/java/com/alibaba/nacos/test/config/ConfigAPI_ITCase.java @@ -626,7 +626,7 @@ public class ConfigAPI_ITCase { */ @Test(timeout = TIME_OUT) public void nacos_removeListener_4() { - iconfig.removeListener(dataId, group, (Listener) null); + iconfig.removeListener(dataId, group, null); Assert.assertTrue(true); } From f17f40e0af70f5519765af347811b21eb06456d1 Mon Sep 17 00:00:00 2001 From: slievrly Date: Tue, 25 Jun 2019 21:42:13 +0800 Subject: [PATCH 2/2] fix review Signed-off-by: slievrly --- .../filter/impl/ConfigFilterChainManager.java | 4 +-- .../client/config/impl/ServerListManager.java | 29 +++++++++++------ .../nacos/client/config/utils/ParamUtils.java | 4 +-- .../server/service/ServerListService.java | 31 +++++++++++-------- .../nacos/config/server/utils/ParamUtils.java | 11 +++---- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java index 8650a0d73..be55bacc3 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.api.config.filter.IConfigFilterChain; import com.alibaba.nacos.api.config.filter.IConfigRequest; import com.alibaba.nacos.api.config.filter.IConfigResponse; import com.alibaba.nacos.api.exception.NacosException; + import com.google.common.collect.Lists; /** @@ -72,8 +73,7 @@ public class ConfigFilterChainManager implements IConfigFilterChain { @Override public void doFilter(final IConfigRequest request, final IConfigResponse response) throws NacosException { - if (this.currentPosition == this.additionalFilters.size()) { - } else { + if (this.currentPosition != this.additionalFilters.size()) { this.currentPosition++; IConfigFilter nextFilter = this.additionalFilters.get(this.currentPosition - 1); nextFilter.doFilter(request, response, this); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index f28c00dde..85bd6282d 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -15,21 +15,31 @@ */ package com.alibaba.nacos.client.config.impl; +import java.io.IOException; +import java.io.StringReader; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.IOUtils; -import com.alibaba.nacos.client.utils.*; -import org.slf4j.Logger; +import com.alibaba.nacos.client.utils.EnvUtil; +import com.alibaba.nacos.client.utils.LogUtils; +import com.alibaba.nacos.client.utils.ParamUtil; +import com.alibaba.nacos.client.utils.StringUtils; +import com.alibaba.nacos.client.utils.TemplateUtils; -import java.io.IOException; -import java.io.StringReader; -import java.net.HttpURLConnection; -import java.util.*; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; /** * Serverlist Manager @@ -284,8 +294,7 @@ public class ServerListManager { List lines = IOUtils.readLines(new StringReader(httpResult.content)); List result = new ArrayList(lines.size()); for (String serverAddr : lines) { - if (null == serverAddr || serverAddr.trim().isEmpty()) { - } else { + if (org.apache.commons.lang3.StringUtils.isNotBlank(serverAddr)) { String[] ipPort = serverAddr.trim().split(":"); String ip = ipPort[0].trim(); if (ipPort.length == 1) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java b/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java index 55defb6d2..477db7e27 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/utils/ParamUtils.java @@ -43,9 +43,7 @@ public class ParamUtils { int length = param.length(); for (int i = 0; i < length; i++) { char ch = param.charAt(i); - if (Character.isLetterOrDigit(ch)) { - } else if (isValidChar(ch)) { - } else { + if (!Character.isLetterOrDigit(ch) && !isValidChar(ch)) { return false; } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java index cb60eebe1..0a6f36c1d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java @@ -15,6 +15,20 @@ */ package com.alibaba.nacos.config.server.service; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import javax.annotation.PostConstruct; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletResponse; + import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.service.notify.NotifyService; @@ -23,6 +37,7 @@ import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.PropertyUtil; import com.alibaba.nacos.config.server.utils.RunningConfigUtils; import com.alibaba.nacos.config.server.utils.event.EventDispatcher; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; @@ -39,20 +54,11 @@ import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import javax.annotation.PostConstruct; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.io.StringReader; -import java.util.*; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; - +import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; +import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP; import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE; import static com.alibaba.nacos.core.utils.SystemUtils.readClusterConf; -import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; -import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; /** * Serverlist service @@ -261,8 +267,7 @@ public class ServerListService implements ApplicationListener lines = IOUtils.readLines(new StringReader(result.content)); List ips = new ArrayList(lines.size()); for (String serverAddr : lines) { - if (null == serverAddr || serverAddr.trim().isEmpty()) { - } else { + if (StringUtils.isNotBlank(serverAddr)) { ips.add(getFormatServerAddr(serverAddr)); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java b/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java index 413ef404b..793810ed7 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/utils/ParamUtils.java @@ -15,11 +15,12 @@ */ package com.alibaba.nacos.config.server.utils; -import com.alibaba.nacos.config.server.exception.NacosException; -import org.apache.commons.lang3.StringUtils; - import java.util.Map; +import com.alibaba.nacos.config.server.exception.NacosException; + +import org.apache.commons.lang3.StringUtils; + /** * 参数合法性检查工具类 * @@ -46,9 +47,7 @@ public class ParamUtils { int length = param.length(); for (int i = 0; i < length; i++) { char ch = param.charAt(i); - if (Character.isLetterOrDigit(ch)) { - } else if (isValidChar(ch)) { - } else { + if (!Character.isLetterOrDigit(ch) && !isValidChar(ch)) { return false; } }