Merge pull request #2566 from chuntaojun/hotfix_2560

hotfix issue 2560
This commit is contained in:
Peter Zhu 2020-04-02 09:52:30 +08:00 committed by GitHub
commit c684b7e6a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 256 additions and 75 deletions

View File

@ -33,6 +33,7 @@ import com.alibaba.nacos.client.config.utils.ContentUtils;
import com.alibaba.nacos.client.config.utils.ParamUtils;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -70,6 +71,7 @@ public class NacosConfigService implements ConfigService {
private ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager();
public NacosConfigService(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);
String encodeTmp = properties.getProperty(PropertyKeyConst.ENCODE);
if (StringUtils.isBlank(encodeTmp)) {
encode = Constants.ENCODE;

View File

@ -306,7 +306,7 @@ public class ServerListManager {
List<String> lines = IoUtils.readLines(new StringReader(httpResult.content));
List<String> result = new ArrayList<String>(lines.size());
for (String serverAddr : lines) {
if (org.apache.commons.lang3.StringUtils.isNotBlank(serverAddr)) {
if (StringUtils.isNotBlank(serverAddr)) {
String[] ipPort = serverAddr.trim().split(":");
String ip = ipPort[0].trim();
if (ipPort.length == 1) {

View File

@ -27,6 +27,7 @@ import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Map;
@ -50,16 +51,15 @@ public class NacosNamingMaintainService implements NamingMaintainService {
public NacosNamingMaintainService(String serverList) {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
init(properties);
}
public NacosNamingMaintainService(Properties properties) {
init(properties);
}
private void init(Properties properties) {
ValidatorUtils.checkInitParam(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
initServerAddr(properties);
InitUtils.initWebRootContext();

View File

@ -35,6 +35,7 @@ import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
@ -77,7 +78,6 @@ public class NacosNamingService implements NamingService {
public NacosNamingService(String serverList) {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
init(properties);
}
@ -86,6 +86,7 @@ public class NacosNamingService implements NamingService {
}
private void init(Properties properties) {
ValidatorUtils.checkInitParam(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
initServerAddr(properties);
InitUtils.initWebRootContext();

View File

@ -25,13 +25,14 @@ import java.util.Locale;
* string util
*
* @author Nacos
* @deprecated Use {@link org.apache.commons.lang3.StringUtils} instead
*/
@Deprecated
public class StringUtils {
private static final int INDEX_NOT_FOUND = -1;
public static final String COMMA = ",";
public static final String EMPTY = "";
public static boolean isBlank(String str) {
@ -100,6 +101,7 @@ public class StringUtils {
return stringBuilder.toString();
}
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true, true);
}

View File

@ -0,0 +1,68 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.utils;
import com.alibaba.nacos.api.PropertyKeyConst;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* All parameter validation tools
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public final class ValidatorUtils {
private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+");
private static final Pattern IP_MATCH = Pattern.compile("([^\\/:]+)(:\\d+)");
public static void checkInitParam(Properties properties) {
checkServerAddr(properties.getProperty(PropertyKeyConst.SERVER_ADDR));
checkContextPath(properties.getProperty(PropertyKeyConst.CONTEXT_PATH));
}
public static void checkServerAddr(String serverAddr) {
if (StringUtils.isEmpty(serverAddr)) {
throw new IllegalArgumentException("Please set the serverAddr");
}
String[] addrs;
if (serverAddr.contains(StringUtils.COMMA)) {
addrs = serverAddr.split(StringUtils.COMMA);
} else {
addrs = new String[]{serverAddr};
}
for (String addr : addrs) {
Matcher matcher = IP_MATCH.matcher(addr.trim());
if (!matcher.find()) {
throw new IllegalArgumentException("Incorrect serverAddr address : " + addr + ", example should like ip:port or domain:port");
}
}
}
public static void checkContextPath(String contextPath) {
if (contextPath == null) {
return;
}
Matcher matcher = CONTEXT_PATH_MATCH.matcher(contextPath);
if (matcher.find()) {
throw new IllegalArgumentException("Illegal url path expression");
}
}
}

View File

@ -1,63 +0,0 @@
package com.alibaba.nacos.client;
import com.alibaba.nacos.client.utils.StringUtils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import static com.alibaba.nacos.client.utils.StringUtils.*;
import static org.junit.Assert.*;
@Deprecated
public class StringUtilsTest {
@Test
public void testisNotBlank() {
assertTrue(isNotBlank("foo"));
assertFalse(isNotBlank(" "));
assertFalse(isNotBlank(null));
}
@Test
public void testIsNotEmpty() {
assertFalse(isNotEmpty(""));
assertTrue(isNotEmpty("foo"));
}
@Test
public void testDefaultIfEmpty() {
assertEquals("foo", defaultIfEmpty("", "foo"));
assertEquals("bar", defaultIfEmpty("bar", "foo"));
}
@Test
public void testEquals() {
assertTrue(StringUtils.equals("foo", "foo"));
assertFalse(StringUtils.equals("bar", "foo"));
assertFalse(StringUtils.equals(" ", "foo"));
assertFalse(StringUtils.equals("foo", null));
}
@Test
public void testSubstringBetween() {
assertNull(substringBetween(null, null, null));
assertNull(substringBetween("", "foo", ""));
assertNull(substringBetween("foo", "bar", "baz"));
assertEquals("", substringBetween("foo", "foo", ""));
}
@Test
public void testJoin() {
assertNull(join(null, ""));
Collection collection = new ArrayList();
collection.add("foo");
collection.add("bar");
assertEquals("foo,bar", join(collection, ","));
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.client.utils;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import static com.alibaba.nacos.client.utils.StringUtils.*;
import static org.junit.Assert.*;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class StringUtilsTest {
@Test
public void testisNotBlank() {
assertTrue(isNotBlank("foo"));
assertFalse(isNotBlank(" "));
assertFalse(isNotBlank(null));
}
@Test
public void testIsNotEmpty() {
assertFalse(isNotEmpty(""));
assertTrue(isNotEmpty("foo"));
}
@Test
public void testDefaultIfEmpty() {
assertEquals("foo", defaultIfEmpty("", "foo"));
assertEquals("bar", defaultIfEmpty("bar", "foo"));
}
@Test
public void testEquals() {
assertTrue(StringUtils.equals("foo", "foo"));
assertFalse(StringUtils.equals("bar", "foo"));
assertFalse(StringUtils.equals(" ", "foo"));
assertFalse(StringUtils.equals("foo", null));
}
@Test
public void testSubstringBetween() {
assertNull(substringBetween(null, null, null));
assertNull(substringBetween("", "foo", ""));
assertNull(substringBetween("foo", "bar", "baz"));
assertEquals("", substringBetween("foo", "foo", ""));
}
@Test
public void testJoin() {
assertNull(join(null, ""));
Collection collection = new ArrayList();
collection.add("foo");
collection.add("bar");
assertEquals("foo,bar", join(collection, ","));
}
}

View File

@ -0,0 +1,89 @@
package com.alibaba.nacos.client.utils;
import org.junit.Test;
public class ValidatorUtilsTest {
@Test
public void test_context_path_legal() {
String contextPath1 = "/nacos";
ValidatorUtils.checkContextPath(contextPath1);
String contextPath2 = "nacos";
ValidatorUtils.checkContextPath(contextPath2);
String contextPath3 = "/";
ValidatorUtils.checkContextPath(contextPath3);
String contextPath4 = "";
ValidatorUtils.checkContextPath(contextPath4);
}
@Test(expected = IllegalArgumentException.class)
public void test_context_path_illegal_1() {
String contextPath1 = "//nacos/";
ValidatorUtils.checkContextPath(contextPath1);
}
@Test(expected = IllegalArgumentException.class)
public void test_context_path_illegal_2() {
String contextPath2 = "/nacos//";
ValidatorUtils.checkContextPath(contextPath2);
}
@Test(expected = IllegalArgumentException.class)
public void test_context_path_illegal_3() {
String contextPath3 = "///";
ValidatorUtils.checkContextPath(contextPath3);
}
@Test(expected = IllegalArgumentException.class)
public void test_context_path_illegal_4() {
String contextPath4 = "//";
ValidatorUtils.checkContextPath(contextPath4);
}
@Test
public void test_server_addr() {
String serverAddr = "127.0.0.1:8848";
ValidatorUtils.checkServerAddr(serverAddr);
String serverAddrs = "127.0.0.1:8848,127.0.0.1:80,127.0.0.1:8809";
ValidatorUtils.checkServerAddr(serverAddrs);
}
@Test
public void test_server_addr_k8s() {
String serverAddr = "busybox-1.busybox-subdomain.default.svc.cluster.local:80";
ValidatorUtils.checkServerAddr(serverAddr);
String serverAddrs = "busybox-1.busybox-subdomain.default.svc.cluster.local:80,busybox-1.busybox-subdomain.default.svc.cluster.local:8111, busybox-1.busybox-subdomain.default.svc.cluster.local:8098";
ValidatorUtils.checkServerAddr(serverAddrs);
}
@Test(expected = IllegalArgumentException.class)
public void test_server_addr_err() {
String serverAddr = "127.0.0.1";
ValidatorUtils.checkServerAddr(serverAddr);
}
@Test(expected = IllegalArgumentException.class)
public void test_server_addr_illegal_err() {
String serverAddr = "127.0.0.1:";
ValidatorUtils.checkServerAddr(serverAddr);
}
@Test(expected = IllegalArgumentException.class)
public void test_server_addrs_err() {
String serverAddrs = "127.0.0.1:8848,127.0.0.1,127.0.0.1:8809";
ValidatorUtils.checkServerAddr(serverAddrs);
}
@Test(expected = IllegalArgumentException.class)
public void test_server_addr_k8s_err() {
String serverAddr = "busybox-1.busybox-subdomain.default.svc.cluster.local";
ValidatorUtils.checkServerAddr(serverAddr);
}
@Test(expected = IllegalArgumentException.class)
public void test_server_addrs_k8s_err() {
String serverAddrs = "busybox-1.busybox-subdomain.default.svc.cluster.local,busybox-1.busybox-subdomain.default.svc.cluster.local:8111, busybox-1.busybox-subdomain.default.svc.cluster.local:8098";
ValidatorUtils.checkServerAddr(serverAddrs);
}
}

View File

@ -40,13 +40,15 @@ public class Md5Utils {
private static final int HEX_VALUE_COUNT = 16;
public static String getMD5(byte[] bytes) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get();
if (messageDigest != null) {
return new BigInteger(1, messageDigest.digest(bytes)).toString(HEX_VALUE_COUNT);
try {
MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get();
if (messageDigest != null) {
return new BigInteger(1, messageDigest.digest(bytes)).toString(HEX_VALUE_COUNT);
}
throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error");
} finally {
MESSAGE_DIGEST_LOCAL.remove();
}
throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error");
}
public static String getMD5(String value, String encode) {