Search in sources :

Example 6 with MessageOutputStream

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

the class IbisDebuggerAdvice method debugProvideOutputStream.

/**
 * Provides advice for {@link IOutputStreamingSupport#provideOutputStream(PipeLineSession session, IForwardTarget next)}
 */
public MessageOutputStream debugProvideOutputStream(ProceedingJoinPoint proceedingJoinPoint, PipeLineSession session) throws Throwable {
    if (!isEnabled()) {
        return (MessageOutputStream) proceedingJoinPoint.proceed();
    }
    String correlationId = session == null ? null : session.getMessageId();
    if (log.isDebugEnabled())
        log.debug("debugProvideOutputStream thread id [" + Thread.currentThread().getId() + "] thread name [" + Thread.currentThread().getName() + "] correlationId [" + correlationId + "]");
    if (proceedingJoinPoint.getTarget() instanceof ISender) {
        ISender sender = (ISender) proceedingJoinPoint.getTarget();
        // Use WriterPlaceHolder to make the contents that is later written to the MessageOutputStream appear as input of the Sender
        WriterPlaceHolder writerPlaceHolder = ibisDebugger.senderInput(sender, correlationId, new WriterPlaceHolder());
        MessageOutputStream resultStream = (MessageOutputStream) proceedingJoinPoint.proceed();
        String resultMessage = handleMessageOutputStream(writerPlaceHolder, resultStream);
        ibisDebugger.senderOutput(sender, correlationId, resultMessage);
        return resultStream;
    }
    if (proceedingJoinPoint.getTarget() instanceof IPipe) {
        IPipe pipe = (IPipe) proceedingJoinPoint.getTarget();
        PipeLine pipeLine = pipe instanceof AbstractPipe ? ((AbstractPipe) pipe).getPipeLine() : new PipeLine();
        // Use WriterPlaceHolder to make the contents that is later written to the MessageOutputStream appear as input of the Pipe
        WriterPlaceHolder writerPlaceHolder = ibisDebugger.pipeInput(pipeLine, pipe, correlationId, new WriterPlaceHolder());
        MessageOutputStream resultStream = (MessageOutputStream) proceedingJoinPoint.proceed();
        String resultMessage = handleMessageOutputStream(writerPlaceHolder, resultStream);
        ibisDebugger.pipeOutput(pipeLine, pipe, correlationId, resultMessage);
        return resultStream;
    }
    log.warn("Could not identify outputstream provider [" + proceedingJoinPoint.getTarget().getClass().getName() + "] as pipe or sender");
    return (MessageOutputStream) proceedingJoinPoint.proceed();
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) AbstractPipe(nl.nn.adapterframework.pipes.AbstractPipe) ISender(nl.nn.adapterframework.core.ISender) PipeLine(nl.nn.adapterframework.core.PipeLine) IPipe(nl.nn.adapterframework.core.IPipe)

Example 7 with MessageOutputStream

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

