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-10285: beanio - Allow custom error handler to add objects to results so you can build you own error DTOs
Date Wed, 07 Sep 2016 15:21:28 GMT
Repository: camel
Updated Branches:
  refs/heads/master 1e4c9a2ca -> be9ee3490


CAMEL-10285: beanio - Allow custom error handler to add objects to results so you can build
you own error DTOs


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

Branch: refs/heads/master
Commit: 942ee3d085be44147e025763d844a7bbbf553d1a
Parents: 1e4c9a2
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Sep 7 17:07:34 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Sep 7 17:07:34 2016 +0200

----------------------------------------------------------------------
 .../dataformat/beanio/BeanIODataFormat.java     |  2 +-
 .../dataformat/beanio/BeanIOErrorHandler.java   | 22 ++++++++++++
 .../camel/dataformat/beanio/BeanIOHelper.java   |  6 +++-
 .../camel/dataformat/beanio/BeanIOSplitter.java |  2 +-
 ...tSimpleCustomBeanReaderErrorHandlerTest.java |  9 +++--
 .../camel/dataformat/beanio/MyErrorDto.java     | 36 ++++++++++++++++++++
 .../camel/dataformat/beanio/MyErrorHandler.java |  6 ++++
 7 files changed, 78 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java
b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java
index 007f39d..7e59042 100644
--- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java
+++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIODataFormat.java
@@ -145,7 +145,7 @@ public class BeanIODataFormat extends ServiceSupport implements DataFormat,
Data
 
         BeanReader in = factory.createReader(getStreamName(), streamReader);
 
-        BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration,
exchange);
+        BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration,
exchange, results);
         in.setErrorHandler(errorHandler);
 
         try {

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOErrorHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOErrorHandler.java
b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOErrorHandler.java
index 8548641..2baf8fc 100644
--- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOErrorHandler.java
+++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOErrorHandler.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.dataformat.beanio;
 
+import java.util.List;
+
 import org.apache.camel.Exchange;
 import org.beanio.BeanReaderErrorHandler;
 import org.beanio.BeanReaderErrorHandlerSupport;
@@ -37,6 +39,7 @@ public class BeanIOErrorHandler extends BeanReaderErrorHandlerSupport {
 
     private BeanIOConfiguration configuration;
     private Exchange exchange;
+    private List<Object> results;
 
     public BeanIOErrorHandler() {
     }
@@ -45,6 +48,9 @@ public class BeanIOErrorHandler extends BeanReaderErrorHandlerSupport {
         // any custom init code here
     }
 
+    /**
+     * The configuration
+     */
     public BeanIOConfiguration getConfiguration() {
         return configuration;
     }
@@ -53,6 +59,9 @@ public class BeanIOErrorHandler extends BeanReaderErrorHandlerSupport {
         this.configuration = configuration;
     }
 
+    /**
+     * The current exchange
+     */
     public Exchange getExchange() {
         return exchange;
     }
@@ -61,6 +70,19 @@ public class BeanIOErrorHandler extends BeanReaderErrorHandlerSupport {
         this.exchange = exchange;
     }
 
+    /**
+     * Gets the current list of POJOs mapped when unmarshalling.
+     *
+     * @return the current list, or <tt>null</tt> if not unmarshalling
+     */
+    public List<Object> getResults() {
+        return results;
+    }
+
+    public void setResults(List<Object> results) {
+        this.results = results;
+    }
+
     @Override
     public void invalidRecord(InvalidRecordException ex) throws Exception {
         String msg = LOG_PREFIX + "InvalidRecord: " + ex.getMessage() + ": " + ex.getRecordContext().getRecordText();

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOHelper.java
b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOHelper.java
index 8bfbb58..7c15c02 100644
--- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOHelper.java
+++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOHelper.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.dataformat.beanio;
 
+import java.util.List;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.util.ObjectHelper;
 import org.beanio.BeanReaderErrorHandler;
@@ -29,7 +31,8 @@ public final class BeanIOHelper {
         // utility class
     }
 
-    public static BeanReaderErrorHandler getOrCreateBeanReaderErrorHandler(BeanIOConfiguration
configuration, Exchange exchange) throws Exception {
+    public static BeanReaderErrorHandler getOrCreateBeanReaderErrorHandler(BeanIOConfiguration
configuration,
+                                                                           Exchange exchange,
List<Object> results) throws Exception {
         BeanReaderErrorHandler answer = null;
 
         if (ObjectHelper.isNotEmpty(configuration.getBeanReaderErrorHandlerType())) {
@@ -49,6 +52,7 @@ public final class BeanIOHelper {
             BeanIOErrorHandler eh = (BeanIOErrorHandler) answer;
             eh.setConfiguration(configuration);
             eh.setExchange(exchange);
+            eh.setResults(results);
             eh.init();
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOSplitter.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOSplitter.java
b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOSplitter.java
index 2679722..9b5a0b6 100644
--- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOSplitter.java
+++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOSplitter.java
@@ -98,7 +98,7 @@ public class BeanIOSplitter implements Expression {
             beanReader = factory.createReader(getStreamName(), reader);
         }
 
-        BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration,
exchange);
+        BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration,
exchange, null);
         beanReader.setErrorHandler(errorHandler);
 
         return new BeanIOIterator(beanReader);

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest.java
b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest.java
index f9576c0..d078183 100644
--- a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest.java
+++ b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest.java
@@ -66,13 +66,18 @@ public class BeanIODataFormatSimpleCustomBeanReaderErrorHandlerTest extends
Came
 
     @Test
     public void testUnmarshalFail() throws Exception {
-        // there should be 1 splitted that failed
+        // there should be 1 splitted that failed we get also
         MockEndpoint mock = getMockEndpoint("mock:beanio-unmarshal");
-        mock.expectedMessageCount(2);
+        mock.expectedMessageCount(3);
+        mock.message(0).body().isInstanceOf(Employee.class);
+        mock.message(1).body().isInstanceOf(Employee.class);
+        mock.message(2).body().isInstanceOf(MyErrorDto.class);
 
         template.sendBody("direct:unmarshal", FIXED_FAIL_DATA);
 
         mock.assertIsSatisfied();
+
+        assertEquals("employee", mock.getReceivedExchanges().get(2).getIn().getBody(MyErrorDto.class).getRecord());
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorDto.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorDto.java
b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorDto.java
new file mode 100644
index 0000000..468f53f
--- /dev/null
+++ b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorDto.java
@@ -0,0 +1,36 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.dataformat.beanio;
+
+public class MyErrorDto {
+
+    private String record;
+    private String message;
+
+    public MyErrorDto(String record, String message) {
+        this.record = record;
+        this.message = message;
+    }
+
+    public String getRecord() {
+        return record;
+    }
+
+    public String getMessage() {
+        return message;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/942ee3d0/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorHandler.java
b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorHandler.java
index dc2fa49..8699203 100644
--- a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorHandler.java
+++ b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/MyErrorHandler.java
@@ -28,6 +28,12 @@ public class MyErrorHandler extends BeanIOErrorHandler {
         String id = getExchange().getExchangeId();
         String line = "ExchangeId: " + id + " Invalid record: " + ex.getMessage() + ": "
+ ex.getRecordContext().getRecordText();
 
+        // lets store to the results a dummy error DTO
+        MyErrorDto dto = new MyErrorDto(ex.getRecordName(), ex.getMessage());
+        getResults().add(dto);
+
+        getExchange().getOut().setHeader("record", ex.getRecordName());
+
         LOG.warn(line);
     }
 


Mime
View raw message