Search in sources :

Example 1 with BeanReader

use of org.beanio.BeanReader 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 2 with BeanReader

use of org.beanio.BeanReader 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

BeanIOHelper.getOrCreateBeanReaderErrorHandler (org.apache.camel.dataformat.beanio.BeanIOHelper.getOrCreateBeanReaderErrorHandler)2 BeanReader (org.beanio.BeanReader)2 BeanReaderErrorHandler (org.beanio.BeanReaderErrorHandler)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