camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8323: camel-jcr to support node type header. Thanks to Maksim Kviatkouski for the patch.
Date Wed, 19 Aug 2015 09:21:04 GMT
Repository: camel
Updated Branches:
  refs/heads/master b8c877e9d -> e172ed735


CAMEL-8323: camel-jcr to support node type header. Thanks to Maksim Kviatkouski for the patch.


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

Branch: refs/heads/master
Commit: e172ed735d199dff32fb8982f9264ef4b19bbffc
Parents: b8c877e
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Aug 19 11:29:13 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Aug 19 11:29:13 2015 +0200

----------------------------------------------------------------------
 .../camel/component/jcr/JcrConstants.java       |  1 +
 .../apache/camel/component/jcr/JcrProducer.java | 20 ++++++++++----
 .../camel/component/jcr/JcrProducerTest.java    | 29 ++++++++++++++++++--
 3 files changed, 42 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e172ed73/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConstants.java
b/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConstants.java
index 116fcbd..db05fcd 100644
--- a/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConstants.java
+++ b/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrConstants.java
@@ -28,6 +28,7 @@ public final class JcrConstants {
     public static final String JCR_OPERATION = "CamelJcrOperation";
     public static final String JCR_INSERT = "CamelJcrInsert";
     public static final String JCR_GET_BY_ID = "CamelJcrGetById";
+    public static final String JCR_NODE_TYPE = "CamelJcrNodeType";
 
     private JcrConstants() {
         // Utility class

http://git-wip-us.apache.org/repos/asf/camel/blob/e172ed73/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
b/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
index aca67b6..cca3522 100644
--- a/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
+++ b/components/camel-jcr/src/main/java/org/apache/camel/component/jcr/JcrProducer.java
@@ -21,7 +21,6 @@ import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
-
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyIterator;
@@ -50,8 +49,8 @@ public class JcrProducer extends DefaultProducer {
         String operation = determineOperation(message);
         try {
             if (JcrConstants.JCR_INSERT.equals(operation)) {
-                Node base = findOrCreateNode(session.getRootNode(), getJcrEndpoint().getBase());
-                Node node = findOrCreateNode(base, getNodeName(message));
+                Node base = findOrCreateNode(session.getRootNode(), getJcrEndpoint().getBase(),
"");
+                Node node = findOrCreateNode(base, getNodeName(message), getNodeType(message));
                 Map<String, Object> headers = filterComponentHeaders(message.getHeaders());
                 for (String key : headers.keySet()) {
                     Object header = message.getHeader(key);
@@ -95,7 +94,7 @@ public class JcrProducer extends DefaultProducer {
         Map<String, Object> result = new HashMap<String, Object>(properties.size());
         for (Map.Entry<String, Object> entry : properties.entrySet()) {
             String key = entry.getKey();
-            if (!key.equals(JcrConstants.JCR_NODE_NAME) && !key.equals(JcrConstants.JCR_OPERATION))
{
+            if (!key.equals(JcrConstants.JCR_NODE_NAME) && !key.equals(JcrConstants.JCR_OPERATION)
&& !key.equals(JcrConstants.JCR_NODE_TYPE)) {
                 result.put(entry.getKey(), entry.getValue());
             }
         }
@@ -145,7 +144,12 @@ public class JcrProducer extends DefaultProducer {
         return nodeName != null ? nodeName : message.getExchange().getExchangeId();
     }
 
-    private Node findOrCreateNode(Node parent, String path) throws RepositoryException {
+    private String getNodeType(Message message) {
+        String nodeType = message.getHeader(JcrConstants.JCR_NODE_TYPE, String.class);
+        return nodeType != null ? nodeType : "";
+    }
+
+    private Node findOrCreateNode(Node parent, String path, String nodeType) throws RepositoryException
{
         Node result = parent;
         for (String component : path.split("/")) {
             component = Text.escapeIllegalJcrChars(component);
@@ -153,7 +157,11 @@ public class JcrProducer extends DefaultProducer {
                 if (result.hasNode(component)) {
                     result = result.getNode(component);
                 } else {
-                    result = result.addNode(component);
+                    if (ObjectHelper.isNotEmpty(nodeType)) {
+                        result = result.addNode(component, nodeType);
+                    } else {
+                        result = result.addNode(component);
+                    }
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/e172ed73/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerTest.java
b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerTest.java
index 9e2d3c5..ec72b05 100644
--- a/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerTest.java
+++ b/components/camel-jcr/src/test/java/org/apache/camel/component/jcr/JcrProducerTest.java
@@ -28,6 +28,8 @@ public class JcrProducerTest extends JcrRouteTestSupport {
     @Test
     public void testJcrProducer() throws Exception {
         Exchange exchange = createExchangeWithBody("<hello>world!</hello>");
+        exchange.getIn().setHeader(JcrConstants.JCR_NODE_NAME, "node");
+        exchange.getIn().setHeader("my.contents.property", exchange.getIn().getBody());
         Exchange out = template.send("direct:a", exchange);
         assertNotNull(out);
         String uuid = out.getOut().getBody(String.class);
@@ -44,14 +46,37 @@ public class JcrProducerTest extends JcrRouteTestSupport {
         }
     }
 
+    @Test
+    public void testNodeTypeIsSpecified() throws Exception {
+        Exchange exchange = createExchangeWithBody("Test");
+        exchange.getIn().removeHeader("testClass"); //there is no definition of such property
in nt:resource
+        exchange.getIn().setHeader(JcrConstants.JCR_NODE_NAME, "typedNode");
+        exchange.getIn().setHeader(JcrConstants.JCR_NODE_TYPE, "nt:folder");
+        Exchange out = template.send("direct:a", exchange);
+        assertNotNull(out);
+        String uuid = out.getOut().getBody(String.class);
+        Session session = openSession();
+        try {
+            Node node = session.getNodeByIdentifier(uuid);
+            assertNotNull(node);
+            assertEquals("/home/test/typedNode", node.getPath());
+            assertEquals("nt:folder", node.getPrimaryNodeType().getName());
+        } finally {
+            if (session != null && session.isLive()) {
+                session.logout();
+            }
+        }
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
                 // START SNIPPET: jcr-create-node
-                from("direct:a").setHeader(JcrConstants.JCR_NODE_NAME, constant("node"))
-                        .setHeader("my.contents.property", body())
+                context.setUseBreadcrumb(false);
+
+                from("direct:a")
                         .to("jcr://user:pass@repository/home/test");
                 // END SNIPPET: jcr-create-node
             }


Mime
View raw message