Search in sources :

Example 1 with ThreadConnector

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

Example 2 with ThreadConnector

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

the class XsltExceptionTest method testXsltException.

public void testXsltException(boolean expectChildThreads, int tailCount) throws Exception {
    String xpathExpression = "*/*";
    int xsltVersion = 1;
    TransformerPool tp = TransformerPool.configureTransformer0(null, null, null, xpathExpression, null, OutputType.XML, false, null, xsltVersion);
    XmlWriter writer = new XmlWriter();
    FullXmlFilter filter = new FullXmlFilter(writer) {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
            if (localName.equals("error")) {
                throw new SaxException("Found error");
            }
            super.startElement(uri, localName, qName, atts);
        }
    };
    try (ThreadConnector threadConnector = expectChildThreads ? new ThreadConnector(null, null, null, (PipeLineSession) null) : null) {
        TransformerFilter transformer = tp.getTransformerFilter(threadConnector, filter);
        try {
            try (SaxDocumentBuilder seb = new SaxDocumentBuilder("root", transformer)) {
                seb.addElement("elem");
                seb.addElement("error");
                for (int i = 0; i < tailCount; i++) {
                    seb.addElement("elem");
                }
            }
            fail("Expected exception to be caught while processing");
        } catch (Exception e) {
            System.out.println("Expected exception: " + e.getMessage());
        }
        System.out.println(writer);
    }
}
Also used : FullXmlFilter(nl.nn.adapterframework.xml.FullXmlFilter) TransformerFilter(nl.nn.adapterframework.xml.TransformerFilter) Attributes(org.xml.sax.Attributes) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) SaxDocumentBuilder(nl.nn.adapterframework.xml.SaxDocumentBuilder) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) SaxException(nl.nn.adapterframework.xml.SaxException) TransformerPool(nl.nn.adapterframework.util.TransformerPool) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) SaxException(nl.nn.adapterframework.xml.SaxException) SAXException(org.xml.sax.SAXException)

Example 3 with ThreadConnector

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

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

the class ForEachChildElementPipe method iterateOverInput.

@Override
protected StopReason iterateOverInput(Message input, PipeLineSession session, Map<String, Object> threadContext, ItemCallback callback) throws SenderException, TimeoutException {
    InputSource src;
    if (isProcessFile()) {
        try {
            String filename;
            try {
                filename = input.asString();
            } catch (IOException e) {
                throw new SenderException(getLogPrefix(session) + "cannot find filename", e);
            }
            src = new InputSource(new FileInputStream(filename));
        } catch (FileNotFoundException e) {
            throw new SenderException("could not find file [" + input + "]", e);
        }
    } else {
        try {
            src = input.asInputSource();
        } catch (IOException e) {
            throw new SenderException("could not get InputSource", e);
        }
    }
    HandlerRecord handlerRecord = new HandlerRecord();
    try (ThreadConnector threadConnector = streamingXslt ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null) {
        try {
            createHandler(handlerRecord, threadConnector, session, callback);
        } catch (TransformerException e) {
            throw new SenderException(handlerRecord.errorMessage, e);
        }
        try {
            XmlUtils.parseXml(src, handlerRecord.inputHandler);
        } catch (Exception e) {
            try {
                if (e instanceof SaxTimeoutException) {
                    if (e.getCause() != null && e.getCause() instanceof TimeoutException) {
                        throw (TimeoutException) e.getCause();
                    }
                    throw new TimeoutException(e);
                }
                if (!(e instanceof SaxAbortException)) {
                    throw new SenderException(e);
                }
            } finally {
                try {
                    handlerRecord.inputHandler.endDocument();
                } catch (Exception e2) {
                    log.warn("Exception in endDocument()", e2);
                }
            }
        }
    } catch (IOException e) {
        throw new SenderException(e);
    }
    return handlerRecord.itemHandler.stopReason;
// 2020-06-12 removing below 'rethrowTransformerException()', as it does not break the tests, and cannot be implemented when providing an OutputStream.
// However, if cases popup of errors not being signaled, this modification could be the cause.
// rethrowTransformerException(handlerRecord.transformerErrorListener, handlerRecord.errorMessage);
}
Also used : InputSource(org.xml.sax.InputSource) FileNotFoundException(java.io.FileNotFoundException) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) IOException(java.io.IOException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) SaxException(nl.nn.adapterframework.xml.SaxException) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) SenderException(nl.nn.adapterframework.core.SenderException) SaxAbortException(nl.nn.adapterframework.stream.SaxAbortException) SenderException(nl.nn.adapterframework.core.SenderException) TransformerException(javax.xml.transform.TransformerException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SaxTimeoutException(nl.nn.adapterframework.stream.SaxTimeoutException)

Example 5 with ThreadConnector

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

the class ForEachChildElementPipe method provideOutputStream.

@Override
protected MessageOutputStream provideOutputStream(PipeLineSession session) throws StreamingException {
    HandlerRecord handlerRecord = new HandlerRecord();
    try {
        ThreadConnector threadConnector = streamingXslt ? new ThreadConnector(this, threadLifeCycleEventListener, txManager, session) : null;
        MessageOutputStream target = getTargetStream(session);
        Writer resultWriter = target.asWriter();
        ItemCallback callback = createItemCallBack(session, getSender(), resultWriter);
        createHandler(handlerRecord, threadConnector, session, callback);
        return new MessageOutputStream(this, handlerRecord.inputHandler, target, threadLifeCycleEventListener, txManager, session, threadConnector);
    } catch (TransformerException e) {
        throw new StreamingException(handlerRecord.errorMessage, e);
    }
}
Also used : StreamingException(nl.nn.adapterframework.stream.StreamingException) MessageOutputStream(nl.nn.adapterframework.stream.MessageOutputStream) ThreadConnector(nl.nn.adapterframework.stream.ThreadConnector) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) TransformerException(javax.xml.transform.TransformerException)

Aggregations

ThreadConnector (nl.nn.adapterframework.stream.ThreadConnector)6 MessageOutputStream (nl.nn.adapterframework.stream.MessageOutputStream)4 StreamingException (nl.nn.adapterframework.stream.StreamingException)3 ContentHandler (org.xml.sax.ContentHandler)3 SAXException (org.xml.sax.SAXException)3 TransformerException (javax.xml.transform.TransformerException)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 SenderException (nl.nn.adapterframework.core.SenderException)2 SaxException (nl.nn.adapterframework.xml.SaxException)2 XmlWriter (nl.nn.adapterframework.xml.XmlWriter)2 InputSource (org.xml.sax.InputSource)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)1 PipeStartException (nl.nn.adapterframework.core.PipeStartException)1 TimeoutException (nl.nn.adapterframework.core.TimeoutException)1