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;
}
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);
}
Aggregations