Search in sources :

Example 1 with MessageOutputStream

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

the class MongoDbSender method sendMessage.

@Override
public PipeRunResult sendMessage(Message message, PipeLineSession session, IForwardTarget next) throws SenderException, TimeoutException {
    message.closeOnCloseOf(session, this);
    MongoAction mngaction = getAction();
    try (MessageOutputStream target = mngaction == MongoAction.FINDONE || mngaction == MongoAction.FINDMANY ? MessageOutputStream.getTargetStream(this, session, next) : new MessageOutputStreamCap(this, next)) {
        ParameterValueList pvl = ParameterValueList.get(getParameterList(), message, session);
        MongoDatabase mongoDatabase = getDatabase(pvl);
        MongoCollection<Document> mongoCollection = getCollection(mongoDatabase, pvl);
        switch(mngaction) {
            case INSERTONE:
                renderResult(mongoCollection.insertOne(getDocument(message)), target);
                break;
            case INSERTMANY:
                renderResult(mongoCollection.insertMany(getDocuments(message)), target);
                break;
            case FINDONE:
                renderResult(mongoCollection.find(getFilter(pvl, message)).first(), target);
                break;
            case FINDMANY:
                renderResult(mongoCollection.find(getFilter(pvl, message)).limit(getLimit(pvl)), target);
                break;
            case UPDATEONE:
                renderResult(mongoCollection.updateOne(getFilter(pvl, null), getDocument(message)), target);
                break;
            case UPDATEMANY:
                renderResult(mongoCollection.updateMany(getFilter(pvl, null), getDocument(message)), target);
                break;
            case DELETEONE:
                renderResult(mongoCollection.deleteOne(getFilter(pvl, message)), target);
                break;
            case DELETEMANY:
                renderResult(mongoCollection.deleteMany(getFilter(pvl, message)), target);
                break;
            default:
                throw new SenderException("Unknown action [" + getAction() + "]");
        }
        return target.getPipeRunResult();
    } catch (Exception e) {
        throw new SenderException("Cannot execute action [" + getAction() + "]", e);
    }
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) MessageOutputStreamCap(nl.nn.adapterframework.stream.MessageOutputStreamCap) Document(org.bson.Document) SenderException(nl.nn.adapterframework.core.SenderException) 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) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 2 with MessageOutputStream

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

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

the class CompressPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    try {
        Object result;
        InputStream in = null;
        boolean zipMultipleFiles = false;
        String filename = null;
        if (messageIsContent) {
            in = message.asInputStream();
        } else {
            filename = message.asString();
            if (compress) {
                zipMultipleFiles = StringUtils.contains(filename, ";");
            } else {
                in = new FileInputStream(filename);
            }
        }
        if (resultIsContent) {
            try (MessageOutputStream target = getTargetStream(session)) {
                try (OutputStream stream = convert2String ? new WriterOutputStream(target.asWriter(), StreamUtil.DEFAULT_CHARSET) : target.asStream()) {
                    processStream(stream, in, zipMultipleFiles, filename, session);
                }
                return target.getPipeRunResult();
            }
        }
        String outFilename = null;
        if (messageIsContent) {
            outFilename = FileUtils.getFilename(getParameterList(), session, (File) null, filenamePattern);
        } else {
            outFilename = FileUtils.getFilename(getParameterList(), session, new File(filename), filenamePattern);
        }
        File outFile = new File(outputDirectory, outFilename);
        result = outFile.getAbsolutePath();
        try (OutputStream stream = new FileOutputStream(outFile)) {
            processStream(stream, in, zipMultipleFiles, filename, session);
        }
        return new PipeRunResult(getSuccessForward(), result);
    } catch (Exception e) {
        PipeForward exceptionForward = findForward(PipeForward.EXCEPTION_FORWARD_NAME);
        if (exceptionForward != null) {
            log.warn(getLogPrefix(session) + "exception occured, forwarded to [" + exceptionForward.getPath() + "]", e);
            return new PipeRunResult(exceptionForward, new ErrorMessageFormatter().format(getLogPrefix(session), e, this, message, session.getMessageId(), 0));
        }
        throw new PipeRunException(this, getLogPrefix(session) + "Unexpected exception during compression", e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) PipeForward(nl.nn.adapterframework.core.PipeForward) FileInputStream(java.io.FileInputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ErrorMessageFormatter(nl.nn.adapterframework.errormessageformatters.ErrorMessageFormatter) FileOutputStream(java.io.FileOutputStream) PipeRunException(nl.nn.adapterframework.core.PipeRunException) File(java.io.File)

Example 4 with MessageOutputStream

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

the class JsonXsltSender method createHandler.

@Override
protected ContentHandler createHandler(Message input, ThreadConnector threadConnector, PipeLineSession session, MessageOutputStream target) throws StreamingException {
    if (!isJsonResult()) {
        return super.createHandler(input, threadConnector, session, target);
    }
    XmlJsonWriter xjw = new XmlJsonWriter(target.asWriter());
    MessageOutputStream prev = new MessageOutputStream(this, xjw, target, threadLifeCycleEventListener, txManager, session, null);
    ContentHandler handler = super.createHandler(input, threadConnector, session, prev);
    if (getXmlDebugger() != null) {
        handler = getXmlDebugger().inspectXml(session, "XML to be converted to JSON", handler);
    }
    return handler;
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) XmlJsonWriter(nl.nn.adapterframework.util.XmlJsonWriter) ContentHandler(org.xml.sax.ContentHandler)

Example 5 with MessageOutputStream

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

the class XsltSender method sendMessage.

/*
	 * alternative implementation of send message, that should do the same as the original, but reuses the streaming content handler
	 */
@Override
public PipeRunResult sendMessage(Message message, PipeLineSession session, IForwardTarget next) throws SenderException {
    if (message == null) {
        throw new SenderException(getLogPrefix() + "got null input");
    }
    try {
        try (ThreadConnector threadConnector = streamingXslt ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null) {
            try (MessageOutputStream target = MessageOutputStream.getTargetStream(this, session, next)) {
                ContentHandler handler = createHandler(message, threadConnector, session, target);
                if (isDebugInput() && log.isDebugEnabled()) {
                    handler = new XmlTap(handler) {

                        @Override
                        public void endDocument() throws SAXException {
                            super.endDocument();
                            log.debug(getLogPrefix() + " xml input [" + getWriter() + "]");
                        }
                    };
                }
                XMLReader reader = getXmlReader(session, handler);
                InputSource source = message.asInputSource();
                reader.parse(source);
                return target.getPipeRunResult();
            }
        }
    } catch (Exception e) {
        throw new SenderException(getLogPrefix() + "Exception on transforming input", e);
    }
}
Also used : XmlTap(nl.nn.adapterframework.stream.xml.XmlTap) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) InputSource(org.xml.sax.InputSource) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) SenderException(nl.nn.adapterframework.core.SenderException) ContentHandler(org.xml.sax.ContentHandler) XMLReader(org.xml.sax.XMLReader) 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) SAXException(org.xml.sax.SAXException)

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