Repository: camel Updated Branches: refs/heads/master 0ab557c0c -> 0ff5785a9
CAMEL-11177: CoAP component starts redundant server instance Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0ff5785a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0ff5785a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0ff5785a Branch: refs/heads/master Commit: 0ff5785a9e6034d8452cb1021b054211955ccebb Parents: 0ab557c Author: James Netherton <jamesnether...@gmail.com> Authored: Thu Apr 20 16:31:54 2017 +0100 Committer: James Netherton <jamesnether...@gmail.com> Committed: Thu Apr 20 16:54:53 2017 +0100 ---------------------------------------------------------------------- .../org/apache/camel/coap/CoAPComponent.java | 27 ++++------- .../camel/coap/CoAPConsumerDefaultPortTest.java | 48 ++++++++++++++++++++ .../camel/coap/CoAPRestComponentTest.java | 6 +-- 3 files changed, 60 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0ff5785a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java index b6b600e..801bf23 100644 --- a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java +++ b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java @@ -38,9 +38,10 @@ import org.eclipse.californium.core.network.config.NetworkConfig; * Represents the component that manages {@link CoAPEndpoint}. */ public class CoAPComponent extends UriEndpointComponent implements RestConsumerFactory { + static final int DEFAULT_PORT = 5684; + final Map<Integer, CoapServer> servers = new ConcurrentHashMap<>(); - CoapServer defaultServer; - + public CoAPComponent() { super(CoAPEndpoint.class); } @@ -52,10 +53,7 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF public synchronized CoapServer getServer(int port) { CoapServer server = servers.get(port); if (server == null && port == -1) { - server = defaultServer; - } - if (server == null && port == -1) { - server = servers.get(5684); + server = getServer(DEFAULT_PORT); } if (server == null) { NetworkConfig config = new NetworkConfig(); @@ -68,7 +66,7 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF } return server; } - + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { Endpoint endpoint = new CoAPEndpoint(uri, this); setProperties(endpoint, parameters); @@ -76,12 +74,12 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF } @Override - public Consumer createConsumer(CamelContext camelContext, - Processor processor, + public Consumer createConsumer(CamelContext camelContext, + Processor processor, String verb, String basePath, String uriTemplate, - String consumes, + String consumes, String produces, RestConfiguration configuration, Map<String, Object> parameters) throws Exception { @@ -129,7 +127,7 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF if (!query.isEmpty()) { url += "&" + query; } - + CoAPEndpoint endpoint = camelContext.getEndpoint(url, CoAPEndpoint.class); setProperties(endpoint, parameters); @@ -145,13 +143,6 @@ public class CoAPComponent extends UriEndpointComponent implements RestConsumerF protected void doStart() throws Exception { super.doStart(); - RestConfiguration config = getCamelContext().getRestConfiguration("coap", true); - // configure additional options on coap configuration - if (config.getComponentProperties() != null && !config.getComponentProperties().isEmpty()) { - setProperties(this, config.getComponentProperties()); - } - defaultServer = getServer(config.getPort()); - for (CoapServer s : servers.values()) { s.start(); } http://git-wip-us.apache.org/repos/asf/camel/blob/0ff5785a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPConsumerDefaultPortTest.java ---------------------------------------------------------------------- diff --git a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPConsumerDefaultPortTest.java b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPConsumerDefaultPortTest.java new file mode 100644 index 0000000..5a56e26 --- /dev/null +++ b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPConsumerDefaultPortTest.java @@ -0,0 +1,48 @@ +/** + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.coap; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.eclipse.californium.core.CoapClient; +import org.eclipse.californium.core.CoapResponse; +import org.eclipse.californium.core.network.config.NetworkConfig; +import org.junit.Test; + +public class CoAPConsumerDefaultPortTest extends CamelTestSupport { + + @Test + public void testCoAPConsumerWithDefaultPort() throws Exception { + NetworkConfig.createStandardWithoutFile(); + + CoapClient client = new CoapClient("coap://localhost:" + CoAPComponent.DEFAULT_PORT + "/greeting"); + CoapResponse response = client.get(); + + assertEquals("Hello World", response.getResponseText()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("coap:localhost/greeting") + .setBody(constant("Hello World")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0ff5785a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java index a104804..e35f98f 100644 --- a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java +++ b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPRestComponentTest.java @@ -42,8 +42,8 @@ public class CoAPRestComponentTest extends CamelTestSupport { assertEquals("Hello Ducky", rsp.getResponseText()); rsp = client.post("data", MediaTypeRegistry.TEXT_PLAIN); assertEquals("Hello Ducky: data", rsp.getResponseText()); - - client = new CoapClient("coap://localhost:" + coapport + "/TestParms?id=Ducky"); + + client = new CoapClient("coap://localhost:" + coapport + "/TestParams?id=Ducky"); client.setTimeout(1000000); rsp = client.get(); assertEquals("Hello Ducky", rsp.getResponseText()); @@ -59,7 +59,7 @@ public class CoAPRestComponentTest extends CamelTestSupport { public void configure() throws Exception { restConfiguration("coap").host("localhost").port(coapport); - rest("/TestParms") + rest("/TestParams") .get().to("direct:get1") .post().to("direct:post1");