use of org.beanio.BeanReaderErrorHandler in project camel by apache.
the class BeanIOHelper method getOrCreateBeanReaderErrorHandler.
public static BeanReaderErrorHandler getOrCreateBeanReaderErrorHandler(BeanIOConfiguration configuration, Exchange exchange, List<Object> results, BeanIOIterator iterator) throws Exception {
BeanReaderErrorHandler answer = null;
if (ObjectHelper.isNotEmpty(configuration.getBeanReaderErrorHandlerType())) {
Class<?> clazz = exchange.getContext().getClassResolver().resolveMandatoryClass(configuration.getBeanReaderErrorHandlerType());
Object instance = exchange.getContext().getInjector().newInstance(clazz);
answer = (BeanReaderErrorHandler) instance;
}
if (answer == null && ObjectHelper.isNotEmpty(configuration.getBeanReaderErrorHandler())) {
answer = configuration.getBeanReaderErrorHandler();
}
if (answer == null) {
answer = new BeanIOErrorHandler();
}
// and then inject the current exchange and init
if (answer instanceof BeanIOErrorHandler) {
BeanIOErrorHandler eh = (BeanIOErrorHandler) answer;
eh.setConfiguration(configuration);
eh.setExchange(exchange);
eh.setResults(results);
eh.setIterator(iterator);
eh.init();
}
return answer;
}
use of org.beanio.BeanReaderErrorHandler in project camel by apache.
the class BeanIOSplitter method evaluate.
public Object evaluate(Exchange exchange) throws Exception {
Message msg = exchange.getIn();
Object body = msg.getBody();
if (factory == null) {
factory = createStreamFactory(exchange.getContext());
}
BeanReader beanReader = null;
if (body instanceof WrappedFile) {
body = ((WrappedFile) body).getFile();
}
if (body instanceof File) {
File file = (File) body;
beanReader = factory.createReader(getStreamName(), file);
}
if (beanReader == null) {
Reader reader = msg.getMandatoryBody(Reader.class);
beanReader = factory.createReader(getStreamName(), reader);
}
BeanIOIterator iterator = new BeanIOIterator(beanReader);
BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, null, iterator);
beanReader.setErrorHandler(errorHandler);
return iterator;
}
use of org.beanio.BeanReaderErrorHandler in project camel by apache.
the class BeanIODataFormat method readModels.
private List<Object> readModels(Exchange exchange, InputStream stream) throws Exception {
List<Object> results = new ArrayList<Object>();
BufferedReader streamReader = IOHelper.buffered(new InputStreamReader(stream, getEncoding()));
BeanReader in = factory.createReader(getStreamName(), streamReader);
BeanReaderErrorHandler errorHandler = getOrCreateBeanReaderErrorHandler(configuration, exchange, results, null);
in.setErrorHandler(errorHandler);
try {
Object readObject;
while ((readObject = in.read()) != null) {
if (readObject instanceof BeanIOHeader) {
exchange.getOut().getHeaders().putAll(((BeanIOHeader) readObject).getHeaders());
}
results.add(readObject);
}
} finally {
in.close();
}
return results;
}
Aggregations