Search in sources :

Example 31 with XmlWriter

use of nl.nn.adapterframework.xml.XmlWriter in project iaf by ibissource.

the class XmlParserTest method testParseXmlNoExternalEntityInjection.

@Test
public void testParseXmlNoExternalEntityInjection() throws IOException, SAXException {
    String input = getInputAsString("/XmlUtils/EntityResolution/in-file-entity.xml");
    String expected = TestFileUtils.getTestFile("/XmlUtils/EntityResolution/out-not-resolved.xml");
    XmlWriter writer = new XmlWriter();
    XmlUtils.parseXml(input, writer);
    MatchUtils.assertXmlEquals(expected, writer.toString());
}
Also used : XmlWriter(nl.nn.adapterframework.xml.XmlWriter) Test(org.junit.Test)

Example 32 with XmlWriter

use of nl.nn.adapterframework.xml.XmlWriter in project iaf by ibissource.

the class IbisDebuggerAdvice method inspectXml.

@Override
public ContentHandler inspectXml(PipeLineSession session, String label, ContentHandler contentHandler) {
    if (!isEnabled()) {
        return contentHandler;
    }
    String correlationId = session == null ? null : session.getMessageId();
    WriterPlaceHolder writerPlaceHolder = ibisDebugger.showValue(correlationId, label, new WriterPlaceHolder());
    if (writerPlaceHolder != null && writerPlaceHolder.getWriter() != null) {
        Writer writer = writerPlaceHolder.getWriter();
        session.scheduleCloseOnSessionExit(writer, "debugger for inspectXml labeled [" + label + "]");
        XmlWriter xmlWriter = new XmlWriter(StreamUtil.limitSize(writer, writerPlaceHolder.getSizeLimit()), true);
        contentHandler = new XmlTee(contentHandler, new PrettyPrintFilter(xmlWriter));
    }
    return contentHandler;
}
Also used : XmlTee(nl.nn.adapterframework.stream.xml.XmlTee) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Example 33 with XmlWriter

use of nl.nn.adapterframework.xml.XmlWriter in project iaf by ibissource.

the class PipeDescriptionProvider method getPipeDescription.

/**
 * Get a PipeDescription object for the specified pipe. The returned object
 * is cached.
 */
