Search in sources :

Example 1 with StreamingException

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

the class ChecksumPipe method provideOutputStream.

@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
    ChecksumGenerator cg;
    try {
        cg = createChecksumGenerator();
    } catch (NoSuchAlgorithmException e) {
        throw new StreamingException("Cannot create ChecksumGenerator", e);
    }
    OutputStream targetStream = new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            cg.update(b);
        }

        @Override
        public void write(byte[] buf, int offset, int length) throws IOException {
            cg.update(buf, offset, length);
        }
    };
    return new MessageOutputStream(this, targetStream, getNextPipe(), getCharset()) {

        @Override
        public Message getResponse() {
            return new Message(cg.getResult());
        }
    };
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) Message(nl.nn.adapterframework.stream.Message) OutputStream(java.io.OutputStream) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with StreamingException

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

the class XsltSender method createHandler.

protected ContentHandler createHandler(Message input, ThreadConnector threadConnector, PipeLineSession session, MessageOutputStream target) throws StreamingException {
    ContentHandler handler = null;
    try {
        ParameterValueList pvl = null;
        if (paramList != null) {
            pvl = paramList.getValues(input, session);
        }
        TransformerPool poolToUse = transformerPool;
        if (StringUtils.isNotEmpty(styleSheetNameSessionKey)) {
            Message styleSheetNameToUse = session.getMessage(styleSheetNameSessionKey);
            if (!Message.isEmpty(styleSheetNameToUse)) {
                String styleSheetNameFromSessionKey = styleSheetNameToUse.asString();
                if (!dynamicTransformerPoolMap.containsKey(styleSheetNameFromSessionKey)) {
                    dynamicTransformerPoolMap.put(styleSheetNameFromSessionKey, poolToUse = TransformerPool.configureTransformer(getLogPrefix(), this, null, null, styleSheetNameFromSessionKey, null, true, getParameterList()));
                    poolToUse.open();
                } else {
                    poolToUse = dynamicTransformerPoolMap.get(styleSheetNameFromSessionKey);
                }
            }
            if (poolToUse == null) {
                throw new SenderException("no XSLT stylesheet found from styleSheetNameSessionKey [" + styleSheetNameSessionKey + "], and neither one statically configured");
            }
        }
        OutputType outputType = getOutputType();
        if (log.isTraceEnabled())
            log.trace("Configured outputmethod [" + outputType + "]");
        if (outputType == null) {
            String parsedOutputType = poolToUse.getOutputMethod();
            if (StringUtils.isNotEmpty(parsedOutputType)) {
                outputType = EnumUtils.parse(OutputType.class, parsedOutputType);
            }
            if (log.isTraceEnabled())
                log.trace("Detected outputmethod [" + parsedOutputType + "]");
        }
        if (outputType == null) {
            outputType = DEFAULT_OUTPUT_METHOD;
            if (log.isTraceEnabled())
                log.trace("Default outputmethod [" + outputType + "]");
        }
        Object targetStream = target.asNative();
        Boolean indentXml = getIndentXml();
        if (log.isTraceEnabled())
            log.trace("Configured indentXml [" + indentXml + "]");
        if (indentXml == null) {
            indentXml = poolToUse.getIndent();
            if (log.isTraceEnabled())
                log.trace("Detected indentXml [" + indentXml + "]");
        }
        if (indentXml == null) {
            indentXml = DEFAULT_INDENT;
            if (log.isTraceEnabled())
                log.trace("Default indentXml [" + indentXml + "]");
        }
        Boolean omitXmlDeclaration = getOmitXmlDeclaration();
        if (targetStream instanceof ContentHandler) {
            handler = (ContentHandler) targetStream;
        } else {
            XmlWriter xmlWriter = new XmlWriter(target.asWriter());
            if (log.isTraceEnabled())
                log.trace("Configured omitXmlDeclaration [" + omitXmlDeclaration + "]");
            if (outputType == OutputType.XML) {
                if (omitXmlDeclaration == null) {
                    omitXmlDeclaration = poolToUse.getOmitXmlDeclaration();
                    if (log.isTraceEnabled())
                        log.trace("Detected omitXmlDeclaration [" + omitXmlDeclaration + "]");
                    if (omitXmlDeclaration == null) {
                        omitXmlDeclaration = DEFAULT_OMIT_XML_DECLARATION;
                        if (log.isTraceEnabled())
                            log.trace("Default omitXmlDeclaration [" + omitXmlDeclaration + "]");
                    }
                }
                xmlWriter.setIncludeXmlDeclaration(!omitXmlDeclaration);
                if (indentXml) {
                    xmlWriter.setNewlineAfterXmlDeclaration(true);
                }
            } else {
                xmlWriter.setTextMode(true);
            }
            handler = xmlWriter;
        }
        if (indentXml) {
            handler = new PrettyPrintFilter(handler);
        }
        if (isSkipEmptyTags()) {
            handler = new SkipEmptyTagsFilter(handler);
        }
        TransformerFilter mainFilter = poolToUse.getTransformerFilter(threadConnector, handler);
        if (pvl != null) {
            XmlUtils.setTransformerParameters(mainFilter.getTransformer(), pvl.getValueMap());
        }
        handler = filterInput(mainFilter, session);
        return handler;
    } catch (Exception e) {
        // log.warn(getLogPrefix()+"intermediate exception logging",e);
        throw new StreamingException(getLogPrefix() + "Exception on creating transformerHandler chain", e);
    }
}
Also used : TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) StreamingException(nl.nn.adapterframework.stream.StreamingException) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message) ContentHandler(org.xml.sax.ContentHandler) TransformerPool(nl.nn.adapterframework.util.TransformerPool) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) SkipEmptyTagsFilter(nl.nn.adapterframework.xml.SkipEmptyTagsFilter) SenderException(nl.nn.adapterframework.core.SenderException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) OutputType(nl.nn.adapterframework.util.TransformerPool.OutputType) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Example 3 with StreamingException

use of nl.nn.adapterframework.stream.StreamingException 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 4 with StreamingException

use of nl.nn.adapterframework.stream.StreamingException 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 5 with StreamingException

use of nl.nn.adapterframework.stream.StreamingException 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

StreamingException (nl.nn.adapterframework.stream.StreamingException)8 MessageOutputStream (nl.nn.adapterframework.stream.MessageOutputStream)5 IOException (java.io.IOException)4 ParameterException (nl.nn.adapterframework.core.ParameterException)4 SenderException (nl.nn.adapterframework.core.SenderException)4 Message (nl.nn.adapterframework.stream.Message)4 OutputStream (java.io.OutputStream)3 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)3 TimeoutException (nl.nn.adapterframework.core.TimeoutException)3 SAXException (org.xml.sax.SAXException)3 Writer (java.io.Writer)2 NamingException (javax.naming.NamingException)2 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)2 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)2 Base64OutputStream (org.apache.commons.codec.binary.Base64OutputStream)2 Document (org.bson.Document)2 DocumentCodec (org.bson.codecs.DocumentCodec)2 JsonWriterSettings (org.bson.json.JsonWriterSettings)2 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1