Search in sources :

Example 1 with FileAttachment

use of microsoft.exchange.webservices.data.property.complex.FileAttachment in project nifi by apache.

the class ConsumeEWS method parseMessage.

public MimeMessage parseMessage(EmailMessage item, List<String> hdrIncludeList, List<String> hdrExcludeList) throws Exception {
    EmailMessage ewsMessage = item;
    final String bodyText = ewsMessage.getBody().toString();
    MultiPartEmail mm;
    if (ewsMessage.getBody().getBodyType() == BodyType.HTML) {
        mm = new HtmlEmail().setHtmlMsg(bodyText);
    } else {
        mm = new MultiPartEmail();
        mm.setMsg(bodyText);
    }
    mm.setHostName("NiFi-EWS");
    // from
    mm.setFrom(ewsMessage.getFrom().getAddress());
    // to recipients
    ewsMessage.getToRecipients().forEach(x -> {
        try {
            mm.addTo(x.getAddress());
        } catch (EmailException e) {
            throw new ProcessException("Failed to add TO recipient.", e);
        }
    });
    // cc recipients
    ewsMessage.getCcRecipients().forEach(x -> {
        try {
            mm.addCc(x.getAddress());
        } catch (EmailException e) {
            throw new ProcessException("Failed to add CC recipient.", e);
        }
    });
    // subject
    mm.setSubject(ewsMessage.getSubject());
    // sent date
    mm.setSentDate(ewsMessage.getDateTimeSent());
    // add message headers
    ewsMessage.getInternetMessageHeaders().getItems().stream().filter(x -> (hdrIncludeList == null || hdrIncludeList.isEmpty() || hdrIncludeList.contains(x.getName())) && (hdrExcludeList == null || hdrExcludeList.isEmpty() || !hdrExcludeList.contains(x.getName()))).forEach(x -> mm.addHeader(x.getName(), x.getValue()));
    // Any attachments
    if (ewsMessage.getHasAttachments()) {
        ewsMessage.getAttachments().forEach(x -> {
            try {
                FileAttachment file = (FileAttachment) x;
                file.load();
                ByteArrayDataSource bds = new ByteArrayDataSource(file.getContent(), file.getContentType());
                mm.attach(bds, file.getName(), "", EmailAttachment.ATTACHMENT);
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    mm.buildMimeMessage();
    return mm.getMimeMessage();
}
Also used : OutputStreamCallback(org.apache.nifi.processor.io.OutputStreamCallback) StandardValidators(org.apache.nifi.processor.util.StandardValidators) Message(javax.mail.Message) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) FindItemsResults(microsoft.exchange.webservices.data.search.FindItemsResults) LoggerFactory(org.slf4j.LoggerFactory) FolderTraversal(microsoft.exchange.webservices.data.core.enumeration.search.FolderTraversal) MessagingException(javax.mail.MessagingException) StringUtils(org.apache.commons.lang3.StringUtils) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) EmailMessageSchema(microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema) FolderSchema(microsoft.exchange.webservices.data.core.service.schema.FolderSchema) PropertySet(microsoft.exchange.webservices.data.core.PropertySet) IAutodiscoverRedirectionUrl(microsoft.exchange.webservices.data.autodiscover.IAutodiscoverRedirectionUrl) URI(java.net.URI) BodyType(microsoft.exchange.webservices.data.core.enumeration.property.BodyType) FlowFile(org.apache.nifi.flowfile.FlowFile) ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) DeleteMode(microsoft.exchange.webservices.data.core.enumeration.service.DeleteMode) ExchangeVersion(microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) FolderView(microsoft.exchange.webservices.data.search.FolderView) ItemSchema(microsoft.exchange.webservices.data.core.service.schema.ItemSchema) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) SortDirection(microsoft.exchange.webservices.data.core.enumeration.search.SortDirection) InputRequirement(org.apache.nifi.annotation.behavior.InputRequirement) List(java.util.List) ConflictResolutionMode(microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionMode) EmailException(org.apache.commons.mail.EmailException) Tags(org.apache.nifi.annotation.documentation.Tags) FileAttachment(microsoft.exchange.webservices.data.property.complex.FileAttachment) ExchangeCredentials(microsoft.exchange.webservices.data.credential.ExchangeCredentials) Address(javax.mail.Address) SearchFilter(microsoft.exchange.webservices.data.search.filter.SearchFilter) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) EmailMessage(microsoft.exchange.webservices.data.core.service.item.EmailMessage) HtmlEmail(org.apache.commons.mail.HtmlEmail) Flags(javax.mail.Flags) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EmailAttachment(org.apache.commons.mail.EmailAttachment) Relationship(org.apache.nifi.processor.Relationship) LogicalOperator(microsoft.exchange.webservices.data.core.enumeration.search.LogicalOperator) OutputStream(java.io.OutputStream) WellKnownFolderName(microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName) Logger(org.slf4j.Logger) Validator(org.apache.nifi.components.Validator) ItemView(microsoft.exchange.webservices.data.search.ItemView) ProcessContext(org.apache.nifi.processor.ProcessContext) ProcessSession(org.apache.nifi.processor.ProcessSession) IOException(java.io.IOException) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) MimeMessage(javax.mail.internet.MimeMessage) TimeUnit(java.util.concurrent.TimeUnit) Item(microsoft.exchange.webservices.data.core.service.item.Item) WebCredentials(microsoft.exchange.webservices.data.credential.WebCredentials) FindFoldersResults(microsoft.exchange.webservices.data.search.FindFoldersResults) AbstractProcessor(org.apache.nifi.processor.AbstractProcessor) Folder(microsoft.exchange.webservices.data.core.service.folder.Folder) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) EmailMessage(microsoft.exchange.webservices.data.core.service.item.EmailMessage) ProcessException(org.apache.nifi.processor.exception.ProcessException) MessagingException(javax.mail.MessagingException) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) FileAttachment(microsoft.exchange.webservices.data.property.complex.FileAttachment) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException) EmailException(org.apache.commons.mail.EmailException) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 TimeUnit (java.util.concurrent.TimeUnit)1 Address (javax.mail.Address)1 Flags (javax.mail.Flags)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1 IAutodiscoverRedirectionUrl (microsoft.exchange.webservices.data.autodiscover.IAutodiscoverRedirectionUrl)1 ExchangeService (microsoft.exchange.webservices.data.core.ExchangeService)1