Search in sources :

Example 1 with DBMailAttachment

use of org.olat.core.util.mail.model.DBMailAttachment in project OpenOLAT by OpenOLAT.

the class MailManagerImpl method deleteMail.

private void deleteMail(DBMailLight mail, Identity identity, boolean forceRemoveRecipient) {
    boolean delete = true;
    List<DBMailRecipient> updates = new ArrayList<DBMailRecipient>();
    if (mail.getFrom() != null && mail.getFrom().getRecipient() != null) {
        if (identity.equalsByPersistableKey(mail.getFrom().getRecipient())) {
            DBMailRecipient from = mail.getFrom();
            from.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                from.setRecipient(null);
            }
            updates.add(from);
        }
        if (mail.getFrom().getDeleted() != null) {
            delete &= mail.getFrom().getDeleted().booleanValue();
        }
    }
    for (DBMailRecipient recipient : mail.getRecipients()) {
        if (recipient == null)
            continue;
        if (recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
            recipient.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                recipient.setRecipient(null);
            }
            updates.add(recipient);
        }
        if (recipient.getDeleted() != null) {
            delete &= recipient.getDeleted().booleanValue();
        }
    }
    if (delete) {
        Set<String> paths = new HashSet<String>();
        // all marked as deleted -> delete the mail
        List<DBMailAttachment> attachments = getAttachments(mail);
        for (DBMailAttachment attachment : attachments) {
            // reload from the hibernate session
            mail = attachment.getMail();
            dbInstance.deleteObject(attachment);
            if (StringHelper.containsNonWhitespace(attachment.getPath())) {
                paths.add(attachment.getPath());
            }
        }
        dbInstance.deleteObject(mail);
        // try to remove orphans file
        for (String path : paths) {
            int count = countAttachment(path);
            if (count == 0) {
                VFSItem item = mailModule.getRootForAttachments().resolve(path);
                if (item instanceof VFSLeaf) {
                    ((VFSLeaf) item).delete();
                }
            }
        }
    } else {
        for (DBMailRecipient update : updates) {
            dbInstance.updateObject(update);
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient) HashSet(java.util.HashSet)

Example 2 with DBMailAttachment

use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.

the class MailManagerImpl method deleteMail.

private void deleteMail(DBMailLight mail, Identity identity, boolean forceRemoveRecipient) {
    boolean delete = true;
    List<DBMailRecipient> updates = new ArrayList<DBMailRecipient>();
    if (mail.getFrom() != null && mail.getFrom().getRecipient() != null) {
        if (identity.equalsByPersistableKey(mail.getFrom().getRecipient())) {
            DBMailRecipient from = mail.getFrom();
            from.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                from.setRecipient(null);
            }
            updates.add(from);
        }
        if (mail.getFrom().getDeleted() != null) {
            delete &= mail.getFrom().getDeleted().booleanValue();
        }
    }
    for (DBMailRecipient recipient : mail.getRecipients()) {
        if (recipient == null)
            continue;
        if (recipient.getRecipient() != null && recipient.getRecipient().equalsByPersistableKey(identity)) {
            recipient.setDeleted(Boolean.TRUE);
            if (forceRemoveRecipient) {
                recipient.setRecipient(null);
            }
            updates.add(recipient);
        }
        if (recipient.getDeleted() != null) {
            delete &= recipient.getDeleted().booleanValue();
        }
    }
    if (delete) {
        Set<String> paths = new HashSet<String>();
        // all marked as deleted -> delete the mail
        List<DBMailAttachment> attachments = getAttachments(mail);
        for (DBMailAttachment attachment : attachments) {
            // reload from the hibernate session
            mail = attachment.getMail();
            dbInstance.deleteObject(attachment);
            if (StringHelper.containsNonWhitespace(attachment.getPath())) {
                paths.add(attachment.getPath());
            }
        }
        dbInstance.deleteObject(mail);
        // try to remove orphans file
        for (String path : paths) {
            int count = countAttachment(path);
            if (count == 0) {
                VFSItem item = mailModule.getRootForAttachments().resolve(path);
                if (item instanceof VFSLeaf) {
                    ((VFSLeaf) item).delete();
                }
            }
        }
    } else {
        for (DBMailRecipient update : updates) {
            dbInstance.updateObject(update);
        }
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) ArrayList(java.util.ArrayList) VFSItem(org.olat.core.util.vfs.VFSItem) DBMailRecipient(org.olat.core.util.mail.model.DBMailRecipient) HashSet(java.util.HashSet)

Example 3 with DBMailAttachment

use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.

the class MailManagerImpl method forwardToRealInbox.

@Override
public MailerResult forwardToRealInbox(Identity identity, DBMail mail, MailerResult result) {
    if (result == null) {
        result = new MailerResult();
    }
    List<DBMailAttachment> attachments = getAttachments(mail);
    Address to = createAddress(identity, result, true);
    MimeMessage message = createForwardMimeMessage(to, to, mail.getSubject(), mail.getBody(), attachments, result);
    if (message != null) {
        sendMessage(message, result);
    }
    return result;
}
Also used : DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) MailerResult(org.olat.core.util.mail.MailerResult) MimeMessage(javax.mail.internet.MimeMessage)

Example 4 with DBMailAttachment

