You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/02/01 14:06:33 UTC

[camel-k] branch master updated: The example RestWithRestlet does not work calling its service via curl or webbrowser #402

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new de163ac  The example RestWithRestlet does not work calling its service via curl or webbrowser #402
de163ac is described below

commit de163acde2542a5471112116135da3622d522694
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Jan 31 22:02:43 2019 +0100

    The example RestWithRestlet does not work calling its service via curl or webbrowser #402
---
 examples/RestWithRestlet.java                      |  2 +-
 .../camel/k/jvm/loader/JavaSourceLoader.java       | 41 +++++++++++++++++++---
 .../org/apache/camel/k/jvm/RoutesLoadersTest.java  | 18 ++++++++++
 .../resources/MyRoutesWithRestConfiguration.java   | 14 ++++++++
 4 files changed, 69 insertions(+), 6 deletions(-)

diff --git a/examples/RestWithRestlet.java b/examples/RestWithRestlet.java
index fa48418..b26c511 100644
--- a/examples/RestWithRestlet.java
+++ b/examples/RestWithRestlet.java
@@ -8,7 +8,7 @@ public class RestWithRestlet extends org.apache.camel.builder.RouteBuilder {
     public void configure() throws Exception {
         restConfiguration()
             .component("restlet")
-            .host("localhost")
+            .host("0.0.0.0")
             .port("8080");
 
         rest()
diff --git a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaSourceLoader.java b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaSourceLoader.java
index 757c16b..85adec2 100644
--- a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaSourceLoader.java
+++ b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaSourceLoader.java
@@ -20,12 +20,17 @@ import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.k.RoutesLoader;
 import org.apache.camel.k.RuntimeRegistry;
 import org.apache.camel.k.Source;
 import org.apache.camel.k.support.URIResolver;
+import org.apache.camel.model.rest.RestConfigurationDefinition;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.joor.Reflect;
@@ -41,18 +46,44 @@ public class JavaSourceLoader implements RoutesLoader {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                try (InputStream is = URIResolver.resolve(getContext(), source)) {
-                    String name = StringUtils.substringAfter(source.getLocation(), ":");
+                final CamelContext context = getContext();
+
+                try (InputStream is = URIResolver.resolve(context, source)) {
+                    String name = source.getLocation();
+                    name = StringUtils.substringAfter(name, ":");
                     name = StringUtils.removeEnd(name, ".java");
 
                     if (name.contains("/")) {
                         name = StringUtils.substringAfterLast(name, "/");
                     }
 
+                    RoutesBuilder builder = Reflect.compile(name, IOUtils.toString(is, StandardCharsets.UTF_8)).create().get();
+
                     // Wrap routes builder
-                    includeRoutes(
-                        Reflect.compile(name, IOUtils.toString(is, StandardCharsets.UTF_8)).create().get()
-                    );
+                    includeRoutes(builder);
+
+                    if (builder instanceof RouteBuilder) {
+                        Map<String, RestConfigurationDefinition> configurations = ((RouteBuilder) builder).getRestConfigurations();
+
+                        //
+                        // TODO: RouteBuilder.getRestConfigurations() should not
+                        //       return null
+                        //
+                        if (configurations != null) {
+                            for (RestConfigurationDefinition definition : configurations.values()) {
+                                RestConfiguration conf = definition.asRestConfiguration(context);
+
+                                //
+                                // this is an hack to copy routes configuration
+                                // to the camel context
+                                //
+                                // TODO: fix RouteBuilder.includeRoutes to include
+                                //       rest configurations
+                                //
+                                context.addRestConfiguration(conf);
+                            }
+                        }
+                    }
                 }
             }
         };
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
index 64394c7..907f0d0 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RoutesLoadersTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.k.jvm;
 
 import java.util.List;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
 import org.apache.camel.k.InMemoryRegistry;
@@ -107,6 +108,23 @@ public class RoutesLoadersTest {
     }
 
     @Test
+    public void testLoadJavaWithRestConfiguration() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+        Source source = Source.create("classpath:MyRoutesWithRestConfiguration.java");
+        RoutesLoader loader = RuntimeSupport.loaderFor(new DefaultCamelContext(), source);
+        RouteBuilder builder = loader.load(new InMemoryRegistry(), source);
+
+        assertThat(loader).isInstanceOf(JavaSourceLoader.class);
+        assertThat(builder).isNotNull();
+
+        builder.setContext(context);
+        builder.configure();
+
+        assertThat(context.getRestConfigurations()).hasSize(1);
+        assertThat(context.getRestConfigurations().iterator().next()).hasFieldOrPropertyWithValue("component", "restlet");
+    }
+
+    @Test
     public void testLoadJavaScript() throws Exception {
         Source source = Source.create("classpath:routes.js");
         RoutesLoader loader = RuntimeSupport.loaderFor(new DefaultCamelContext(), source);
diff --git a/runtime/camel-k-runtime-jvm/src/test/resources/MyRoutesWithRestConfiguration.java b/runtime/camel-k-runtime-jvm/src/test/resources/MyRoutesWithRestConfiguration.java
new file mode 100644
index 0000000..dfddda2
--- /dev/null
+++ b/runtime/camel-k-runtime-jvm/src/test/resources/MyRoutesWithRestConfiguration.java
@@ -0,0 +1,14 @@
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyRoutesWithRestConfiguration extends RouteBuilder {
+    @Override
+    public void configure() throws Exception {
+        restConfiguration()
+            .component("restlet")
+            .host("localhost")
+            .port("8080");
+
+        from("timer:tick")
+            .to("log:info");
+    }
+}
\ No newline at end of file