Search in sources :

Example 1 with XmlWriter

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

the class MailListener method extractMessage.

@Override
public Message extractMessage(M rawMessage, Map<String, Object> threadContext) throws ListenerException {
    if (MIME_MESSAGE_TYPE.equals(getMessageType())) {
        try {
            return getFileSystem().getMimeContent(rawMessage);
        } catch (FileSystemException e) {
            throw new ListenerException("cannot get MimeContents", e);
        }
    }
    if (!EMAIL_MESSAGE_TYPE.equals(getMessageType())) {
        return super.extractMessage(rawMessage, threadContext);
    }
    XmlWriter writer = new XmlWriter();
    try (SaxElementBuilder emailXml = new SaxElementBuilder("email", writer)) {
        if (isSimple()) {
            MailFileSystemUtils.addEmailInfoSimple(getFileSystem(), rawMessage, emailXml);
        } else {
            getFileSystem().extractEmail(rawMessage, emailXml);
        }
        if (StringUtils.isNotEmpty(getStoreEmailAsStreamInSessionKey())) {
            Message mimeContent = getFileSystem().getMimeContent(rawMessage);
            threadContext.put(getStoreEmailAsStreamInSessionKey(), mimeContent.asInputStream());
        }
    } catch (SAXException | IOException | FileSystemException e) {
        throw new ListenerException(e);
    }
    return new Message(writer.toString());
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) Message(nl.nn.adapterframework.stream.Message) SaxElementBuilder(nl.nn.adapterframework.xml.SaxElementBuilder) IOException(java.io.IOException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) SAXException(org.xml.sax.SAXException)

Example 2 with XmlWriter

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

the class MessageOutputStream method captureBinaryStream.

@SuppressWarnings("resource")
public void captureBinaryStream(OutputStream outputStream, int maxSize) {
    log.debug("creating capture of " + ClassUtils.nameOf(requestStream));
    closeOnClose(outputStream);
    if (requestStream instanceof OutputStream) {
        requestStream = StreamUtil.captureOutputStream((OutputStream) requestStream, outputStream, maxSize);
        return;
    }
    if (requestStream instanceof ContentHandler) {
        requestStream = new XmlTee((ContentHandler) requestStream, new PrettyPrintFilter(new XmlWriter(StreamUtil.limitSize(outputStream, maxSize))));
        return;
    }
    if (requestStream instanceof JsonEventHandler) {
        requestStream = new JsonTee((JsonEventHandler) requestStream, new JsonWriter(StreamUtil.limitSize(outputStream, maxSize)));
        return;
    }
    if (requestStream instanceof Writer) {
        requestStream = StreamUtil.captureWriter((Writer) requestStream, new OutputStreamWriter(outputStream, StreamUtil.DEFAULT_CHARSET), maxSize);
        return;
    }
    log.warn("captureBinaryStream() called before stream is installed.");
}
Also used : XmlTee(nl.nn.adapterframework.stream.xml.XmlTee) JsonTee(nl.nn.adapterframework.stream.json.JsonTee) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(nl.nn.adapterframework.stream.json.JsonWriter) ContentHandler(org.xml.sax.ContentHandler) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(nl.nn.adapterframework.stream.json.JsonWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Example 3 with XmlWriter

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

the class MessageOutputStream method asContentHandler.

public ContentHandler asContentHandler() throws StreamingException {
    if (requestStream instanceof ContentHandler) {
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "returning ContentHandler as ContentHandler");
        return (ContentHandler) requestStream;
    }
    if (requestStream instanceof JsonEventHandler) {
        throw new StreamingException("Cannot handle XML as JSON");
    }
    if (requestStream instanceof OutputStream) {
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "returning OutputStream as ContentHandler");
        XmlWriter xmlWriter = new XmlWriter((OutputStream) requestStream);
        xmlWriter.setIncludeXmlDeclaration(true);
        return xmlWriter;
    }
    if (requestStream instanceof Writer) {
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "returning Writer as ContentHandler");
        return new XmlWriter((Writer) requestStream);
    }
    return null;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) ContentHandler(org.xml.sax.ContentHandler) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(nl.nn.adapterframework.stream.json.JsonWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter)

Example 4 with XmlWriter

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

the class MessageOutputStream method captureCharacterStream.

@SuppressWarnings("resource")
public void captureCharacterStream(Writer writer, int maxSize) {
    log.debug("creating capture of " + ClassUtils.nameOf(requestStream));
    closeOnClose(writer);
    if (requestStream instanceof Writer) {
        requestStream = StreamUtil.captureWriter((Writer) requestStream, writer, maxSize);
        return;
    }
    if (requestStream instanceof ContentHandler) {
        requestStream = new XmlTee((ContentHandler) requestStream, new PrettyPrintFilter(new XmlWriter(StreamUtil.limitSize(writer, maxSize))));
        return;
    }
    if (requestStream instanceof JsonEventHandler) {
        requestStream = new JsonTee((JsonEventHandler) requestStream, new JsonWriter(StreamUtil.limitSize(writer, maxSize)));
        return;
    }
    if (requestStream instanceof OutputStream) {
        requestStream = StreamUtil.captureOutputStream((OutputStream) requestStream, new WriterOutputStream(writer, StreamUtil.DEFAULT_CHARSET), maxSize);
        return;
    }
    log.warn("captureCharacterStream() called before stream is installed.");
}
Also used : XmlTee(nl.nn.adapterframework.stream.xml.XmlTee) JsonTee(nl.nn.adapterframework.stream.json.JsonTee) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) JsonWriter(nl.nn.adapterframework.stream.json.JsonWriter) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(nl.nn.adapterframework.stream.json.JsonWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) ContentHandler(org.xml.sax.ContentHandler) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter)

Example 5 with XmlWriter

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

the class XmlBuilder method toXML.

public String toXML(boolean xmlHeader) {
    XmlWriter writer = new XmlWriter();
    PrettyPrintFilter ppf = new PrettyPrintFilter(writer);
    try {
        toXML(ppf);
    } catch (SAXException | IOException e) {
        log.warn("cannot write XML", e);
        return e.getMessage();
    }
    return writer.toString();
}
Also used : IOException(java.io.IOException) XmlWriter(nl.nn.adapterframework.xml.XmlWriter) PrettyPrintFilter(nl.nn.adapterframework.xml.PrettyPrintFilter) SAXException(org.xml.sax.SAXException)

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