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