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);
}
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations