Search in sources :

Example 41 with ProcessException

use of org.apache.nifi.processor.exception.ProcessException in project nifi by apache.

the class AbstractEmailProcessor method buildUrl.

/**
 * Builds the url used to connect to the email server.
 */
String buildUrl(ProcessContext processContext) {
    String host = processContext.getProperty(HOST).evaluateAttributeExpressions().getValue();
    String port = processContext.getProperty(PORT).evaluateAttributeExpressions().getValue();
    String user = processContext.getProperty(USER).evaluateAttributeExpressions().getValue();
    String password = processContext.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
    String folder = processContext.getProperty(FOLDER).evaluateAttributeExpressions().getValue();
    StringBuilder urlBuilder = new StringBuilder();
    try {
        urlBuilder.append(URLEncoder.encode(user, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new ProcessException(e);
    }
    urlBuilder.append(":");
    try {
        urlBuilder.append(URLEncoder.encode(password, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new ProcessException(e);
    }
    urlBuilder.append("@");
    urlBuilder.append(host);
    urlBuilder.append(":");
    urlBuilder.append(port);
    urlBuilder.append("/");
    urlBuilder.append(folder);
    String protocol = this.getProtocol(processContext);
    String finalUrl = protocol + "://" + urlBuilder.toString();
    // build display-safe URL
    int passwordStartIndex = urlBuilder.indexOf(":") + 1;
    int passwordEndIndex = urlBuilder.indexOf("@");
    urlBuilder.replace(passwordStartIndex, passwordEndIndex, "[password]");
    this.displayUrl = protocol + "://" + urlBuilder.toString();
    if (this.logger.isInfoEnabled()) {
        this.logger.info("Connecting to Email server at the following URL: " + this.displayUrl);
    }
    return finalUrl;
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 42 with ProcessException

use of org.apache.nifi.processor.exception.ProcessException in project nifi by apache.

the class ConsumeEWS method initializeIfNecessary.

protected ExchangeService initializeIfNecessary(ProcessContext context) throws ProcessException {
    ExchangeVersion ver = ExchangeVersion.valueOf(context.getProperty(EXCHANGE_VERSION).getValue());
    ExchangeService service = new ExchangeService(ver);
    final String timeoutInMillis = String.valueOf(context.getProperty(CONNECTION_TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.MILLISECONDS));
    service.setTimeout(Integer.parseInt(timeoutInMillis));
    String userEmail = context.getProperty(USER).getValue();
    String password = context.getProperty(PASSWORD).getValue();
    ExchangeCredentials credentials = new WebCredentials(userEmail, password);
    service.setCredentials(credentials);
    Boolean useAutodiscover = context.getProperty(USE_AUTODISCOVER).asBoolean();
    if (useAutodiscover) {
        try {
            service.autodiscoverUrl(userEmail, new RedirectionUrlCallback());
        } catch (Exception e) {
            throw new ProcessException("Failure setting Autodiscover URL from email address.", e);
        }
    } else {
        String ewsURL = context.getProperty(EWS_URL).getValue();
        try {
            service.setUrl(new URI(ewsURL));
        } catch (URISyntaxException e) {
            throw new ProcessException("Failure setting EWS URL.", e);
        }
    }
    return service;
}
Also used : ExchangeService(microsoft.exchange.webservices.data.core.ExchangeService) ExchangeVersion(microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion) ProcessException(org.apache.nifi.processor.exception.ProcessException) ExchangeCredentials(microsoft.exchange.webservices.data.credential.ExchangeCredentials) URISyntaxException(java.net.URISyntaxException) WebCredentials(microsoft.exchange.webservices.data.credential.WebCredentials) URI(java.net.URI) 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 43 with ProcessException

use of org.apache.nifi.processor.exception.ProcessException 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 44 with ProcessException

use of org.apache.nifi.processor.exception.ProcessException 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 45 with ProcessException

use of org.apache.nifi.processor.exception.ProcessException in project nifi by apache.

the class ParseEvtx method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    ComponentLog logger = getLogger();
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    String basename = getBasename(flowFile, logger);
    String granularity = context.getProperty(GRANULARITY).getValue();
    if (FILE.equals(granularity)) {
        // File granularity will emit a FlowFile for each input
        FlowFile original = session.clone(flowFile);
        AtomicReference<Exception> exceptionReference = new AtomicReference<>(null);
        FlowFile updated = session.write(flowFile, (in, out) -> {
            processFileGranularity(session, logger, original, basename, exceptionReference, in, out);
        });
        session.transfer(original, REL_ORIGINAL);
        resultProcessor.process(session, logger, updated, exceptionReference.get(), getName(basename, null, null, XML_EXTENSION));
    } else {
        session.read(flowFile, in -> {
            if (RECORD.equals(granularity)) {
                // Record granularity will emit a FlowFile for every record (event)
                processRecordGranularity(session, logger, flowFile, basename, in);
            } else if (CHUNK.equals(granularity)) {
                // Chunk granularity will emit a FlowFile for each chunk of the file
                processChunkGranularity(session, logger, flowFile, basename, in);
            }
        });
        session.transfer(flowFile, REL_ORIGINAL);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AtomicReference(java.util.concurrent.atomic.AtomicReference) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedChunkException(org.apache.nifi.processors.evtx.parser.MalformedChunkException) IOException(java.io.IOException)

Aggregations

ProcessException (org.apache.nifi.processor.exception.ProcessException)274 FlowFile (org.apache.nifi.flowfile.FlowFile)169 IOException (java.io.IOException)162 InputStream (java.io.InputStream)79 HashMap (java.util.HashMap)78 ComponentLog (org.apache.nifi.logging.ComponentLog)78 OutputStream (java.io.OutputStream)62 ArrayList (java.util.ArrayList)55 Map (java.util.Map)52 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)39 InputStreamCallback (org.apache.nifi.processor.io.InputStreamCallback)38 StopWatch (org.apache.nifi.util.StopWatch)37 HashSet (java.util.HashSet)36 ProcessSession (org.apache.nifi.processor.ProcessSession)35 Relationship (org.apache.nifi.processor.Relationship)33 List (java.util.List)31 OutputStreamCallback (org.apache.nifi.processor.io.OutputStreamCallback)29 AtomicReference (java.util.concurrent.atomic.AtomicReference)28 Set (java.util.Set)26 ProcessContext (org.apache.nifi.processor.ProcessContext)25