camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-10537: Fixed rest-dsl to remove/add rests at runtime due to rest configuration not using the existing one.
Date Wed, 01 Mar 2017 11:54:01 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.18.x 3a2e9dac2 -> 8f616e218
  refs/heads/master d569b80d8 -> e6fe23bb9


CAMEL-10537: Fixed rest-dsl to remove/add rests at runtime due to rest configuration not using
the existing one.


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

Branch: refs/heads/master
Commit: e6fe23bb9c38a76f12f80122d11ea07b045113d9
Parents: d569b80
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Mar 1 12:32:15 2017 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Mar 1 12:32:25 2017 +0100

----------------------------------------------------------------------
 .../camel/component/rest/RestComponent.java     | 23 +++---
 .../RestJettyRemoveAddRestAndRouteTest.java     | 83 ++++++++++++++++++++
 2 files changed, 97 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e6fe23bb/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java
index 883168d..c49cee4 100644
--- a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java
@@ -21,6 +21,7 @@ import java.util.Map;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.UriEndpointComponent;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 
@@ -42,15 +43,19 @@ public class RestComponent extends UriEndpointComponent {
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
+        String restConfigurationName = getAndRemoveParameter(parameters, "componentName",
String.class, componentName);
+
         RestEndpoint answer = new RestEndpoint(uri, this);
-        answer.setComponentName(componentName);
+        answer.setComponentName(restConfigurationName);
         answer.setApiDoc(apiDoc);
 
+        RestConfiguration config = getCamelContext().getRestConfiguration(restConfigurationName,
true);
+
         // if no explicit host was given, then fallback and use default configured host
         String h = resolveAndRemoveReferenceParameter(parameters, "host", String.class, host);
-        if (h == null && getCamelContext().getRestConfiguration() != null) {
-            h = getCamelContext().getRestConfiguration().getHost();
-            int port = getCamelContext().getRestConfiguration().getPort();
+        if (h == null && config != null) {
+            h = config.getHost();
+            int port = config.getPort();
             // is there a custom port number
             if (port > 0 && port != 80 && port != 443) {
                 h += ":" + port;
@@ -96,17 +101,17 @@ public class RestComponent extends UriEndpointComponent {
         answer.setUriTemplate(uriTemplate);
 
         // if no explicit component name was given, then fallback and use default configured
component name
-        if (answer.getComponentName() == null && getCamelContext().getRestConfiguration()
!= null) {
-            String name = getCamelContext().getRestConfiguration().getProducerComponent();
+        if (answer.getComponentName() == null && config != null) {
+            String name = config.getProducerComponent();
             if (name == null) {
                 // fallback and use the consumer name
-                name = getCamelContext().getRestConfiguration().getComponent();
+                name = config.getComponent();
             }
             answer.setComponentName(name);
         }
         // if no explicit producer api was given, then fallback and use default configured
-        if (answer.getApiDoc() == null && getCamelContext().getRestConfiguration()
!= null) {
-            answer.setApiDoc(getCamelContext().getRestConfiguration().getProducerApiDoc());
+        if (answer.getApiDoc() == null && config != null) {
+            answer.setApiDoc(config.getProducerApiDoc());
         }
 
         return answer;

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fe23bb/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyRemoveAddRestAndRouteTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyRemoveAddRestAndRouteTest.java
b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyRemoveAddRestAndRouteTest.java
new file mode 100644
index 0000000..15d5ca8
--- /dev/null
+++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyRemoveAddRestAndRouteTest.java
@@ -0,0 +1,83 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty.rest;
+
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+public class RestJettyRemoveAddRestAndRouteTest extends BaseJettyTest {
+
+    @Test
+    public void testCallRoute() throws Exception {
+        InputStream stream = new URL("http://localhost:" + getPort() + "/issues/35").openStream();
+        assertEquals("Here's your issue 35", IOUtils.toString(stream));
+
+        stream = new URL("http://localhost:" + getPort() + "/listings").openStream();
+        assertEquals("some listings", IOUtils.toString(stream));
+    }
+
+    @Test
+    public void testRemoveAddRoute() throws Exception {
+        context.stopRoute("issues");
+        boolean removed = context.removeRoute("issues");
+
+        assertTrue("Should have removed route", removed);
+
+        try (InputStream stream = new URL("http://localhost:" + getPort() + "/issues/35").openStream())
{
+            fail();
+        } catch (Exception e) {
+        }
+
+        new RouteBuilder(context) {
+            @Override
+            public void configure() throws Exception {
+                rest("/")
+                    .get("/issues/{isin}/{sedol}").route().id("issues")
+                        .process(e -> e.getOut().setBody("Here's your issue " + e.getIn().getHeader("isin")
+ ":" + e.getIn().getHeader("sedol")))
+                    .endRest();
+            }
+        }.addRoutesToCamelContext(context);
+        // exception here since we have 2 rest configurations
+        // org.apache.camel.model.rest.RestDefinition.asRouteDefinition(CamelContext camelContext)
line 607 will have 2 rest contexts
+        // and duplicate route definitions for the same route which will cause exception
+
+        InputStream stream = new URL("http://localhost:" + getPort() + "/issues/35/65").openStream();
+        assertEquals("Here's your issue 35:65", IOUtils.toString(stream));
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                restConfiguration("jetty").host("localhost").port(getPort());
+
+                rest("/")
+                    .get("/issues/{isin}").route().id("issues")
+                        .process(e -> e.getOut().setBody("Here's your issue " + e.getIn().getHeader("isin"))).endRest()
+                    .get("/listings").route().id("listings")
+                        .process(e -> e.getOut().setBody("some listings"));
+            }
+        };
+    }
+}


Mime
View raw message