Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanse nihms email content for logging #140

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.Part;
import org.apache.commons.lang3.StringUtils;

/**
* @author Russ Poetker ([email protected])
Expand All @@ -31,7 +32,7 @@ private MailUtil() {}

static String getHtmlText(Part part) throws MessagingException, IOException {
if (part.isMimeType("text/html")) {
return part.getContent().toString();
return cleanseContent(part.getContent().toString());
}

if (part.isMimeType("multipart/alternative")) {
Expand All @@ -40,7 +41,7 @@ static String getHtmlText(Part part) throws MessagingException, IOException {
for (int i = 0; i < count; i++) {
Part bodyPart = multipart.getBodyPart(i);
if (bodyPart.isMimeType("text/html")) {
return bodyPart.getContent().toString();
return cleanseContent(bodyPart.getContent().toString());
} else if (bodyPart.isMimeType("multipart/*")) {
return getHtmlText(bodyPart);
}
Expand All @@ -52,11 +53,15 @@ static String getHtmlText(Part part) throws MessagingException, IOException {
Part bodyPart = multipart.getBodyPart(i);
String content = getHtmlText(bodyPart);
if (Objects.nonNull(content)) {
return content;
return cleanseContent(content);
}
}
}

return null;
}

private static String cleanseContent(String content) {
return StringUtils.normalizeSpace(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public void handleReceivedMail(MimeMessage receivedMessage) {
}
LOG.warn("Email is from Nihms");
String content = getHtmlText(receivedMessage);
LOG.warn("Nihms Email content:" + content);
LOG.warn("Nihms Email content: {}", content);
if (Objects.isNull(content)) {
LOG.error("No HTML content found in nihms email: " + receivedMessage.getSubject());
return;
}
Elements messageElements = getMessageElements(content);
if (messageElements.isEmpty()) {
LOG.error("No messages found in nihms email: " + content);
LOG.error("No messages found in nihms email: {}", content);
return;
}
processMessages(messageElements);
Expand Down
Loading