[CAMEL-8969] Add start of camel-coap component

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

Branch: refs/heads/master
Commit: fdcf9182fb59929dd9239406f1db6dc8bc0c22b3
Parents: 17d39d9
Author: Daniel Kulp <dk...@apache.org>
Authored: Tue Jul 14 13:00:36 2015 -0400
Committer: Daniel Kulp <dk...@apache.org>
Committed: Tue Jul 14 16:18:15 2015 -0400

----------------------------------------------------------------------
 components/camel-coap/pom.xml                   | 71 +++++++++++++++
 .../org/apache/camel/coap/CoAPComponent.java    | 93 ++++++++++++++++++++
 .../org/apache/camel/coap/CoAPConsumer.java     | 80 +++++++++++++++++
 .../org/apache/camel/coap/CoAPEndpoint.java     | 76 ++++++++++++++++
 .../org/apache/camel/coap/CoAPProducer.java     | 40 +++++++++
 .../services/org/apache/camel/component/coap    |  1 +
 .../apache/camel/coap/CoAPComponentTest.java    | 49 +++++++++++
 .../src/test/resources/log4j.properties         | 14 +++
 ...elSpringDelegatingTestContextLoaderTest.java |  2 +-
 components/pom.xml                              |  1 +
 10 files changed, 426 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/pom.xml