public PipeDescription getPipeDescription(PipeLine pipeLine, IPipe pipe) {
    PipeDescription pipeDescription;
    INamedObject pipeLineOwner = pipeLine.getOwner();
    String adapterName = pipeLineOwner == null ? "?" : pipeLineOwner.getName();
    String pipeName = pipe.getName();
    String checkpointName = null;
    String xpathExpression = null;
    if (pipeLine.getPipe(pipeName) == null) {
        if (PipeLine.INPUT_VALIDATOR_NAME.equals(pipeName)) {
            checkpointName = INPUT_VALIDATOR_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/inputValidator";
        } else if (PipeLine.OUTPUT_VALIDATOR_NAME.equals(pipeName)) {
            checkpointName = OUTPUT_VALIDATOR_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/outputValidator";
        } else if (PipeLine.INPUT_WRAPPER_NAME.equals(pipeName)) {
            checkpointName = INPUT_WRAPPER_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/inputWrapper";
        } else if (PipeLine.OUTPUT_WRAPPER_NAME.equals(pipeName)) {
            checkpointName = OUTPUT_WRAPPER_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/outputWrapper";
        } else if (pipeName.startsWith(MessageSendingPipe.INPUT_VALIDATOR_NAME_PREFIX) && pipeName.endsWith(MessageSendingPipe.INPUT_VALIDATOR_NAME_SUFFIX)) {
            checkpointName = INPUT_VALIDATOR_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.INPUT_VALIDATOR_NAME_PREFIX, MessageSendingPipe.INPUT_VALIDATOR_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + parentPipeName + "\"]/inputValidator";
        } else if (pipeName.startsWith(MessageSendingPipe.OUTPUT_VALIDATOR_NAME_PREFIX) && pipeName.endsWith(MessageSendingPipe.OUTPUT_VALIDATOR_NAME_SUFFIX)) {
            checkpointName = OUTPUT_VALIDATOR_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.OUTPUT_VALIDATOR_NAME_PREFIX, MessageSendingPipe.OUTPUT_VALIDATOR_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + parentPipeName + "\"]/outputValidator";
        } else if (pipeName.startsWith(MessageSendingPipe.INPUT_WRAPPER_NAME_PREFIX) && pipeName.endsWith(MessageSendingPipe.INPUT_WRAPPER_NAME_SUFFIX)) {
            checkpointName = INPUT_WRAPPER_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.INPUT_WRAPPER_NAME_PREFIX, MessageSendingPipe.INPUT_WRAPPER_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + parentPipeName + "\"]/inputWrapper";
        } else if (pipeName.startsWith(MessageSendingPipe.OUTPUT_WRAPPER_NAME_PREFIX) && pipeName.endsWith(MessageSendingPipe.OUTPUT_WRAPPER_NAME_SUFFIX)) {
            checkpointName = OUTPUT_WRAPPER_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.OUTPUT_WRAPPER_NAME_PREFIX, MessageSendingPipe.OUTPUT_WRAPPER_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + parentPipeName + "\"]/outputWrapper";
        }
    } else {
        xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + pipeName + "\"]";
    }
    synchronized (pipeDescriptionCaches) {
        // this is a WeakHashMap.
        if (!(pipeLine.getApplicationContext() instanceof Configuration)) {
            pipeDescription = new PipeDescription();
            pipeDescription.setCheckpointName(getCheckpointName(pipe, checkpointName));
            pipeDescription.setDescription("Unable to find pipe information, configuration not found.");
            return pipeDescription;
        }
        Configuration configuration = (Configuration) pipeLine.getApplicationContext();
        Map<String, PipeDescription> pipeDescriptionCache = pipeDescriptionCaches.get(configuration);
        if (pipeDescriptionCache == null) {
            pipeDescriptionCache = new HashMap<>();
            pipeDescriptionCaches.put(configuration, pipeDescriptionCache);
        }
        pipeDescription = pipeDescriptionCache.get(xpathExpression);
        if (pipeDescription == null) {
            pipeDescription = new PipeDescription();
            pipeDescription.setCheckpointName(getCheckpointName(pipe, checkpointName));
            if (xpathExpression == null) {
                pipeDescription.setDescription("Could not create xpath to extract pipe from configuration");
                pipeDescriptionCache.put(xpathExpression, pipeDescription);
            } else {
                Document document = documents.get(configuration);
                if (document == null) {
                    try {
                        String config = configuration.getLoadedConfiguration();
                        document = XmlUtils.buildDomDocument(config);
                        documents.put(configuration, document);
                    } catch (DomBuilderException e) {
                        pipeDescription = new PipeDescription();
                        pipeDescription.setCheckpointName(getCheckpointName(pipe, checkpointName));
                        pipeDescription.setDescription("Could not parse configuration: " + e.getMessage());
                        pipeDescriptionCache.put(xpathExpression, pipeDescription);
                    }
                }
                if (document != null) {
                    Node node = doXPath(document, xpathExpression);
                    if (node != null) {
                        XmlWriter xmlWriter = new XmlWriter();
                        ContentHandler handler = new PrettyPrintFilter(xmlWriter);
                        try {
                            String input = XmlUtils.nodeToString(node);
                            XmlUtils.parseXml(input, handler);
                            pipeDescription.setDescription(xmlWriter.toString());
                        } catch (IOException | TransformerException | SAXException e) {
                            pipeDescription.setDescription("Exception: " + e.getMessage());
                        }
                        addResourceNamesToPipeDescription(node, pipeDescription);
                    } else {
                        pipeDescription.setDescription("Pipe not found in configuration.");
                    }
                }
            }
            pipeDescriptionCache.put(xpathExpression, pipeDescription);
        }
    }
    return pipeDescription;
}
Also used : Configuration(nl.nn.adapterframework.configuration.Configuration) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) INamedObject(nl.nn.adapterframework.core.INamedObject) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) TransformerException(javax.xml.transform.TransformerException) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Aggregations

XmlWriter (nl.nn.adapterframework.xml.XmlWriter)33 Test (org.junit.Test)15 PrettyPrintFilter (nl.nn.adapterframework.xml.PrettyPrintFilter)11 ContentHandler (org.xml.sax.ContentHandler)11 SAXException (org.xml.sax.SAXException)9 Resource (nl.nn.adapterframework.core.Resource)7 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Writer (java.io.Writer)4 XmlTee (nl.nn.adapterframework.stream.xml.XmlTee)4 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 URL (java.net.URL)3 ValidatorHandler (javax.xml.validation.ValidatorHandler)3 ConfigurationDigester (nl.nn.adapterframework.configuration.ConfigurationDigester)3 Message (nl.nn.adapterframework.stream.Message)3 JsonWriter (nl.nn.adapterframework.stream.json.JsonWriter)3 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)3 Properties (java.util.Properties)2