Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #491 from clevertension/my-main
add rest mapping such as GetMapping
- Loading branch information
Showing
11 changed files
with
799 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ target/ | |
.DS_Store | ||
*~ | ||
packages/ | ||
**/dependency-reduced-pom.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...n/src/main/java/org/skywalking/apm/plugin/spring/mvc/RequestMappingMethodInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.skywalking.apm.plugin.spring.mvc; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
/** | ||
* The <code>RequestMappingMethodInterceptor</code> only use the first mapping value. | ||
* it will inteceptor with <code>@RequestMapping</code> | ||
* @author clevertension | ||
*/ | ||
public class RequestMappingMethodInterceptor extends AbstractMethodInteceptor { | ||
@Override | ||
public String getRequestURL(Method method) { | ||
String requestURL = ""; | ||
RequestMapping methodRequestMapping = method.getAnnotation(RequestMapping.class); | ||
if (methodRequestMapping.value().length > 0) { | ||
requestURL = methodRequestMapping.value()[0]; | ||
} else if (methodRequestMapping.path().length > 0) { | ||
requestURL = methodRequestMapping.path()[0]; | ||
} | ||
return requestURL; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ugin/src/main/java/org/skywalking/apm/plugin/spring/mvc/RestMappingMethodInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.skywalking.apm.plugin.spring.mvc; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* The <code>RestMappingMethodInterceptor</code> only use the first mapping value. | ||
* it will inteceptor with | ||
* <code>@GetMapping</code>, <code>@PostMapping</code>, <code>@PutMapping</code> | ||
* <code>@DeleteMapping</code>, <code>@PatchMapping</code> | ||
* @author clevertension | ||
*/ | ||
public class RestMappingMethodInterceptor extends AbstractMethodInteceptor { | ||
@Override | ||
public String getRequestURL(Method method) { | ||
String requestURL = ""; | ||
GetMapping getMapping = method.getAnnotation(GetMapping.class); | ||
PostMapping postMapping = method.getAnnotation(PostMapping.class); | ||
PutMapping putMapping = method.getAnnotation(PutMapping.class); | ||
DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class); | ||
PatchMapping patchMapping = method.getAnnotation(PatchMapping.class); | ||
if (getMapping != null) { | ||
if (getMapping.value().length > 0) { | ||
requestURL = getMapping.value()[0]; | ||
} else if (getMapping.path().length > 0) { | ||
requestURL = getMapping.path()[0]; | ||
} | ||
} else if (postMapping != null) { | ||
if (postMapping.value().length > 0) { | ||
requestURL = postMapping.value()[0]; | ||
} else if (postMapping.path().length > 0) { | ||
requestURL = postMapping.path()[0]; | ||
} | ||
} else if (putMapping != null) { | ||
if (putMapping.value().length > 0) { | ||
requestURL = putMapping.value()[0]; | ||
} else if (putMapping.path().length > 0) { | ||
requestURL = putMapping.path()[0]; | ||
} | ||
} else if (deleteMapping != null) { | ||
if (deleteMapping.value().length > 0) { | ||
requestURL = deleteMapping.value()[0]; | ||
} else if (deleteMapping.path().length > 0) { | ||
requestURL = deleteMapping.path()[0]; | ||
} | ||
} else if (patchMapping != null) { | ||
if (patchMapping.value().length > 0) { | ||
requestURL = patchMapping.value()[0]; | ||
} else if (patchMapping.path().length > 0) { | ||
requestURL = patchMapping.path()[0]; | ||
} | ||
} | ||
return requestURL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
.../test/java/org/skywalking/apm/plugin/spring/mvc/ControllerConstructorInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.skywalking.apm.plugin.spring.mvc; | ||
|
||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.modules.junit4.PowerMockRunnerDelegate; | ||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; | ||
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PowerMockRunnerDelegate(TracingSegmentRunner.class) | ||
public class ControllerConstructorInterceptorTest { | ||
private ControllerConstructorInterceptor controllerConstructorInterceptor; | ||
private final MockEnhancedInstance1 inst1 = new MockEnhancedInstance1(); | ||
private final MockEnhancedInstance2 inst2 = new MockEnhancedInstance2(); | ||
private final MockEnhancedInstance3 inst3 = new MockEnhancedInstance3(); | ||
@Before | ||
public void setUp() throws Exception { | ||
controllerConstructorInterceptor = new ControllerConstructorInterceptor(); | ||
} | ||
|
||
@Test | ||
public void testOnConstruct_Accuracy1() throws Throwable { | ||
controllerConstructorInterceptor.onConstruct(inst1, null); | ||
PathMappingCache cache = (PathMappingCache)inst1.getSkyWalkingDynamicField(); | ||
Assert.assertNotNull(cache); | ||
|
||
Object obj = new Object(); | ||
Method m = obj.getClass().getMethods()[0]; | ||
cache.addPathMapping(m, "#toString"); | ||
|
||
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test1#toString"); | ||
} | ||
|
||
@Test | ||
public void testOnConstruct_Accuracy2() throws Throwable { | ||
controllerConstructorInterceptor.onConstruct(inst2, null); | ||
PathMappingCache cache = (PathMappingCache)inst2.getSkyWalkingDynamicField(); | ||
Assert.assertNotNull(cache); | ||
|
||
Object obj = new Object(); | ||
Method m = obj.getClass().getMethods()[0]; | ||
cache.addPathMapping(m, "#toString"); | ||
|
||
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "#toString"); | ||
} | ||
|
||
@Test | ||
public void testOnConstruct_Accuracy3() throws Throwable { | ||
controllerConstructorInterceptor.onConstruct(inst3, null); | ||
PathMappingCache cache = (PathMappingCache)inst3.getSkyWalkingDynamicField(); | ||
Assert.assertNotNull(cache); | ||
|
||
Object obj = new Object(); | ||
Method m = obj.getClass().getMethods()[0]; | ||
cache.addPathMapping(m, "#toString"); | ||
|
||
Assert.assertEquals("the two value should be equal", cache.findPathMapping(m), "/test3#toString"); | ||
} | ||
|
||
@RequestMapping(value="/test1") | ||
private class MockEnhancedInstance1 implements EnhancedInstance { | ||
private Object value; | ||
@Override | ||
public Object getSkyWalkingDynamicField() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setSkyWalkingDynamicField(Object value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
private class MockEnhancedInstance2 implements EnhancedInstance { | ||
private Object value; | ||
@Override | ||
public Object getSkyWalkingDynamicField() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setSkyWalkingDynamicField(Object value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
@RequestMapping(path="/test3") | ||
private class MockEnhancedInstance3 implements EnhancedInstance { | ||
private Object value; | ||
@Override | ||
public Object getSkyWalkingDynamicField() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setSkyWalkingDynamicField(Object value) { | ||
this.value = value; | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...n-4.x-plugin/src/test/java/org/skywalking/apm/plugin/spring/mvc/PathMappingCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.skywalking.apm.plugin.spring.mvc; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.modules.junit4.PowerMockRunnerDelegate; | ||
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PowerMockRunnerDelegate(TracingSegmentRunner.class) | ||
public class PathMappingCacheTest { | ||
private PathMappingCache pathMappingCache; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
pathMappingCache = new PathMappingCache("org.skywalking.apm.plugin.spring.mvc"); | ||
} | ||
|
||
@Test | ||
public void testAddPathMapping1() throws Throwable { | ||
Object obj = new Object(); | ||
Method m = obj.getClass().getMethods()[0]; | ||
pathMappingCache.addPathMapping(m, "#toString"); | ||
|
||
Assert.assertEquals("the two value should be equal", pathMappingCache.findPathMapping(m), "org.skywalking.apm.plugin.spring.mvc#toString"); | ||
|
||
} | ||
} |
Oops, something went wrong.