You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by GitBox <gi...@apache.org> on 2018/06/06 07:41:32 UTC

[GitHub] wu-sheng closed pull request #1315: Spring mvc plugin auto add '/' if necessary

wu-sheng closed pull request #1315: Spring mvc plugin auto add '/' if necessary
URL: https://github.com/apache/incubator-skywalking/pull/1315
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/ControllerConstructorInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/ControllerConstructorInterceptorTest.java
index 4e22e0d65..a9e429be2 100644
--- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/ControllerConstructorInterceptorTest.java
+++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/ControllerConstructorInterceptorTest.java
@@ -54,7 +54,7 @@ public void testOnConstruct_Accuracy1() throws Throwable {
         Method m = obj.getClass().getMethods()[0];
         cache.addPathMapping(m, "#toString");
 
-        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1#toString");
+        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1/#toString");
     }
 
     @Test
@@ -67,7 +67,7 @@ public void testOnConstruct_Accuracy2() throws Throwable {
         Method m = obj.getClass().getMethods()[0];
         cache.addPathMapping(m, "#toString");
 
-        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "#toString");
+        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/#toString");
     }
 
     @Test
@@ -80,7 +80,7 @@ public void testOnConstruct_Accuracy3() throws Throwable {
         Method m = obj.getClass().getMethods()[0];
         cache.addPathMapping(m, "#toString");
 
-        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3#toString");
+        Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3/#toString");
     }
 
     @RequestMapping(value = "/test1")
diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/PathMappingCacheTest.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/PathMappingCacheTest.java
index c98693d43..776d4585f 100644
--- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/PathMappingCacheTest.java
+++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-4.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/spring/mvc/v4/PathMappingCacheTest.java
@@ -45,7 +45,31 @@ public void testAddPathMapping1() throws Throwable {
         Method m = obj.getClass().getMethods()[0];
         pathMappingCache.addPathMapping(m, "#toString");
 
-        Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "org.apache.skywalking.apm.plugin.spring.mvc#toString");
+        Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "/org.apache.skywalking.apm.plugin.spring.mvc/#toString");
 
     }
+
+    @Test
+    public void testAutoAddPathSeparator() {
+        String rightPath = "/root/sub";
+
+        Object obj = new Object();
+        Method m = obj.getClass().getMethods()[0];
+
+        PathMappingCache cache = new PathMappingCache("root");
+        cache.addPathMapping(m, "sub");
+        Assert.assertEquals(cache.findPathMapping(m), rightPath);
+
+        PathMappingCache cache2 = new PathMappingCache("/root");
+        cache2.addPathMapping(m, "/sub");
+        Assert.assertEquals(cache2.findPathMapping(m), rightPath);
+
+        PathMappingCache cache3 = new PathMappingCache("root");
+        cache3.addPathMapping(m, "/sub");
+        Assert.assertEquals(cache3.findPathMapping(m), rightPath);
+
+        PathMappingCache cache4 = new PathMappingCache("/root");
+        cache4.addPathMapping(m, "sub");
+        Assert.assertEquals(cache4.findPathMapping(m), rightPath);
+    }
 }
diff --git a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/PathMappingCache.java b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/PathMappingCache.java
index a89c131c2..494b80053 100644
--- a/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/PathMappingCache.java
+++ b/apm-sniffer/apm-sdk-plugin/spring-plugins/mvc-annotation-commons/src/main/java/org/apache/skywalking/apm/plugin/spring/mvc/commons/PathMappingCache.java
@@ -19,6 +19,8 @@
 
 package org.apache.skywalking.apm.plugin.spring.mvc.commons;
 
+import org.apache.skywalking.apm.util.StringUtil;
+
 import java.lang.reflect.Method;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -28,11 +30,17 @@
  * @author zhangxin
  */
 public class PathMappingCache {
+
+    private static final String PATH_SEPARATOR = "/";
+
     private String classPath = "";
 
     private ConcurrentHashMap<Method, String> methodPathMapping = new ConcurrentHashMap<Method, String>();
 
     public PathMappingCache(String classPath) {
+        if (!StringUtil.isEmpty(classPath) && !classPath.startsWith(PATH_SEPARATOR)) {
+            classPath = PATH_SEPARATOR + classPath;
+        }
         this.classPath = classPath;
     }
 
@@ -41,6 +49,10 @@ public String findPathMapping(Method method) {
     }
 
     public void addPathMapping(Method method, String methodPath) {
+        if (!StringUtil.isEmpty(methodPath) && !methodPath.startsWith(PATH_SEPARATOR)
+                && !classPath.endsWith(PATH_SEPARATOR)) {
+            methodPath = PATH_SEPARATOR + methodPath;
+        }
         methodPathMapping.put(method, classPath + methodPath);
     }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services