Merge pull request #1404 from slievrly/charset

optimize charset、continue and return
This commit is contained in:
Fury Zhu 2019-07-01 13:12:03 +08:00 committed by GitHub
commit 819d5d1db5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 75 additions and 67 deletions

View File

@ -18,7 +18,7 @@ package com.alibaba.nacos.api.config.listener;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
/** /**
* Listner Adapter,use default notify thread * Listener Adapter,use default notify thread
* *
* @author water.lyl * @author water.lyl
*/ */

View File

@ -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.IConfigRequest;
import com.alibaba.nacos.api.config.filter.IConfigResponse; import com.alibaba.nacos.api.config.filter.IConfigResponse;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
/** /**
@ -72,9 +73,7 @@ public class ConfigFilterChainManager implements IConfigFilterChain {
@Override @Override
public void doFilter(final IConfigRequest request, final IConfigResponse response) throws NacosException { public void doFilter(final IConfigRequest request, final IConfigResponse response) throws NacosException {
if (this.currentPosition == this.additionalFilters.size()) { if (this.currentPosition != this.additionalFilters.size()) {
return;
} else {
this.currentPosition++; this.currentPosition++;
IConfigFilter nextFilter = this.additionalFilters.get(this.currentPosition - 1); IConfigFilter nextFilter = this.additionalFilters.get(this.currentPosition - 1);
nextFilter.doFilter(request, response, this); nextFilter.doFilter(request, response, this);

View File

@ -305,7 +305,6 @@ public class ClientWorker {
cacheData.setContent(content); cacheData.setContent(content);
LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}", LOGGER.warn("[{}] [failover-change] failover file changed. dataId={}, group={}, tenant={}, md5={}, content={}",
agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content)); agent.getName(), dataId, group, tenant, md5, ContentUtils.truncateContent(content));
return;
} }
} }

View File

@ -15,22 +15,31 @@
*/ */
package com.alibaba.nacos.client.config.impl; 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.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst; import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent; import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEvent;
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils; import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.identify.Constants; import com.alibaba.nacos.client.identify.Constants;
import com.alibaba.nacos.client.utils.*; import com.alibaba.nacos.client.utils.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException;
import java.io.StringReader; import org.slf4j.Logger;
import java.net.HttpURLConnection;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/** /**
* Serverlist Manager * Serverlist Manager
@ -291,9 +300,7 @@ public class ServerListManager {
List<String> lines = IOUtils.readLines(new StringReader(httpResult.content)); List<String> lines = IOUtils.readLines(new StringReader(httpResult.content));
List<String> result = new ArrayList<String>(lines.size()); List<String> result = new ArrayList<String>(lines.size());
for (String serverAddr : lines) { for (String serverAddr : lines) {
if (null == serverAddr || serverAddr.trim().isEmpty()) { if (org.apache.commons.lang3.StringUtils.isNotBlank(serverAddr)) {
continue;
} else {
String[] ipPort = serverAddr.trim().split(":"); String[] ipPort = serverAddr.trim().split(":");
String ip = ipPort[0].trim(); String ip = ipPort[0].trim();
if (ipPort.length == 1) { if (ipPort.length == 1) {

View File

@ -43,11 +43,7 @@ public class ParamUtils {
int length = param.length(); int length = param.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char ch = param.charAt(i); char ch = param.charAt(i);
if (Character.isLetterOrDigit(ch)) { if (!Character.isLetterOrDigit(ch) && !isValidChar(ch)) {
continue;
} else if (isValidChar(ch)) {
continue;
} else {
return false; return false;
} }
} }

View File

@ -15,6 +15,20 @@
*/ */
package com.alibaba.nacos.config.server.service; 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.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.service.notify.NotifyService; 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.PropertyUtil;
import com.alibaba.nacos.config.server.utils.RunningConfigUtils; import com.alibaba.nacos.config.server.utils.RunningConfigUtils;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher; import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@ -39,20 +54,11 @@ import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct; import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
import javax.servlet.ServletContext; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
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.core.utils.SystemUtils.LOCAL_IP; 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.STANDALONE_MODE;
import static com.alibaba.nacos.core.utils.SystemUtils.readClusterConf; 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 * Serverlist service
@ -261,9 +267,7 @@ public class ServerListService implements ApplicationListener<WebServerInitializ
List<String> lines = IOUtils.readLines(new StringReader(result.content)); List<String> lines = IOUtils.readLines(new StringReader(result.content));
List<String> ips = new ArrayList<String>(lines.size()); List<String> ips = new ArrayList<String>(lines.size());
for (String serverAddr : lines) { for (String serverAddr : lines) {
if (null == serverAddr || serverAddr.trim().isEmpty()) { if (StringUtils.isNotBlank(serverAddr)) {
continue;
} else {
ips.add(getFormatServerAddr(serverAddr)); ips.add(getFormatServerAddr(serverAddr));
} }
} }

