From f2d8a83e0d1bdb83b0f6c3ecee30cc6e6b48ae22 Mon Sep 17 00:00:00 2001 From: "liujianjun.ljj" Date: Tue, 2 Jan 2024 12:04:41 +0800 Subject: [PATCH] Refactoring Fury Serialization Integration --- bom/pom.xml | 2 +- .../common/BlackAndWhiteListFileLoader.java} | 65 ++- .../sofa-rpc/serialize_blacklist.txt | 0 codec/codec-fury/pom.xml | 5 - .../sofa/rpc/codec/fury/FuryHelper.java | 107 ----- ...ccessConfig.java => FurySecurityMode.java} | 15 +- .../sofa/rpc/codec/fury/FurySerializer.java | 393 +++++++----------- .../src/main/resources/configuration.yml | 14 - .../sofa/rpc/codec/fury/FuryHelperTest.java | 169 -------- .../rpc/codec/fury/FurySerializerTest.java | 295 ++++++------- .../fury/model/Registered/HelloService.java | 28 -- .../model/Registered/RegisteredClass.java | 27 -- .../{NotRegister.java => BlackListClass.java} | 6 +- .../NoneClassHasBlackClass.java} | 22 +- .../DemoRequest.java | 2 +- .../DemoResponse.java | 2 +- .../fury/model/whitelist/DemoService.java | 3 - .../codec/fury/model/whitelist/Person.java | 46 -- ...mpl2.java => WhiteClassHasBlackClass.java} | 17 +- codec/codec-fury/src/test/resources/conf.yml | 13 - .../sofa-rpc/serialize_blacklist.txt | 1 + .../sofa-rpc/serialize_whitelist.txt | 1 + .../sofahessian/SofaHessianSerializer.java | 3 +- ...a => BlackAndWhiteListFileLoaderTest.java} | 35 +- .../alipay/sofa/rpc/common/SofaOptions.java | 4 + .../sofa/rpc/common/config/RpcConfigKeys.java | 7 +- 26 files changed, 402 insertions(+), 880 deletions(-) rename codec/{codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoader.java => codec-api/src/main/java/com/alipay/sofa/rpc/codec/common/BlackAndWhiteListFileLoader.java} (63%) rename codec/{codec-sofa-hessian => codec-api}/src/main/resources/sofa-rpc/serialize_blacklist.txt (100%) delete mode 100644 codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FuryHelper.java rename codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/{AccessConfig.java => FurySecurityMode.java} (74%) delete mode 100644 codec/codec-fury/src/main/resources/configuration.yml delete mode 100644 codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FuryHelperTest.java delete mode 100644 codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloService.java delete mode 100644 codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/RegisteredClass.java rename codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/{NotRegister.java => BlackListClass.java} (86%) rename codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/{Registered/HelloServiceImpl.java => none/NoneClassHasBlackClass.java} (64%) rename codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/{Registered => whitelist}/DemoRequest.java (94%) rename codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/{Registered => whitelist}/DemoResponse.java (96%) delete mode 100644 codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/Person.java rename codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/{HelloServiceImpl2.java => WhiteClassHasBlackClass.java} (71%) delete mode 100644 codec/codec-fury/src/test/resources/conf.yml create mode 100644 codec/codec-fury/src/test/resources/sofa-rpc/serialize_blacklist.txt create mode 100644 codec/codec-fury/src/test/resources/sofa-rpc/serialize_whitelist.txt rename codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/{BlackListFileLoaderTest.java => BlackAndWhiteListFileLoaderTest.java} (71%) diff --git a/bom/pom.xml b/bom/pom.xml index ea50fa3ad..31dc42d31 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -39,9 +39,9 @@ 2.12.7.1 0.6.12 1.5.9 + 0.4.1 1.53.0 - 0.3.0 4.4.13 4.5.13 diff --git a/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoader.java b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/common/BlackAndWhiteListFileLoader.java similarity index 63% rename from codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoader.java rename to codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/common/BlackAndWhiteListFileLoader.java index 8b48c7a7b..d2ea773a8 100644 --- a/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoader.java +++ b/codec/codec-api/src/main/java/com/alipay/sofa/rpc/codec/common/BlackAndWhiteListFileLoader.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.sofahessian; +package com.alipay.sofa.rpc.codec.common; import com.alipay.sofa.rpc.common.SofaConfigs; import com.alipay.sofa.rpc.common.SofaOptions; @@ -37,17 +37,20 @@ * * @author GengZhang */ -public class BlackListFileLoader { +public class BlackAndWhiteListFileLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(BlackListFileLoader.class); + private static final Logger LOGGER = LoggerFactory + .getLogger(BlackAndWhiteListFileLoader.class); - public static final List SOFA_SERIALIZE_BLACK_LIST = loadFile("/sofa-rpc/serialize_blacklist.txt"); + public static final List SOFA_SERIALIZE_BLACK_LIST = loadBlackListFile("/sofa-rpc/serialize_blacklist.txt"); - static List loadFile(String path) { - List blackPrefixList = new ArrayList(); + public static final List SOFA_SERIALIZER_WHITE_LIST = loadWhiteListFile("/sofa-rpc/serialize_whitelist.txt"); + + public static List loadBlackListFile(String path) { + List blackPrefixList = new ArrayList<>(); InputStream input = null; try { - input = BlackListFileLoader.class.getResourceAsStream(path); + input = BlackAndWhiteListFileLoader.class.getResourceAsStream(path); if (input != null) { readToList(input, "UTF-8", blackPrefixList); } @@ -68,6 +71,31 @@ static List loadFile(String path) { return blackPrefixList; } + public static List loadWhiteListFile(String path) { + List whitePrefixList = new ArrayList<>(); + InputStream input = null; + try { + input = BlackAndWhiteListFileLoader.class.getResourceAsStream(path); + if (input != null) { + readToList(input, "UTF-8", whitePrefixList); + } + String overStr = SofaConfigs.getStringValue(SofaOptions.CONFIG_SERIALIZE_WHITELIST_OVERRIDE, ""); + if (StringUtils.isNotBlank(overStr)) { + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Serialize whitelist will override with configuration: {}", overStr); + } + overrideWhiteList(whitePrefixList, overStr); + } + } catch (Exception e) { + if (LOGGER.isErrorEnabled()) { + LOGGER.error(e.getMessage(), e); + } + } finally { + closeQuietly(input); + } + return whitePrefixList; + } + /** * 读文件,将结果丢入List * @@ -100,12 +128,12 @@ private static void readToList(InputStream input, String encoding, List /** * Override blacklist with override string. - * - * @param originList Origin black list + * + * @param originList Origin black list * @param overrideStr The override string */ - static void overrideBlackList(List originList, String overrideStr) { - List adds = new LinkedList(); + public static void overrideBlackList(List originList, String overrideStr) { + List adds = new LinkedList<>(); String[] overrideItems = StringUtils.splitWithCommaOrSemicolon(overrideStr); for (String overrideItem : overrideItems) { if (StringUtils.isNotBlank(overrideItem)) { @@ -127,4 +155,19 @@ static void overrideBlackList(List originList, String overrideStr) { originList.addAll(adds); } } + + public static void overrideWhiteList(List originList, String overrideStr) { + List adds = new LinkedList<>(); + String[] overrideItems = StringUtils.splitWithCommaOrSemicolon(overrideStr); + for (String overrideItem : overrideItems) { + if (StringUtils.isNotBlank(overrideItem)) { + if (!originList.contains(overrideItem)) { + adds.add(overrideItem); + } + } + } + if (adds.size() > 0) { + originList.addAll(adds); + } + } } diff --git a/codec/codec-sofa-hessian/src/main/resources/sofa-rpc/serialize_blacklist.txt b/codec/codec-api/src/main/resources/sofa-rpc/serialize_blacklist.txt similarity index 100% rename from codec/codec-sofa-hessian/src/main/resources/sofa-rpc/serialize_blacklist.txt rename to codec/codec-api/src/main/resources/sofa-rpc/serialize_blacklist.txt diff --git a/codec/codec-fury/pom.xml b/codec/codec-fury/pom.xml index 71da43e15..14bf688c9 100644 --- a/codec/codec-fury/pom.xml +++ b/codec/codec-fury/pom.xml @@ -32,11 +32,6 @@ fury-core - - org.yaml - snakeyaml - - org.slf4j slf4j-log4j12 diff --git a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FuryHelper.java b/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FuryHelper.java deleted file mode 100644 index f057114d3..000000000 --- a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FuryHelper.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.sofa.rpc.codec.fury; - -import java.lang.reflect.Method; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import com.alipay.sofa.rpc.common.utils.ClassTypeUtils; -import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator; -import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException; -import com.alipay.sofa.rpc.log.LogCodes; - -/** - * @author lipan - */ -public class FuryHelper { - private final ConcurrentMap[]> requestClassCache = new ConcurrentHashMap<>(); - private final ConcurrentMap[]> responseClassCache = new ConcurrentHashMap<>(); - - public Class[] getReqClass(String service, String methodName) { - return getClasses(service, methodName, requestClassCache); - } - - public Class[] getRespClass(String service, String methodName) { - return getClasses(service, methodName, responseClassCache); - } - - private Class[] getClasses(String service, String methodName, - ConcurrentMap[]> responseClassCache) { - String key = buildMethodKey(service, methodName); - Class[] respClass = responseClassCache.get(key); - if (respClass == null) { - String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service); - Class clazz = ClassTypeUtils.getClass(interfaceClass); - loadClassToCache(key, clazz, methodName); - } else { - // Check if the class loader has changed due to hot update - String interfaceClass = ConfigUniqueNameGenerator.getInterfaceName(service); - ClassLoader currentClassLoader = getClassLoader(interfaceClass); - if (!currentClassLoader.equals(respClass[0].getClassLoader())) { - Class clazz = ClassTypeUtils.getClass(interfaceClass); - loadClassToCache(key, clazz, methodName); - } - } - return responseClassCache.get(key); - } - - private String buildMethodKey(String serviceName, String methodName) { - return serviceName + "#" + methodName; - } - - private void loadClassToCache(String key, Class clazz, String methodName) { - if (clazz == null) { - throw new SofaRpcRuntimeException("Failed to load : " + key); - } - Method pbMethod = null; - Method[] methods = clazz.getMethods(); - for (Method method : methods) { - if (methodName.equals(method.getName())) { - pbMethod = method; - break; - } - } - if (pbMethod == null) { - throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_METHOD_NOT_FOUND, clazz.getName(), - methodName)); - } - Class[] parameterTypes = pbMethod.getParameterTypes(); - if (parameterTypes.length == 0) { - throw new SofaRpcRuntimeException(LogCodes.getLog("fury", clazz.getName())); - } - - requestClassCache.put(key, parameterTypes); - Class respClass = pbMethod.getReturnType(); - if (respClass == void.class) { - throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROTOBUF_RETURN, clazz.getName())); - } - responseClassCache.put(key, new Class[] { respClass }); - } - - public ClassLoader getClassLoader(String className) { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - if (classLoader == null) { - classLoader = FuryHelper.class.getClassLoader(); - } - Class clazz = ClassTypeUtils.getClass(className); - if (clazz != null) { - return clazz.getClassLoader(); - } else { - return classLoader; - } - } -} \ No newline at end of file diff --git a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/AccessConfig.java b/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySecurityMode.java similarity index 74% rename from codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/AccessConfig.java rename to codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySecurityMode.java index c967cd02e..5af3db212 100644 --- a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/AccessConfig.java +++ b/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySecurityMode.java @@ -19,16 +19,17 @@ /** * @author lipan */ -public enum AccessConfig { - WHITELIST_CONFIG("whitelist"), BLACKLIST_CONFIG("blacklist"), NONE_CONFIG("none"); +public enum FurySecurityMode { - private final String configType; + WHITELIST_MODE("whitelist"), BLACKLIST_MODE("blacklist"), NONE_MODE("none"); - AccessConfig(String configType) { - this.configType = configType; + private final String securityMode; + + FurySecurityMode(String securityMode) { + this.securityMode = securityMode; } - public String getConfigType() { - return configType; + public String getSecurityMode() { + return securityMode; } } diff --git a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySerializer.java b/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySerializer.java index c1a1b854a..abfae76b7 100644 --- a/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySerializer.java +++ b/codec/codec-fury/src/main/java/com/alipay/sofa/rpc/codec/fury/FurySerializer.java @@ -16,34 +16,27 @@ */ package com.alipay.sofa.rpc.codec.fury; -import org.yaml.snakeyaml.Yaml; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import com.alipay.sofa.common.config.SofaConfigs; import com.alipay.sofa.rpc.codec.AbstractSerializer; +import com.alipay.sofa.rpc.codec.common.BlackAndWhiteListFileLoader; import com.alipay.sofa.rpc.common.RemotingConstants; import com.alipay.sofa.rpc.common.config.RpcConfigKeys; -import com.alipay.sofa.rpc.common.utils.CodecUtils; -import com.alipay.sofa.rpc.common.utils.StringUtils; import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator; -import com.alipay.sofa.rpc.context.RpcInvokeContext; import com.alipay.sofa.rpc.core.exception.SofaRpcException; import com.alipay.sofa.rpc.core.request.SofaRequest; import com.alipay.sofa.rpc.core.response.SofaResponse; import com.alipay.sofa.rpc.ext.Extension; import com.alipay.sofa.rpc.transport.AbstractByteBuf; import com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf; - import io.fury.Fury; import io.fury.ThreadLocalFury; import io.fury.config.Language; +import io.fury.memory.MemoryBuffer; import io.fury.resolver.AllowListChecker; + +import java.util.List; +import java.util.Map; + import static io.fury.config.CompatibleMode.COMPATIBLE; /** @@ -52,162 +45,130 @@ @Extension(value = "fury2", code = 22) public class FurySerializer extends AbstractSerializer { - private final FuryHelper furyHelper = new FuryHelper(); - - private final ThreadLocalFury fury; + private final ThreadLocalFury fury; - private static final AllowListChecker checker = new AllowListChecker(AllowListChecker.CheckLevel.STRICT); + private final ThreadLocal writeBufferLocal = ThreadLocal.withInitial(() -> MemoryBuffer.newHeapBuffer(32)); - private static final String CHECKER_MODE = SofaConfigs.getOrDefault(RpcConfigKeys.CHECKER_MODE); + private final String checkerMode = SofaConfigs.getOrDefault(RpcConfigKeys.CHECKER_MODE); public FurySerializer() { - // Load the register for higher efficiency - ArrayList> registerList = new ArrayList<>(); - List classList = loadConf("register"); - if (classList != null) { - for (String className : classList) { - Class clazz; - try { - clazz = Class.forName(className); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - registerList.add(clazz); - } - - } - fury = new ThreadLocalFury(classLoader -> { Fury f = Fury.builder().withLanguage(Language.JAVA) - .withRefTracking(true) - .withCodegen(true) - .withNumberCompressed(true) - .withCompatibleMode(COMPATIBLE) - .requireClassRegistration(false) - .withClassLoader(classLoader) - .withAsyncCompilation(true) - .build(); - // Register classes is CanonicalName - if (!registerList.isEmpty()) { - for (Class clazz : registerList) { - f.register(clazz); - } - } + .withRefTracking(true) + .withCodegen(true) + .withNumberCompressed(true) + .withCompatibleMode(COMPATIBLE) + .requireClassRegistration(false) + .withClassLoader(classLoader) + .withAsyncCompilation(true) + .build(); + f.register(SofaRequest.class); + f.register(SofaResponse.class); + f.register(SofaRpcException.class); // Do not use any configuration - if (CHECKER_MODE.equals(AccessConfig.NONE_CONFIG.getConfigType())) { + if (checkerMode.equalsIgnoreCase(FurySecurityMode.NONE_MODE.getSecurityMode())) { + AllowListChecker noChecker = new AllowListChecker(AllowListChecker.CheckLevel.DISABLE); + f.getClassResolver().setClassChecker(noChecker); return f; - } - - if (CHECKER_MODE.equals(AccessConfig.WHITELIST_CONFIG.getConfigType())) { - List whiteList = loadConf(AccessConfig.WHITELIST_CONFIG.getConfigType()); - if (whiteList != null) { - // To setting checker - f.getClassResolver().setClassChecker(checker); - checker.addListener(f.getClassResolver()); - // WhiteList classes use wildcards - for (String key : whiteList) { - checker.allowClass(key); - } + } else if (checkerMode.equalsIgnoreCase(FurySecurityMode.BLACKLIST_MODE.getSecurityMode())) { + AllowListChecker blackListChecker = new AllowListChecker(AllowListChecker.CheckLevel.WARN); + List blackList = BlackAndWhiteListFileLoader.SOFA_SERIALIZE_BLACK_LIST; + // To setting checker + f.getClassResolver().setClassChecker(blackListChecker); + blackListChecker.addListener(f.getClassResolver()); + // BlackList classes use wildcards + for (String key : blackList) { + blackListChecker.disallowClass(key + "*"); + } + } else if (checkerMode.equalsIgnoreCase(FurySecurityMode.WHITELIST_MODE.getSecurityMode())) { + AllowListChecker blackAndWhiteListChecker = new AllowListChecker(AllowListChecker.CheckLevel.STRICT); + List whiteList = BlackAndWhiteListFileLoader.SOFA_SERIALIZER_WHITE_LIST; + // To setting checker + f.getClassResolver().setClassChecker(blackAndWhiteListChecker); + blackAndWhiteListChecker.addListener(f.getClassResolver()); + // WhiteList classes use wildcards + for (String key : whiteList) { + blackAndWhiteListChecker.allowClass(key + "*"); } - } else if (CHECKER_MODE.equals(AccessConfig.BLACKLIST_CONFIG.getConfigType())) { - List blackList = loadConf(AccessConfig.BLACKLIST_CONFIG.getConfigType()); - if (blackList != null) { - // To setting checker - f.getClassResolver().setClassChecker(checker); - checker.addListener(f.getClassResolver()); - // BlackList classes use wildcards - for (String key : blackList) { - checker.disallowClass(key); - } + List blackList = BlackAndWhiteListFileLoader.SOFA_SERIALIZE_BLACK_LIST; + // To setting checker + f.getClassResolver().setClassChecker(blackAndWhiteListChecker); + blackAndWhiteListChecker.addListener(f.getClassResolver()); + // BlackList classes use wildcards + for (String key : blackList) { + blackAndWhiteListChecker.disallowClass(key + "*"); } } return f; }); } - public void addWhiteList(String address) { - checker.allowClass(address); - } - - public void addBlackList(String address) { - checker.disallowClass(address); - } - - private static List loadConf(String name) { - List confList = null; - try { - // Use the configuration file to obtain the whitelist - Enumeration urls = ClassLoader.getSystemResources("configuration.yml"); - while (urls.hasMoreElements()) { - URL url = urls.nextElement(); - InputStream inputStream = url.openStream(); - Yaml yaml = new Yaml(); - Map yamlContent = yaml.load(inputStream); - confList = (List) yamlContent.get(name); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - return confList; - } - @Override public AbstractByteBuf encode(final Object object, final Map context) throws SofaRpcException { - if (object == null) { - throw buildSerializeError("Unsupported null message!"); - } else if (object instanceof SofaRequest) { - return encodeSofaRequest((SofaRequest) object, context); - } else if (object instanceof SofaResponse) { - return encodeSofaResponse((SofaResponse) object, context); - } else { - return new ByteArrayWrapperByteBuf(fury.serialize(object)); - } - } - - private AbstractByteBuf encodeSofaRequest(SofaRequest sofaRequest, Map context) - throws SofaRpcException { - Object[] args = sofaRequest.getMethodArgs(); - if (args.length == 1) { - return encode(args[0], context); - } else { - return encode(args, context); - } - } - - private AbstractByteBuf encodeSofaResponse(SofaResponse sofaResponse, Map context) - throws SofaRpcException { - AbstractByteBuf byteBuf; - if (sofaResponse.isError()) { - byteBuf = encode(sofaResponse.getErrorMsg(), context); - } else { - Object appResponse = sofaResponse.getAppResponse(); - if (appResponse instanceof Throwable) { - byteBuf = encode(((Throwable) appResponse).getMessage(), context); + try { + fury.setClassLoader(Thread.currentThread().getContextClassLoader()); + MemoryBuffer writeBuffer = writeBufferLocal.get(); + writeBuffer.writerIndex(0); + if (object == null) { + throw buildSerializeError("Unsupported null message!"); + } else if (object instanceof SofaRequest) { + SofaRequest sofaRequest = (SofaRequest) object; + // 根据SerializeType信息决定序列化器 + boolean genericSerialize = context != null && + isGenericRequest(context.get(RemotingConstants.HEAD_GENERIC_TYPE)); + if (genericSerialize) { + // TODO support generic call + throw buildSerializeError("Generic call is not supported for now."); + } + fury.serialize(writeBuffer, object); + final Object[] args = sofaRequest.getMethodArgs(); + fury.serialize(writeBuffer, args); + } else if (object instanceof SofaResponse) { + fury.serialize(writeBuffer, object); } else { - byteBuf = encode(appResponse, context); + fury.serialize(writeBuffer, object); } + return new ByteArrayWrapperByteBuf(writeBuffer.getBytes(0, writeBuffer.writerIndex())); + } catch (Exception e) { + throw buildSerializeError(e.getMessage(), e); } - return byteBuf; } @Override public Object decode(final AbstractByteBuf data, final Class clazz, final Map context) - throws SofaRpcException { - Object result = null; - if (clazz == null) { - throw buildDeserializeError("class is null!"); - } else if (data.readableBytes() <= 0) { - try { - result = clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw buildDeserializeError(e.getMessage()); + throws SofaRpcException { + if(data.readableBytes() <= 0) { + throw buildDeserializeError("Deserialized array is empty."); + } + MemoryBuffer readBuffer = MemoryBuffer.fromByteArray(data.array()); + try { + fury.setClassLoader(Thread.currentThread().getContextClassLoader()); + if (clazz.equals(SofaRequest.class)) { + SofaRequest sofaRequest = (SofaRequest) fury.deserialize(readBuffer); + String targetServiceName = sofaRequest.getTargetServiceUniqueName(); + if (targetServiceName == null) { + throw buildDeserializeError("Target service name of request is null!"); + } + String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName); + sofaRequest.setInterfaceName(interfaceName); + final Object[] args = (Object[]) fury.deserialize(readBuffer); + sofaRequest.setMethodArgs(args); + return sofaRequest; + } else if (clazz.equals(SofaResponse.class)) { + boolean genericSerialize = context != null && isGenericResponse( + context.get(RemotingConstants.HEAD_GENERIC_TYPE)); + if (genericSerialize) { + // TODO support generic call + throw buildSerializeError("Generic call is not supported for now."); + } + return fury.deserialize(readBuffer); + } else { + return fury.deserialize(readBuffer); } - return result; - } else { - result = fury.deserialize(data.array()); + } catch (Exception e) { + throw buildDeserializeError(e.getMessage(), e); } - return result; } @Override @@ -216,7 +177,7 @@ public void decode(final AbstractByteBuf data, final Object template, final Map< if (template == null) { throw buildDeserializeError("template is null!"); } else if (template instanceof SofaRequest) { - decodeSofaRequest(data, (SofaRequest) template, context); + decodeSofaRequest(data, (SofaRequest) template); } else if (template instanceof SofaResponse) { decodeSofaResponse(data, (SofaResponse) template, context); } else { @@ -225,114 +186,64 @@ public void decode(final AbstractByteBuf data, final Object template, final Map< } - private void decodeSofaRequest(AbstractByteBuf data, SofaRequest sofaRequest, Map head) { - if (head == null) { - throw buildDeserializeError("head is null!"); - } - String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE); - if (targetService != null) { - sofaRequest.setTargetServiceUniqueName(targetService); - String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetService); - sofaRequest.setInterfaceName(interfaceName); - } else { - throw buildDeserializeError("HEAD_TARGET_SERVICE is null"); - } - String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME); - if (methodName != null) { - sofaRequest.setMethodName(methodName); - } else { - throw buildDeserializeError("HEAD_METHOD_NAME is null"); - } - String targetApp = head.remove(RemotingConstants.HEAD_TARGET_APP); - if (targetApp != null) { - sofaRequest.setTargetAppName(targetApp); - } - - // parse tracer and baggage - parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, head, sofaRequest); - if (RpcInvokeContext.isBaggageEnable()) { - parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, head, sofaRequest); - } - for (Map.Entry entry : head.entrySet()) { - sofaRequest.addRequestProp(entry.getKey(), entry.getValue()); + private void decodeSofaRequest(AbstractByteBuf data, SofaRequest template) { + try { + if(data.readableBytes() <= 0) { + throw buildDeserializeError("Deserialized array is empty."); + } + fury.setClassLoader(Thread.currentThread().getContextClassLoader()); + MemoryBuffer readBuffer = MemoryBuffer.fromByteArray(data.array()); + SofaRequest tmp = (SofaRequest) fury.deserialize(readBuffer); + String targetServiceName = tmp.getTargetServiceUniqueName(); + if (targetServiceName == null) { + throw buildDeserializeError("Target service name of request is null!"); + } + // copy values to template + template.setMethodName(tmp.getMethodName()); + template.setMethodArgSigs(tmp.getMethodArgSigs()); + template.setTargetServiceUniqueName(tmp.getTargetServiceUniqueName()); + template.setTargetAppName(tmp.getTargetAppName()); + template.addRequestProps(tmp.getRequestProps()); + String interfaceName = ConfigUniqueNameGenerator.getInterfaceName(targetServiceName); + template.setInterfaceName(interfaceName); + final Object[] args = (Object[]) fury.deserialize(readBuffer); + template.setMethodArgs(args); + } catch (Exception e) { + throw buildDeserializeError(e.getMessage(), e); } - - // according interface and method name to find parameter types - Class[] requestClass = furyHelper.getReqClass(targetService, - sofaRequest.getMethodName()); - Object[] pbReq = decode(data, requestClass, head); - sofaRequest.setMethodArgs(pbReq); - sofaRequest.setMethodArgSigs(parseArgSigs(requestClass)); } - private Object[] decode(final AbstractByteBuf data, final Class[] templateList, - final Map context) - throws SofaRpcException { - ArrayList objectList = new ArrayList<>(); - for (Class clazz : templateList) { - Object result = null; - if (clazz == null) { - throw buildDeserializeError("class is null!"); - } else if (data.readableBytes() <= 0) { - try { - result = clazz.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - throw buildDeserializeError(e.getMessage()); - } + private void decodeSofaResponse(AbstractByteBuf data, SofaResponse template, Map context) { + try { + if(data.readableBytes() <= 0) { + throw buildDeserializeError("Deserialized array is empty."); + } + fury.setClassLoader(Thread.currentThread().getContextClassLoader()); + MemoryBuffer readBuffer = MemoryBuffer.fromByteArray(data.array()); + // 根据SerializeType信息决定序列化器 + boolean genericSerialize = context != null && isGenericResponse( + context.get(RemotingConstants.HEAD_GENERIC_TYPE)); + if (genericSerialize) { + // TODO support generic call + throw buildDeserializeError("Generic call is not supported for now."); } else { - result = fury.deserialize(data.array()); + SofaResponse tmp = (SofaResponse) fury.deserialize(readBuffer); + // copy values to template + template.setErrorMsg(tmp.getErrorMsg()); + template.setAppResponse(tmp.getAppResponse()); + template.setResponseProps(tmp.getResponseProps()); } - objectList.add(result); - } - return objectList.toArray(new Object[objectList.size()]); - } - - private void decodeSofaResponse(AbstractByteBuf data, SofaResponse sofaResponse, Map head) { - if (head == null) { - throw buildDeserializeError("head is null!"); - } - String targetService = head.remove(RemotingConstants.HEAD_TARGET_SERVICE); - if (targetService == null) { - throw buildDeserializeError("HEAD_TARGET_SERVICE is null"); - } - String methodName = head.remove(RemotingConstants.HEAD_METHOD_NAME); - if (methodName == null) { - throw buildDeserializeError("HEAD_METHOD_NAME is null"); - } - - boolean isError = false; - if (StringUtils.TRUE.equals(head.remove(RemotingConstants.HEAD_RESPONSE_ERROR))) { - isError = true; - } - if (!head.isEmpty()) { - sofaResponse.setResponseProps(head); - } - if (isError) { - String errorMessage = (String) decode(data, String.class, head); - sofaResponse.setErrorMsg(errorMessage); - } else { - Class[] responseClass = furyHelper.getRespClass(targetService, methodName); - Object pbRes = decode(data, responseClass[0], head); - sofaResponse.setAppResponse(pbRes); + } catch (Exception e) { + throw buildDeserializeError(e.getMessage(), e); } } - private void parseRequestHeader(String key, Map headerMap, - SofaRequest sofaRequest) { - Map traceMap = new HashMap(8); - CodecUtils.treeCopyTo(key + ".", headerMap, traceMap, true); - if (!traceMap.isEmpty()) { - sofaRequest.addRequestProp(key, traceMap); - } + protected boolean isGenericRequest(String serializeType) { + return serializeType != null && !serializeType.equals(RemotingConstants.SERIALIZE_FACTORY_NORMAL); } - private String[] parseArgSigs(Class[] reqList) { - List argSigs = new ArrayList(); - for (Class type : reqList) { - argSigs.add(type.getCanonicalName()); - } - - return argSigs.toArray(new String[argSigs.size()]); + protected boolean isGenericResponse(String serializeType) { + return serializeType != null && serializeType.equals(RemotingConstants.SERIALIZE_FACTORY_GENERIC); } } diff --git a/codec/codec-fury/src/main/resources/configuration.yml b/codec/codec-fury/src/main/resources/configuration.yml deleted file mode 100644 index e36df54cd..000000000 --- a/codec/codec-fury/src/main/resources/configuration.yml +++ /dev/null @@ -1,14 +0,0 @@ -whitelist: - - com.alipay.sofa.rpc.codec.fury.model.whitelist.* - - com.alipay.sofa.rpc.codec.fury.model.Registered.* - - -register: - - com.alipay.sofa.rpc.codec.fury.model.Registered.DemoRequest - - com.alipay.sofa.rpc.codec.fury.model.Registered.DemoResponse - - com.alipay.sofa.rpc.codec.fury.model.Registered.HelloService - - com.alipay.sofa.rpc.codec.fury.model.Registered.HelloServiceImpl - - com.alipay.sofa.rpc.codec.fury.model.Registered.RegisteredClass - -blacklist: - - com.alipay.sofa.rpc.codec.fury.model.blacklist.* \ No newline at end of file diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FuryHelperTest.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FuryHelperTest.java deleted file mode 100644 index 15efcbe77..000000000 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FuryHelperTest.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.sofa.rpc.codec.fury; - -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoRequest; -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoResponse; -import com.alipay.sofa.rpc.codec.fury.model.whitelist.DemoService; -import com.alipay.sofa.rpc.codec.fury.model.whitelist.HelloServiceImpl2; -import org.junit.Assert; -import org.junit.Test; - -import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; - -import static org.junit.Assert.assertSame; - -/** - * @author lipan - */ -public class FuryHelperTest { - - private final FuryHelper furyHelper = new FuryHelper(); - - @Test - public void testGetReqClass() { - Class[] req = furyHelper.getReqClass( - DemoService.class.getCanonicalName(), "say"); - assertSame(req[0], DemoRequest.class); - } - - @Test - public void testGetResClass() { - Class[] res = furyHelper.getRespClass( - DemoService.class.getCanonicalName(), "say"); - assertSame(res[0], DemoResponse.class); - } - - @Test - public void testHotUpdate() throws ClassNotFoundException { - FuryHelper furyHelper = new FuryHelper(); - - // 获取初始的请求类和响应类 - Class[] initialReqClasses = furyHelper - .getReqClass("com.alipay.sofa.rpc.codec.fury.model.Registered.HelloServiceImpl", "sayHello"); - Class[] initialRespClass = furyHelper.getRespClass( - "com.alipay.sofa.rpc.codec.fury.model.Registered.HelloServiceImpl", - "sayHello"); - - URL url4Impl2 = HelloServiceImpl2.class.getProtectionDomain().getCodeSource().getLocation(); - - SpecificTestClassLoader cl2 = new SpecificTestClassLoader("cl2", new URL[] { url4Impl2 }); - cl2.addWhiteListClass(HelloServiceImpl2.class.getName()); - - // 使用类加载器加载更新后的类 - Class updatedInterfaceClass = cl2 - .loadClass("com.alipay.sofa.rpc.codec.fury.model.whitelist.HelloServiceImpl2"); - - // 更新FuryHelper中的类加载器 - ClassLoader loader = furyHelper.getClassLoader(updatedInterfaceClass.getName()); - - // 获取更新后的请求类和响应类 - Class[] updatedReqClasses = furyHelper.getReqClass(updatedInterfaceClass.getName(), "sayHello"); - Class[] updatedRespClass = furyHelper.getRespClass(updatedInterfaceClass.getName(), "sayHello"); - - // 检查是否使用了新的类加载器 - Assert.assertEquals(initialReqClasses[0].getClassLoader(), updatedReqClasses[0].getClassLoader()); - Assert.assertEquals(initialRespClass[0].getClassLoader(), updatedRespClass[0].getClassLoader()); - } - - class SpecificTestClassLoader extends URLClassLoader { - - private String identity; - - private ClassLoader refClassLoader; - - private Set blackList = new HashSet<>(); - - /** active only if not null */ - private Set whiteList = new HashSet<>(); - - public SpecificTestClassLoader(String identity, URL[] urls) { - super(urls); - this.identity = identity; - } - - public SpecificTestClassLoader(String identity, URL[] urls, ClassLoader ref) { - super(urls); - this.identity = identity; - this.refClassLoader = ref; - } - - @Override - public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - Class clazz = null; - - // skip load if in black list - if (!blackList.contains(name)) { - clazz = whiteListLoad(name, resolve); - } - - if (clazz == null) { - clazz = refClassLoader.loadClass(name); - } - - if (clazz == null) { - throw new ClassNotFoundException(); - } - - return clazz; - } - - @Override - public Enumeration getResources(String name) throws IOException { - Enumeration urls = super.getResources(name); - if (!urls.hasMoreElements()) { - urls = refClassLoader.getResources(name); - } - return urls; - } - - @Override - public URL getResource(String name) { - URL url = super.getResource(name); - if (url == null) { - url = refClassLoader.getResource(name); - } - return url; - } - - /** do load only if white list is not empty and class do in white list */ - private Class whiteListLoad(String className, boolean resolve) throws ClassNotFoundException { - Class clazz = null; - if (!whiteList.isEmpty()) { - if (!whiteList.contains(className)) { - // class is not allowed to load with current cl - return null; - } - } - - return super.loadClass(className, resolve); - } - - public void addBlackListClass(String className) { - this.blackList.add(className); - } - - public void addWhiteListClass(String className) { - this.whiteList.add(className); - } - } -} diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FurySerializerTest.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FurySerializerTest.java index 844c81c43..32e3c3f3c 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FurySerializerTest.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/FurySerializerTest.java @@ -17,15 +17,12 @@ package com.alipay.sofa.rpc.codec.fury; import com.alipay.sofa.rpc.codec.Serializer; -import com.alipay.sofa.rpc.codec.fury.model.blacklist.NotRegister; -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoRequest; -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoResponse; +import com.alipay.sofa.rpc.codec.fury.model.blacklist.BlackListClass; +import com.alipay.sofa.rpc.codec.fury.model.none.NoneClassHasBlackClass; +import com.alipay.sofa.rpc.codec.fury.model.whitelist.DemoRequest; +import com.alipay.sofa.rpc.codec.fury.model.whitelist.DemoResponse; +import com.alipay.sofa.rpc.codec.fury.model.whitelist.WhiteClassHasBlackClass; import com.alipay.sofa.rpc.codec.fury.model.whitelist.DemoService; -import com.alipay.sofa.rpc.codec.fury.model.Registered.RegisteredClass; -import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory; -import org.junit.Assert; -import org.junit.Test; - import com.alipay.sofa.rpc.common.RemotingConstants; import com.alipay.sofa.rpc.common.RpcConstants; import com.alipay.sofa.rpc.core.exception.SofaRpcException; @@ -33,13 +30,13 @@ import com.alipay.sofa.rpc.core.request.RequestBase; import com.alipay.sofa.rpc.core.request.SofaRequest; import com.alipay.sofa.rpc.core.response.SofaResponse; +import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory; import com.alipay.sofa.rpc.transport.AbstractByteBuf; import com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf; +import org.junit.Assert; +import org.junit.Test; -import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -69,20 +66,6 @@ public void encodeAndDecode() { String dst = (String) serializer.decode(data, String.class, null); Assert.assertEquals("xxx", dst); - Date object = new Date(); - try { - AbstractByteBuf encode = serializer.encode(object, null); - Assert.assertEquals(object, serializer.decode(encode, Date.class, null)); - } catch (Exception e) { - Assert.fail(); - } - - try { - serializer.encode(1, null); - } catch (Exception e) { - Assert.fail(); - } - try { serializer.decode(data, null, null); Assert.fail(); @@ -95,111 +78,38 @@ public void encodeAndDecode() { } catch (Exception e) { } - - } - - @Test - public void testChecker() throws Exception { - RegisteredClass registeredClass = new RegisteredClass(); - try { - // registered this class - serializer.encode(registeredClass, null); - } catch (Exception e) { - Assert.fail(); - } - - NotRegister notRegister = new NotRegister(); - try { - //Not registered this class - serializer.encode(notRegister, null); - Assert.fail(); - } catch (Exception e) { - - } - - // test add or delete - serializer.addWhiteList("com.alipay.sofa.rpc.codec.fury.model.blacklist.*"); - try { - //Not registered this class - serializer.encode(notRegister, null); - } catch (Exception e) { - Assert.fail(); - } - - serializer.addBlackList("com.alipay.sofa.rpc.codec.fury.model.blacklist.*"); - try { - //Not registered this class - serializer.encode(notRegister, null); - Assert.fail(); - } catch (Exception e) { - - } - } @Test public void testSofaRequest() throws Exception { SofaRequest request = buildRequest(); AbstractByteBuf data = serializer.encode(request, null); - try { - serializer.decode(data, SofaRequest.class, null); - } catch (Exception e) { - Assert.fail(); - } - try { - serializer.decode(data, new SofaRequest(), null); - Assert.fail(); - } catch (Exception e) { - - } - - Map head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say"); - head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); - head.put("unkown", "yes"); + SofaRequest decode = (SofaRequest) serializer.decode(data, SofaRequest.class, null); + assertEqualsSofaRequest(request, decode); SofaRequest newRequest = new SofaRequest(); - serializer.decode(data, newRequest, head); - - Assert.assertEquals(newRequest.getInterfaceName(), request.getInterfaceName()); - Assert.assertEquals(newRequest.getMethodName(), request.getMethodName()); - Assert.assertArrayEquals(newRequest.getMethodArgSigs(), request.getMethodArgSigs()); - Assert.assertEquals(newRequest.getMethodArgs().length, request.getMethodArgs().length); - Assert.assertEquals("name", ((DemoRequest) newRequest.getMethodArgs()[0]).getName()); - Assert.assertEquals(newRequest.getTargetServiceUniqueName(), request.getTargetServiceUniqueName()); - Assert.assertEquals(newRequest.getTargetAppName(), request.getTargetAppName()); - Assert.assertEquals(newRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME), - request.getRequestProp(RemotingConstants.RPC_TRACE_NAME)); + serializer.decode(data, newRequest, null); + assertEqualsSofaRequest(request, newRequest); // null request - head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say"); - head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); newRequest = new SofaRequest(); - serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newRequest, head); - final Object[] methodArgs = newRequest.getMethodArgs(); - Assert.assertEquals(null, ((DemoRequest) methodArgs[0]).getName()); + try { + serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newRequest, null); + Assert.fail(); + } catch (Exception e) { + + } } @Test public void testSofaResponse() throws Exception { SofaResponse response = new SofaResponse(); - response.setAppResponse("1233"); + final DemoResponse demoAppResponse = new DemoResponse(); + demoAppResponse.setWord("result"); + response.setAppResponse(demoAppResponse); AbstractByteBuf data = serializer.encode(response, null); - try { - serializer.decode(data, SofaResponse.class, null); - } catch (Exception e) { - Assert.fail(); - } - try { serializer.decode(data, null, null); Assert.fail(); @@ -208,85 +118,42 @@ public void testSofaResponse() throws Exception { } try { - serializer.decode(data, new SofaResponse(), null); + serializer.decode(data, new Object(), null); Assert.fail(); } catch (Exception e) { } + SofaResponse decode = (SofaResponse) serializer.decode(data, SofaResponse.class, null); + Assert.assertFalse(decode.isError()); + Assert.assertEquals(response.getAppResponse(), decode.getAppResponse()); + Assert.assertEquals("result", ((DemoResponse) decode.getAppResponse()).getWord()); + // success response - Map head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say"); - head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); - response = new SofaResponse(); - final DemoResponse response1 = new DemoResponse(); - response1.setWord("result"); - response.setAppResponse(response1); - data = serializer.encode(response, null); SofaResponse newResponse = new SofaResponse(); - serializer.decode(data, newResponse, head); + serializer.decode(data, newResponse, null); Assert.assertFalse(newResponse.isError()); Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse()); Assert.assertEquals("result", ((DemoResponse) newResponse.getAppResponse()).getWord()); // null response - head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); newResponse = new SofaResponse(); - serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, head); - Assert.assertFalse(newResponse.isError()); - Assert.assertNotNull(newResponse.getAppResponse()); - Assert.assertEquals(null, ((DemoResponse) newResponse.getAppResponse()).getWord()); + try { + serializer.decode(new ByteArrayWrapperByteBuf(new byte[0]), newResponse, null); + Assert.fail(); + } catch (Exception e) { + } // error response - head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); - head.put(RemotingConstants.HEAD_RESPONSE_ERROR, "true"); response = new SofaResponse(); response.setErrorMsg("1233"); data = serializer.encode(response, null); newResponse = new SofaResponse(); - serializer.decode(data, newResponse, head); + serializer.decode(data, newResponse, null); Assert.assertTrue(newResponse.isError()); Assert.assertEquals(response.getErrorMsg(), newResponse.getErrorMsg()); } - @Test - public void testListResponse() { - // success response - Map head = new HashMap(); - head.put(RemotingConstants.HEAD_TARGET_SERVICE, DemoService.class.getCanonicalName() + ":1.0"); - head.put(RemotingConstants.HEAD_METHOD_NAME, "say3"); - head.put(RemotingConstants.HEAD_TARGET_APP, "targetApp"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".a", "xxx"); - head.put(RemotingConstants.RPC_TRACE_NAME + ".b", "yyy"); - SofaResponse response = new SofaResponse(); - final DemoResponse response1 = new DemoResponse(); - response1.setWord("result"); - - List listResponse = new ArrayList(); - listResponse.add(response1); - - response.setAppResponse(listResponse); - - AbstractByteBuf data = serializer.encode(response, null); - SofaResponse newResponse = new SofaResponse(); - serializer.decode(data, newResponse, head); - Assert.assertFalse(newResponse.isError()); - Assert.assertEquals(response.getAppResponse(), newResponse.getAppResponse()); - Assert.assertEquals("result", ((List) newResponse.getAppResponse()).get(0).getWord()); - - } - private SofaRequest buildRequest() throws NoSuchMethodException { SofaRequest request = new SofaRequest(); request.setInterfaceName(DemoService.class.getName()); @@ -324,4 +191,98 @@ public void onSofaException(SofaRpcException sofaException, String methodName, R return request; } + private void assertEqualsSofaRequest(SofaRequest request, SofaRequest decode) { + Assert.assertEquals(decode.getInterfaceName(), request.getInterfaceName()); + Assert.assertEquals(decode.getMethodName(), request.getMethodName()); + Assert.assertArrayEquals(decode.getMethodArgSigs(), request.getMethodArgSigs()); + Assert.assertEquals(decode.getMethodArgs().length, request.getMethodArgs().length); + Assert.assertEquals("name", ((DemoRequest) decode.getMethodArgs()[0]).getName()); + Assert.assertEquals(decode.getTargetServiceUniqueName(), request.getTargetServiceUniqueName()); + Assert.assertEquals(decode.getTargetAppName(), request.getTargetAppName()); + Assert.assertEquals(decode.getRequestProp(RemotingConstants.RPC_TRACE_NAME), + request.getRequestProp(RemotingConstants.RPC_TRACE_NAME)); + } + + @Test + public void testChecker() throws Exception { + // default fury checkMode is whitelist + WhiteClassHasBlackClass whiteClassNullBlackClass = new WhiteClassHasBlackClass(); + NoneClassHasBlackClass noneClassNullBlackClass = new NoneClassHasBlackClass(); + + BlackListClass blackListClass = new BlackListClass(); + WhiteClassHasBlackClass whiteClassHasBlackClass = new WhiteClassHasBlackClass(); + whiteClassHasBlackClass.setBlackListClass(blackListClass); + NoneClassHasBlackClass noneClassHasBlackClass = new NoneClassHasBlackClass(); + noneClassHasBlackClass.setBlackListClass(blackListClass); + + try { + serializer.encode(noneClassNullBlackClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + try { + serializer.encode(noneClassHasBlackClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + try { + serializer.encode(blackListClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + serializer.encode(whiteClassNullBlackClass, null); + try { + serializer.encode(whiteClassHasBlackClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + // test change fury checkMode to blacklist + System.getProperties().put("sofa.rpc.codec.fury.checkMode", "blacklist"); + FurySerializer furySerializer = new FurySerializer(); + + furySerializer.encode(noneClassNullBlackClass, null); + + try { + furySerializer.encode(noneClassHasBlackClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + try { + //Not registered this class + furySerializer.encode(blackListClass, null); + Assert.fail(); + } catch (Exception e) { + + } + + furySerializer.encode(whiteClassNullBlackClass, null); + try { + furySerializer.encode(whiteClassHasBlackClass, null); + Assert.fail(); + } catch (Exception e) { + + } + System.getProperties().remove("sofa.rpc.codec.fury.checkMode"); + + // test change fury checkMode to none + System.getProperties().put("sofa.rpc.codec.fury.checkMode", "none"); + FurySerializer noneFurySerializer = new FurySerializer(); + noneFurySerializer.encode(noneClassNullBlackClass, null); + noneFurySerializer.encode(noneClassHasBlackClass, null); + noneFurySerializer.encode(blackListClass, null); + noneFurySerializer.encode(whiteClassNullBlackClass, null); + noneFurySerializer.encode(whiteClassHasBlackClass, null); + System.getProperties().remove("sofa.rpc.codec.fury.checkMode"); + } + } \ No newline at end of file diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloService.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloService.java deleted file mode 100644 index 31852a6ca..000000000 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloService.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.sofa.rpc.codec.fury.model.Registered; - -import com.alipay.sofa.rpc.codec.fury.model.whitelist.Person; - -/** - * @author lipan - */ -public interface HelloService { - - String sayHello(Person person); - -} \ No newline at end of file diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/RegisteredClass.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/RegisteredClass.java deleted file mode 100644 index bf81d3531..000000000 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/RegisteredClass.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.sofa.rpc.codec.fury.model.Registered; - -import com.alipay.sofa.rpc.codec.fury.model.blacklist.NotRegister; - -/** - * @author lipan - */ -public class RegisteredClass { - public Integer key; - public NotRegister temp; -} diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/NotRegister.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/BlackListClass.java similarity index 86% rename from codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/NotRegister.java rename to codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/BlackListClass.java index 6d8548d70..78281a910 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/NotRegister.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/blacklist/BlackListClass.java @@ -16,11 +16,9 @@ */ package com.alipay.sofa.rpc.codec.fury.model.blacklist; -import com.alipay.sofa.rpc.codec.fury.model.Registered.RegisteredClass; - /** * @author lipan */ -public class NotRegister { - public RegisteredClass request; +public class BlackListClass { + private String test; } diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloServiceImpl.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/none/NoneClassHasBlackClass.java similarity index 64% rename from codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloServiceImpl.java rename to codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/none/NoneClassHasBlackClass.java index d982efd9c..8ddaaa9a8 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/HelloServiceImpl.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/none/NoneClassHasBlackClass.java @@ -14,17 +14,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.fury.model.Registered; +package com.alipay.sofa.rpc.codec.fury.model.none; -import com.alipay.sofa.rpc.codec.fury.model.whitelist.Person; +import com.alipay.sofa.rpc.codec.fury.model.blacklist.BlackListClass; /** - * @author lipan + * @author Even + * @date 2024/1/2 11:33 */ -public class HelloServiceImpl implements HelloService { +public class NoneClassHasBlackClass { - @Override - public String sayHello(Person person) { - return "Hello, " + person.getName() + ". You are " + person.getAge() + " years old."; + private BlackListClass blackListClass; + + public BlackListClass getBlackListClass() { + return blackListClass; + } + + public void setBlackListClass(BlackListClass blackListClass) { + this.blackListClass = blackListClass; } -} \ No newline at end of file +} diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoRequest.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoRequest.java similarity index 94% rename from codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoRequest.java rename to codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoRequest.java index f1068e5b3..7b9bb7129 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoRequest.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoRequest.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.fury.model.Registered; +package com.alipay.sofa.rpc.codec.fury.model.whitelist; /** * @author lipan diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoResponse.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoResponse.java similarity index 96% rename from codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoResponse.java rename to codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoResponse.java index e3a14ffbd..ffca06616 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/Registered/DemoResponse.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoResponse.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.rpc.codec.fury.model.Registered; +package com.alipay.sofa.rpc.codec.fury.model.whitelist; /** * @author lipan diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoService.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoService.java index 8d2c831f1..604473b7b 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoService.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/DemoService.java @@ -16,9 +16,6 @@ */ package com.alipay.sofa.rpc.codec.fury.model.whitelist; -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoRequest; -import com.alipay.sofa.rpc.codec.fury.model.Registered.DemoResponse; - import java.util.List; import java.util.Map; diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/Person.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/Person.java deleted file mode 100644 index 4e8f05376..000000000 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/Person.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.alipay.sofa.rpc.codec.fury.model.whitelist; - -/** - * @author lipan - */ -public class Person { - private String name; - private int age; - - public Person(String name, int age) { - this.name = name; - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } -} \ No newline at end of file diff --git a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/HelloServiceImpl2.java b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/WhiteClassHasBlackClass.java similarity index 71% rename from codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/HelloServiceImpl2.java rename to codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/WhiteClassHasBlackClass.java index ee3acc373..10e3943fb 100644 --- a/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/HelloServiceImpl2.java +++ b/codec/codec-fury/src/test/java/com/alipay/sofa/rpc/codec/fury/model/whitelist/WhiteClassHasBlackClass.java @@ -16,15 +16,20 @@ */ package com.alipay.sofa.rpc.codec.fury.model.whitelist; -import com.alipay.sofa.rpc.codec.fury.model.Registered.HelloService; +import com.alipay.sofa.rpc.codec.fury.model.blacklist.BlackListClass; /** * @author lipan */ -public class HelloServiceImpl2 implements HelloService { +public class WhiteClassHasBlackClass { - @Override - public String sayHello(Person person) { - return "yes, my name is " + person.getName() + ". " + person.getAge() + " years old."; + private BlackListClass blackListClass; + + public BlackListClass getBlackListClass() { + return blackListClass; + } + + public void setBlackListClass(BlackListClass blackListClass) { + this.blackListClass = blackListClass; } -} \ No newline at end of file +} diff --git a/codec/codec-fury/src/test/resources/conf.yml b/codec/codec-fury/src/test/resources/conf.yml deleted file mode 100644 index 7ff308938..000000000 --- a/codec/codec-fury/src/test/resources/conf.yml +++ /dev/null @@ -1,13 +0,0 @@ -whitelist: - com.alipay.sofa.rpc.codec.fury.model.whitelist.*: true - com.alipay.sofa.rpc.codec.fury.model.Registered.*: true - -register: - com.alipay.sofa.rpc.codec.fury.model.Registered.DemoRequest: true - com.alipay.sofa.rpc.codec.fury.model.Registered.DemoResponse: true - com.alipay.sofa.rpc.codec.fury.model.Registered.HelloService: false - com.alipay.sofa.rpc.codec.fury.model.Registered.HelloServiceImpl: true - com.alipay.sofa.rpc.codec.fury.model.Registered.RegisteredClass: true - -blacklist: - com.alipay.sofa.rpc.codec.fury.model.blacklist.*: true \ No newline at end of file diff --git a/codec/codec-fury/src/test/resources/sofa-rpc/serialize_blacklist.txt b/codec/codec-fury/src/test/resources/sofa-rpc/serialize_blacklist.txt new file mode 100644 index 000000000..991e2a54d --- /dev/null +++ b/codec/codec-fury/src/test/resources/sofa-rpc/serialize_blacklist.txt @@ -0,0 +1 @@ +com.alipay.sofa.rpc.codec.fury.model.blacklist. \ No newline at end of file diff --git a/codec/codec-fury/src/test/resources/sofa-rpc/serialize_whitelist.txt b/codec/codec-fury/src/test/resources/sofa-rpc/serialize_whitelist.txt new file mode 100644 index 000000000..6f1718654 --- /dev/null +++ b/codec/codec-fury/src/test/resources/sofa-rpc/serialize_whitelist.txt @@ -0,0 +1 @@ +com.alipay.sofa.rpc.codec.fury.model.whitelist. \ No newline at end of file diff --git a/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/SofaHessianSerializer.java b/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/SofaHessianSerializer.java index ae7eac3f0..06ec4a4a6 100644 --- a/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/SofaHessianSerializer.java +++ b/codec/codec-sofa-hessian/src/main/java/com/alipay/sofa/rpc/codec/sofahessian/SofaHessianSerializer.java @@ -19,6 +19,7 @@ import com.alipay.hessian.ClassNameResolver; import com.alipay.hessian.NameBlackListFilter; import com.alipay.sofa.rpc.codec.AbstractSerializer; +import com.alipay.sofa.rpc.codec.common.BlackAndWhiteListFileLoader; import com.alipay.sofa.rpc.codec.sofahessian.serialize.CustomHessianSerializer; import com.alipay.sofa.rpc.codec.sofahessian.serialize.SofaRequestHessianSerializer; import com.alipay.sofa.rpc.codec.sofahessian.serialize.SofaResponseHessianSerializer; @@ -95,7 +96,7 @@ public SofaHessianSerializer() { if (RpcConfigs.getBooleanValue(RpcOptions.SERIALIZE_BLACKLIST_ENABLE) && SofaConfigs.getBooleanValue(SofaOptions.CONFIG_SERIALIZE_BLACKLIST, true)) { ClassNameResolver resolver = new ClassNameResolver(); - resolver.addFilter(new NameBlackListFilter(BlackListFileLoader.SOFA_SERIALIZE_BLACK_LIST, 8192)); + resolver.addFilter(new NameBlackListFilter(BlackAndWhiteListFileLoader.SOFA_SERIALIZE_BLACK_LIST, 8192)); serializerFactory.setClassNameResolver(resolver); genericSerializerFactory.setClassNameResolver(resolver); } else { diff --git a/codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoaderTest.java b/codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackAndWhiteListFileLoaderTest.java similarity index 71% rename from codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoaderTest.java rename to codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackAndWhiteListFileLoaderTest.java index df934b6a1..05d279e4f 100644 --- a/codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackListFileLoaderTest.java +++ b/codec/codec-sofa-hessian/src/test/java/com/alipay/sofa/rpc/codec/sofahessian/BlackAndWhiteListFileLoaderTest.java @@ -17,6 +17,7 @@ package com.alipay.sofa.rpc.codec.sofahessian; import com.alipay.hessian.NameBlackListFilter; +import com.alipay.sofa.rpc.codec.common.BlackAndWhiteListFileLoader; import com.alipay.sofa.rpc.common.SofaOptions; import org.junit.Assert; import org.junit.Test; @@ -29,17 +30,17 @@ * * @author GengZhang */ -public class BlackListFileLoaderTest { +public class BlackAndWhiteListFileLoaderTest { @Test public void testAll() throws Exception { - List blacks = BlackListFileLoader.SOFA_SERIALIZE_BLACK_LIST; + List blacks = BlackAndWhiteListFileLoader.SOFA_SERIALIZE_BLACK_LIST; Assert.assertNotNull(blacks); String s = System.getProperty(SofaOptions.CONFIG_SERIALIZE_BLACKLIST_OVERRIDE); try { System.setProperty(SofaOptions.CONFIG_SERIALIZE_BLACKLIST_OVERRIDE, "-java.net.Socket"); - blacks = BlackListFileLoader.loadFile("/sofa-rpc/serialize_blacklist.txt"); + blacks = BlackAndWhiteListFileLoader.loadBlackListFile("/sofa-rpc/serialize_blacklist.txt"); } finally { if (s != null) { System.setProperty(SofaOptions.CONFIG_SERIALIZE_BLACKLIST_OVERRIDE, s); @@ -69,51 +70,51 @@ public void testAll() throws Exception { @Test public void overrideBlackList() { List origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-*"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-*"); Assert.assertTrue(origin.size() == 0); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "!*"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "!*"); Assert.assertTrue(origin.size() == 0); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-default"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-default"); Assert.assertTrue(origin.size() == 0); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "!default"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "!default"); Assert.assertTrue(origin.size() == 0); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-*,-com.xxx"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-*,-com.xxx"); Assert.assertTrue(origin.size() == 0); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "aaa,-*,-com.xxx"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "aaa,-*,-com.xxx"); Assert.assertTrue(origin.size() == 1); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-*,aaa"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-*,aaa"); Assert.assertTrue(origin.size() == 1); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-com.xxx"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-com.xxx"); Assert.assertTrue(origin.size() == 2); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "-com.xxx,-com.yyy"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "-com.xxx,-com.yyy"); Assert.assertTrue(origin.size() == 1); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "com.xxx,-com.yyy"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "com.xxx,-com.yyy"); Assert.assertTrue(origin.size() == 2); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "com.aaa,-com.yyy"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "com.aaa,-com.yyy"); Assert.assertTrue(origin.size() == 3); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "com.aaa"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "com.aaa"); Assert.assertTrue(origin.size() == 4); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "com.xxx;com.yyy;com.zzz"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "com.xxx;com.yyy;com.zzz"); Assert.assertTrue(origin.size() == 3); origin = buildOriginList(); - BlackListFileLoader.overrideBlackList(origin, "com.aaa,com.bbb,com.ccc"); + BlackAndWhiteListFileLoader.overrideBlackList(origin, "com.aaa,com.bbb,com.ccc"); Assert.assertTrue(origin.size() == 6); } diff --git a/core/common/src/main/java/com/alipay/sofa/rpc/common/SofaOptions.java b/core/common/src/main/java/com/alipay/sofa/rpc/common/SofaOptions.java index ae46681d5..4a9e1753c 100644 --- a/core/common/src/main/java/com/alipay/sofa/rpc/common/SofaOptions.java +++ b/core/common/src/main/java/com/alipay/sofa/rpc/common/SofaOptions.java @@ -160,6 +160,10 @@ public class SofaOptions { * 序列化覆盖 */ public static final String CONFIG_SERIALIZE_BLACKLIST_OVERRIDE = "rpc_serialize_blacklist_override"; + /** + * 序列化覆盖 + */ + public static final String CONFIG_SERIALIZE_WHITELIST_OVERRIDE = "rpc_serialize_whitelist_override"; //========= GRPC 相关配置 ========== /** diff --git a/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java b/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java index 10f817445..ad6cba191 100644 --- a/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java +++ b/core/common/src/main/java/com/alipay/sofa/rpc/common/config/RpcConfigKeys.java @@ -102,12 +102,13 @@ public class RpcConfigKeys { false, "judge the generic object exception fields.", new String[] { "sofa_rpc_generic_exception_fields" }); + public static final ConfigKey CHECKER_MODE = ConfigKey .build( - "sofa.rpc.codec.fury.whitelist", + "sofa.rpc.codec.fury.checkMode", "whitelist", true, - " The default filtering mode is whitelist.", - new String[] { "sofa_rpc_codec_fury_whitelist" }); + " The default filtering mode is whitelist.", + new String[] { "sofa_rpc_codec_fury_checkMode" }); }