MAILET-144 Fix decrypting messages with attachments
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/76cabea0 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/76cabea0 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/76cabea0 Branch: refs/heads/master Commit: 76cabea09fe9600a6a921817436160bf44d45c25 Parents: 5cff53c Author: Antoine Duprat <adup...@linagora.com> Authored: Tue Jan 10 15:33:34 2017 +0100 Committer: Antoine Duprat <adup...@linagora.com> Committed: Mon Jan 30 10:33:27 2017 +0100 ---------------------------------------------------------------------- mailet/crypto/pom.xml | 4 +++ .../james/transport/mailets/SMIMEDecrypt.java | 30 +++++++++++--------- mailet/pom.xml | 6 ++++ .../crypto/SMIMEDecryptIntegrationTest.java | 15 ++++++---- 4 files changed, 36 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/76cabea0/mailet/crypto/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/crypto/pom.xml b/mailet/crypto/pom.xml index 07ccb08..b9d9824 100644 --- a/mailet/crypto/pom.xml +++ b/mailet/crypto/pom.xml @@ -52,6 +52,10 @@ <artifactId>bcmail-jdk15on</artifactId> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-io</artifactId> + </dependency> + <dependency> <groupId>org.apache.james</groupId> <artifactId>apache-mailet-base</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/76cabea0/mailet/crypto/src/main/java/org/apache/james/transport/mailets/SMIMEDecrypt.java ---------------------------------------------------------------------- diff --git a/mailet/crypto/src/main/java/org/apache/james/transport/mailets/SMIMEDecrypt.java b/mailet/crypto/src/main/java/org/apache/james/transport/mailets/SMIMEDecrypt.java index 47422ab..215ce34 100644 --- a/mailet/crypto/src/main/java/org/apache/james/transport/mailets/SMIMEDecrypt.java +++ b/mailet/crypto/src/main/java/org/apache/james/transport/mailets/SMIMEDecrypt.java @@ -29,10 +29,10 @@ import java.util.ArrayList; import java.util.Collection; import javax.mail.MessagingException; -import javax.mail.Multipart; import javax.mail.Part; import javax.mail.internet.MimeMessage; +import org.apache.commons.io.IOUtils; import org.apache.james.transport.SMIMEKeyHolder; import org.apache.mailet.Mail; import org.apache.mailet.MailetConfig; @@ -46,6 +46,8 @@ import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient; import org.bouncycastle.mail.smime.SMIMEEnveloped; import org.bouncycastle.mail.smime.SMIMEUtil; +import com.google.common.base.Charsets; + /** * This mailet decrypts a s/mime encrypted message. It takes as input an * encrypted message and it tries to dechiper it using the key specified in its @@ -110,9 +112,9 @@ public class SMIMEDecrypt extends GenericMailet { try { return new X509CertificateHolder(certificate.getEncoded()); } catch (CertificateEncodingException e) { - throw new MessagingException("Error during the parsing of the certificate",e); + throw new MessagingException("Error during the parsing of the certificate", e); } catch (IOException e) { - throw new MessagingException("Error during the parsing of the certificate",e); + throw new MessagingException("Error during the parsing of the certificate", e); } } @@ -145,7 +147,7 @@ public class SMIMEDecrypt extends GenericMailet { } } } catch (CMSException e) { - throw new MessagingException("Error during the decryption of the message",e); + throw new MessagingException("Error during the decryption of the message", e); } } @@ -162,21 +164,21 @@ public class SMIMEDecrypt extends GenericMailet { // I start the message stripping. try { - MimeMessage newmex = new MimeMessage(message); - Object obj = strippedMessage.getContent(); - if (obj instanceof Multipart) { - log("The message is multipart, content type "+((Multipart)obj).getContentType()); - newmex.setContent((Multipart)obj); - } else { - newmex.setContent(obj, strippedMessage.getContentType()); - newmex.setDisposition(null); + MimeMessage newMessage = new MimeMessage(message); + newMessage.setText(text(strippedMessage), Charsets.UTF_8.name()); + if (!strippedMessage.isMimeType("multipart/*")) { + newMessage.setDisposition(null); } - newmex.saveChanges(); - mail.setMessage(newmex); + newMessage.saveChanges(); + mail.setMessage(newMessage); } catch (IOException e) { log("Error during the strip of the encrypted message"); throw new MessagingException("Error during the stripping of the encrypted message",e); } } } + + private String text(Part mimePart) throws IOException, MessagingException { + return IOUtils.toString(mimePart.getDataHandler().getInputStream()); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/76cabea0/mailet/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/pom.xml b/mailet/pom.xml index 23b39cf..d676af7 100644 --- a/mailet/pom.xml +++ b/mailet/pom.xml @@ -64,6 +64,7 @@ <guavate.version>1.0.0</guavate.version> <ical4j.version>1.0.2</ical4j.version> <guavate.version>1.0.0</guavate.version> + <apache-commons-io.version>1.3.2</apache-commons-io.version> </properties> @@ -126,6 +127,11 @@ <version>1.52</version> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-io</artifactId> + <version>${apache-commons-io.version}</version> + </dependency> + <dependency> <groupId>org.apache.maven.artifact</groupId> <artifactId>maven-artifact</artifactId> <version>${maven-artifact.version}</version> http://git-wip-us.apache.org/repos/asf/james-project/blob/76cabea0/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMEDecryptIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMEDecryptIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMEDecryptIntegrationTest.java index f4fa500..2732c0e 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMEDecryptIntegrationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMEDecryptIntegrationTest.java @@ -21,6 +21,8 @@ package org.apache.james.mailets.crypto; import static org.assertj.core.api.Assertions.assertThat; +import java.time.ZonedDateTime; + import org.apache.commons.io.IOUtils; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailets.TemporaryJamesServer; @@ -30,6 +32,7 @@ import org.apache.james.mailets.configuration.MailetContainer; import org.apache.james.mailets.configuration.ProcessorConfiguration; import org.apache.james.mailets.utils.IMAPMessageReader; import org.apache.james.mailets.utils.SMTPMessageSender; +import org.apache.james.util.date.ZonedDateTimeProvider; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -43,6 +46,7 @@ import com.jayway.awaitility.core.ConditionFactory; public class SMIMEDecryptIntegrationTest { + private static final ZonedDateTime DATE_2015 = ZonedDateTime.parse("2015-10-15T14:10:00Z"); private static final String LOCALHOST_IP = "127.0.0.1"; private static final int IMAP_PORT = 1143; private static final int SMTP_SECURE_PORT = 10465; @@ -73,10 +77,6 @@ public class SMIMEDecryptIntegrationTest { .addProperty("name", "bcc") .build()) .addMailet(MailetConfiguration.builder() - .match("RecipientIsLocal") - .clazz("org.apache.james.jmap.mailet.VacationMailet") - .build()) - .addMailet(MailetConfiguration.builder() .clazz("SMIMEDecrypt") .match("All") .addProperty("keyStoreFileName", temporaryFolder.getRoot().getAbsoluteFile().getAbsolutePath() + "/conf/smime.p12") @@ -86,12 +86,17 @@ public class SMIMEDecryptIntegrationTest { .build()) .addMailet(MailetConfiguration.builder() .match("RecipientIsLocal") + .clazz("org.apache.james.jmap.mailet.VacationMailet") + .build()) + .addMailet(MailetConfiguration.builder() + .match("RecipientIsLocal") .clazz("LocalDelivery") .build()) .build()) .build(); - jamesServer = new TemporaryJamesServer(temporaryFolder, mailetContainer); + jamesServer = new TemporaryJamesServer(temporaryFolder, mailetContainer, + binder -> binder.bind(ZonedDateTimeProvider.class).toInstance(() -> DATE_2015)); Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await(); --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org