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-9654: camel-undertow - rest-dsl should support {name} placeholders from path
Date Sun, 28 Feb 2016 11:47:08 GMT
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 <davsclaus@apache.org>
Authored: Sun Feb 28 12:43:21 2016 +0100
Committer: Claus Ibsen <davsclaus@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;
     }
 


Mime
View raw message