View File

@ -123,7 +123,6 @@ public class NotifySingleService {
if (null == executors.putIfAbsent(ip, executor)) { if (null == executors.putIfAbsent(ip, executor)) {
logger.warn("[notify-thread-pool] setup thread target ip {} ok.", ip); logger.warn("[notify-thread-pool] setup thread target ip {} ok.", ip);
continue;
} }
} }

View File

@ -15,11 +15,12 @@
*/ */
package com.alibaba.nacos.config.server.utils; 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 java.util.Map;
import com.alibaba.nacos.config.server.exception.NacosException;
import org.apache.commons.lang3.StringUtils;
/** /**
* 参数合法性检查工具类 * 参数合法性检查工具类
* *
@ -46,11 +47,7 @@ public class ParamUtils {
int length = param.length(); int length = param.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char ch = param.charAt(i); char ch = param.charAt(i);
if (Character.isLetterOrDigit(ch)) { if (!Character.isLetterOrDigit(ch) && !isValidChar(ch)) {
continue;
} else if (isValidChar(ch)) {
continue;
} else {
return false; return false;
} }
} }

View File

@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -127,7 +128,8 @@ public class SystemUtils {
Reader reader = null; Reader reader = null;
try { 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<String> lines = IoUtils.readLines(reader); List<String> lines = IoUtils.readLines(reader);
String comment = "#"; String comment = "#";
for (String line : lines) { for (String line : lines) {

View File

@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/** /**
* @author nkorange * @author nkorange
@ -34,7 +35,7 @@ public class WebUtils {
String encoding = req.getParameter("encoding"); String encoding = req.getParameter("encoding");
if (!StringUtils.isEmpty(encoding)) { if (!StringUtils.isEmpty(encoding)) {
try { try {
value = new String(value.getBytes("UTF-8"), encoding); value = new String(value.getBytes(StandardCharsets.UTF_8), encoding);
} catch (UnsupportedEncodingException ignore) { } catch (UnsupportedEncodingException ignore) {
} }
} }
@ -57,7 +58,7 @@ public class WebUtils {
String encoding = req.getParameter("encoding"); String encoding = req.getParameter("encoding");
if (!StringUtils.isEmpty(encoding)) { if (!StringUtils.isEmpty(encoding)) {
try { try {
value = new String(value.getBytes("UTF-8"), encoding); value = new String(value.getBytes(StandardCharsets.UTF_8), encoding);
} catch (UnsupportedEncodingException ignore) { } catch (UnsupportedEncodingException ignore) {
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package com.alibaba.nacos.naming.acl; package com.alibaba.nacos.naming.acl;
import java.nio.charset.StandardCharsets;
/** /**
* @author nkorange * @author nkorange
*/ */
@ -50,7 +52,7 @@ public class AuthInfo {
public String toString() { public String toString() {
try { try {
// very simple encryption is enough // very simple encryption is enough
byte[] authBytes = (operator + ":" + appKey).getBytes("UTF-8"); byte[] authBytes = (operator + ":" + appKey).getBytes(StandardCharsets.UTF_8);
StringBuilder authBuilder = new StringBuilder(); StringBuilder authBuilder = new StringBuilder();
for (byte authByte : authBytes) { for (byte authByte : authBytes) {
authBuilder.append((byte) (~((short) authByte))).append(","); authBuilder.append((byte) (~((short) authByte))).append(",");

View File

@ -24,6 +24,7 @@ import com.alibaba.nacos.naming.pojo.Record;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -45,17 +46,13 @@ public class FastJsonSerializer implements Serializer {
@Override @Override
public <T> T deserialize(byte[] data, Class<T> clazz) { public <T> T deserialize(byte[] data, Class<T> clazz) {
try { return JSON.parseObject(new String(data, StandardCharsets.UTF_8), clazz);
return JSON.parseObject(new String(data, UTF_8), clazz);
} catch (UnsupportedEncodingException e) {
return null;
}
} }
@Override @Override
public <T> T deserialize(byte[] data, TypeReference<T> clazz) { public <T> T deserialize(byte[] data, TypeReference<T> clazz) {
try { try {
String dataString = new String(data, UTF_8); String dataString = new String(data, StandardCharsets.UTF_8);
return JSON.parseObject(dataString, clazz); return JSON.parseObject(dataString, clazz);
} catch (Exception e) { } catch (Exception e) {
Loggers.SRV_LOG.error("deserialize data failed.", e); Loggers.SRV_LOG.error("deserialize data failed.", e);
@ -66,7 +63,7 @@ public class FastJsonSerializer implements Serializer {
@Override @Override
public <T extends Record> Map<String, Datum<T>> deserializeMap(byte[] data, Class<T> clazz) { public <T extends Record> Map<String, Datum<T>> deserializeMap(byte[] data, Class<T> clazz) {
try { try {
String dataString = new String(data, UTF_8); String dataString = new String(data, StandardCharsets.UTF_8);
Map<String, JSONObject> dataMap = JSON.parseObject(dataString, new TypeReference<Map<String, JSONObject>>() { Map<String, JSONObject> dataMap = JSON.parseObject(dataString, new TypeReference<Map<String, JSONObject>>() {
}); });

View File

@ -45,6 +45,7 @@ import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
@ -525,11 +526,11 @@ public class RaftCore {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out); GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(content.getBytes("UTF-8")); gzip.write(content.getBytes(StandardCharsets.UTF_8));
gzip.close(); gzip.close();
byte[] compressedBytes = out.toByteArray(); 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: {}", Loggers.RAFT.info("raw beat data size: {}, size of compressed data: {}",
content.length(), compressedContent.length()); content.length(), compressedContent.length());

View File

@ -38,6 +38,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
@ -119,7 +120,7 @@ public class RaftStore {
buffer = ByteBuffer.allocate((int) file.length()); buffer = ByteBuffer.allocate((int) file.length());
fc.read(buffer); fc.read(buffer);
String json = new String(buffer.array(), "UTF-8"); String json = new String(buffer.array(), StandardCharsets.UTF_8);
if (StringUtils.isBlank(json)) { if (StringUtils.isBlank(json)) {
return null; return null;
} }
@ -220,7 +221,7 @@ public class RaftStore {
FileChannel fc = null; FileChannel fc = null;
ByteBuffer data; ByteBuffer data;
data = ByteBuffer.wrap(JSON.toJSONString(datum).getBytes("UTF-8")); data = ByteBuffer.wrap(JSON.toJSONString(datum).getBytes(StandardCharsets.UTF_8));
try { try {
fc = new FileOutputStream(cacheFile, false).getChannel(); fc = new FileOutputStream(cacheFile, false).getChannel();

View File

@ -38,6 +38,8 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -114,11 +116,11 @@ public class DistroController {
for (String key : keys.split(keySplitter)) { for (String key : keys.split(keySplitter)) {
datumMap.put(key, consistencyService.get(key)); 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) @RequestMapping(value = "/datums", method = RequestMethod.GET)
public void getAllDatums(HttpServletRequest request, HttpServletResponse response) throws Exception { 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));
} }
} }

View File

@ -45,6 +45,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -82,7 +83,7 @@ public class RaftController {
@RequestMapping(value = "/beat", method = RequestMethod.POST) @RequestMapping(value = "/beat", method = RequestMethod.POST)
public JSONObject beat(HttpServletRequest request, HttpServletResponse response) throws Exception { 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"); String value = URLDecoder.decode(entity, "UTF-8");
value = URLDecoder.decode(value, "UTF-8"); value = URLDecoder.decode(value, "UTF-8");

View File

@ -28,7 +28,6 @@ public class NoneHealthCheckProcessor implements HealthCheckProcessor {
@Override @Override
public void process(HealthCheckTask task) { public void process(HealthCheckTask task) {
return;
} }
@Override @Override

View File

@ -44,10 +44,10 @@ public class NetUtils {
int[] b = new int[4]; int[] b = new int[4];
String x = ""; String x = "";
b[0] = (int) ((ip >> 24) & 0xff); b[0] = (ip >> 24) & 0xff;
b[1] = (int) ((ip >> 16) & 0xff); b[1] = (ip >> 16) & 0xff;
b[2] = (int) ((ip >> 8) & 0xff); b[2] = (ip >> 8) & 0xff;
b[3] = (int) (ip & 0xff); b[3] = ip & 0xff;
x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]); x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);
return x; return x;

View File

@ -33,6 +33,7 @@ import java.net.DatagramSocket;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -512,7 +513,7 @@ public class PushService {
String dataStr = JSON.toJSONString(data); String dataStr = JSON.toJSONString(data);
try { try {
byte[] dataBytes = dataStr.getBytes("UTF-8"); byte[] dataBytes = dataStr.getBytes(StandardCharsets.UTF_8);
dataBytes = compressIfNecessary(dataBytes); dataBytes = compressIfNecessary(dataBytes);
DatagramPacket packet = new DatagramPacket(dataBytes, dataBytes.length, client.socketAddr); DatagramPacket packet = new DatagramPacket(dataBytes, dataBytes.length, client.socketAddr);

View File

@ -626,7 +626,7 @@ public class ConfigAPI_ITCase {
*/ */
@Test(timeout = TIME_OUT) @Test(timeout = TIME_OUT)
public void nacos_removeListener_4() { public void nacos_removeListener_4() {
iconfig.removeListener(dataId, group, (Listener) null); iconfig.removeListener(dataId, group, null);
Assert.assertTrue(true); Assert.assertTrue(true);
} }