use of microsoft.exchange.webservices.data.core.service.item.EmailMessage in project nifi by apache.
the class ConsumeEWS method flushRemainingMessages.
/**
* Will flush the remaining messages when this processor is stopped.
*/
protected void flushRemainingMessages(ProcessContext processContext) {
Message emailMessage;
try {
while ((emailMessage = this.messageQueue.poll(1, TimeUnit.MILLISECONDS)) != null) {
this.transfer(emailMessage, processContext, this.processSession);
this.processSession.commit();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
this.logger.debug("Current thread is interrupted");
}
}
use of microsoft.exchange.webservices.data.core.service.item.EmailMessage 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 microsoft.exchange.webservices.data.core.service.item.EmailMessage in project nifi by apache.
the class ConsumeEWS method onTrigger.
@Override
public void onTrigger(ProcessContext context, ProcessSession processSession) throws ProcessException {
if (this.messageQueue == null) {
int fetchSize = context.getProperty(FETCH_SIZE).evaluateAttributeExpressions().asInteger();
this.messageQueue = new ArrayBlockingQueue<>(fetchSize);
}
this.folderName = context.getProperty(FOLDER).getValue();
Message emailMessage = this.receiveMessage(context);
if (emailMessage != null) {
this.transfer(emailMessage, context, processSession);
} else {
// No new messages found, yield the processor
context.yield();
}
}
use of microsoft.exchange.webservices.data.core.service.item.EmailMessage in project nifi by apache.
the class ConsumeEWS method receiveMessage.
/**
* Receives message from the internal queue filling up the queue if
* necessary.
*/
protected Message receiveMessage(ProcessContext context) {
Message emailMessage = null;
try {
this.fillMessageQueueIfNecessary(context);
emailMessage = this.messageQueue.poll(1, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
context.yield();
this.logger.error("Failed retrieving messages from EWS.", e);
Thread.currentThread().interrupt();
this.logger.debug("Current thread is interrupted");
}
return emailMessage;
}
use of microsoft.exchange.webservices.data.core.service.item.EmailMessage 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();
}
Aggregations