camel-mybatis: add outputHeader parameter (CAMEL-8192)

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

Branch: refs/heads/master
Commit: c88effa908591f824c989a43e625178744bf12eb
Parents: 7e7c659
Author: Askannon <askan...@flexarc.com>
Authored: Wed Dec 31 13:33:28 2014 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Fri Jan 2 08:30:44 2015 +0100

----------------------------------------------------------------------
 .../component/mybatis/MyBatisConsumer.java      |  7 +++-
 .../component/mybatis/MyBatisEndpoint.java      | 17 ++++++++
 .../component/mybatis/MyBatisProducer.java      | 32 +++++++++++----
 .../MyBatisSelectOneWithOutputHeaderTest.java   | 42 ++++++++++++++++++++
 4 files changed, 90 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c88effa9/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisConsumer.java
 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisConsumer.java
index 0b0cd68..225f588 100644
--- 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisConsumer.java
+++ 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisConsumer.java
@@ -156,9 +156,14 @@ public class MyBatisConsumer extends 
ScheduledBatchPollingConsumer {
     private Exchange createExchange(Object data) {
         final MyBatisEndpoint endpoint = getEndpoint();
         final Exchange exchange = 
endpoint.createExchange(ExchangePattern.InOnly);
+        final String outputHeader = getEndpoint().getOutputHeader();
 
         Message msg = exchange.getIn();
-        msg.setBody(data);
+        if(outputHeader != null) {
+               msg.setHeader(outputHeader, data);
+        } else {
+               msg.setBody(data);
+        }
         msg.setHeader(MyBatisConstants.MYBATIS_STATEMENT_NAME, 
endpoint.getStatement());
 
         return exchange;

http://git-wip-us.apache.org/repos/asf/camel/blob/c88effa9/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
index 50a6eb1..93a2c12 100644
--- 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
+++ 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisEndpoint.java
@@ -44,6 +44,8 @@ public class MyBatisEndpoint extends DefaultPollingEndpoint {
     private StatementType statementType;
     @UriParam
     private int maxMessagesPerPoll;
+    @UriParam
+    private String outputHeader;
 
     public MyBatisEndpoint() {
     }
@@ -138,4 +140,19 @@ public class MyBatisEndpoint extends 
DefaultPollingEndpoint {
         this.maxMessagesPerPoll = maxMessagesPerPoll;
     }
 
+       public String getOutputHeader() {
+               return outputHeader;
+       }
+
+    /**
+     * Store the query result in a header instead of the message body.
+     * By default, outputHeader == null and the query result is stored in the 
message body,
+     * any existing content in the message body is discarded.
+     * If outputHeader is set, the value is used as the name of the header to 
store the
+     * query result and the original message body is preserved.
+     */
+       public void setOutputHeader(String outputHeader) {
+               this.outputHeader = outputHeader;
+       }
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c88effa9/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
index 6465636..f37c6d3 100644
--- 
a/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
+++ 
b/components/camel-mybatis/src/main/java/org/apache/camel/component/mybatis/MyBatisProducer.java
@@ -236,22 +236,35 @@ public class MyBatisProducer extends DefaultProducer {
 
             // we should not set the body if its a stored procedure as the 
result is already in its OUT parameter
             MappedStatement ms = 
session.getConfiguration().getMappedStatement(statement);
+            final String outputHeader = getEndpoint().getOutputHeader();
             if (ms != null && ms.getStatementType() == 
org.apache.ibatis.mapping.StatementType.CALLABLE) {
                 if (result == null) {
                     LOG.trace("Setting result as existing body as MyBatis 
statement type is Callable, and there was no result.");
                     answer.setBody(exchange.getIn().getBody());
                 } else {
-                    // set the result as body for insert
-                    LOG.trace("Setting result as body: {}", result);
-                    answer.setBody(result);
+                       if(outputHeader != null) {
+                               // set the result as header for insert
+                       LOG.trace("Setting result as header [{}]: {}", 
outputHeader, result);
+                               answer.setHeader(outputHeader, result);
+                       } else {
+                           // set the result as body for insert
+                           LOG.trace("Setting result as body: {}", result);
+                           answer.setBody(result);
+                           answer.setHeader(MyBatisConstants.MYBATIS_RESULT, 
result);
+                       }
                 }
             } else {
-                // set the result as body for insert
-                LOG.trace("Setting result as body: {}", result);
-                answer.setBody(result);
+               if(outputHeader != null) {
+                       LOG.trace("Setting result as header [{}]: {}", 
outputHeader, result);
+                       answer.setHeader(outputHeader, result);
+               } else {
+                       // set the result as body for insert
+                       LOG.trace("Setting result as body: {}", result);
+                       answer.setBody(result);
+                       answer.setHeader(MyBatisConstants.MYBATIS_RESULT, 
result);
+               }
             }
 
-            answer.setHeader(MyBatisConstants.MYBATIS_RESULT, result);
             answer.setHeader(MyBatisConstants.MYBATIS_STATEMENT_NAME, 
statement);
         } else {
             Message msg = exchange.getIn();
@@ -260,4 +273,9 @@ public class MyBatisProducer extends DefaultProducer {
         }
     }
 
+    @Override
+    public MyBatisEndpoint getEndpoint() {
+        return (MyBatisEndpoint) super.getEndpoint();
+    }
+    
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/c88effa9/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectOneWithOutputHeaderTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectOneWithOutputHeaderTest.java
 
b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectOneWithOutputHeaderTest.java
new file mode 100644
index 0000000..c095511
--- /dev/null
+++ 
b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisSelectOneWithOutputHeaderTest.java
@@ -0,0 +1,42 @@
+package org.apache.camel.component.mybatis;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class MyBatisSelectOneWithOutputHeaderTest extends MyBatisTestSupport {
+
+       private static final String TEST_CASE_HEADER_NAME = "testCaseHeader";
+       private static final int TEST_ACCOUNT_ID = 456;
+       
+    @Test
+    public void testSelectOneWithOutputHeader() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        
mock.message(0).header(TEST_CASE_HEADER_NAME).isInstanceOf(Account.class);
+        mock.message(0).body().isEqualTo(TEST_ACCOUNT_ID);
+        mock.message(0).header(MyBatisConstants.MYBATIS_RESULT).isNull();
+
+        template.sendBody("direct:start", TEST_ACCOUNT_ID);
+
+        assertMockEndpointsSatisfied();
+
+        Account account = 
mock.getReceivedExchanges().get(0).getIn().getHeader(TEST_CASE_HEADER_NAME, 
Account.class);
+        assertEquals("Claus", account.getFirstName());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                from("direct:start")
+                    
.to("mybatis:selectAccountById?statementType=SelectOne&outputHeader=" + 
TEST_CASE_HEADER_NAME)
+                    .to("mock:result");
+                // END SNIPPET: e1
+            }
+        };
+    }
+       
+}

Reply via email to