Search in sources :

Example 1 with ByteArrayDataSource

use of org.apache.soap.util.mime.ByteArrayDataSource in project iaf by ibissource.

the class MailSender method retrieveAttachments.

private Collection<Attachment> retrieveAttachments(Collection<Node> attachmentsNode, ParameterResolutionContext prc) throws SenderException {
    Collection<Attachment> attachments = null;
    Iterator iter = attachmentsNode.iterator();
    if (iter.hasNext()) {
        attachments = new LinkedList<Attachment>();
        while (iter.hasNext()) {
            Element attachmentElement = (Element) iter.next();
            String name = attachmentElement.getAttribute("name");
            String sessionKey = attachmentElement.getAttribute("sessionKey");
            String url = attachmentElement.getAttribute("url");
            boolean base64 = Boolean.parseBoolean(attachmentElement.getAttribute("base64"));
            Object value = null;
            if (StringUtils.isNotEmpty(sessionKey)) {
                Object object = prc.getSession().get(sessionKey);
                if (object instanceof InputStream) {
                    DataSource attachmentDataSource;
                    try {
                        attachmentDataSource = new ByteArrayDataSource((InputStream) object, "application/octet-stream");
                    } catch (IOException e) {
                        throw new SenderException("error retrieving attachment from sessionkey", e);
                    }
                    value = new DataHandler(attachmentDataSource);
                } else if (object instanceof String) {
                    String skValue = (String) object;
                    if (base64) {
                        value = decodeBase64(skValue);
                    } else {
                        value = skValue;
                    }
                } else {
                    throw new SenderException("MailSender [" + getName() + "] received unknown attachment type [" + object.getClass().getName() + "] in sessionkey");
                }
            } else {
                if (StringUtils.isNotEmpty(url)) {
                    DataSource attachmentDataSource;
                    try {
                        attachmentDataSource = new URLDataSource(new URL(url));
                    } catch (MalformedURLException e) {
                        throw new SenderException("error retrieving attachment from url", e);
                    }
                    value = new DataHandler(attachmentDataSource);
                } else {
                    String nodeValue = XmlUtils.getStringValue(attachmentElement);
                    if (base64) {
                        value = decodeBase64(nodeValue);
                    } else {
                        value = nodeValue;
                    }
                }
            }
            Attachment attachment = new Attachment(value, name);
            attachments.add(attachment);
        }
    }
    return attachments;
}
Also used : MalformedURLException(java.net.MalformedURLException) URLDataSource(javax.activation.URLDataSource) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) IOException(java.io.IOException) DataHandler(javax.activation.DataHandler) URL(java.net.URL) URLDataSource(javax.activation.URLDataSource) ByteArrayDataSource(org.apache.soap.util.mime.ByteArrayDataSource) DataSource(javax.activation.DataSource) Iterator(java.util.Iterator) SenderException(nl.nn.adapterframework.core.SenderException) ByteArrayDataSource(org.apache.soap.util.mime.ByteArrayDataSource)

Example 2 with ByteArrayDataSource

use of org.apache.soap.util.mime.ByteArrayDataSource in project iaf by ibissource.

the class MailSender method decodeBase64.

private DataHandler decodeBase64(String str) {
    byte[] bytesDecoded = Base64.decode(str);
    String encodingType = "application/octet-stream";
    DataSource ads = new ByteArrayDataSource(bytesDecoded, encodingType);
    return new DataHandler(ads);
}
Also used : DataHandler(javax.activation.DataHandler) ByteArrayDataSource(org.apache.soap.util.mime.ByteArrayDataSource) URLDataSource(javax.activation.URLDataSource) ByteArrayDataSource(org.apache.soap.util.mime.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Aggregations

DataHandler (javax.activation.DataHandler)2 DataSource (javax.activation.DataSource)2 URLDataSource (javax.activation.URLDataSource)2 ByteArrayDataSource (org.apache.soap.util.mime.ByteArrayDataSource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1 SenderException (nl.nn.adapterframework.core.SenderException)1 Element (org.w3c.dom.Element)1