Search in sources :

Example 16 with MessageOutputStream

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

the class CsvParserPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    try (MessageOutputStream target = getTargetStream(session)) {
        try (Reader reader = message.asReader()) {
            try (SaxDocumentBuilder document = new SaxDocumentBuilder("csv", target.asContentHandler())) {
                CSVParser csvParser = format.parse(reader);
                for (CSVRecord record : csvParser) {
                    try (SaxElementBuilder element = document.startElement("record")) {
                        for (Entry<String, String> entry : record.toMap().entrySet()) {
                            String key = entry.getKey();
                            if (getHeaderCase() != null) {
                                key = getHeaderCase() == HeaderCase.LOWERCASE ? key.toLowerCase() : key.toUpperCase();
                            }
                            element.addElement(key, entry.getValue());
                        }
                    } catch (SAXException e) {
                        throw new PipeRunException(this, "Exception caught at line [" + record.getRecordNumber() + "] pos [" + record.getCharacterPosition() + "]", e);
                    }
                }
            }
        }
        return target.getPipeRunResult();
    } catch (Exception e) {
        if (e instanceof PipeRunException) {
            throw (PipeRunException) e;
        }
        throw new PipeRunException(this, "Cannot parse CSV", e);
    }
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) CSVParser(org.apache.commons.csv.CSVParser) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Reader(java.io.Reader) CSVRecord(org.apache.commons.csv.CSVRecord) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 17 with MessageOutputStream

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

the class ForEachChildElementPipe method provideOutputStream.

@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
    HandlerRecord handlerRecord = new HandlerRecord();
    try {
        ThreadConnector threadConnector = streamingXslt ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null;
        MessageOutputStream target = getTargetStream(session);
        Writer resultWriter = target.asWriter();
        ItemCallback callback = createItemCallBack(session, getSender(), resultWriter);
        createHandler(handlerRecord, threadConnector, session, callback);
        return new MessageOutputStream(this, handlerRecord.inputHandler, target, threadLifeCycleEventListener, txManager, session, threadConnector);
    } catch (TransformerException e) {
        throw new StreamingException(handlerRecord.errorMessage, e);
    }
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) TransformerException(javax.xml.transform.TransformerException)

Example 18 with MessageOutputStream

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

the class Base64Pipe method provideOutputStream.

@SuppressWarnings("resource")
@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
    MessageOutputStream target = getTargetStream(session);
    // TRUE encode - FALSE decode
    boolean directionEncode = getDirection() == Direction.ENCODE;
    OutputStream targetStream;
    String outputCharset = directionEncode ? StandardCharsets.US_ASCII.name() : getCharset();
    if (getOutputTypeEnum() == OutputTypes.STRING) {
        targetStream = new WriterOutputStream(target.asWriter(), outputCharset != null ? outputCharset : StreamUtil.DEFAULT_INPUT_STREAM_ENCODING);
    } else {
        targetStream = target.asStream(outputCharset);
    }
    OutputStream base64 = new Base64OutputStream(targetStream, directionEncode, getLineLength(), lineSeparatorArray);
    if (directionEncode && StringUtils.isNotEmpty(getCharset())) {
        try {
            return new MessageOutputStream(this, new OutputStreamWriter(base64, getCharset()), target);
        } catch (UnsupportedEncodingException e) {
            throw new StreamingException("cannot open OutputStreamWriter", e);
        }
    }
    return new MessageOutputStream(this, base64, target);
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) OutputStream(java.io.OutputStream) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream)

Example 19 with MessageOutputStream

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

the class JsonXsltSender method provideOutputStream.

@Override
public MessageOutputStream provideOutputStream(PipeLineSession session, IForwardTarget next) throws StreamingException {
    if (!canProvideOutputStream()) {
        log.debug("sender [{}] cannot provide outputstream", () -> getName());
        return null;
    }
    ThreadConnector threadConnector = isStreamingXslt() ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null;
    MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next);
    ContentHandler handler = createHandler(null, threadConnector, session, target);
    JsonEventHandler jsonEventHandler = new JsonXslt3XmlHandler(handler);
    return new MessageOutputStream(this, jsonEventHandler, target, threadLifeCycleEventListener, txManager, session, threadConnector);
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) JsonEventHandler(nl.nn.adapterframework.stream.JsonEventHandler) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) ContentHandler(org.xml.sax.ContentHandler) JsonXslt3XmlHandler(nl.nn.adapterframework.stream.xml.JsonXslt3XmlHandler)

Example 20 with MessageOutputStream

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

the class FileSystemActorTest method fileSystemActorWriteActionTestWithOutputStream.

@Test
public void fileSystemActorWriteActionTestWithOutputStream() throws Exception {
    String filename = "uploadedwithOutputStream" + FILE1;
    String contents = "Some text content to test upload action\n";
    if (_fileExists(filename)) {
        _deleteFile(null, filename);
    }
    // InputStream stream = new ByteArrayInputStream(contents.getBytes("UTF-8"));
    PipeLineSession session = new PipeLineSession();
    ParameterList paramlist = new ParameterList();
    paramlist.add(new Parameter("filename", filename));
    paramlist.configure();
    actor.setAction(FileSystemAction.WRITE);
    actor.configure(fileSystem, paramlist, owner);
    actor.open();
    assertTrue(actor.canProvideOutputStream());
    MessageOutputStream target = actor.provideOutputStream(session, null);
    // stream the contents
    try (Writer writer = target.asWriter()) {
        writer.write(contents);
    }
    // verify the filename is properly returned
    String stringResult = target.getPipeRunResult().getResult().asString();
    TestAssertions.assertXpathValueEquals(filename, stringResult, "file/@name");
    // verify the file contents
    waitForActionToFinish();
    String actualContents = readFile(null, filename);
    assertEquals(contents, actualContents);
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) ParameterList(nl.nn.adapterframework.parameters.ParameterList) Parameter(nl.nn.adapterframework.parameters.Parameter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

MessageOutputStream (nl.nn.adapterframework.stream.MessageOutputStream)21 StreamingException (nl.nn.adapterframework.stream.StreamingException)9 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)7 ContentHandler (org.xml.sax.ContentHandler)6 IOException (java.io.IOException)5 Writer (java.io.Writer)5 ParameterException (nl.nn.adapterframework.core.ParameterException)5 SenderException (nl.nn.adapterframework.core.SenderException)5 Message (nl.nn.adapterframework.stream.Message)5 OutputStream (java.io.OutputStream)4 PipeRunException (nl.nn.adapterframework.core.PipeRunException)4 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)4 TimeoutException (nl.nn.adapterframework.core.TimeoutException)4 ThreadConnector (nl.nn.adapterframework.stream.ThreadConnector)4 SAXException (org.xml.sax.SAXException)4 PipeForward (nl.nn.adapterframework.core.PipeForward)3 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)3 Parameter (nl.nn.adapterframework.parameters.Parameter)3 Test (org.junit.Test)3 SQLException (java.sql.SQLException)2