You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/02/28 12:47:08 UTC

[1/2] camel git commit: CAMEL-9654: camel-undertow - rest-dsl should support {name} placeholders from path

Repository: camel
Updated Branches:
  refs/heads/camel-2.16.x c7d7d31cc -> b7624cb81
  refs/heads/master 18a91701a -> 14914c798


CAMEL-9654: camel-undertow - rest-dsl should support {name} placeholders from path


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

Branch: refs/heads/master
Commit: 14914c7981d8ab2c22edebfe256dc13b77226c7c
Parents: 18a9170
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Feb 28 12:43:21 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Feb 28 12:43:21 2016 +0100

----------------------------------------------------------------------
 .../undertow/RestUndertowHttpBinding.java       | 67 ++++++++++++++++++++
 .../component/undertow/UndertowComponent.java   | 10 +++
 2 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/14914c79/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
new file mode 100644
index 0000000..c994871
--- /dev/null
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
@@ -0,0 +1,67 @@
+/**
+ * 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 java.util.Map;
+
+import io.undertow.server.HttpServerExchange;
+import org.apache.camel.Exchange;
+
+public class RestUndertowHttpBinding extends DefaultUndertowHttpBinding {
+
+    @Override
+    public void populateCamelHeaders(HttpServerExchange httpExchange, Map<String, Object> headersMap, Exchange exchange) throws Exception {
+        super.populateCamelHeaders(httpExchange, headersMap, exchange);
+
+        String path = httpExchange.getRequestPath();
+        if (path == null) {
+            return;
+        }
+
+        // in the endpoint the user may have defined rest {} placeholders
+        // so we need to map those placeholders with data from the incoming request context path
+
+        UndertowEndpoint endpoint = (UndertowEndpoint) exchange.getFromEndpoint();
+        String consumerPath = endpoint.getHttpURI().getPath();
+
+        if (useRestMatching(consumerPath)) {
+
+            // split using single char / is optimized in the jdk
+            String[] paths = path.split("/");
+            String[] consumerPaths = consumerPath.split("/");
+
+            for (int i = 0; i < consumerPaths.length; i++) {
+                if (paths.length < i) {
+                    break;
+                }
+                String p1 = consumerPaths[i];
+                if (p1.startsWith("{") && p1.endsWith("}")) {
+                    String key = p1.substring(1, p1.length() - 1);
+                    String value = paths[i];
+                    if (value != null) {
+                        headersMap.put(key, value);
+                    }
+                }
+            }
+        }
+    }
+
+    private boolean useRestMatching(String path) {
+        // only need to do rest matching if using { } placeholders
+        return path.indexOf('{') > -1;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/14914c79/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 6ac84cb..3015559 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
@@ -186,7 +186,17 @@ public class UndertowComponent extends UriEndpointComponent implements RestConsu
         UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class);
         setProperties(endpoint, parameters);
 
+        if (!map.containsKey("undertowHttpBinding")) {
+            // use the rest binding, if not using a custom http binding
+            endpoint.setUndertowHttpBinding(new RestUndertowHttpBinding());
+        }
+
+        // configure consumer properties
         Consumer consumer = endpoint.createConsumer(processor);
+        if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
+            setProperties(consumer, config.getConsumerProperties());
+        }
+
         return consumer;
     }
 


[2/2] camel git commit: CAMEL-9654: camel-undertow - rest-dsl should support {name} placeholders from path

Posted by da...@apache.org.
CAMEL-9654: camel-undertow - rest-dsl should support {name} placeholders from path


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

Branch: refs/heads/camel-2.16.x
Commit: b7624cb81a805d3d5b7f199349fcda9fc8fc88e6
Parents: c7d7d31
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Feb 28 12:43:21 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Feb 28 12:46:59 2016 +0100

----------------------------------------------------------------------
 .../undertow/RestUndertowHttpBinding.java       | 67 ++++++++++++++++++++
 .../component/undertow/UndertowComponent.java   | 10 +++
 2 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b7624cb8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
new file mode 100644
index 0000000..c994871
--- /dev/null
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/RestUndertowHttpBinding.java
@@ -0,0 +1,67 @@
+/**
+ * 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 java.util.Map;
+
+import io.undertow.server.HttpServerExchange;
+import org.apache.camel.Exchange;
+
+public class RestUndertowHttpBinding extends DefaultUndertowHttpBinding {
+
+    @Override
+    public void populateCamelHeaders(HttpServerExchange httpExchange, Map<String, Object> headersMap, Exchange exchange) throws Exception {
+        super.populateCamelHeaders(httpExchange, headersMap, exchange);
+
+        String path = httpExchange.getRequestPath();
+        if (path == null) {
+            return;
+        }
+
+        // in the endpoint the user may have defined rest {} placeholders
+        // so we need to map those placeholders with data from the incoming request context path
+
+        UndertowEndpoint endpoint = (UndertowEndpoint) exchange.getFromEndpoint();
+        String consumerPath = endpoint.getHttpURI().getPath();
+
+        if (useRestMatching(consumerPath)) {
+
+            // split using single char / is optimized in the jdk
+            String[] paths = path.split("/");
+            String[] consumerPaths = consumerPath.split("/");
+
+            for (int i = 0; i < consumerPaths.length; i++) {
+                if (paths.length < i) {
+                    break;
+                }
+                String p1 = consumerPaths[i];
+                if (p1.startsWith("{") && p1.endsWith("}")) {
+                    String key = p1.substring(1, p1.length() - 1);
+                    String value = paths[i];
+                    if (value != null) {
+                        headersMap.put(key, value);
+                    }
+                }
+            }
+        }
+    }
+
+    private boolean useRestMatching(String path) {
+        // only need to do rest matching if using { } placeholders
+        return path.indexOf('{') > -1;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/b7624cb8/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 695e376..4538243 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
@@ -160,7 +160,17 @@ public class UndertowComponent extends UriEndpointComponent implements RestConsu
         UndertowEndpoint endpoint = camelContext.getEndpoint(url, UndertowEndpoint.class);
         setProperties(endpoint, parameters);
 
+        if (!map.containsKey("undertowHttpBinding")) {
+            // use the rest binding, if not using a custom http binding
+            endpoint.setUndertowHttpBinding(new RestUndertowHttpBinding());
+        }
+
+        // configure consumer properties
         Consumer consumer = endpoint.createConsumer(processor);
+        if (config.getConsumerProperties() != null && !config.getConsumerProperties().isEmpty()) {
+            setProperties(consumer, config.getConsumerProperties());
+        }
+
         return consumer;
     }