From 1592c2bd9e2ede43a1c935aeeb3e994238f65aff Mon Sep 17 00:00:00 2001 From: deepanshuagarwal Date: Wed, 9 Mar 2022 12:15:57 +0530 Subject: [PATCH 1/5] Catering to all possible routes for Post Request Signed-off-by: deepanshuagarwal --- .../springboot/DaprBeanPostProcessor.java | 88 +++++++++++++++---- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java index 56cae1b298..82edc1d494 100644 --- a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java +++ b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java @@ -13,20 +13,24 @@ package io.dapr.springboot; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.dapr.Topic; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.EmbeddedValueResolver; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +import io.dapr.Topic; /** * Handles Dapr annotations in Spring Controllers. @@ -68,7 +72,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw * Subscribe to topics based on {@link Topic} annotations on the given class and any of ancestor classes. * @param clazz Controller class where {@link Topic} is expected. */ - private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embeddedValueResolver) { + private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embeddedValueResolver) { if (clazz == null) { return; } @@ -79,16 +83,12 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde if (topic == null) { continue; } - - String route = topic.name(); - PostMapping mapping = method.getAnnotation(PostMapping.class); - - if (mapping != null && mapping.path() != null && mapping.path().length >= 1) { - route = mapping.path()[0]; - } else if (mapping != null && mapping.value() != null && mapping.value().length >= 1) { - route = mapping.value()[0]; + RequestMapping clazzRequestMapping = (RequestMapping) clazz.getAnnotation(RequestMapping.class); + String[] clazzLevelRoute = null; + if(clazzRequestMapping != null) { + clazzLevelRoute = clazzRequestMapping.value(); } - + String[] postValueArray = getRouteForPost(method, topic.name()); String topicName = embeddedValueResolver.resolveStringValue(topic.name()); String pubSubName = embeddedValueResolver.resolveStringValue(topic.pubsubName()); if ((topicName != null) && (topicName.length() > 0) && pubSubName != null && pubSubName.length() > 0) { @@ -96,11 +96,63 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde TypeReference> typeRef = new TypeReference>() {}; Map metadata = MAPPER.readValue(topic.metadata(), typeRef); - DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, metadata); + + if(postValueArray != null && postValueArray.length >= 1) { + for(String postvalue: postValueArray) { + if(clazzLevelRoute != null && clazzLevelRoute.length >= 1) { + for(String clazzLevelValue: clazzLevelRoute) { + String route = clazzLevelValue + confirmLeadingSlash(postvalue); + System.out.println("route: " + route); + DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, metadata); + } + } else { + System.out.println("postValue: " + postvalue); + DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, postvalue, metadata); + } + } + } } catch (JsonProcessingException e) { throw new IllegalArgumentException("Error while parsing metadata: " + e.toString()); } } } } + + private static String confirmLeadingSlash(String s) { + if (s != null && s.length() >= 1) { + if (!s.substring(0, 1).equals("/")) { + return "/" + s; + } + } + return s; + } + + private static String[] getRouteForPost(Method method, String topicName) { + String[] postValueArray = new String[] {topicName}; + PostMapping postMapping = method.getAnnotation(PostMapping.class); + if (postMapping != null) { + if(postMapping.path() != null && postMapping.path().length >= 1) { + postValueArray = postMapping.path(); + } else if (postMapping.value() != null && postMapping.value().length >= 1) { + postValueArray = new String[postMapping.value().length]; + postValueArray = postMapping.value(); + } + } else { + RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); + for(RequestMethod reqMethod: reqMapping.method()) { + if(reqMethod == RequestMethod.POST) { + if(reqMapping.path() != null && reqMapping.path().length >= 1) { + postValueArray = new String[reqMapping.path().length]; + postValueArray = reqMapping.path(); + } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { + postValueArray = new String[reqMapping.value().length]; + postValueArray = reqMapping.value(); + } + break; + } + } + } + return postValueArray; + } + } From a2edb213552f93e366937f6baa0576481fe4f263 Mon Sep 17 00:00:00 2001 From: deepanshuagarwal Date: Mon, 14 Mar 2022 18:36:58 +0530 Subject: [PATCH 2/5] Correcting checkstyle related violations Signed-off-by: deepanshuagarwal --- .../springboot/DaprBeanPostProcessor.java | 130 +++++++++--------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java index 82edc1d494..61491c0ba6 100644 --- a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java +++ b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java @@ -13,10 +13,10 @@ package io.dapr.springboot; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dapr.Topic; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -25,12 +25,9 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import io.dapr.Topic; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; /** * Handles Dapr annotations in Spring Controllers. @@ -72,7 +69,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw * Subscribe to topics based on {@link Topic} annotations on the given class and any of ancestor classes. * @param clazz Controller class where {@link Topic} is expected. */ - private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embeddedValueResolver) { + private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embeddedValueResolver) { if (clazz == null) { return; } @@ -83,33 +80,36 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embe if (topic == null) { continue; } - RequestMapping clazzRequestMapping = (RequestMapping) clazz.getAnnotation(RequestMapping.class); + RequestMapping clazzRequestMapping = + (RequestMapping) clazz.getAnnotation(RequestMapping.class); String[] clazzLevelRoute = null; - if(clazzRequestMapping != null) { - clazzLevelRoute = clazzRequestMapping.value(); + if (clazzRequestMapping != null) { + clazzLevelRoute = clazzRequestMapping.value(); } String[] postValueArray = getRouteForPost(method, topic.name()); String topicName = embeddedValueResolver.resolveStringValue(topic.name()); String pubSubName = embeddedValueResolver.resolveStringValue(topic.pubsubName()); - if ((topicName != null) && (topicName.length() > 0) && pubSubName != null && pubSubName.length() > 0) { + if ((topicName != null) && (topicName.length() > 0) + && pubSubName != null && pubSubName.length() > 0) { try { - TypeReference> typeRef - = new TypeReference>() {}; + TypeReference> typeRef = + new TypeReference>() { + }; Map metadata = MAPPER.readValue(topic.metadata(), typeRef); - - if(postValueArray != null && postValueArray.length >= 1) { - for(String postvalue: postValueArray) { - if(clazzLevelRoute != null && clazzLevelRoute.length >= 1) { - for(String clazzLevelValue: clazzLevelRoute) { - String route = clazzLevelValue + confirmLeadingSlash(postvalue); - System.out.println("route: " + route); - DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, metadata); - } - } else { - System.out.println("postValue: " + postvalue); - DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, postvalue, metadata); - } - } + + if (postValueArray != null && postValueArray.length >= 1) { + for (String postValue : postValueArray) { + if (clazzLevelRoute != null && clazzLevelRoute.length >= 1) { + for (String clazzLevelValue : clazzLevelRoute) { + String route = clazzLevelValue + confirmLeadingSlash(postValue); + DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, + metadata); + } + } else { + DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, postValue, + metadata); + } + } } } catch (JsonProcessingException e) { throw new IllegalArgumentException("Error while parsing metadata: " + e.toString()); @@ -117,42 +117,42 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embe } } } - - private static String confirmLeadingSlash(String s) { - if (s != null && s.length() >= 1) { - if (!s.substring(0, 1).equals("/")) { - return "/" + s; - } - } - return s; - } - + + private static String confirmLeadingSlash(String s) { + if (s != null && s.length() >= 1) { + if (!s.substring(0, 1).equals("/")) { + return "/" + s; + } + } + return s; + } + private static String[] getRouteForPost(Method method, String topicName) { - String[] postValueArray = new String[] {topicName}; - PostMapping postMapping = method.getAnnotation(PostMapping.class); - if (postMapping != null) { - if(postMapping.path() != null && postMapping.path().length >= 1) { - postValueArray = postMapping.path(); - } else if (postMapping.value() != null && postMapping.value().length >= 1) { - postValueArray = new String[postMapping.value().length]; - postValueArray = postMapping.value(); + String[] postValueArray = new String[] {topicName}; + PostMapping postMapping = method.getAnnotation(PostMapping.class); + if (postMapping != null) { + if (postMapping.path() != null && postMapping.path().length >= 1) { + postValueArray = postMapping.path(); + } else if (postMapping.value() != null && postMapping.value().length >= 1) { + postValueArray = new String[postMapping.value().length]; + postValueArray = postMapping.value(); + } + } else { + RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); + for (RequestMethod reqMethod : reqMapping.method()) { + if (reqMethod == RequestMethod.POST) { + if (reqMapping.path() != null && reqMapping.path().length >= 1) { + postValueArray = new String[reqMapping.path().length]; + postValueArray = reqMapping.path(); + } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { + postValueArray = new String[reqMapping.value().length]; + postValueArray = reqMapping.value(); } - } else { - RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); - for(RequestMethod reqMethod: reqMapping.method()) { - if(reqMethod == RequestMethod.POST) { - if(reqMapping.path() != null && reqMapping.path().length >= 1) { - postValueArray = new String[reqMapping.path().length]; - postValueArray = reqMapping.path(); - } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { - postValueArray = new String[reqMapping.value().length]; - postValueArray = reqMapping.value(); - } - break; - } - } + break; + } } - return postValueArray; + } + return postValueArray; } - + } From 70435c51344db71706b98817589466b9b85af8ee Mon Sep 17 00:00:00 2001 From: deepanshuagarwal Date: Wed, 16 Mar 2022 14:01:18 +0530 Subject: [PATCH 3/5] Refactoring and Adding UTs for post method routes Signed-off-by: deepanshuagarwal --- sdk-springboot/pom.xml | 8 ++ .../springboot/DaprBeanPostProcessor.java | 75 +++------------ .../util/SpringProcessorHelper.java | 96 +++++++++++++++++++ .../dapr/springboot/util/MockController.java | 35 +++++++ .../util/MockControllerNoClazzAnnotation.java | 36 +++++++ .../util/SpringProcessorHelperTest.java | 60 ++++++++++++ 6 files changed, 248 insertions(+), 62 deletions(-) create mode 100644 sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java create mode 100644 sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java create mode 100644 sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java create mode 100644 sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index 042c065807..88ac928286 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -88,6 +88,11 @@ compile true + + junit + junit + test + @@ -149,6 +154,9 @@ BUNDLE + + io.dapr.springboot.util + LINE diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java index 61491c0ba6..98e16144e1 100644 --- a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java +++ b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.dapr.Topic; +import io.dapr.springboot.util.SpringProcessorHelper; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -25,8 +26,11 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; + +import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -80,37 +84,20 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde if (topic == null) { continue; } - RequestMapping clazzRequestMapping = - (RequestMapping) clazz.getAnnotation(RequestMapping.class); - String[] clazzLevelRoute = null; - if (clazzRequestMapping != null) { - clazzLevelRoute = clazzRequestMapping.value(); - } - String[] postValueArray = getRouteForPost(method, topic.name()); + String topicName = embeddedValueResolver.resolveStringValue(topic.name()); String pubSubName = embeddedValueResolver.resolveStringValue(topic.pubsubName()); - if ((topicName != null) && (topicName.length() > 0) - && pubSubName != null && pubSubName.length() > 0) { + if ((topicName != null) && (topicName.length() > 0) && pubSubName != null && pubSubName.length() > 0) { try { - TypeReference> typeRef = - new TypeReference>() { - }; + TypeReference> typeRef + = new TypeReference>() {}; Map metadata = MAPPER.readValue(topic.metadata(), typeRef); - - if (postValueArray != null && postValueArray.length >= 1) { - for (String postValue : postValueArray) { - if (clazzLevelRoute != null && clazzLevelRoute.length >= 1) { - for (String clazzLevelValue : clazzLevelRoute) { - String route = clazzLevelValue + confirmLeadingSlash(postValue); - DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, - metadata); - } - } else { - DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, postValue, - metadata); - } - } + List routes = SpringProcessorHelper.getAllCompleteRoutesForPost(clazz, method, topic.name()); + for (String route : routes) { + DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, + metadata); } + } catch (JsonProcessingException e) { throw new IllegalArgumentException("Error while parsing metadata: " + e.toString()); } @@ -118,41 +105,5 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde } } - private static String confirmLeadingSlash(String s) { - if (s != null && s.length() >= 1) { - if (!s.substring(0, 1).equals("/")) { - return "/" + s; - } - } - return s; - } - - private static String[] getRouteForPost(Method method, String topicName) { - String[] postValueArray = new String[] {topicName}; - PostMapping postMapping = method.getAnnotation(PostMapping.class); - if (postMapping != null) { - if (postMapping.path() != null && postMapping.path().length >= 1) { - postValueArray = postMapping.path(); - } else if (postMapping.value() != null && postMapping.value().length >= 1) { - postValueArray = new String[postMapping.value().length]; - postValueArray = postMapping.value(); - } - } else { - RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); - for (RequestMethod reqMethod : reqMapping.method()) { - if (reqMethod == RequestMethod.POST) { - if (reqMapping.path() != null && reqMapping.path().length >= 1) { - postValueArray = new String[reqMapping.path().length]; - postValueArray = reqMapping.path(); - } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { - postValueArray = new String[reqMapping.value().length]; - postValueArray = reqMapping.value(); - } - break; - } - } - } - return postValueArray; - } } diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java b/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java new file mode 100644 index 0000000000..26b04ba069 --- /dev/null +++ b/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java @@ -0,0 +1,96 @@ +/* + * Copyright 2021 The Dapr Authors + * 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 io.dapr.springboot.util; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * Helper for any/all spring related utility jobs required. + */ +public class SpringProcessorHelper { + + /** + * Method to provide all possible complete routes list fos this post method present in this controller class, + * for mentioned topic. + * + * @param clazz Controller class + * @param method Declared method for posting data + * @param topicName Associated topic name + * @return All possible routes for post mapping for this class and post method + */ + public static List getAllCompleteRoutesForPost(Class clazz, Method method, String topicName) { + List routesList = new ArrayList<>(); + RequestMapping clazzRequestMapping = + (RequestMapping) clazz.getAnnotation(RequestMapping.class); + String[] clazzLevelRoute = null; + if (clazzRequestMapping != null) { + clazzLevelRoute = clazzRequestMapping.value(); + } + String[] postValueArray = getRoutesForPost(method, topicName); + if (postValueArray != null && postValueArray.length >= 1) { + for (String postValue : postValueArray) { + if (clazzLevelRoute != null && clazzLevelRoute.length >= 1) { + for (String clazzLevelValue : clazzLevelRoute) { + String route = clazzLevelValue + confirmLeadingSlash(postValue); + routesList.add(route); + } + } else { + routesList.add(postValue); + } + } + } + return routesList; + } + + private static String[] getRoutesForPost(Method method, String topicName) { + String[] postValueArray = new String[] {topicName}; + PostMapping postMapping = method.getAnnotation(PostMapping.class); + if (postMapping != null) { + if (postMapping.path() != null && postMapping.path().length >= 1) { + postValueArray = postMapping.path(); + } else if (postMapping.value() != null && postMapping.value().length >= 1) { + postValueArray = postMapping.value(); + } + } else { + RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); + for (RequestMethod reqMethod : reqMapping.method()) { + if (reqMethod == RequestMethod.POST) { + if (reqMapping.path() != null && reqMapping.path().length >= 1) { + postValueArray = reqMapping.path(); + } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { + postValueArray = reqMapping.value(); + } + break; + } + } + } + return postValueArray; + } + + private static String confirmLeadingSlash(String path) { + if (path != null && path.length() >= 1) { + if (!path.substring(0, 1).equals("/")) { + return "/" + path; + } + } + return path; + } + +} diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java b/sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java new file mode 100644 index 0000000000..2d93e94d9a --- /dev/null +++ b/sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java @@ -0,0 +1,35 @@ +package io.dapr.springboot.util; + +import org.springframework.web.bind.annotation.*; + +@RequestMapping(value = {"v1", "v2"}) +public class MockController { + + @RequestMapping(value = {"", "/page1", "page2"}, method = {RequestMethod.POST, RequestMethod.PUT}) + public void testMethod1() { + // Do nothing + } + + @PostMapping(path = {"", "/page3", "page4"}) + public void testMethod2() { + // Do nothing + } + + @PostMapping("foo") + public void testMethod3() { + // Do nothing + } + + @PostMapping({"/foo1", "foo2"}) + public void testMethod4() { + // Do nothing + } + + + @RequestMapping(path = {"/bar", "bar1"}, method = {RequestMethod.GET}) + public void testMethod5() { + // Do nothing + } + + +} \ No newline at end of file diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java b/sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java new file mode 100644 index 0000000000..d28b9fc0ba --- /dev/null +++ b/sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java @@ -0,0 +1,36 @@ +package io.dapr.springboot.util; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +public class MockControllerNoClazzAnnotation { + + @RequestMapping(value = {"", "page1", "page2"}, method = {RequestMethod.POST, RequestMethod.PUT}) + public void testMethod1() { + // Do nothing + } + + @PostMapping(path = {"", "page3", "page4"}) + public void testMethod2() { + // Do nothing + } + + @PostMapping("foo") + public void testMethod3() { + // Do nothing + } + + @PostMapping({"foo1", "foo2"}) + public void testMethod4() { + // Do nothing + } + + + @RequestMapping(path = {"bar", "bar1"}, method = {RequestMethod.GET}) + public void testMethod5() { + // Do nothing + } + + +} \ No newline at end of file diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java b/sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java new file mode 100644 index 0000000000..8c7d99b0cf --- /dev/null +++ b/sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java @@ -0,0 +1,60 @@ +package io.dapr.springboot.util; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +@RunWith(Parameterized.class) +public class SpringProcessorHelperTest { + + private final Class clazzToBeTested; + private final String methodToBeTested; + private final String[] expected; + private final boolean expectedResult; + private static final String TOPIC_NAME = "topicName1"; + + public SpringProcessorHelperTest(Class clazzToBeTested, String methodToBeTested, String[] expected, + boolean expectedResult) { + this.clazzToBeTested = clazzToBeTested; + this.methodToBeTested = methodToBeTested; + this.expected = expected; + this.expectedResult = expectedResult; + } + + @Parameterized.Parameters + public static Collection routesTester() { + return Arrays.asList(new Object[][] { + {MockController.class, "testMethod1", new String[] {"v1", "v2", "v1/page1", "v2/page1", "v1/page2", "v2/page2"}, + true}, + {MockController.class, "testMethod2", new String[] {"v1", "v2", "v1/page3", "v2/page3", "v1/page4", "v2/page4"}, + true}, + {MockController.class, "testMethod3", new String[] {"v1/foo", "v2/foo"}, true}, + {MockController.class, "testMethod4", new String[] {"v1/foo1", "v2/foo1", "v1/foo2", "v2/foo2"}, true}, + {MockController.class, "testMethod5", new String[] {"v1/" + TOPIC_NAME, "v2/" + TOPIC_NAME}, true}, + {MockControllerNoClazzAnnotation.class, "testMethod1", new String[] {"", "page1", "page2"}, true}, + {MockControllerNoClazzAnnotation.class, "testMethod2", new String[] {"", "page3", "page4"}, true}, + {MockControllerNoClazzAnnotation.class, "testMethod3", new String[] {"foo"}, true}, + {MockControllerNoClazzAnnotation.class, "testMethod4", new String[] {"foo1", "foo2"}, true}, + {MockControllerNoClazzAnnotation.class, "testMethod5", new String[] {TOPIC_NAME}, true} + }); + } + + + @Test + public void testAllPostRoutesGeneration() throws NoSuchMethodException { + List routesArrayTestMethod1 = SpringProcessorHelper.getAllCompleteRoutesForPost(clazzToBeTested, + clazzToBeTested.getMethod(methodToBeTested), TOPIC_NAME); + Assert.assertEquals(expectedResult, + testingListForOrderAgnosticEquality(Arrays.asList(expected), routesArrayTestMethod1)); + } + + private boolean testingListForOrderAgnosticEquality(List first, List second) { + return (first.size() == second.size() && first.containsAll(second) && second.containsAll(first)); + } + +} From 245575686f205cf27ec1ca77356dd6b6383d252f Mon Sep 17 00:00:00 2001 From: deepanshuagarwal Date: Wed, 16 Mar 2022 22:48:03 +0530 Subject: [PATCH 4/5] Using resolved topic name Signed-off-by: deepanshuagarwal --- .../src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java index 98e16144e1..b12614c338 100644 --- a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java +++ b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java @@ -92,7 +92,7 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde TypeReference> typeRef = new TypeReference>() {}; Map metadata = MAPPER.readValue(topic.metadata(), typeRef); - List routes = SpringProcessorHelper.getAllCompleteRoutesForPost(clazz, method, topic.name()); + List routes = SpringProcessorHelper.getAllCompleteRoutesForPost(clazz, method, topicName); for (String route : routes) { DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, metadata); From 76f49eb50bbbf3d8226f7fb9a7ce5781e1d1ab48 Mon Sep 17 00:00:00 2001 From: deepanshuagarwal Date: Thu, 17 Mar 2022 21:13:49 +0530 Subject: [PATCH 5/5] Shifting routesPostMethod to reduce its access Signed-off-by: deepanshuagarwal --- sdk-springboot/pom.xml | 2 +- .../springboot/DaprBeanPostProcessor.java | 70 +++++++++++++- .../util/SpringProcessorHelper.java | 96 ------------------- ...st.java => DaprBeanPostProcessorTest.java} | 21 ++-- .../springboot/{util => }/MockController.java | 2 +- .../MockControllerNoClazzAnnotation.java | 2 +- 6 files changed, 86 insertions(+), 107 deletions(-) delete mode 100644 sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java rename sdk-springboot/src/test/java/io/dapr/springboot/{util/SpringProcessorHelperTest.java => DaprBeanPostProcessorTest.java} (75%) rename sdk-springboot/src/test/java/io/dapr/springboot/{util => }/MockController.java (95%) rename sdk-springboot/src/test/java/io/dapr/springboot/{util => }/MockControllerNoClazzAnnotation.java (95%) diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index 88ac928286..cd6b2df505 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -155,7 +155,7 @@ BUNDLE - io.dapr.springboot.util + io.dapr.springboot.DaprBeanPostProcessor diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java index b12614c338..cc591184a2 100644 --- a/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java +++ b/sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java @@ -17,7 +17,6 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.dapr.Topic; -import io.dapr.springboot.util.SpringProcessorHelper; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -29,6 +28,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -92,7 +92,7 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde TypeReference> typeRef = new TypeReference>() {}; Map metadata = MAPPER.readValue(topic.metadata(), typeRef); - List routes = SpringProcessorHelper.getAllCompleteRoutesForPost(clazz, method, topicName); + List routes = getAllCompleteRoutesForPost(clazz, method, topicName); for (String route : routes) { DaprRuntime.getInstance().addSubscribedTopic(pubSubName, topicName, route, metadata); @@ -105,5 +105,71 @@ private static void subscribeToTopics(Class clazz, EmbeddedValueResolver embedde } } + /** + * Method to provide all possible complete routes list fos this post method present in this controller class, + * for mentioned topic. + * + * @param clazz Controller class + * @param method Declared method for posting data + * @param topicName Associated topic name + * @return All possible routes for post mapping for this class and post method + */ + private static List getAllCompleteRoutesForPost(Class clazz, Method method, String topicName) { + List routesList = new ArrayList<>(); + RequestMapping clazzRequestMapping = + (RequestMapping) clazz.getAnnotation(RequestMapping.class); + String[] clazzLevelRoute = null; + if (clazzRequestMapping != null) { + clazzLevelRoute = clazzRequestMapping.value(); + } + String[] postValueArray = getRoutesForPost(method, topicName); + if (postValueArray != null && postValueArray.length >= 1) { + for (String postValue : postValueArray) { + if (clazzLevelRoute != null && clazzLevelRoute.length >= 1) { + for (String clazzLevelValue : clazzLevelRoute) { + String route = clazzLevelValue + confirmLeadingSlash(postValue); + routesList.add(route); + } + } else { + routesList.add(postValue); + } + } + } + return routesList; + } + + private static String[] getRoutesForPost(Method method, String topicName) { + String[] postValueArray = new String[] {topicName}; + PostMapping postMapping = method.getAnnotation(PostMapping.class); + if (postMapping != null) { + if (postMapping.path() != null && postMapping.path().length >= 1) { + postValueArray = postMapping.path(); + } else if (postMapping.value() != null && postMapping.value().length >= 1) { + postValueArray = postMapping.value(); + } + } else { + RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); + for (RequestMethod reqMethod : reqMapping.method()) { + if (reqMethod == RequestMethod.POST) { + if (reqMapping.path() != null && reqMapping.path().length >= 1) { + postValueArray = reqMapping.path(); + } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { + postValueArray = reqMapping.value(); + } + break; + } + } + } + return postValueArray; + } + + private static String confirmLeadingSlash(String path) { + if (path != null && path.length() >= 1) { + if (!path.substring(0, 1).equals("/")) { + return "/" + path; + } + } + return path; + } } diff --git a/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java b/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java deleted file mode 100644 index 26b04ba069..0000000000 --- a/sdk-springboot/src/main/java/io/dapr/springboot/util/SpringProcessorHelper.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2021 The Dapr Authors - * 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 io.dapr.springboot.util; - -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -/** - * Helper for any/all spring related utility jobs required. - */ -public class SpringProcessorHelper { - - /** - * Method to provide all possible complete routes list fos this post method present in this controller class, - * for mentioned topic. - * - * @param clazz Controller class - * @param method Declared method for posting data - * @param topicName Associated topic name - * @return All possible routes for post mapping for this class and post method - */ - public static List getAllCompleteRoutesForPost(Class clazz, Method method, String topicName) { - List routesList = new ArrayList<>(); - RequestMapping clazzRequestMapping = - (RequestMapping) clazz.getAnnotation(RequestMapping.class); - String[] clazzLevelRoute = null; - if (clazzRequestMapping != null) { - clazzLevelRoute = clazzRequestMapping.value(); - } - String[] postValueArray = getRoutesForPost(method, topicName); - if (postValueArray != null && postValueArray.length >= 1) { - for (String postValue : postValueArray) { - if (clazzLevelRoute != null && clazzLevelRoute.length >= 1) { - for (String clazzLevelValue : clazzLevelRoute) { - String route = clazzLevelValue + confirmLeadingSlash(postValue); - routesList.add(route); - } - } else { - routesList.add(postValue); - } - } - } - return routesList; - } - - private static String[] getRoutesForPost(Method method, String topicName) { - String[] postValueArray = new String[] {topicName}; - PostMapping postMapping = method.getAnnotation(PostMapping.class); - if (postMapping != null) { - if (postMapping.path() != null && postMapping.path().length >= 1) { - postValueArray = postMapping.path(); - } else if (postMapping.value() != null && postMapping.value().length >= 1) { - postValueArray = postMapping.value(); - } - } else { - RequestMapping reqMapping = method.getAnnotation(RequestMapping.class); - for (RequestMethod reqMethod : reqMapping.method()) { - if (reqMethod == RequestMethod.POST) { - if (reqMapping.path() != null && reqMapping.path().length >= 1) { - postValueArray = reqMapping.path(); - } else if (reqMapping.value() != null && reqMapping.value().length >= 1) { - postValueArray = reqMapping.value(); - } - break; - } - } - } - return postValueArray; - } - - private static String confirmLeadingSlash(String path) { - if (path != null && path.length() >= 1) { - if (!path.substring(0, 1).equals("/")) { - return "/" + path; - } - } - return path; - } - -} diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java b/sdk-springboot/src/test/java/io/dapr/springboot/DaprBeanPostProcessorTest.java similarity index 75% rename from sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java rename to sdk-springboot/src/test/java/io/dapr/springboot/DaprBeanPostProcessorTest.java index 8c7d99b0cf..403d5e21c3 100644 --- a/sdk-springboot/src/test/java/io/dapr/springboot/util/SpringProcessorHelperTest.java +++ b/sdk-springboot/src/test/java/io/dapr/springboot/DaprBeanPostProcessorTest.java @@ -1,16 +1,18 @@ -package io.dapr.springboot.util; +package io.dapr.springboot; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; import java.util.List; @RunWith(Parameterized.class) -public class SpringProcessorHelperTest { +public class DaprBeanPostProcessorTest { private final Class clazzToBeTested; private final String methodToBeTested; @@ -18,7 +20,7 @@ public class SpringProcessorHelperTest { private final boolean expectedResult; private static final String TOPIC_NAME = "topicName1"; - public SpringProcessorHelperTest(Class clazzToBeTested, String methodToBeTested, String[] expected, + public DaprBeanPostProcessorTest(Class clazzToBeTested, String methodToBeTested, String[] expected, boolean expectedResult) { this.clazzToBeTested = clazzToBeTested; this.methodToBeTested = methodToBeTested; @@ -44,11 +46,18 @@ public static Collection routesTester() { }); } - @Test public void testAllPostRoutesGeneration() throws NoSuchMethodException { - List routesArrayTestMethod1 = SpringProcessorHelper.getAllCompleteRoutesForPost(clazzToBeTested, - clazzToBeTested.getMethod(methodToBeTested), TOPIC_NAME); + Method allPostRoutesMethod = DaprBeanPostProcessor.class. + getDeclaredMethod("getAllCompleteRoutesForPost", Class.class, Method.class, String.class); + allPostRoutesMethod.setAccessible(true); + List routesArrayTestMethod1 = null; + try { + routesArrayTestMethod1 = (List) allPostRoutesMethod.invoke(DaprBeanPostProcessor.class, clazzToBeTested, + clazzToBeTested.getMethod(methodToBeTested), TOPIC_NAME); + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } Assert.assertEquals(expectedResult, testingListForOrderAgnosticEquality(Arrays.asList(expected), routesArrayTestMethod1)); } diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java b/sdk-springboot/src/test/java/io/dapr/springboot/MockController.java similarity index 95% rename from sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java rename to sdk-springboot/src/test/java/io/dapr/springboot/MockController.java index 2d93e94d9a..ee32c15cd0 100644 --- a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockController.java +++ b/sdk-springboot/src/test/java/io/dapr/springboot/MockController.java @@ -1,4 +1,4 @@ -package io.dapr.springboot.util; +package io.dapr.springboot; import org.springframework.web.bind.annotation.*; diff --git a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java b/sdk-springboot/src/test/java/io/dapr/springboot/MockControllerNoClazzAnnotation.java similarity index 95% rename from sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java rename to sdk-springboot/src/test/java/io/dapr/springboot/MockControllerNoClazzAnnotation.java index d28b9fc0ba..1a64920dba 100644 --- a/sdk-springboot/src/test/java/io/dapr/springboot/util/MockControllerNoClazzAnnotation.java +++ b/sdk-springboot/src/test/java/io/dapr/springboot/MockControllerNoClazzAnnotation.java @@ -1,4 +1,4 @@ -package io.dapr.springboot.util; +package io.dapr.springboot; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping;