Search in sources :

Example 1 with SearchFilter

use of microsoft.exchange.webservices.data.search.filter.SearchFilter in project nifi by apache.

the class ConsumeEWS method fillMessageQueueIfNecessary.

/**
 * Fills the internal message queue if such queue is empty. This is due to
 * the fact that per single session there may be multiple messages retrieved
 * from the email server (see FETCH_SIZE).
 */
protected void fillMessageQueueIfNecessary(ProcessContext context) throws ProcessException {
    if (this.messageQueue.isEmpty()) {
        ExchangeService service = this.initializeIfNecessary(context);
        boolean deleteOnRead = context.getProperty(SHOULD_DELETE_MESSAGES).getValue().equals("true");
        boolean markAsRead = context.getProperty(SHOULD_MARK_READ).getValue().equals("true");
        String includeHeaders = context.getProperty(INCLUDE_EMAIL_HEADERS).getValue();
        String excludeHeaders = context.getProperty(EXCLUDE_EMAIL_HEADERS).getValue();
        List<String> includeHeadersList = null;
        List<String> excludeHeadersList = null;
        if (!StringUtils.isEmpty(includeHeaders)) {
            includeHeadersList = Arrays.asList(includeHeaders.split(","));
        }
        if (!StringUtils.isEmpty(excludeHeaders)) {
            excludeHeadersList = Arrays.asList(excludeHeaders.split(","));
        }
        try {
            // Get Folder
            Folder folder = getFolder(service);
            ItemView view = new ItemView(messageQueue.remainingCapacity());
            view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
            SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
            FindItemsResults<Item> findResults = service.findItems(folder.getId(), sf, view);
            if (findResults == null || findResults.getItems().size() == 0) {
                return;
            }
            service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
            for (Item item : findResults) {
                EmailMessage ewsMessage = (EmailMessage) item;
                messageQueue.add(parseMessage(ewsMessage, includeHeadersList, excludeHeadersList));
                if (deleteOnRead) {
                    ewsMessage.delete(DeleteMode.HardDelete);
                } else if (markAsRead) {
                    ewsMessage.setIsRead(true);
                    ewsMessage.update(ConflictResolutionMode.AlwaysOverwrite);
                }
            }
            service.close();
        } catch (Exception e) {
            throw new ProcessException("Failed retrieving new messages from EWS.", e);
        }
    }
}
Also used : EmailMessage(microsoft.exchange.webservices.data.core.service.item.EmailMessage) SearchFilter(microsoft.exchange.webservices.data.search.filter.SearchFilter) Folder(microsoft.exchange.webservices.data.core.service.folder.Folder) 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) ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) Item(microsoft.exchange.webservices.data.core.service.item.Item) ItemView(microsoft.exchange.webservices.data.search.ItemView) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Example 2 with SearchFilter

use of microsoft.exchange.webservices.data.search.filter.SearchFilter in project nifi by apache.

the class ConsumeEWS method getFolder.

protected Folder getFolder(ExchangeService service) {
    Folder folder;
    if (folderName.equals("INBOX")) {
        try {
            folder = Folder.bind(service, WellKnownFolderName.Inbox);
        } catch (Exception e) {
            throw new ProcessException("Failed to bind Inbox Folder on EWS Server", e);
        }
    } else {
        FolderView view = new FolderView(10);
        view.setTraversal(FolderTraversal.Deep);
        SearchFilter searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, folderName);
        try {
            FindFoldersResults foldersResults = service.findFolders(WellKnownFolderName.Root, searchFilter, view);
            ArrayList<Folder> folderIds = foldersResults.getFolders();
            if (folderIds.size() > 1) {
                throw new ProcessException("More than 1 folder found with the name " + folderName);
            }
            folder = Folder.bind(service, folderIds.get(0).getId());
        } catch (Exception e) {
            throw new ProcessException("Search for Inbox Subfolder failed.", e);
        }
    }
    return folder;
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) FolderView(microsoft.exchange.webservices.data.search.FolderView) FindFoldersResults(microsoft.exchange.webservices.data.search.FindFoldersResults) SearchFilter(microsoft.exchange.webservices.data.search.filter.SearchFilter) Folder(microsoft.exchange.webservices.data.core.service.folder.Folder) 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)

Example 3 with SearchFilter

use of microsoft.exchange.webservices.data.search.filter.SearchFilter in project iaf by ibissource.

the class ExchangeMailListener method configure.

public void configure() throws ConfigurationException {
    try {
        exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUserName(), getPassword());
        ExchangeCredentials credentials = new WebCredentials(cf.getUsername(), cf.getPassword());
        exchangeService.setCredentials(credentials);
        if (StringUtils.isNotEmpty(getMailAddress())) {
            exchangeService.autodiscoverUrl(getMailAddress());
        } else {
            exchangeService.setUrl(new URI(getUrl()));
        }
        FolderId inboxId;
        if (StringUtils.isNotEmpty(getMailAddress())) {
            Mailbox mailbox = new Mailbox(getMailAddress());
            inboxId = new FolderId(WellKnownFolderName.Inbox, mailbox);
        } else {
            inboxId = new FolderId(WellKnownFolderName.Inbox);
        }
        FindFoldersResults findFoldersResultsIn;
        FolderView folderViewIn = new FolderView(10);
        if (StringUtils.isNotEmpty(getInputFolder())) {
            SearchFilter searchFilterIn = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, getInputFolder());
            findFoldersResultsIn = exchangeService.findFolders(inboxId, searchFilterIn, folderViewIn);
            if (findFoldersResultsIn.getTotalCount() == 0) {
                throw new ConfigurationException("no (in) folder found with name [" + getInputFolder() + "]");
            } else if (findFoldersResultsIn.getTotalCount() > 1) {
                throw new ConfigurationException("multiple (in) folders found with name [" + getInputFolder() + "]");
            }
        } else {
            findFoldersResultsIn = exchangeService.findFolders(inboxId, folderViewIn);
        }
        folderIn = findFoldersResultsIn.getFolders().get(0);
        if (StringUtils.isNotEmpty(getFilter())) {
            if (!getFilter().equalsIgnoreCase("NDR")) {
                throw new ConfigurationException("illegal value for filter [" + getFilter() + "], must be 'NDR' or empty");
            }
        }
        if (StringUtils.isNotEmpty(getOutputFolder())) {
            SearchFilter searchFilterOut = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, getOutputFolder());
            FolderView folderViewOut = new FolderView(10);
            FindFoldersResults findFoldersResultsOut = exchangeService.findFolders(inboxId, searchFilterOut, folderViewOut);
            if (findFoldersResultsOut.getTotalCount() == 0) {
                throw new ConfigurationException("no (out) folder found with name [" + getOutputFolder() + "]");
            } else if (findFoldersResultsOut.getTotalCount() > 1) {
                throw new ConfigurationException("multiple (out) folders found with name [" + getOutputFolder() + "]");
            }
            folderOut = findFoldersResultsOut.getFolders().get(0);
        }
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}
Also used : FolderView(microsoft.exchange.webservices.data.search.FolderView) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) FindFoldersResults(microsoft.exchange.webservices.data.search.FindFoldersResults) SearchFilter(microsoft.exchange.webservices.data.search.filter.SearchFilter) FolderId(microsoft.exchange.webservices.data.property.complex.FolderId) URI(java.net.URI) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) Mailbox(microsoft.exchange.webservices.data.property.complex.Mailbox) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ExchangeCredentials(microsoft.exchange.webservices.data.credential.ExchangeCredentials) WebCredentials(microsoft.exchange.webservices.data.credential.WebCredentials)

Example 4 with SearchFilter

use of microsoft.exchange.webservices.data.search.filter.SearchFilter in project iaf by ibissource.

the class ExchangeMailListener method getRawMessage.

public Object getRawMessage(Map threadContext) throws ListenerException {
    try {
        ItemView view = new ItemView(1);
        view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
        FindItemsResults<Item> findResults;
        if ("NDR".equalsIgnoreCase(getFilter())) {
            SearchFilter searchFilterBounce = new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "REPORT.IPM.Note.NDR");
            findResults = exchangeService.findItems(folderIn.getId(), searchFilterBounce, view);
        } else {
            findResults = exchangeService.findItems(folderIn.getId(), view);
        }
        if (findResults.getTotalCount() == 0) {
            return null;
        } else {
            return findResults.getItems().get(0);
        }
    } catch (Exception e) {
        throw new ListenerException(e);
    }
}
Also used : Item(microsoft.exchange.webservices.data.core.service.item.Item) ItemView(microsoft.exchange.webservices.data.search.ItemView) ListenerException(nl.nn.adapterframework.core.ListenerException) SearchFilter(microsoft.exchange.webservices.data.search.filter.SearchFilter) ServiceLocalException(microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

SearchFilter (microsoft.exchange.webservices.data.search.filter.SearchFilter)4 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 MessagingException (javax.mail.MessagingException)2 ExchangeService (microsoft.exchange.webservices.data.core.ExchangeService)2 ServiceLocalException (microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException)2 Folder (microsoft.exchange.webservices.data.core.service.folder.Folder)2 Item (microsoft.exchange.webservices.data.core.service.item.Item)2 FindFoldersResults (microsoft.exchange.webservices.data.search.FindFoldersResults)2 FolderView (microsoft.exchange.webservices.data.search.FolderView)2 ItemView (microsoft.exchange.webservices.data.search.ItemView)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 ListenerException (nl.nn.adapterframework.core.ListenerException)2 EmailException (org.apache.commons.mail.EmailException)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 URI (java.net.URI)1 EmailMessage (microsoft.exchange.webservices.data.core.service.item.EmailMessage)1 ExchangeCredentials (microsoft.exchange.webservices.data.credential.ExchangeCredentials)1 WebCredentials (microsoft.exchange.webservices.data.credential.WebCredentials)1 FolderId (microsoft.exchange.webservices.data.property.complex.FolderId)1