use of org.olat.core.util.mail.model.DBMailAttachment in project openolat by klemens.

the class MailManagerImpl method createForwardMimeMessage.

private MimeMessage createForwardMimeMessage(Address from, Address to, String subject, String body, List<DBMailAttachment> attachments, MailerResult result) {
    try {
        MimeMessage msg = createMessage(subject, from);
        if (to != null) {
            msg.addRecipient(RecipientType.TO, to);
        }
        if (attachments != null && !attachments.isEmpty()) {
            // with attachment use multipart message
            Multipart multipart = new MimeMultipart("mixed");
            // 1) add body part
            if (StringHelper.isHtml(body)) {
                Multipart alternativePart = createMultipartAlternative(body);
                MimeBodyPart wrap = new MimeBodyPart();
                wrap.setContent(alternativePart);
                multipart.addBodyPart(wrap);
            } else {
                BodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setText(body);
                multipart.addBodyPart(messageBodyPart);
            }
            // 2) add attachments
            for (DBMailAttachment attachment : attachments) {
                // abort if attachment does not exist
                if (attachment == null || attachment.getSize() <= 0) {
                    result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
                    log.error("Tried to send mail wit attachment that does not exist::" + (attachment == null ? null : attachment.getName()), null);
                    return msg;
                }
                BodyPart messageBodyPart = new MimeBodyPart();
                VFSLeaf data = getAttachmentDatas(attachment);
                DataSource source = new VFSDataSource(attachment.getName(), attachment.getMimetype(), data);
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName(attachment.getName());
                multipart.addBodyPart(messageBodyPart);
            }
            // Put parts in message
            msg.setContent(multipart);
        } else {
            // without attachment everything is easy, just set as text
            if (StringHelper.isHtml(body)) {
                msg.setContent(createMultipartAlternative(body));
            } else {
                msg.setText(body, "utf-8");
            }
        }
        msg.setSentDate(new Date());
        msg.saveChanges();
        return msg;
    } catch (MessagingException | UnsupportedEncodingException e) {
        log.error("", e);
        return null;
    }
}
Also used : BodyPart(javax.mail.BodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DataHandler(javax.activation.DataHandler) Date(java.util.Date) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 5 with DBMailAttachment

use of org.olat.core.util.mail.model.DBMailAttachment in project OpenOLAT by OpenOLAT.

the class MailManagerImpl method createForwardMimeMessage.

private MimeMessage createForwardMimeMessage(Address from, Address to, String subject, String body, List<DBMailAttachment> attachments, MailerResult result) {
    try {
        MimeMessage msg = createMessage(subject, from);
        if (to != null) {
            msg.addRecipient(RecipientType.TO, to);
        }
        if (attachments != null && !attachments.isEmpty()) {
            // with attachment use multipart message
            Multipart multipart = new MimeMultipart("mixed");
            // 1) add body part
            if (StringHelper.isHtml(body)) {
                Multipart alternativePart = createMultipartAlternative(body);
                MimeBodyPart wrap = new MimeBodyPart();
                wrap.setContent(alternativePart);
                multipart.addBodyPart(wrap);
            } else {
                BodyPart messageBodyPart = new MimeBodyPart();
                messageBodyPart.setText(body);
                multipart.addBodyPart(messageBodyPart);
            }
            // 2) add attachments
            for (DBMailAttachment attachment : attachments) {
                // abort if attachment does not exist
                if (attachment == null || attachment.getSize() <= 0) {
                    result.setReturnCode(MailerResult.ATTACHMENT_INVALID);
                    log.error("Tried to send mail wit attachment that does not exist::" + (attachment == null ? null : attachment.getName()), null);
                    return msg;
                }
                BodyPart messageBodyPart = new MimeBodyPart();
                VFSLeaf data = getAttachmentDatas(attachment);
                DataSource source = new VFSDataSource(attachment.getName(), attachment.getMimetype(), data);
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName(attachment.getName());
                multipart.addBodyPart(messageBodyPart);
            }
            // Put parts in message
            msg.setContent(multipart);
        } else {
            // without attachment everything is easy, just set as text
            if (StringHelper.isHtml(body)) {
                msg.setContent(createMultipartAlternative(body));
            } else {
                msg.setText(body, "utf-8");
            }
        }
        msg.setSentDate(new Date());
        msg.saveChanges();
        return msg;
    } catch (MessagingException | UnsupportedEncodingException e) {
        log.error("", e);
        return null;
    }
}
Also used : BodyPart(javax.mail.BodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DataHandler(javax.activation.DataHandler) Date(java.util.Date) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) DBMailAttachment(org.olat.core.util.mail.model.DBMailAttachment) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

DBMailAttachment (org.olat.core.util.mail.model.DBMailAttachment)8 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Address (javax.mail.Address)4 InternetAddress (javax.mail.internet.InternetAddress)4 MimeMessage (javax.mail.internet.MimeMessage)4 MailerResult (org.olat.core.util.mail.MailerResult)4 DBMailRecipient (org.olat.core.util.mail.model.DBMailRecipient)4 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HashSet (java.util.HashSet)2 Adler32 (java.util.zip.Adler32)2 DataHandler (javax.activation.DataHandler)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 BodyPart (javax.mail.BodyPart)2