From 7623bb82b2e6581321ffb61b16f24a54e6ac6ced Mon Sep 17 00:00:00 2001 From: KomachiSion Date: Wed, 23 Mar 2022 11:47:54 +0800 Subject: [PATCH] Add requestClass to resource properties for grpc resource parser. --- .../parser/grpc/AbstractGrpcResourceParser.java | 11 ++++++++++- .../parser/grpc/ConfigGrpcResourceParser.java | 6 ------ .../parser/grpc/NamingGrpcResourceParser.java | 7 ------- .../grpc/NamingGrpcResourceParserTest.java | 16 ++++++++++++++++ .../nacos/plugin/auth/constant/Constants.java | 2 ++ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/AbstractGrpcResourceParser.java b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/AbstractGrpcResourceParser.java index f49fd86bb..762fe1640 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/AbstractGrpcResourceParser.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/AbstractGrpcResourceParser.java @@ -18,6 +18,9 @@ package com.alibaba.nacos.auth.parser.grpc; import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.auth.parser.AbstractResourceParser; +import com.alibaba.nacos.plugin.auth.constant.Constants; + +import java.util.Properties; /** * Abstract Grpc Resource Parser. @@ -25,5 +28,11 @@ import com.alibaba.nacos.auth.parser.AbstractResourceParser; * @author xiweng.yy */ public abstract class AbstractGrpcResourceParser extends AbstractResourceParser { - + + @Override + protected Properties getProperties(Request request) { + Properties properties = new Properties(); + properties.setProperty(Constants.Resource.REQUEST_CLASS, request.getClass().getSimpleName()); + return properties; + } } diff --git a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java index dc1ad496d..5f1ec8138 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/ConfigGrpcResourceParser.java @@ -22,7 +22,6 @@ import com.alibaba.nacos.common.utils.ReflectUtils; import com.alibaba.nacos.common.utils.StringUtils; import java.util.List; -import java.util.Properties; /** * Config Grpc resource parser. @@ -59,9 +58,4 @@ public class ConfigGrpcResourceParser extends AbstractGrpcResourceParser { .getFieldValue(request, com.alibaba.nacos.api.common.Constants.DATAID, StringUtils.EMPTY); return StringUtils.isBlank(dataId) ? StringUtils.EMPTY : dataId; } - - @Override - protected Properties getProperties(Request request) { - return new Properties(); - } } diff --git a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java index 13abea610..86c248b8d 100644 --- a/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java +++ b/auth/src/main/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParser.java @@ -23,8 +23,6 @@ import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.common.utils.ReflectUtils; import com.alibaba.nacos.common.utils.StringUtils; -import java.util.Properties; - /** * Naming Grpc resource parser. * @@ -61,9 +59,4 @@ public class NamingGrpcResourceParser extends AbstractGrpcResourceParser { } return StringUtils.isBlank(serviceName) ? StringUtils.EMPTY : serviceName; } - - @Override - protected Properties getProperties(Request request) { - return new Properties(); - } } diff --git a/auth/src/test/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParserTest.java b/auth/src/test/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParserTest.java index 7e71d1544..a175ba3b3 100644 --- a/auth/src/test/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParserTest.java +++ b/auth/src/test/java/com/alibaba/nacos/auth/parser/grpc/NamingGrpcResourceParserTest.java @@ -50,6 +50,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -62,6 +64,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -74,6 +78,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -86,6 +92,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -98,6 +106,8 @@ public class NamingGrpcResourceParserTest { assertEquals(StringUtils.EMPTY, actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -110,6 +120,8 @@ public class NamingGrpcResourceParserTest { assertEquals(StringUtils.EMPTY, actual.getGroup()); assertEquals("testS", actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -122,6 +134,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals(StringUtils.EMPTY, actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(MockNamingRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } @Test @@ -134,6 +148,8 @@ public class NamingGrpcResourceParserTest { assertEquals("testG", actual.getGroup()); assertEquals(StringUtils.EMPTY, actual.getName()); assertEquals(Constants.Naming.NAMING_MODULE, actual.getType()); + assertEquals(NotifySubscriberRequest.class.getSimpleName(), actual.getProperties().getProperty( + com.alibaba.nacos.plugin.auth.constant.Constants.Resource.REQUEST_CLASS)); } private AbstractNamingRequest mockNamingRequest(String testNs, String testG, String testS) { diff --git a/plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java b/plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java index 298aa7dbc..76b5b87a3 100644 --- a/plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java +++ b/plugin/auth/src/main/java/com/alibaba/nacos/plugin/auth/constant/Constants.java @@ -45,6 +45,8 @@ public class Constants { public static final String ANY = "*"; public static final String ACTION = "action"; + + public static final String REQUEST_CLASS = "requestClass"; } public static class Identity {