From 3c8b31561978705e58573754f5e49b12d94b5dcd Mon Sep 17 00:00:00 2001 From: bbottema Date: Sun, 14 Apr 2019 11:50:24 +0200 Subject: [PATCH] #7: fix missing S/MIME details on the resulting Outlook message --- .../model/OutlookMessage.java | 120 ++++++++++++++---- .../model/OutlookMessageTest.java | 27 ++++ 2 files changed, 122 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/simplejavamail/outlookmessageparser/model/OutlookMessageTest.java diff --git a/src/main/java/org/simplejavamail/outlookmessageparser/model/OutlookMessage.java b/src/main/java/org/simplejavamail/outlookmessageparser/model/OutlookMessage.java index 883a7a3..5bd983e 100644 --- a/src/main/java/org/simplejavamail/outlookmessageparser/model/OutlookMessage.java +++ b/src/main/java/org/simplejavamail/outlookmessageparser/model/OutlookMessage.java @@ -22,6 +22,7 @@ import java.util.Set; import java.util.TreeMap; +import static java.util.Arrays.copyOfRange; import static java.util.regex.Pattern.compile; /** @@ -64,6 +65,18 @@ public class OutlookMessage { * The name part of Reply-To header */ private String replyToName; + /** + * The MIME part of the S/MIME header + */ + private String smimeMime; + /** + * The S/MIME type part of the S/MIME header + */ + private String smimeType; + /** + * The S/MIME name part of the S/MIME header + */ + private String smimeName; /** * The mail's subject. */ @@ -239,6 +252,9 @@ public void setProperty(final OutlookMessageProperty msgProp) { case 0x39: //CLIENT SUBMIT TIME setClientSubmitTime(stringValue); break; + case 0x8005: // S/MIME details + setSmime(stringValue); + break; } // save all properties (incl. those identified above) @@ -261,7 +277,7 @@ public void setProperty(final OutlookMessageProperty msgProp) { // 3003: email address // 1008 rtf sync } - + private String convertValueToString(final Object value) { if (value == null) { return null; @@ -379,47 +395,43 @@ private byte[] decompressRtfBytes(final byte[] value) { @Override public String toString() { - final StringBuilder sb = new StringBuilder(); - sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n"); - sb.append("To: ").append(createMailString(toEmail, toName)).append("\n"); - if (date != null) { - final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH); - sb.append("Date: ").append(formatter.format(date)).append("\n"); - } - if (subject != null) { - sb.append("Subject: ").append(subject).append("\n"); - } - sb.append("").append(outlookAttachments.size()).append(" outlookAttachments."); + final StringBuilder sb = commonToString(); + sb.append(outlookAttachments.size()).append(" outlookAttachments."); return sb.toString(); } - + /** * @return All information of this message object. */ public String toLongString() { - final StringBuilder sb = new StringBuilder(); - sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n"); - sb.append("To: ").append(createMailString(toEmail, toName)).append("\n"); - if (date != null) { - final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH); - sb.append("Date: ").append(formatter.format(date)).append("\n"); - } - if (subject != null) { - sb.append("Subject: ").append(subject).append("\n"); - } + final StringBuilder sb = commonToString(); sb.append("\n"); if (bodyText != null) { sb.append(bodyText); } if (!outlookAttachments.isEmpty()) { sb.append("\n"); - sb.append("").append(outlookAttachments.size()).append(" outlookAttachments.\n"); + sb.append(outlookAttachments.size()).append(" outlookAttachments.\n"); for (final OutlookAttachment att : outlookAttachments) { sb.append(att).append("\n"); } } return sb.toString(); } + + private StringBuilder commonToString() { + final StringBuilder sb = new StringBuilder(); + sb.append("From: ").append(createMailString(fromEmail, fromName)).append("\n"); + sb.append("To: ").append(createMailString(toEmail, toName)).append("\n"); + if (date != null) { + final SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.ENGLISH); + sb.append("Date: ").append(formatter.format(date)).append("\n"); + } + if (subject != null) { + sb.append("Subject: ").append(subject).append("\n"); + } + return sb; + } /** * Convenience method for creating an email address expression (including the name, the address, or both). @@ -443,6 +455,22 @@ private String createMailString(final String mail, final String name) { } return "\"" + name + "\" <" + mail + ">"; } + + void setSmime(String smimeHeader) { + // application/pkcs7-mime;smime-type=signed-data;name=smime.p7m + if (smimeHeader != null) { + final String[] smimeHeaderParts = smimeHeader.split(";"); + setSmimeMime(smimeHeaderParts[0]); + for (String smimeHeaderParam : copyOfRange(smimeHeaderParts, 1, smimeHeaderParts.length)) { + final String[] smimeParamParts = smimeHeaderParam.split("="); + if (smimeParamParts[0].equals("smime-type")) { + setSmimeType(smimeParamParts[1]); + } else if (smimeParamParts[0].equals("name")) { + setSmimeName(smimeParamParts[1]); + } + } + } + } /** * Bean getter for {@link #outlookAttachments}. @@ -1002,11 +1030,53 @@ public void setReplyToEmail(final String replyToEmail) { public String getReplyToName() { return replyToName; } - + /** * Bean setter for {@link #replyToName}. */ public void setReplyToName(final String replyToName) { this.replyToName = replyToName; } + + /** + * Bean getter for {@link #smimeMime}. + */ + public String getSmimeMime() { + return smimeMime; + } + + /** + * Bean setter for {@link #smimeMime}. + */ + public void setSmimeMime(String smimeMime) { + this.smimeMime = smimeMime; + } + + /** + * Bean getter for {@link #smimeType}. + */ + public String getSmimeType() { + return smimeType; + } + + /** + * Bean setter for {@link #smimeType}. + */ + public void setSmimeType(String smimeType) { + this.smimeType = smimeType; + } + + /** + * Bean getter for {@link #smimeName}. + */ + public String getSmimeName() { + return smimeName; + } + + /** + * Bean setter for {@link #smimeName}. + */ + public void setSmimeName(String smimeName) { + this.smimeName = smimeName; + } } \ No newline at end of file diff --git a/src/test/java/org/simplejavamail/outlookmessageparser/model/OutlookMessageTest.java b/src/test/java/org/simplejavamail/outlookmessageparser/model/OutlookMessageTest.java new file mode 100644 index 0000000..e48afae --- /dev/null +++ b/src/test/java/org/simplejavamail/outlookmessageparser/model/OutlookMessageTest.java @@ -0,0 +1,27 @@ +package org.simplejavamail.outlookmessageparser.model; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OutlookMessageTest { + @Test + public void testSetSmime() { + // application/pkcs7-mime;smime-type=signed-data;name=smime.p7m + testSmime(null, null, null, null); + testSmime("application/pkcs7-mime", "application/pkcs7-mime", null, null); + testSmime("application/pkcs7-mime;", "application/pkcs7-mime", null, null); + testSmime("application/pkcs7-mime;name=moo", "application/pkcs7-mime", null, "moo"); + testSmime("application/pkcs7-mime;smime-type=signed-data;name=smime.p7m", "application/pkcs7-mime", "signed-data", "smime.p7m"); + testSmime("application/pkcs7-mime;name=smime.p7m;smime-type=signed-data", "application/pkcs7-mime", "signed-data", "smime.p7m"); + testSmime("application/pkcs7-mime;name=smime.p7m;smime-type=signed-data;", "application/pkcs7-mime", "signed-data", "smime.p7m"); + } + + private void testSmime(String smimeHeader, String smimeMime, String smimeType, String smimeName) { + OutlookMessage msg = new OutlookMessage(); + msg.setSmime(smimeHeader); + assertThat(msg.getSmimeMime()).isEqualTo(smimeMime); + assertThat(msg.getSmimeType()).isEqualTo(smimeType); + assertThat(msg.getSmimeName()).isEqualTo(smimeName); + } +} \ No newline at end of file