the class Text2XmlPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    if (message.asObject() == null) {
        return new PipeRunResult(getSuccessForward(), new Message("<" + getXmlTag() + " nil=\"true\" />"));
    } else if (message.isEmpty() && isUseCdataSection()) {
        return new PipeRunResult(getSuccessForward(), new Message("<" + getXmlTag() + "><![CDATA[]]></" + getXmlTag() + ">"));
    }
    try (MessageOutputStream target = getTargetStream(session)) {
        ContentHandler handler = target.asContentHandler();
        try {
            handler.startDocument();
            handler.startElement("", getXmlTag(), getXmlTag(), new AttributesImpl());
            try (BufferedReader reader = new BufferedReader(message.asReader())) {
                String line;
                boolean lineWritten = false;
                while ((line = reader.readLine()) != null) {
                    if (lineWritten) {
                        handler.characters("\n".toCharArray(), 0, "\n".length());
                    }
                    if (isSplitLines()) {
                        handler.startElement("", SPLITTED_LINE_TAG, SPLITTED_LINE_TAG, new AttributesImpl());
                    }
                    if (isUseCdataSection()) {
                        ((LexicalHandler) handler).startCDATA();
                    }
                    line = isReplaceNonXmlChars() ? XmlUtils.encodeCdataString(line) : line;
                    handler.characters(line.toCharArray(), 0, line.length());
                    lineWritten = true;
                    if (isUseCdataSection()) {
                        ((LexicalHandler) handler).endCDATA();
                    }
                    if (isSplitLines()) {
                        handler.endElement("", SPLITTED_LINE_TAG, SPLITTED_LINE_TAG);
                    }
                }
            }
            handler.endElement("", getXmlTag(), getXmlTag());
        } finally {
            handler.endDocument();
        }
        return target.getPipeRunResult();
    } catch (Exception e) {
        throw new PipeRunException(this, "Unexpected exception during splitting", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) AttributesImpl(org.xml.sax.helpers.AttributesImpl) Message(nl.nn.adapterframework.stream.Message) LexicalHandler(org.xml.sax.ext.LexicalHandler) BufferedReader(java.io.BufferedReader) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ContentHandler(org.xml.sax.ContentHandler) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Example 8 with MessageOutputStream

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

the class TextSplitterPipe method doPipe.

@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
    try {
        String[] result = new String[100];
        int p, s, o = 0;
        String inputString = message.asString();
        if (softSplit) {
            for (p = 0; p < inputString.length() - maxBlockLength; ) {
                // find last space in msg part
                for (s = p + maxBlockLength >= inputString.length() ? inputString.length() - 1 : p + maxBlockLength; s >= p && !Character.isWhitespace(inputString.charAt(s)) && inputString.charAt(s) != '-'; s--) ;
                // now skip spaces
                for (; s >= p && Character.isWhitespace(inputString.charAt(s)); s--) ;
                // spaces found, soft break possible
                if (s >= p) {
                    result[o++] = inputString.substring(p, s + 1);
                    for (p = s + 1; p < inputString.length() && Character.isWhitespace(inputString.charAt(p)); p++) ;
                } else // no space found, soft-break not possible
                {
                    result[o++] = inputString.substring(p, p + maxBlockLength < inputString.length() ? p + maxBlockLength : inputString.length());
                    p += maxBlockLength;
                }
            }
            result[o++] = inputString.substring(p);
        } else {
            for (p = 0; p < inputString.length(); p += maxBlockLength) {
                if (p + maxBlockLength <= inputString.length()) {
                    result[o++] = inputString.substring(p, p + maxBlockLength);
                } else {
                    result[o++] = inputString.substring(p);
                }
            }
        }
        try (MessageOutputStream target = getTargetStream(session)) {
            try (SaxDocumentBuilder saxBuilder = new SaxDocumentBuilder("text", target.asContentHandler())) {
                for (int counter = 0; result[counter] != null; counter++) {
                    saxBuilder.addElement("block", result[counter]);
                }
            }
            return target.getPipeRunResult();
        }
    } catch (Exception e) {
        throw new PipeRunException(this, "Cannot create text blocks", e);
    }
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 9 with MessageOutputStream

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

the class MessageSendingPipe method provideOutputStream.

@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
    MessageOutputStream result = null;
    if (sender instanceof IOutputStreamingSupport) {
        // TODO insert output validator
        // TODO insert output wrapper
        IOutputStreamingSupport streamingSender = (IOutputStreamingSupport) sender;
        result = streamingSender.provideOutputStream(session, getNextPipe());
    // TODO insert input wrapper
    // TODO insert input validator
    }
    return result;
}
Also used : MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) IOutputStreamingSupport(nl.nn.adapterframework.stream.IOutputStreamingSupport)

Example 10 with MessageOutputStream

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

the class IteratingPipe method sendMessage.

@Override
protected PipeRunResult sendMessage(Message input, PipeLineSession session, ISender sender, Map<String, Object> threadContext) throws SenderException, TimeoutException, IOException {
    // sendResult has a messageID for async senders, the result for sync senders
    StopReason stopReason = null;
    try (MessageOutputStream target = getTargetStream(session)) {
        try (Writer resultWriter = target.asWriter()) {
            ItemCallback callback = createItemCallBack(session, sender, resultWriter);
            stopReason = iterateOverInput(input, session, threadContext, callback);
        }
        PipeRunResult prr = target.getPipeRunResult();
        if (stopReason != null) {
            PipeForward forward = getForwards().get(stopReason.getForwardName());
            if (forward != null) {
                prr.setPipeForward(forward);
            }
        }
        return prr;
    } catch (SenderException | TimeoutException | IOException e) {
        throw e;
    } catch (Exception e) {
        throw new SenderException(getLogPrefix(session) + "Exception on transforming input", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) PipeForward(nl.nn.adapterframework.core.PipeForward) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

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