You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/08/27 10:42:32 UTC

svn commit: r1377599 - in /camel/trunk/components/camel-saxon/src: main/java/org/apache/camel/component/xquery/ test/java/org/apache/camel/component/xquery/

Author: ningjiang
Date: Mon Aug 27 08:42:31 2012
New Revision: 1377599

URL: http://svn.apache.org/viewvc?rev=1377599&view=rev
Log:
CAMEL-5503 Added headerName for the @XQuery

Added:
    camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/BeanWithXQueryInjectionUsingHeaderValueTest.java
Modified:
    camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQuery.java
    camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryAnnotationExpressionFactory.java
    camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java

Modified: camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQuery.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQuery.java?rev=1377599&r1=1377598&r2=1377599&view=diff
==============================================================================
--- camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQuery.java (original)
+++ camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQuery.java Mon Aug 27 08:42:31 2012
@@ -43,4 +43,10 @@ public @interface XQuery {
         @NamespacePrefix(prefix = "soap", uri = "http://www.w3.org/2003/05/soap-envelope"),
         @NamespacePrefix(prefix = "xsd", uri = "http://www.w3.org/2001/XMLSchema")
     };
+    
+    /**
+     * @return The name of the header we want to apply the Xquery expression to.
+     *  If this is empty then the Xquery expression will be applied to the body instead.
+     */
+    String headerName() default "";
 }

Modified: camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryAnnotationExpressionFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryAnnotationExpressionFactory.java?rev=1377599&r1=1377598&r2=1377599&view=diff
==============================================================================
--- camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryAnnotationExpressionFactory.java (original)
+++ camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryAnnotationExpressionFactory.java Mon Aug 27 08:42:31 2012
@@ -27,6 +27,7 @@ import org.apache.camel.Expression;
 import org.apache.camel.component.bean.DefaultAnnotationExpressionFactory;
 import org.apache.camel.language.LanguageAnnotation;
 import org.apache.camel.language.NamespacePrefix;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version 
@@ -41,7 +42,9 @@ public class XQueryAnnotationExpressionF
         if (annotation instanceof XQuery) {
             XQuery xQueryAnnotation = (XQuery)annotation;
             builder.setStripsAllWhiteSpace(xQueryAnnotation.stripsAllWhiteSpace());
-
+            if (ObjectHelper.isNotEmpty(xQueryAnnotation.headerName())) {
+                builder.setHeaderName(xQueryAnnotation.headerName());
+            }
             NamespacePrefix[] namespaces = xQueryAnnotation.namespaces();
             if (namespaces != null) {
                 for (NamespacePrefix namespacePrefix : namespaces) {

Modified: camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java?rev=1377599&r1=1377598&r2=1377599&view=diff
==============================================================================
--- camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java (original)
+++ camel/trunk/components/camel-saxon/src/main/java/org/apache/camel/component/xquery/XQueryBuilder.java Mon Aug 27 08:42:31 2012
@@ -93,6 +93,7 @@ public abstract class XQueryBuilder impl
     private boolean stripsAllWhiteSpace = true;
     private ModuleURIResolver moduleURIResolver;
     private boolean allowStAX;
+    private String headerName;
 
     @Override
     public String toString() {
@@ -193,8 +194,8 @@ public abstract class XQueryBuilder impl
         initialize(exchange);
 
         StringWriter buffer = new StringWriter();
-        SequenceIterator<Item<?>> iter = getExpression().iterator(createDynamicContext(exchange));
-        for (Item<?> item = iter.next(); item != null; item = iter.next()) {
+        SequenceIterator iter = getExpression().iterator(createDynamicContext(exchange));
+        for (Item item = iter.next(); item != null; item = iter.next()) {
             buffer.append(item.getStringValueCS());
         }
 
@@ -429,6 +430,14 @@ public abstract class XQueryBuilder impl
         this.stripsAllWhiteSpace = stripsAllWhiteSpace;
     }
 
+    public String getHeaderName() {
+        return headerName;
+    }
+
+    public void setHeaderName(String headerName) {
+        this.headerName = headerName;
+    }
+
     public boolean isAllowStAX() {
         return allowStAX;
     }
@@ -454,12 +463,21 @@ public abstract class XQueryBuilder impl
         DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
 
         Message in = exchange.getIn();
-
-        Item<?> item = in.getBody(Item.class);
+        Item item = null;
+        if (ObjectHelper.isNotEmpty(getHeaderName())) {
+            item = in.getHeader(getHeaderName(), Item.class);
+        } else {
+            item = in.getBody(Item.class);
+        }
         if (item != null) {
             dynamicQueryContext.setContextItem(item);
         } else {
-            Object body = in.getBody();
+            Object body = null;
+            if (ObjectHelper.isNotEmpty(getHeaderName())) {
+                body = in.getHeader(getHeaderName());
+            } else {
+                body = in.getBody();
+            }
 
             // the underlying input stream, which we need to close to avoid locking files or other resources
             InputStream is = null;
@@ -467,7 +485,11 @@ public abstract class XQueryBuilder impl
                 Source source;
                 // only convert to input stream if really needed
                 if (isInputStreamNeeded(exchange)) {
-                    is = exchange.getIn().getBody(InputStream.class);
+                    if (ObjectHelper.isNotEmpty(getHeaderName())) {
+                        is = exchange.getIn().getHeader(getHeaderName(), InputStream.class);
+                    } else {
+                        is = exchange.getIn().getBody(InputStream.class);
+                    }
                     source = getSource(exchange, is);
                 } else {
                     source = getSource(exchange, body);

Added: camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/BeanWithXQueryInjectionUsingHeaderValueTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/BeanWithXQueryInjectionUsingHeaderValueTest.java?rev=1377599&view=auto
==============================================================================
--- camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/BeanWithXQueryInjectionUsingHeaderValueTest.java (added)
+++ camel/trunk/components/camel-saxon/src/test/java/org/apache/camel/component/xquery/BeanWithXQueryInjectionUsingHeaderValueTest.java Mon Aug 27 08:42:31 2012
@@ -0,0 +1,61 @@
+/**
+ * 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.xquery;
+
+import javax.naming.Context;
+
+import org.apache.camel.Handler;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.util.jndi.JndiContext;
+import org.junit.Test;
+
+public class BeanWithXQueryInjectionUsingHeaderValueTest extends CamelTestSupport {
+    protected MyBean myBean = new MyBean();
+
+    @Test
+    public void testConstantXPathHeaders() throws Exception {
+        template.sendBodyAndHeader("bean:myBean", "<response>OK</response>",
+                                   "invoiceDetails", "<invoice><person><name>Alan</name><date>26/08/2012</date></person></invoice>");
+       
+        assertEquals("bean response:  " + myBean, "OK", myBean.response);
+        assertEquals("bean userName: " + myBean, "Alan", myBean.userName);
+        assertEquals("bean date:  " + myBean, "26/08/2012", myBean.date);
+    }
+    
+    @Override
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+        answer.bind("myBean", myBean);
+        return answer;
+    }
+
+    public static class MyBean {
+        public String userName;
+        public String date;
+        public String response;
+
+        @Handler
+        public void handler(@XQuery("/response") String response,
+                            @XQuery(headerName = "invoiceDetails", value = "/invoice/person/name") String userName,
+                            @XQuery(headerName = "invoiceDetails", value = "/invoice/person/date") String date) {
+            this.response = response;
+            this.userName = userName;
+            this.date = date;
+        }
+    }
+
+}