camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/6] camel git commit: CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces
Date Sun, 14 May 2017 08:05:07 GMT
CAMEL-11267 Updated to obtain parameters from original endpoint URI and use curly braces


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/defc6b18
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/defc6b18
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/defc6b18

Branch: refs/heads/master
Commit: defc6b18db23a724680bf45392b46294dff4caf6
Parents: dfb3080
Author: Gary Brown <gary@brownuk.com>
Authored: Fri May 12 17:17:38 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun May 14 10:02:49 2017 +0200

----------------------------------------------------------------------
 .../decorators/RestSpanDecorator.java           | 48 +++++++++++++++-----
 .../decorators/RestSpanDecoratorTest.java       | 15 +++---
 2 files changed, 45 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/defc6b18/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
index 7f8f0c0..1842abc 100644
--- a/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
+++ b/components/camel-opentracing/src/main/java/org/apache/camel/opentracing/decorators/RestSpanDecorator.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.opentracing.decorators;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -24,9 +26,13 @@ import io.opentracing.Span;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
+    private static final Logger LOG = LoggerFactory.getLogger(RestSpanDecorator.class);
+
     @Override
     public String getComponent() {
         return "rest";
@@ -34,19 +40,14 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
 
     @Override
     public String getOperationName(Exchange exchange, Endpoint endpoint) {
-        String path = exchange.getFromEndpoint().getEndpointUri();
-        int queryIndex = path.indexOf('?');
-        if (queryIndex != -1) {
-            path = path.substring(0, queryIndex);
-        }
-        return path;
+        return getPath(endpoint.getEndpointUri());
     }
 
     @Override
     public void pre(Span span, Exchange exchange, Endpoint endpoint) {
         super.pre(span, exchange, endpoint);
 
-        getParameters(exchange.getFromEndpoint().getEndpointUri()).forEach(param -> {
+        getParameters(getPath(endpoint.getEndpointUri())).forEach(param -> {
             Object value = exchange.getIn().getHeader(param);
             if (value != null) {
                 if (value instanceof String) {
@@ -61,18 +62,41 @@ public class RestSpanDecorator extends AbstractHttpSpanDecorator {
         
     }
 
-    public static List<String> getParameters(String uri) {
+    protected static String getPath(String uri) {
+        // Obtain the 'path' part of the URI format: rest://method:path[:uriTemplate]?[options]
+        String path = null;
+        int index = uri.indexOf(':');
+        if (index != -1) {
+            index = uri.indexOf(':', index + 1);
+            if (index != -1) {
+                path = uri.substring(index + 1);
+                index = path.indexOf('?');
+                if (index != -1) {
+                    path = path.substring(0, index);
+                }
+                path = path.replaceAll(":", "");
+                try {
+                    path = URLDecoder.decode(path, "UTF-8");
+                } catch (UnsupportedEncodingException e) {
+                    LOG.error("Failed to decode URL path '" + path + "'", e);
+                }
+            }
+        }
+        return path;
+    }
+
+    protected static List<String> getParameters(String path) {
         List<String> parameters = null;
 
-        int startIndex = uri.indexOf('(');
+        int startIndex = path.indexOf('{');
         while (startIndex != -1) {
-            int endIndex = uri.indexOf(')', startIndex);
+            int endIndex = path.indexOf('}', startIndex);
             if (endIndex != -1) {
                 if (parameters == null) {
                     parameters = new ArrayList<>();
                 }
-                parameters.add(uri.substring(startIndex + 1, endIndex));
-                startIndex = uri.indexOf('(', endIndex);
+                parameters.add(path.substring(startIndex + 1, endIndex));
+                startIndex = path.indexOf('{', endIndex);
             } else {
                 // Break out of loop as no valid end token
                 startIndex = -1;

http://git-wip-us.apache.org/repos/asf/camel/blob/defc6b18/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
----------------------------------------------------------------------
diff --git a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
index 970fd3f..0724e8f 100644
--- a/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
+++ b/components/camel-opentracing/src/test/java/org/apache/camel/opentracing/decorators/RestSpanDecoratorTest.java
@@ -35,9 +35,8 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetOperation() {
-        String query = "?restletMethods=PUT";
-        String path = "/persons/(personId)";
-        String uri = path + query;
+        String path = "/persons/{personId}";
+        String uri = "rest://put:/persons:/%7BpersonId%7D?routeId=route4";
 
         Endpoint endpoint = Mockito.mock(Endpoint.class);
         Exchange exchange = Mockito.mock(Exchange.class);
@@ -52,12 +51,12 @@ public class RestSpanDecoratorTest {
 
     @Test
     public void testGetParameters() {
-        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/(id1)/(id2)"));
+        assertEquals(Arrays.asList("id1", "id2"), RestSpanDecorator.getParameters("/context/{id1}/{id2}"));
     }
 
     @Test
     public void testGetParametersNone() {
-        assertTrue(RestSpanDecorator.getParameters("/context/hello/world").isEmpty());
+        assertTrue(RestSpanDecorator.getParameters("rest://put:/persons/hello/world?routeId=route4").isEmpty());
     }
 
     @Test
@@ -80,7 +79,7 @@ public class RestSpanDecoratorTest {
         Exchange exchange = Mockito.mock(Exchange.class);
         Message message = Mockito.mock(Message.class);
 
-        Mockito.when(endpoint.getEndpointUri()).thenReturn("/context/(" + paramName + ")");
+        Mockito.when(endpoint.getEndpointUri()).thenReturn("rest://put:/context:/%7B" + paramName
+ "%7D?routeId=route4");
         Mockito.when(exchange.getFromEndpoint()).thenReturn(endpoint);
         Mockito.when(exchange.getIn()).thenReturn(message);
         Mockito.when(message.getHeader(paramName)).thenReturn(paramValue);
@@ -95,4 +94,8 @@ public class RestSpanDecoratorTest {
         assertEquals(paramValue, span.tags().get(paramName));
     }
 
+    @Test
+    public void testGetPath() {
+        assertEquals("/persons/{personId}", RestSpanDecorator.getPath("rest://put:/persons:/%7BpersonId%7D?routeId=route4"));
+    }
 }


Mime
View raw message