Repository: camel
Updated Branches:
  refs/heads/master 8fbc14210 -> f7f53a477


[CAMEL-9033] Abstract undertow HttpHandler creation


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

Branch: refs/heads/master
Commit: 3858bce27356da06b32a640346f10238ce5aa301
Parents: 8fbc142
Author: Thomas Diesler <thomas.dies...@jboss.com>
Authored: Wed Jul 29 17:24:43 2015 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Thu Jul 30 08:32:11 2015 +0200

----------------------------------------------------------------------
 .../component/undertow/UndertowComponent.java   | 49 +++++++++-----------
 .../component/undertow/UndertowConsumer.java    | 35 +++++++++++++-
 .../component/undertow/UndertowEndpoint.java    |  7 ++-
 .../camel/component/undertow/UndertowHost.java  | 38 +++++++++++++++
 .../component/undertow/UndertowHostFactory.java | 41 ++++++++++++++++
 5 files changed, 139 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3858bce2/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
index 8f83680..489d2cf 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowComponent.java
@@ -139,31 +139,6 @@ public class UndertowComponent extends 
UriEndpointComponent implements RestConsu
         serversRegistry.clear();
     }
 
-    protected Undertow rebuildServer(UndertowRegistry registy) {
-        Undertow.Builder result = Undertow.builder();
-        int port = registy.getPort();
-        if (registy.getSslContext() != null) {
-            result = result.addHttpsListener(registy.getPort(), 
registy.getHost(), registy.getSslContext());
-        } else {
-            result = result.addHttpListener(registy.getPort(), 
registy.getHost());
-        }
-        PathHandler path = Handlers.path(new NotFoundHandler());
-        for (URI key : registy.getConsumersRegistry().keySet()) {
-            UndertowConsumer consumer = 
registy.getConsumersRegistry().get(key);
-            URI httpUri = consumer.getEndpoint().getHttpURI();
-            if (consumer.getEndpoint().getMatchOnUriPrefix()) {
-                path.addPrefixPath(httpUri.getPath(), new 
HttpCamelHandler(consumer));
-
-            } else {
-                path.addExactPath(httpUri.getPath(), new 
HttpCamelHandler(consumer));
-
-            }
-            LOG.debug("Rebuild for path: {}", httpUri.getPath());
-        }
-        result = result.setHandler(path);
-        return result.build();
-    }
-
     public void registerConsumer(UndertowConsumer consumer) {
         int port = consumer.getEndpoint().getHttpURI().getPort();
         if (serversRegistry.containsKey(port)) {
@@ -195,7 +170,6 @@ public class UndertowComponent extends UriEndpointComponent 
implements RestConsu
         int port = consumer.getEndpoint().getHttpURI().getPort();
         LOG.info("Starting server on port: {}", port);
         UndertowRegistry undertowRegistry = serversRegistry.get(port);
-
         if (undertowRegistry.getServer() != null) {
             //server is running, we need to stop it first and then rebuild
             undertowRegistry.getServer().stop();
@@ -216,4 +190,27 @@ public class UndertowComponent extends 
UriEndpointComponent implements RestConsu
         this.undertowHttpBinding = undertowHttpBinding;
     }
 
+    private Undertow rebuildServer(UndertowRegistry registy) {
+        Undertow.Builder result = Undertow.builder();
+        if (registy.getSslContext() != null) {
+            result = result.addHttpsListener(registy.getPort(), 
registy.getHost(), registy.getSslContext());
+        } else {
+            result = result.addHttpListener(registy.getPort(), 
registy.getHost());
+        }
+        PathHandler path = Handlers.path(new NotFoundHandler());
+        for (URI key : registy.getConsumersRegistry().keySet()) {
+            UndertowConsumer consumer = 
registy.getConsumersRegistry().get(key);
+            URI httpUri = consumer.getEndpoint().getHttpURI();
+            HttpCamelHandler handler = new HttpCamelHandler(consumer);
+            if (consumer.getEndpoint().getMatchOnUriPrefix()) {
+                path.addPrefixPath(httpUri.getPath(), handler);
+            } else {
+                path.addExactPath(httpUri.getPath(), handler);
+            }
+            LOG.debug("Rebuild for path: {}", httpUri.getPath());
+        }
+        result = result.setHandler(path);
+        return result.build();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/3858bce2/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
index 9f3e4cc..77eaa1a 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
@@ -16,7 +16,12 @@
  */
 package org.apache.camel.component.undertow;
 
+import io.undertow.server.HttpHandler;
+
+import java.net.URI;
+
 import org.apache.camel.Processor;
+import org.apache.camel.component.undertow.handlers.HttpCamelHandler;
 import org.apache.camel.impl.DefaultConsumer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,7 +47,17 @@ public class UndertowConsumer extends DefaultConsumer {
         super.doStart();
         LOG.debug("Undertow consumer is starting");
         getEndpoint().getComponent().registerConsumer(this);
-        getEndpoint().getComponent().startServer(this);
+
+        UndertowHostFactory factory = 
UndertowHostFactory.Locator.getUndertowHostFactory();
+        if (factory == null) {
+            factory = new DefaultUndertowHostFactory();
+        }
+
+        URI httpUri = getEndpoint().getHttpURI();
+        UndertowHost host = factory.createUndertowHost();
+
+        host.validateEndpointURI(httpUri);
+        host.registerHandler(httpUri.getPath(), new HttpCamelHandler(this));
     }
 
     @Override
@@ -51,4 +66,22 @@ public class UndertowConsumer extends DefaultConsumer {
         getEndpoint().getComponent().unregisterConsumer(this);
     }
 
+    class DefaultUndertowHostFactory implements UndertowHostFactory {
+
+        @Override
+        public UndertowHost createUndertowHost() {
+            return new UndertowHost () {
+
+                @Override
+                public void validateEndpointURI(URI httpURI) {
+                    // all URIs are good
+                }
+
+                @Override
+                public void registerHandler(String path, HttpHandler handler) {
+                    
getEndpoint().getComponent().startServer(UndertowConsumer.this);
+                }
+            };
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/3858bce2/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
index 71899ca..851e9d5 100644
--- 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
@@ -16,11 +16,13 @@
  */
 package org.apache.camel.component.undertow;
 
+import io.undertow.server.HttpServerExchange;
+
 import java.net.URI;
 import java.net.URISyntaxException;
+
 import javax.net.ssl.SSLContext;
 
-import io.undertow.server.HttpServerExchange;
 import org.apache.camel.Consumer;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -34,8 +36,6 @@ import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.util.jsse.SSLContextParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Represents an Undertow endpoint.
@@ -43,7 +43,6 @@ import org.slf4j.LoggerFactory;
 @UriEndpoint(scheme = "undertow", title = "Undertow", syntax = 
"undertow:httpURI",
     consumerClass = UndertowConsumer.class, label = "http")
 public class UndertowEndpoint extends DefaultEndpoint implements 
HeaderFilterStrategyAware {
-    private static final Logger LOG = 
LoggerFactory.getLogger(UndertowEndpoint.class);
 
     private UndertowComponent component;
     private SSLContext sslContext;

http://git-wip-us.apache.org/repos/asf/camel/blob/3858bce2/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHost.java
----------------------------------------------------------------------
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHost.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHost.java
new file mode 100644
index 0000000..f45309f
--- /dev/null
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHost.java
@@ -0,0 +1,38 @@
+/**
+ * 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.component.undertow;
+
+import io.undertow.server.HttpHandler;
+
+import java.net.URI;
+
+/**
+ * An undertow host abstraction
+ *
+ */
+public interface UndertowHost {
+
+    /**
+     * Validate whether this host can process the given URI
+     */
+    void validateEndpointURI(URI httpURI);
+
+    /**
+     * register a handler on the given path
+     */
+    void registerHandler(String path, HttpHandler handler);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3858bce2/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHostFactory.java
----------------------------------------------------------------------
diff --git 
a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHostFactory.java
 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHostFactory.java
new file mode 100644
index 0000000..b604fbe
--- /dev/null
+++ 
b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHostFactory.java
@@ -0,0 +1,41 @@
+/**
+ * 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.component.undertow;
+
+/**
+ * A factory to create an undertow host
+ */
+public interface UndertowHostFactory {
+
+    /**
+     * Create or get an undertow host
+     */
+    UndertowHost createUndertowHost();
+
+    public class Locator {
+
+        private static UndertowHostFactory globalFactory;
+
+        public static UndertowHostFactory getUndertowHostFactory() {
+            return globalFactory;
+        }
+
+        public static void setUndertowHostFactory(UndertowHostFactory factory) 
{
+            globalFactory = factory;
+        }
+    }
+}
\ No newline at end of file

Reply via email to