This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch uri-assembler in repository https://gitbox.apache.org/repos/asf/camel.git
commit f2034e1df7fa6459a88828669b42335060ffa099 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Sep 25 10:11:26 2020 +0200 CAMEL-15567: components - Generate source code for creating endpoint uri via a map of properties. WIP --- .../jetty9/JettyHttp9EndpointUriAssembler.java | 26 ++++ .../org/apache/camel/assembler/jetty-endpoint | 2 + .../netty/http/NettyHttpEndpointUriAssembler.java | 29 +++++ .../org/apache/camel/assembler/netty-http-endpoint | 2 + .../catalog/CustomEndpointUriAssemblerTest.java | 135 +++++++++++++++++++-- .../component/EndpointUriAssemblerSupport.java | 13 ++ 6 files changed, 194 insertions(+), 13 deletions(-) diff --git a/components/camel-jetty/src/generated/java/org/apache/camel/component/jetty9/JettyHttp9EndpointUriAssembler.java b/components/camel-jetty/src/generated/java/org/apache/camel/component/jetty9/JettyHttp9EndpointUriAssembler.java new file mode 100644 index 0000000..e0f4418 --- /dev/null +++ b/components/camel-jetty/src/generated/java/org/apache/camel/component/jetty9/JettyHttp9EndpointUriAssembler.java @@ -0,0 +1,26 @@ +/* Generated by camel build tools - do NOT edit this file! */ +package org.apache.camel.component.jetty9; + +import java.net.URISyntaxException; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.spi.EndpointUriAssembler; + +/** + * Generated by camel build tools - do NOT edit this file! + */ +public class JettyHttp9EndpointUriAssembler extends org.apache.camel.support.component.EndpointUriAssemblerSupport implements EndpointUriAssembler { + + private static final String SYNTAX = "jetty:httpUri"; + + @Override + public String buildUri(CamelContext camelContext, String scheme, Map<String, Object> parameters) throws URISyntaxException { + String uri = SYNTAX; + + uri = buildPathParameter(camelContext, SYNTAX, uri, "httpUri", null, true, parameters); + uri = buildQueryParameters(camelContext, uri, parameters); + return uri; + } +} + diff --git a/components/camel-jetty/src/generated/resources/META-INF/services/org/apache/camel/assembler/jetty-endpoint b/components/camel-jetty/src/generated/resources/META-INF/services/org/apache/camel/assembler/jetty-endpoint new file mode 100644 index 0000000..7f8e7ed --- /dev/null +++ b/components/camel-jetty/src/generated/resources/META-INF/services/org/apache/camel/assembler/jetty-endpoint @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.jetty9.JettyHttp9EndpointUriAssembler diff --git a/components/camel-netty-http/src/generated/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriAssembler.java b/components/camel-netty-http/src/generated/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriAssembler.java new file mode 100644 index 0000000..e252977 --- /dev/null +++ b/components/camel-netty-http/src/generated/java/org/apache/camel/component/netty/http/NettyHttpEndpointUriAssembler.java @@ -0,0 +1,29 @@ +/* Generated by camel build tools - do NOT edit this file! */ +package org.apache.camel.component.netty.http; + +import java.net.URISyntaxException; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.spi.EndpointUriAssembler; + +/** + * Generated by camel build tools - do NOT edit this file! + */ +public class NettyHttpEndpointUriAssembler extends org.apache.camel.support.component.EndpointUriAssemblerSupport implements EndpointUriAssembler { + + private static final String SYNTAX = "netty-http:protocol:host:port/path"; + + @Override + public String buildUri(CamelContext camelContext, String scheme, Map<String, Object> parameters) throws URISyntaxException { + String uri = SYNTAX; + + uri = buildPathParameter(camelContext, SYNTAX, uri, "protocol", null, true, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "host", null, true, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "port", null, false, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "path", null, false, parameters); + uri = buildQueryParameters(camelContext, uri, parameters); + return uri; + } +} + diff --git a/components/camel-netty-http/src/generated/resources/META-INF/services/org/apache/camel/assembler/netty-http-endpoint b/components/camel-netty-http/src/generated/resources/META-INF/services/org/apache/camel/assembler/netty-http-endpoint new file mode 100644 index 0000000..98e4c6b --- /dev/null +++ b/components/camel-netty-http/src/generated/resources/META-INF/services/org/apache/camel/assembler/netty-http-endpoint @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.netty.http.NettyHttpEndpointUriAssembler diff --git a/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java index f5097da..30ef452 100644 --- a/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/catalog/CustomEndpointUriAssemblerTest.java @@ -35,12 +35,13 @@ public class CustomEndpointUriAssemblerTest extends ContextTestSupport { EndpointUriAssembler assembler = new MyAssembler(); Map<String, Object> params = new HashMap<>(); - params.put("timerName", "foo"); - params.put("period", 123); - params.put("repeatCount", "5"); + params.put("name", "foo"); + params.put("amount", "123"); + params.put("port", 4444); + params.put("verbose", true); - String uri = assembler.buildUri(context, "timer", params); - Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri); + String uri = assembler.buildUri(context, "acme", params); + Assertions.assertEquals("acme:foo:4444?amount=123&verbose=true", uri); } @Test @@ -48,17 +49,104 @@ public class CustomEndpointUriAssemblerTest extends ContextTestSupport { EndpointUriAssembler assembler = new MyAssembler(); Map<String, Object> params = new LinkedHashMap<>(); - params.put("timerName", "foo"); - params.put("repeatCount", 5); - params.put("period", "123"); + params.put("name", "foo"); + params.put("verbose", false); + params.put("port", 4444); + params.put("amount", "123"); - String uri = assembler.buildUri(context, "timer", params); - Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri); + String uri = assembler.buildUri(context, "acme", params); + Assertions.assertEquals("acme:foo:4444?amount=123&verbose=false", uri); + } + + @Test + public void testCustomAssembleNoMandatory() throws Exception { + EndpointUriAssembler assembler = new MyAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("verbose", false); + params.put("port", 4444); + params.put("amount", "123"); + + try { + assembler.buildUri(context, "acme", params); + Assertions.fail(); + } catch (IllegalArgumentException e) { + Assertions.assertEquals("Option name is required when creating endpoint uri with syntax acme:name:port", e.getMessage()); + } + } + + @Test + public void testCustomAssembleDefault() throws Exception { + EndpointUriAssembler assembler = new MyAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("name", "bar"); + params.put("verbose", false); + params.put("amount", "123"); + + String uri = assembler.buildUri(context, "acme", params); + Assertions.assertEquals("acme:bar:8080?amount=123&verbose=false", uri); + } + + @Test + public void testCustomAssembleComplex() throws Exception { + EndpointUriAssembler assembler = new MySecondAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("name", "bar"); + params.put("path", "moes"); + params.put("verbose", true); + params.put("amount", "123"); + + String uri = assembler.buildUri(context, "acme2", params); + Assertions.assertEquals("acme2:bar/moes:8080?amount=123&verbose=true", uri); + } + + @Test + public void testCustomAssembleComplexPort() throws Exception { + EndpointUriAssembler assembler = new MySecondAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("name", "bar"); + params.put("path", "moes"); + params.put("port", "4444"); + params.put("verbose", true); + params.put("amount", "123"); + + String uri = assembler.buildUri(context, "acme2", params); + Assertions.assertEquals("acme2:bar/moes:4444?amount=123&verbose=true", uri); + } + + @Test + public void testCustomAssembleComplexNoPath() throws Exception { + EndpointUriAssembler assembler = new MySecondAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("name", "bar"); + params.put("port", "4444"); + params.put("verbose", true); + params.put("amount", "123"); + + String uri = assembler.buildUri(context, "acme2", params); + Assertions.assertEquals("acme2:bar:4444?amount=123&verbose=true", uri); + } + + @Test + public void testCustomAssembleComplexNoPathNoPort() throws Exception { + EndpointUriAssembler assembler = new MySecondAssembler(); + + Map<String, Object> params = new LinkedHashMap<>(); + params.put("name", "bar"); + params.put("verbose", true); + params.put("amount", "123"); + + String uri = assembler.buildUri(context, "acme2", params); + Assertions.assertEquals("acme2:bar:8080?amount=123&verbose=true", uri); } private class MyAssembler extends EndpointUriAssemblerSupport implements EndpointUriAssembler { - private static final String SYNTAX = "timer:timerName"; + private static final String SYNTAX = "acme:name:port"; @Override public String buildUri(CamelContext camelContext, String scheme, Map<String, Object> parameters) @@ -66,10 +154,31 @@ public class CustomEndpointUriAssemblerTest extends ContextTestSupport { // begin from syntax String uri = SYNTAX; - // TODO: optional path parameters that are missing + // append path parameters + uri = buildPathParameter(camelContext, SYNTAX, uri, "name", null, true, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "port", 8080, false, parameters); + // append remainder parameters + uri = buildQueryParameters(camelContext, uri, parameters); + + return uri; + } + + } + + private class MySecondAssembler extends EndpointUriAssemblerSupport implements EndpointUriAssembler { + + private static final String SYNTAX = "acme2:name/path:port"; + + @Override + public String buildUri(CamelContext camelContext, String scheme, Map<String, Object> parameters) + throws URISyntaxException { + // begin from syntax + String uri = SYNTAX; // append path parameters - uri = buildPathParameter(camelContext, SYNTAX, uri, "timerName", null, true, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "name", null, true, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "path", null, false, parameters); + uri = buildPathParameter(camelContext, SYNTAX, uri, "port", 8080, false, parameters); // append remainder parameters uri = buildQueryParameters(camelContext, uri, parameters); diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java index 14a8aa0..d3e1c8c 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/component/EndpointUriAssemblerSupport.java @@ -44,6 +44,19 @@ public abstract class EndpointUriAssemblerSupport { if (ObjectHelper.isNotEmpty(obj)) { String str = camelContext.getTypeConverter().convertTo(String.class, obj); uri = uri.replace(name, str); + } else { + // the option is optional and we have no default or value for it, so we need to remove it from the syntax + int pos = uri.indexOf(name); + if (pos != -1) { + // remove from syntax + uri = uri.replace(name, ""); + pos = pos - 1; + // remove the separator char + char ch = uri.charAt(pos); + if (!Character.isLetterOrDigit(ch)) { + uri = uri.substring(0, pos) + uri.substring(pos + 1); + } + } } return uri; }