Search in sources :

Example 1 with BindyAbstractFactory

use of org.apache.camel.dataformat.bindy.BindyAbstractFactory in project camel by apache.

the class BindyKeyValuePairDataFormat method marshal.

@SuppressWarnings("unchecked")
public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
    final BindyAbstractFactory factory = getFactory();
    final byte[] crlf = ConverterUtils.getByteReturn(factory.getCarriageReturn());
    final TypeConverter converter = exchange.getContext().getTypeConverter();
    // the body may not be a prepared list of map that bindy expects so help
    // a bit here and create one if needed
    final Iterator<Object> it = ObjectHelper.createIterator(body);
    while (it.hasNext()) {
        Object model = it.next();
        Map<String, Object> row;
        if (model instanceof Map) {
            row = (Map<String, Object>) model;
        } else {
            row = Collections.singletonMap(model.getClass().getName(), model);
        }
        String result = factory.unbind(row);
        outputStream.write(converter.convertTo(byte[].class, exchange, result));
        outputStream.write(crlf);
    }
}
Also used : TypeConverter(org.apache.camel.TypeConverter) BindyAbstractFactory(org.apache.camel.dataformat.bindy.BindyAbstractFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 TypeConverter (org.apache.camel.TypeConverter)1 BindyAbstractFactory (org.apache.camel.dataformat.bindy.BindyAbstractFactory)1