You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/09/07 15:21:29 UTC
[2/2] camel git commit: CAMEL-10285: beanio - Allow custom error
handler to add objects to results so you can build you own error DTOs
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/be9ee349
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/be9ee349
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/be9ee349
Branch: refs/heads/master
Commit: be9ee3490f0414027459051b6ead031b7e4af6bb
Parents: 942ee3d
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Sep 7 17:19:30 2016 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Sep 7 17:19:30 2016 +0200
----------------------------------------------------------------------
.../dataformat/beanio/BeanIODataFormat.java | 2 +-
.../dataformat/beanio/BeanIOErrorHandler.java | 25 ++--
.../camel/dataformat/beanio/BeanIOHelper.java | 5 +-
.../camel/dataformat/beanio/BeanIOIterator.java | 18 +++
.../camel/dataformat/beanio/BeanIOSplitter.java | 6 +-
...plitterCustomBeanReaderErrorHandlerTest.java | 122 +++++++++++++++++++
.../camel/dataformat/beanio/MyErrorDto.java | 6 +-
.../camel/dataformat/beanio/MyErrorHandler.java | 9 +-
8 files changed, 170 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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 7e59042..c01350b 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, results);
+ BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, results, null);
in.setErrorHandler(errorHandler);
try {
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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 2baf8fc..eadbba7 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
@@ -40,6 +40,7 @@ public class BeanIOErrorHandler extends BeanReaderErrorHandlerSupport {
private BeanIOConfiguration configuration;
private Exchange exchange;
private List<Object> results;
+ private BeanIOIterator iterator;
public BeanIOErrorHandler() {
}
@@ -70,17 +71,23 @@ 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;
+ void setResults(List<Object> results) {
+ this.results = results;
}
- public void setResults(List<Object> results) {
- this.results = results;
+ void setIterator(BeanIOIterator iterator) {
+ this.iterator = iterator;
+ }
+
+ /**
+ * Sets a custom POJO as the result from handling an beanio error.
+ */
+ public void handleErrorAndAddAsResult(Object result) {
+ if (results != null) {
+ results.add(result);
+ } else if (iterator != null) {
+ iterator.setNext(result);
+ }
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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 7c15c02..541272a 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
@@ -31,8 +31,8 @@ public final class BeanIOHelper {
// utility class
}
- public static BeanReaderErrorHandler getOrCreateBeanReaderErrorHandler(BeanIOConfiguration configuration,
- Exchange exchange, List<Object> results) throws Exception {
+ public static BeanReaderErrorHandler getOrCreateBeanReaderErrorHandler(BeanIOConfiguration configuration, Exchange exchange,
+ List<Object> results, BeanIOIterator iterator) throws Exception {
BeanReaderErrorHandler answer = null;
if (ObjectHelper.isNotEmpty(configuration.getBeanReaderErrorHandlerType())) {
@@ -53,6 +53,7 @@ public final class BeanIOHelper {
eh.setConfiguration(configuration);
eh.setExchange(exchange);
eh.setResults(results);
+ eh.setIterator(iterator);
eh.init();
}
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOIterator.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOIterator.java b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOIterator.java
index 6a9c5c7..cd3b383 100644
--- a/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOIterator.java
+++ b/components/camel-beanio/src/main/java/org/apache/camel/dataformat/beanio/BeanIOIterator.java
@@ -26,6 +26,7 @@ public class BeanIOIterator implements Iterator<Object>, Closeable {
private BeanReader reader;
private transient Object next;
+ private transient Object forceNext;
public BeanIOIterator(BeanReader reader) {
this.reader = reader;
@@ -50,8 +51,18 @@ public class BeanIOIterator implements Iterator<Object>, Closeable {
Object answer = next;
if (answer == null) {
answer = reader.read();
+ // after read we may force a next
+ if (forceNext != null) {
+ answer = forceNext;
+ forceNext = null;
+ }
} else {
next = reader.read();
+ // after read we may force a next
+ if (forceNext != null) {
+ next = forceNext;
+ forceNext = null;
+ }
}
return answer;
}
@@ -60,4 +71,11 @@ public class BeanIOIterator implements Iterator<Object>, Closeable {
public void remove() {
// noop
}
+
+ /**
+ * Sets a custom object as the next, such as from a custom error handler
+ */
+ public void setNext(Object next) {
+ this.forceNext = next;
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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 9b5a0b6..d9f33f5 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,10 +98,12 @@ public class BeanIOSplitter implements Expression {
beanReader = factory.createReader(getStreamName(), reader);
}
- BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, null);
+ BeanIOIterator iterator = new BeanIOIterator(beanReader);
+
+ BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, null, iterator);
beanReader.setErrorHandler(errorHandler);
- return new BeanIOIterator(beanReader);
+ return iterator;
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIOSplitterCustomBeanReaderErrorHandlerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIOSplitterCustomBeanReaderErrorHandlerTest.java b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIOSplitterCustomBeanReaderErrorHandlerTest.java
new file mode 100644
index 0000000..9eed374
--- /dev/null
+++ b/components/camel-beanio/src/test/java/org/apache/camel/dataformat/beanio/BeanIOSplitterCustomBeanReaderErrorHandlerTest.java
@@ -0,0 +1,122 @@
+/**
+ * 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.dataformat.beanio;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class BeanIOSplitterCustomBeanReaderErrorHandlerTest extends CamelTestSupport {
+
+ // START SNIPPET: e2
+ private static final String FIXED_DATA =
+ "Joe,Smith,Developer,75000,10012009" + LS
+ + "Jane,Doe,Architect,80000,01152008" + LS
+ + "Jon,Anderson,Manager,85000,03182007" + LS;
+ // END SNIPPET: e2
+
+ private static final String FIXED_FAIL_DATA =
+ "Joe,Smith,Developer,75000,10012009" + LS
+ + "Jane,Doe,Architect,80000,01152008" + LS
+ + "Jon,Anderson,Manager,XXX,03182007" + LS;
+
+ @Test
+ public void testSplit() throws Exception {
+ List<Employee> employees = getEmployees();
+
+ MockEndpoint mock = getMockEndpoint("mock:beanio-unmarshal");
+ mock.expectedBodiesReceived(employees);
+
+ template.sendBody("direct:unmarshal", FIXED_DATA);
+
+ mock.assertIsSatisfied();
+ }
+
+ @Test
+ public void testSplitFail() throws Exception {
+ // there should be 1 splitted that failed we get also
+ MockEndpoint mock = getMockEndpoint("mock:beanio-unmarshal");
+ 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
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // START SNIPPET: e1
+ // setup beanio splitter using the mapping file, loaded from the classpath
+ BeanIOSplitter splitter = new BeanIOSplitter();
+ splitter.setMapping("org/apache/camel/dataformat/beanio/mappings.xml");
+ splitter.setStreamName("employeeFile");
+ splitter.setBeanReaderErrorHandlerType(MyErrorHandler.class);
+
+ // a route which uses the bean io data format to format a CSV data
+ // to java objects
+ from("direct:unmarshal")
+ // and then split the message body so we get a message for each row
+ .split(splitter).streaming()
+ .to("log:line")
+ .to("mock:beanio-unmarshal");
+ // END SNIPPET: e1
+ }
+ };
+ }
+
+ private List<Employee> getEmployees() throws ParseException {
+ List<Employee> employees = new ArrayList<Employee>();
+ Employee one = new Employee();
+ one.setFirstName("Joe");
+ one.setLastName("Smith");
+ one.setTitle("Developer");
+ one.setSalary(75000);
+ one.setHireDate(new SimpleDateFormat("MMddyyyy").parse("10012009"));
+ employees.add(one);
+
+ Employee two = new Employee();
+ two.setFirstName("Jane");
+ two.setLastName("Doe");
+ two.setTitle("Architect");
+ two.setSalary(80000);
+ two.setHireDate(new SimpleDateFormat("MMddyyyy").parse("01152008"));
+ employees.add(two);
+
+ Employee three = new Employee();
+ three.setFirstName("Jon");
+ three.setLastName("Anderson");
+ three.setTitle("Manager");
+ three.setSalary(85000);
+ three.setHireDate(new SimpleDateFormat("MMddyyyy").parse("03182007"));
+ employees.add(three);
+ return employees;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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
index 468f53f..743e542 100644
--- 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
@@ -5,9 +5,9 @@
* 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>
+ *
+ * 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.
http://git-wip-us.apache.org/repos/asf/camel/blob/be9ee349/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 8699203..a801593 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
@@ -27,14 +27,11 @@ public class MyErrorHandler extends BeanIOErrorHandler {
public void invalidRecord(InvalidRecordException ex) throws Exception {
String id = getExchange().getExchangeId();
String line = "ExchangeId: " + id + " Invalid record: " + ex.getMessage() + ": " + ex.getRecordContext().getRecordText();
+ LOG.warn(line);
- // lets store to the results a dummy error DTO
+ // lets handle the error and 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);
+ handleErrorAndAddAsResult(dto);
}
}