Search in sources :

Example 1 with BeanReaderErrorHandler

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;
}
Also used : BeanReaderErrorHandler(org.beanio.BeanReaderErrorHandler)

Example 2 with BeanReaderErrorHandler

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;
}
Also used : Message(org.apache.camel.Message) BeanIOHelper.getOrCreateBeanReaderErrorHandler(org.apache.camel.dataformat.beanio.BeanIOHelper.getOrCreateBeanReaderErrorHandler) BeanReaderErrorHandler(org.beanio.BeanReaderErrorHandler) WrappedFile(org.apache.camel.WrappedFile) BeanReader(org.beanio.BeanReader) Reader(java.io.Reader) BeanReader(org.beanio.BeanReader) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile)

Example 3 with BeanReaderErrorHandler

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) BeanReaderErrorHandler(org.beanio.BeanReaderErrorHandler) BeanIOHelper.getOrCreateBeanReaderErrorHandler(org.apache.camel.dataformat.beanio.BeanIOHelper.getOrCreateBeanReaderErrorHandler) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) BeanReader(org.beanio.BeanReader)

Aggregations

BeanReaderErrorHandler (org.beanio.BeanReaderErrorHandler)3 BeanIOHelper.getOrCreateBeanReaderErrorHandler (org.apache.camel.dataformat.beanio.BeanIOHelper.getOrCreateBeanReaderErrorHandler)2 BeanReader (org.beanio.BeanReader)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 Message (org.apache.camel.Message)1 WrappedFile (org.apache.camel.WrappedFile)1