Skip to content

Commit

Permalink
#493: last minute tweak to actually solve the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
bbottema committed Jan 27, 2024
1 parent 0e3720e commit 1f11cd7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public interface SMIMEModule {
* {@link org.simplejavamail.api.email.EmailPopulatingBuilder#fixingMessageId(String)}.
*/
boolean isMessageIdFixingMessage(MimeMessage message);

/**
* @return Whether the given attachment is S/MIME signed / encrypted. Defers to {@code SmimeRecognitionUtil.isGeneratedSmimeMessageId(..)}.
*/
<T> boolean isGeneratedSmimeMessageId(String key, T headerValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
import static org.simplejavamail.email.internal.EmailException.ERROR_READING_SMIME_FROM_INPUTSTREAM;
import static org.simplejavamail.email.internal.EmailException.ERROR_RESOLVING_IMAGE_DATASOURCE;
import static org.simplejavamail.email.internal.EmailException.NAME_MISSING_FOR_EMBEDDED_IMAGE;
import static org.simplejavamail.internal.smimesupport.SmimeRecognitionUtil.isGeneratedSmimeMessageId;
import static org.simplejavamail.internal.util.MiscUtil.defaultTo;
import static org.simplejavamail.internal.util.MiscUtil.extractEmailAddresses;
import static org.simplejavamail.internal.util.MiscUtil.interpretRecipient;
Expand Down Expand Up @@ -1711,7 +1710,9 @@ public <T> EmailPopulatingBuilder withHeaders(@NotNull final Map<String, Collect
public <T> InternalEmailPopulatingBuilder withHeaders(@NotNull final Map<String, Collection<T>> headers, final boolean ignoreSmimeMessageId) {
for (Map.Entry<String, Collection<T>> headerEntry : headers.entrySet()) {
for (final T headerValue : headerEntry.getValue()) {
if (!ignoreSmimeMessageId || !ModuleLoader.smimeModuleAvailable() || !isGeneratedSmimeMessageId(headerEntry.getKey(), headerValue)) {
if (!ignoreSmimeMessageId ||
!ModuleLoader.smimeModuleAvailable() ||
!ModuleLoader.loadSmimeModule().isGeneratedSmimeMessageId(headerEntry.getKey(), headerValue)) {
withHeader(headerEntry.getKey(), headerValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.simplejavamail.internal.smimesupport.SmimeException.*;
import static org.simplejavamail.internal.smimesupport.SmimeRecognitionUtil.SMIME_ATTACHMENT_MESSAGE_ID;
import static org.simplejavamail.internal.smimesupport.SmimeRecognitionUtil.isSmimeContentType;


/**
Expand Down Expand Up @@ -127,7 +125,7 @@ private void initSmimeMetadata(final SmimeParseResultBuilder smimeBuilder, @NotN
try {
if (mimeMessage.getHeader("Content-Type", null) != null) {
ContentType ct = new ContentType(mimeMessage.getHeader("Content-Type", null));
if (isSmimeContentType(ct)) {
if (SmimeRecognitionUtil.isSmimeContentType(ct)) {
smimeBuilder.getOriginalSmimeDetails()
.completeWith(OriginalSmimeDetailsImpl.builder()
.smimeMime(ct.getBaseType())
Expand Down Expand Up @@ -320,7 +318,7 @@ private AttachmentResource handleLiberatedContent(final Object content)
final MimeMessage decryptedMessage = new MimeMessage((Session) null) {
@Override
protected void updateMessageID() throws MessagingException {
setHeader("Message-ID", SMIME_ATTACHMENT_MESSAGE_ID);
setHeader("Message-ID", SmimeRecognitionUtil.SMIME_ATTACHMENT_MESSAGE_ID);
}
};
decryptedMessage.setContent((Multipart) content);
Expand Down Expand Up @@ -481,4 +479,9 @@ private SmimeKey produceSmimeKey(final @NotNull Pkcs12Config pkcs12) {
return new SmimeKeyStore(new ByteArrayInputStream(pkcs12.getPkcs12StoreData()), pkcs12.getStorePassword())
.getPrivateKey(pkcs12.getKeyAlias(), pkcs12.getKeyPassword());
}

@Override
public <T> boolean isGeneratedSmimeMessageId(String key, T headerValue) {
return SmimeRecognitionUtil.isGeneratedSmimeMessageId(key, headerValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public final class SmimeRecognitionUtil {
/**
* Used internally to recognize when we should ignore message ID header when starting a new email as a copy.
*/
public static final String SMIME_ATTACHMENT_MESSAGE_ID = "<generated-for-smime-signed-attachment@simple-java-mail>";
static final String SMIME_ATTACHMENT_MESSAGE_ID = "<generated-for-smime-signed-attachment@simple-java-mail>";

/**
* @return Whether the given attachment is S/MIME signed / encrypted.
Expand All @@ -30,7 +30,7 @@ static boolean isSmimeContentType(@NotNull final ContentType ct) {
isSmimeMultiPartSigned(ct.getBaseType(), ct.getParameter("protocol"));
}

public static <T> boolean isGeneratedSmimeMessageId(@NotNull String headerKey, @NotNull T headerValue) {
static <T> boolean isGeneratedSmimeMessageId(@NotNull String headerKey, @NotNull T headerValue) {
return headerKey.equals("Message-ID") && headerValue.equals(SMIME_ATTACHMENT_MESSAGE_ID);
}

Expand Down

0 comments on commit 1f11cd7

Please sign in to comment.