Search in sources :

Example 1 with ArrayBuilder

use of nl.nn.adapterframework.stream.document.ArrayBuilder in project iaf by ibissource.

the class MongoDbSender method renderResult.

protected void renderResult(FindIterable<Document> findResults, MessageOutputStream target) throws StreamingException {
    try {
        if (isCountOnly()) {
            try (Writer writer = target.asWriter()) {
                int count = 0;
                for (Document doc : findResults) {
                    count++;
                }
                writer.write(Integer.toString(count));
            }
            return;
        }
        try (ArrayBuilder builder = DocumentBuilderFactory.startArrayDocument(getOutputFormat(), "FindManyResult", "item", target)) {
            JsonWriterSettings writerSettings = JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build();
            Encoder<Document> encoder = new DocumentCodec();
            for (Document doc : findResults) {
                try (INodeBuilder element = builder.addElement()) {
                    JsonDocumentWriter jsonWriter = new JsonDocumentWriter(element, writerSettings);
                    encoder.encode(jsonWriter, doc, EncoderContext.builder().build());
                }
            }
        }
    } catch (Exception e) {
        throw new StreamingException("Could not render collection", e);
    }
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) DocumentCodec(org.bson.codecs.DocumentCodec) JsonWriterSettings(org.bson.json.JsonWriterSettings) Document(org.bson.Document) ArrayBuilder(nl.nn.adapterframework.stream.document.ArrayBuilder) INodeBuilder(nl.nn.adapterframework.stream.document.INodeBuilder) Writer(java.io.Writer) NamingException(javax.naming.NamingException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) StreamingException(nl.nn.adapterframework.stream.StreamingException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException)

Aggregations

IOException (java.io.IOException)1 Writer (java.io.Writer)1 NamingException (javax.naming.NamingException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 ParameterException (nl.nn.adapterframework.core.ParameterException)1 SenderException (nl.nn.adapterframework.core.SenderException)1 TimeoutException (nl.nn.adapterframework.core.TimeoutException)1 StreamingException (nl.nn.adapterframework.stream.StreamingException)1 ArrayBuilder (nl.nn.adapterframework.stream.document.ArrayBuilder)1 INodeBuilder (nl.nn.adapterframework.stream.document.INodeBuilder)1 Document (org.bson.Document)1 DocumentCodec (org.bson.codecs.DocumentCodec)1 JsonWriterSettings (org.bson.json.JsonWriterSettings)1 SAXException (org.xml.sax.SAXException)1