----------------------------------------------------------------------
diff --git a/components/camel-coap/pom.xml b/components/camel-coap/pom.xml
new file mode 100644
index 0000000..fe51b5e
--- /dev/null
+++ b/components/camel-coap/pom.xml
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>components</artifactId>
+    <groupId>org.apache.camel</groupId>
+    <version>2.16-SNAPSHOT</version>
+  </parent>
+
+  <groupId>org.apache.camel</groupId>
+  <artifactId>camel-coap</artifactId>
+  <packaging>bundle</packaging>
+  <version>2.16-SNAPSHOT</version>
+
+  <name>Camel :: CoAP</name>
+  <url>http://www.myorganization.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    <camel.osgi.export>
+        org.apache.camel.component.coap;${camel.osgi.version},
+    </camel.osgi.export>
+    <camel.osgi.import>
+        !org.apache.camel.component.coap,
+        *
+    </camel.osgi.import>
+    <camel.osgi.export.service>
+        org.apache.camel.spi.ComponentResolver;component=coap
+    </camel.osgi.export.service>
+    
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.eclipse.californium</groupId>
+      <artifactId>californium-core</artifactId>
+      <version>1.0.0-M3</version>
+    </dependency>
+
+    <!-- logging -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+    <!-- testing -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-test</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/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
new file mode 100644
index 0000000..15ac123
--- /dev/null
+++ 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPComponent.java
@@ -0,0 +1,93 @@
+/**
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.impl.UriEndpointComponent;
+import org.apache.camel.spi.RestConsumerFactory;
+import org.eclipse.californium.core.CoapServer;
+import org.eclipse.californium.core.network.config.NetworkConfig;
+
+/**
+ * Represents the component that manages {@link CoAPEndpoint}.
+ */
+public class CoAPComponent extends UriEndpointComponent implements 
RestConsumerFactory {
+    final Map<Integer, CoapServer> servers = new ConcurrentHashMap<>();
+    
+    public CoAPComponent() {
+        super(CoAPEndpoint.class);
+    }
+
+    public CoAPComponent(CamelContext context) {
+        super(context, CoAPEndpoint.class);
+    }
+
+    public synchronized CoapServer getServer(int port) {
+        CoapServer server = servers.get(port);
+        if (server == null) {
+            NetworkConfig config = new NetworkConfig();
+            //FIXME- configure the network stuff
+            server = new CoapServer(config, port);
+            servers.put(port, server);
+            if (this.isStarted()) {
+                server.start();
+            }
+        }
+        return server;
+    }
+    
+    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+        Endpoint endpoint = new CoAPEndpoint(uri, this);
+        setProperties(endpoint, parameters);
+        return endpoint;
+    }
+
+    @Override
+    public Consumer createConsumer(CamelContext camelContext, 
+                                   Processor processor, 
+                                   String verb,
+                                   String basePath,
+                                   String uriTemplate,
+                                   String consumes, 
+                                   String produces,
+                                   Map<String, Object> parameters) throws 
Exception {
+        return null;
+    }
+    
+    
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        for (CoapServer s : servers.values()) {
+            s.start();
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        for (CoapServer s : servers.values()) {
+            s.stop();
+        }
+        super.doStop();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPConsumer.java 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPConsumer.java
new file mode 100644
index 0000000..fc7c0a3
--- /dev/null
+++ 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPConsumer.java
@@ -0,0 +1,80 @@
+/**
+ * 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.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.impl.DefaultConsumer;
+import org.eclipse.californium.core.CoapResource;
+import org.eclipse.californium.core.coap.CoAP.ResponseCode;
+import org.eclipse.californium.core.network.Exchange;
+import org.eclipse.californium.core.server.resources.CoapExchange;
+
+/**
+ * The CoAP consumer.
+ */
+public class CoAPConsumer extends DefaultConsumer {
+    private final CoAPEndpoint endpoint;
+    private CoapResource resource;
+
+    public CoAPConsumer(final CoAPEndpoint endpoint, final Processor 
processor) {
+        super(endpoint, processor);
+        this.endpoint = endpoint;
+        
+        String path = endpoint.getUri().getPath();
+        if (path.startsWith("/")) {
+            path = path.substring(1);
+        }
+        
+        this.resource = new CoapResource(path) {
+
+            @Override
+            public void handleRequest(Exchange exchange) {
+                CoapExchange cexchange = new CoapExchange(exchange, this);
+                org.apache.camel.Exchange camelExchange = 
endpoint.createExchange();
+                byte bytes[] = exchange.getCurrentRequest().getPayload();
+                camelExchange.getIn().setBody(bytes);
+                try {
+                    processor.process(camelExchange);
+                    
+                    
+                    Message target = camelExchange.hasOut() ? 
camelExchange.getOut() : camelExchange.getIn();
+                    
+                    cexchange.respond(ResponseCode.CONTENT, 
target.getBody(byte[].class));
+
+                } catch (Exception e) {
+                    // TODO Auto-generated catch block
+                    e.printStackTrace();
+                }
+            }
+            
+        };
+    }
+    
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        endpoint.getCoapServer().add(resource);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        endpoint.getCoapServer().remove(resource);
+        super.doStop();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
new file mode 100644
index 0000000..5cc63ca
--- /dev/null
+++ 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPEndpoint.java
@@ -0,0 +1,76 @@
+/**
+ * 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 java.net.URI;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriPath;
+import org.eclipse.californium.core.CoapServer;
+
+/**
+ * Represents a CoAP endpoint.
+ */
+@UriEndpoint(scheme = "coap", title = "CoAP", syntax = "coap:name", 
consumerClass = CoAPConsumer.class, label = "CoAP")
+public class CoAPEndpoint extends DefaultEndpoint {
+    @UriPath
+    private URI uri;
+    
+    
+    private CoAPComponent component;
+    
+    public CoAPEndpoint(String uri, CoAPComponent component) {
+        super(uri, component);
+        try {
+            this.uri = new URI(uri);
+        } catch (java.net.URISyntaxException use) {
+            this.uri = null;
+        }
+        this.component = component;
+    }
+
+    public Producer createProducer() throws Exception {
+        return new CoAPProducer(this);
+    }
+
+    public Consumer createConsumer(Processor processor) throws Exception {
+        return new CoAPConsumer(this, processor);
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+    
+    public void setUri(URI u) {
+        uri = u;
+    }
+    
+    /**
+     * The URI for the CoAP endpoint
+     */
+    public URI getUri() {
+        return uri;
+    }
+
+    public CoapServer getCoapServer() {
+        return component.getServer(getUri().getPort());
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java
new file mode 100644
index 0000000..70ab363
--- /dev/null
+++ 
b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java
@@ -0,0 +1,40 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.impl.DefaultProducer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The CoAP producer.
+ */
+public class CoAPProducer extends DefaultProducer {
+    private static final Logger LOG = 
LoggerFactory.getLogger(CoAPProducer.class);
+    private CoAPEndpoint endpoint;
+
+    public CoAPProducer(CoAPEndpoint endpoint) {
+        super(endpoint);
+        this.endpoint = endpoint;
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        System.out.println(exchange.getIn().getBody());    
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/main/resources/META-INF/services/org/apache/camel/component/coap
----------------------------------------------------------------------
diff --git 
a/components/camel-coap/src/main/resources/META-INF/services/org/apache/camel/component/coap
 
b/components/camel-coap/src/main/resources/META-INF/services/org/apache/camel/component/coap
new file mode 100644
index 0000000..9dfaa26
--- /dev/null
+++ 
b/components/camel-coap/src/main/resources/META-INF/services/org/apache/camel/component/coap
@@ -0,0 +1 @@
+class=org.apache.camel.coap.CoAPComponent

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTest.java
 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTest.java
new file mode 100644
index 0000000..0000f8c
--- /dev/null
+++ 
b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPComponentTest.java
@@ -0,0 +1,49 @@
+/**
+ * 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 CoAPComponentTest extends CamelTestSupport {
+
+    @Test
+    public void testCoAP() throws Exception {
+        NetworkConfig.createStandardWithoutFile();
+        CoapClient client = new 
CoapClient("coap://localhost:7777/TestResource");
+        CoapResponse rsp = client.get();
+        assertEquals("Hello ", rsp.getResponseText());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("coap://localhost:7777/TestResource")
+                    .convertBodyTo(String.class)
+                    .to("log:exch")
+                    .transform(body().prepend("Hello "))
+                    .to("log:exch");                
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-coap/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-coap/src/test/resources/log4j.properties 
b/components/camel-coap/src/test/resources/log4j.properties
new file mode 100644
index 0000000..3b1bd38
--- /dev/null
+++ b/components/camel-coap/src/test/resources/log4j.properties
@@ -0,0 +1,14 @@
+#
+# The logging properties used
+#
+log4j.rootLogger=INFO, out
+
+# uncomment the following line to turn on Camel debugging
+#log4j.logger.org.apache.camel=DEBUG
+
+# CONSOLE appender not used by default
+log4j.appender.out=org.apache.log4j.ConsoleAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
+#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - 
%m%n
+

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java
 
b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java
index a5092fe..4150fc3 100644
--- 
a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java
+++ 
b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/test/CamelSpringDelegatingTestContextLoaderTest.java
@@ -41,7 +41,7 @@ import org.springframework.test.context.ContextConfiguration;
         classes = 
{CamelSpringDelegatingTestContextLoaderTest.TestConfig.class},
         // Since Camel 2.11.0 
         loader = CamelSpringDelegatingTestContextLoader.class
-)
+    )
 @MockEndpoints
 public class CamelSpringDelegatingTestContextLoaderTest {
     @EndpointInject(uri = "mock:direct:end")

http://git-wip-us.apache.org/repos/asf/camel/blob/fdcf9182/components/pom.xml
----------------------------------------------------------------------
diff --git a/components/pom.xml b/components/pom.xml
index 0a6c3b5..7982b57 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -80,6 +80,7 @@
     <module>camel-cdi</module>
     <module>camel-chunk</module>
     <module>camel-cmis</module>
+    <module>camel-coap</module>
     <module>camel-cometd</module>
     <module>camel-context</module>
     <module>camel-couchdb</module>

Reply via email to