Search in sources :

Example 11 with MessageOutputStream

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);
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) ContentHandler(org.xml.sax.ContentHandler)

Example 12 with MessageOutputStream

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());
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) Parameter(nl.nn.adapterframework.parameters.Parameter) PipeForward(nl.nn.adapterframework.core.PipeForward) Writer(java.io.Writer) Test(org.junit.Test)

Example 13 with MessageOutputStream

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);
    }
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) Message(nl.nn.adapterframework.stream.Message) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) OutputStream(java.io.OutputStream) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException)

Example 14 with MessageOutputStream

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);
        }
    });
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) Message(nl.nn.adapterframework.stream.Message) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

Example 15 with MessageOutputStream

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);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) DB2XMLWriter(nl.nn.adapterframework.util.DB2XMLWriter) Message(nl.nn.adapterframework.stream.Message) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ParseException(java.text.ParseException) StreamingException(nl.nn.adapterframework.stream.StreamingException) JMSException(javax.jms.JMSException) SenderException(nl.nn.adapterframework.core.SenderException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) ContentHandler(org.xml.sax.ContentHandler) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

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