use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class XsltSender 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 = streamingXslt ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null;
MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next);
ContentHandler handler = createHandler(null, threadConnector, session, target);
return new MessageOutputStream(this, handler, target, threadLifeCycleEventListener, txManager, session, threadConnector);
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class FileSystemPipeTest method fileSystemPipeUploadActionTestWithOutputStream.
@Test
public void fileSystemPipeUploadActionTestWithOutputStream() 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();
// session.put("uploadActionTarget", stream);
fileSystemPipe.addParameter(new Parameter("filename", filename));
fileSystemPipe.setAction(FileSystemAction.UPLOAD);
fileSystemPipe.configure();
fileSystemPipe.start();
// assertTrue(fileSystemPipe.canProvideOutputStream());
MessageOutputStream target = fileSystemPipe.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);
PipeForward forward = target.getForward();
assertNotNull(forward);
assertEquals("success", forward.getName());
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class FileSystemActor method provideOutputStream.
@SuppressWarnings("resource")
@Override
public MessageOutputStream provideOutputStream(PipeLineSession session, IForwardTarget next) throws StreamingException {
if (!canProvideOutputStream()) {
return null;
}
ParameterValueList pvl = null;
try {
if (parameterList != null) {
pvl = parameterList.getValues(null, session);
}
} catch (ParameterException e) {
throw new StreamingException("caught exception evaluating parameters", e);
}
try {
F file = getFile(null, pvl);
OutputStream out;
if (getAction() == FileSystemAction.APPEND) {
out = ((IWritableFileSystem<F>) fileSystem).appendFile(file);
} else {
out = ((IWritableFileSystem<F>) fileSystem).createFile(file);
}
MessageOutputStream stream = new MessageOutputStream(owner, out, next);
stream.setResponse(new Message(FileSystemUtils.getFileInfo(fileSystem, file).toXML()));
return stream;
} catch (FileSystemException | IOException e) {
throw new StreamingException("cannot obtain OutputStream", e);
}
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class JdbcQuerySenderBase method provideOutputStream.
@Override
public MessageOutputStream provideOutputStream(PipeLineSession session, IForwardTarget next) throws StreamingException {
if (!canProvideOutputStream()) {
return null;
}
return TransactionConnectorCoordinator.doInUnsuspendedTransationContext(() -> {
final Connection connection;
QueryExecutionContext queryExecutionContext;
try {
connection = getConnectionWithTimeout(getTimeout());
queryExecutionContext = getQueryExecutionContext(connection, null, session);
} catch (JdbcException | ParameterException | SQLException | SenderException | TimeoutException e) {
throw new StreamingException(getLogPrefix() + "cannot getQueryExecutionContext", e);
}
try {
PreparedStatement statement = queryExecutionContext.getStatement();
if (queryExecutionContext.getParameterList() != null) {
JdbcUtil.applyParameters(getDbmsSupport(), statement, queryExecutionContext.getParameterList().getValues(new Message(""), session));
}
if (queryExecutionContext.getQueryType() == QueryType.UPDATEBLOB) {
BlobOutputStream blobOutputStream = getBlobOutputStream(statement, blobColumn, isBlobsCompressed());
TransactionConnectorCoordinator.onEndChildThread(() -> {
blobOutputStream.close();
connection.close();
log.warn(getLogPrefix() + "warnings: " + blobOutputStream.getWarnings().toXML());
});
return new MessageOutputStream(this, blobOutputStream, next) {
// perform close() on MessageOutputStream.close(), necessary when no TransactionConnector available for onEndThread()
@Override
public void afterClose() throws SQLException {
if (!connection.isClosed()) {
connection.close();
}
log.warn(getLogPrefix() + "warnings: " + blobOutputStream.getWarnings().toXML());
}
};
}
if (queryExecutionContext.getQueryType() == QueryType.UPDATECLOB) {
ClobWriter clobWriter = getClobWriter(statement, getClobColumn());
TransactionConnectorCoordinator.onEndChildThread(() -> {
clobWriter.close();
connection.close();
log.warn(getLogPrefix() + "warnings: " + clobWriter.getWarnings().toXML());
});
return new MessageOutputStream(this, clobWriter, next) {
// perform close() on MessageOutputStream.close(), necessary when no TransactionConnector available for onEndThread()
@Override
public void afterClose() throws SQLException {
if (!connection.isClosed()) {
connection.close();
}
log.warn(getLogPrefix() + "warnings: " + clobWriter.getWarnings().toXML());
}
};
}
throw new IllegalArgumentException(getLogPrefix() + "illegal queryType [" + queryExecutionContext.getQueryType() + "], must be 'updateBlob' or 'updateClob'");
} catch (JdbcException | SQLException | IOException | ParameterException e) {
throw new StreamingException(getLogPrefix() + "cannot update CLOB or BLOB", e);
}
});
}
use of nl.nn.adapterframework.stream.MessageOutputStream in project iaf by ibissource.
the class JdbcQuerySenderBase method getResult.
protected PipeRunResult getResult(ResultSet resultset, Object blobSessionVar, Object clobSessionVar, HttpServletResponse response, String contentType, String contentDisposition, PipeLineSession session, IForwardTarget next) throws JdbcException, SQLException, IOException {
if (isScalar()) {
String result = null;
if (resultset.next()) {
// result = resultset.getString(1);
ResultSetMetaData rsmeta = resultset.getMetaData();
int numberOfColumns = rsmeta.getColumnCount();
if (numberOfColumns > 1) {
log.warn(getLogPrefix() + "has set scalar=true but the resultset contains [" + numberOfColumns + "] columns. Consider optimizing the query.");
}
if (getDbmsSupport().isBlobType(rsmeta, 1)) {
if (response != null) {
if (StringUtils.isNotEmpty(contentType)) {
response.setHeader("Content-Type", contentType);
}
if (StringUtils.isNotEmpty(contentDisposition)) {
response.setHeader("Content-Disposition", contentDisposition);
}
JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), response.getOutputStream(), isCloseOutputstreamOnExit());
return new PipeRunResult(null, Message.nullMessage());
}
if (blobSessionVar != null) {
JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), blobSessionVar, isCloseOutputstreamOnExit());
return new PipeRunResult(null, Message.nullMessage());
}
if (!isBlobSmartGet()) {
try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
if (StringUtils.isNotEmpty(getBlobCharset())) {
JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, getBlobCharset(), isBlobsCompressed(), getBlobBase64Direction(), target.asWriter(), isCloseOutputstreamOnExit());
} else {
JdbcUtil.streamBlob(getDbmsSupport(), resultset, 1, null, isBlobsCompressed(), getBlobBase64Direction(), target.asStream(), isCloseOutputstreamOnExit());
}
return target.getPipeRunResult();
} catch (Exception e) {
throw new JdbcException(e);
}
}
}
if (getDbmsSupport().isClobType(rsmeta, 1)) {
if (clobSessionVar != null) {
JdbcUtil.streamClob(getDbmsSupport(), resultset, 1, clobSessionVar, isCloseOutputstreamOnExit());
return new PipeRunResult(null, Message.nullMessage());
}
try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
JdbcUtil.streamClob(getDbmsSupport(), resultset, 1, target.asWriter(), isCloseOutputstreamOnExit());
return target.getPipeRunResult();
} catch (Exception e) {
throw new JdbcException(e);
}
}
result = JdbcUtil.getValue(getDbmsSupport(), resultset, 1, rsmeta, getBlobCharset(), isBlobsCompressed(), getNullValue(), isTrimSpaces(), isBlobSmartGet(), getBlobBase64Direction() == Direction.ENCODE);
if (resultset.wasNull()) {
if (isScalarExtended()) {
result = "[null]";
} else {
result = null;
}
} else {
if (result.length() == 0 && isScalarExtended()) {
result = "[empty]";
}
}
if (resultset.next()) {
log.warn(getLogPrefix() + "has set scalar=true but the query returned more than 1 row. Consider optimizing the query.");
}
} else if (isScalarExtended()) {
result = "[absent]";
}
return new PipeRunResult(null, new Message(result));
}
try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
// Create XML and give the maxlength as a parameter
DB2XMLWriter db2xml = new DB2XMLWriter();
db2xml.setNullValue(getNullValue());
db2xml.setTrimSpaces(isTrimSpaces());
if (StringUtils.isNotEmpty(getBlobCharset()))
db2xml.setBlobCharset(getBlobCharset());
db2xml.setDecompressBlobs(isBlobsCompressed());
db2xml.setGetBlobSmart(isBlobSmartGet());
ContentHandler handler = target.asContentHandler();
if (isPrettyPrint()) {
handler = new PrettyPrintFilter(handler);
}
db2xml.getXML(getDbmsSupport(), resultset, getMaxRows(), isIncludeFieldDefinition(), handler);
return target.getPipeRunResult();
} catch (Exception e) {
throw new JdbcException(e);
}
}
Aggregations