This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9d6feef74feefbfdcc06b7544f7aa43ea6abaed2 Author: Benoit Tellier <[email protected]> AuthorDate: Wed Jun 19 13:31:20 2019 +0700 JAMES-2794 Avoid relying on a DTO in RabbitMQ MailQueue logic --- .../org/apache/james/queue/rabbitmq/Dequeuer.java | 8 ++-- .../org/apache/james/queue/rabbitmq/Enqueuer.java | 17 ++++---- .../apache/james/queue/rabbitmq/MailLoader.java | 16 ++++---- .../{MailLoader.java => MailReference.java} | 46 +++++++++------------- .../james/queue/rabbitmq/MailReferenceDTO.java | 25 ++++++++---- .../queue/rabbitmq/RabbitMQMailQueueFactory.java | 3 +- .../apache/james/queue/rabbitmq/MailDTOTest.java | 22 ++++++----- 7 files changed, 68 insertions(+), 69 deletions(-) diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java index 7ed8d3e..e697cc2 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Dequeuer.java @@ -71,12 +71,12 @@ class Dequeuer { } } - private final Function<MailReferenceDTO, Mail> mailLoader; + private final Function<MailReferenceDTO, Pair<EnQueueId, Mail>> mailLoader; private final Metric dequeueMetric; private final MailReferenceSerializer mailReferenceSerializer; private final MailQueueView mailQueueView; - Dequeuer(MailQueueName name, RabbitClient rabbitClient, Function<MailReferenceDTO, Mail> mailLoader, + Dequeuer(MailQueueName name, RabbitClient rabbitClient, Function<MailReferenceDTO, Pair<EnQueueId, Mail>> mailLoader, MailReferenceSerializer serializer, MetricFactory metricFactory, MailQueueView mailQueueView) { this.mailLoader = mailLoader; @@ -129,9 +129,7 @@ class Dequeuer { private Pair<EnQueueId, Mail> loadMail(Delivery response) throws MailQueue.MailQueueException { MailReferenceDTO mailDTO = toMailReference(response); - EnQueueId enQueueId = mailDTO.retrieveEnqueueId(); - Mail mail = mailLoader.apply(mailDTO); - return Pair.of(enQueueId, mail); + return mailLoader.apply(mailDTO); } private MailReferenceDTO toMailReference(Delivery getResponse) throws MailQueue.MailQueueException { diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java index 9081acc..8f0c0fd 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/Enqueuer.java @@ -63,7 +63,8 @@ class Enqueuer { void enQueue(Mail mail) throws MailQueue.MailQueueException { EnQueueId enQueueId = EnQueueId.generate(); saveMail(mail) - .map(Throwing.<MimeMessagePartsId, EnqueuedItem>function(partsId -> publishReferenceToRabbit(enQueueId, mail, partsId)).sneakyThrow()) + .map(partIds -> new MailReference(enQueueId, mail, partIds)) + .map(Throwing.function(this::publishReferenceToRabbit).sneakyThrow()) .flatMap(mailQueueView::storeMail) .thenEmpty(Mono.fromRunnable(enqueueMetric::increment)) .block(); @@ -77,21 +78,21 @@ class Enqueuer { } } - private EnqueuedItem publishReferenceToRabbit(EnQueueId enQueueId, Mail mail, MimeMessagePartsId partsId) throws MailQueue.MailQueueException { - rabbitClient.publish(name, getMailReferenceBytes(enQueueId, mail, partsId)); + private EnqueuedItem publishReferenceToRabbit(MailReference mailReference) throws MailQueue.MailQueueException { + rabbitClient.publish(name, getMailReferenceBytes(mailReference)); return EnqueuedItem.builder() - .enQueueId(enQueueId) + .enQueueId(mailReference.getEnQueueId()) .mailQueueName(name) - .mail(mail) + .mail(mailReference.getMail()) .enqueuedTime(clock.instant()) - .mimeMessagePartsId(partsId) + .mimeMessagePartsId(mailReference.getPartsId()) .build(); } - private byte[] getMailReferenceBytes(EnQueueId enQueueId, Mail mail, MimeMessagePartsId partsId) throws MailQueue.MailQueueException { + private byte[] getMailReferenceBytes(MailReference mailReference) throws MailQueue.MailQueueException { try { - MailReferenceDTO mailDTO = MailReferenceDTO.fromMail(enQueueId, mail, partsId); + MailReferenceDTO mailDTO = MailReferenceDTO.fromMailReference(mailReference); return mailReferenceSerializer.write(mailDTO); } catch (JsonProcessingException e) { throw new MailQueue.MailQueueException("Unable to serialize message", e); diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java index f69c533..ca7df53 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java @@ -23,6 +23,7 @@ import javax.mail.MessagingException; import javax.mail.internet.AddressException; import javax.mail.internet.MimeMessage; +import org.apache.commons.lang3.tuple.Pair; import org.apache.james.blob.api.BlobId; import org.apache.james.blob.api.Store; import org.apache.james.blob.mail.MimeMessagePartsId; @@ -30,7 +31,6 @@ import org.apache.james.queue.api.MailQueue; import org.apache.mailet.Mail; class MailLoader { - private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore; private final BlobId.Factory blobIdFactory; @@ -39,16 +39,14 @@ class MailLoader { this.blobIdFactory = blobIdFactory; } - Mail load(MailReferenceDTO dto) throws MailQueue.MailQueueException { + Pair<EnQueueId, Mail> load(MailReferenceDTO dto) throws MailQueue.MailQueueException { try { - MimeMessage mimeMessage = mimeMessageStore.read( - MimeMessagePartsId.builder() - .headerBlobId(blobIdFactory.from(dto.getHeaderBlobId())) - .bodyBlobId(blobIdFactory.from(dto.getBodyBlobId())) - .build()) - .block(); + MailReference mailReference = dto.toMailReference(blobIdFactory); - return dto.toMailWithMimeMessage(mimeMessage); + Mail mail = mailReference.getMail(); + MimeMessage mimeMessage = mimeMessageStore.read(mailReference.getPartsId()).block(); + mail.setMessage(mimeMessage); + return Pair.of(mailReference.getEnQueueId(), mail); } catch (AddressException e) { throw new MailQueue.MailQueueException("Failed to parse mail address", e); } catch (MessagingException e) { diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReference.java similarity index 50% copy from server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java copy to server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReference.java index f69c533..031d97b 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailLoader.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReference.java @@ -19,40 +19,30 @@ package org.apache.james.queue.rabbitmq; -import javax.mail.MessagingException; -import javax.mail.internet.AddressException; -import javax.mail.internet.MimeMessage; - -import org.apache.james.blob.api.BlobId; -import org.apache.james.blob.api.Store; import org.apache.james.blob.mail.MimeMessagePartsId; -import org.apache.james.queue.api.MailQueue; import org.apache.mailet.Mail; -class MailLoader { +public class MailReference { + + private final EnQueueId enQueueId; + private final Mail mail; + private final MimeMessagePartsId partsId; - private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore; - private final BlobId.Factory blobIdFactory; + public MailReference(EnQueueId enQueueId, Mail mail, MimeMessagePartsId partsId) { + this.enQueueId = enQueueId; + this.mail = mail; + this.partsId = partsId; + } + + public EnQueueId getEnQueueId() { + return enQueueId; + } - MailLoader(Store<MimeMessage, MimeMessagePartsId> mimeMessageStore, BlobId.Factory blobIdFactory) { - this.mimeMessageStore = mimeMessageStore; - this.blobIdFactory = blobIdFactory; + public Mail getMail() { + return mail; } - Mail load(MailReferenceDTO dto) throws MailQueue.MailQueueException { - try { - MimeMessage mimeMessage = mimeMessageStore.read( - MimeMessagePartsId.builder() - .headerBlobId(blobIdFactory.from(dto.getHeaderBlobId())) - .bodyBlobId(blobIdFactory.from(dto.getBodyBlobId())) - .build()) - .block(); - - return dto.toMailWithMimeMessage(mimeMessage); - } catch (AddressException e) { - throw new MailQueue.MailQueueException("Failed to parse mail address", e); - } catch (MessagingException e) { - throw new MailQueue.MailQueueException("Failed to generate mime message", e); - } + public MimeMessagePartsId getPartsId() { + return partsId; } } diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceDTO.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceDTO.java index f119325..f63458f 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceDTO.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceDTO.java @@ -28,10 +28,8 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; - import org.apache.commons.lang3.tuple.Pair; +import org.apache.james.blob.api.BlobId; import org.apache.james.blob.mail.MimeMessagePartsId; import org.apache.james.core.MailAddress; import org.apache.james.core.MaybeSender; @@ -52,9 +50,12 @@ import com.google.common.collect.ImmutableMap; class MailReferenceDTO { - static MailReferenceDTO fromMail(EnQueueId enQueueId, Mail mail, MimeMessagePartsId partsId) { + static MailReferenceDTO fromMailReference(MailReference mailReference) { + Mail mail = mailReference.getMail(); + MimeMessagePartsId partsId = mailReference.getPartsId(); + return new MailReferenceDTO( - enQueueId.serialize(), + mailReference.getEnQueueId().serialize(), Optional.ofNullable(mail.getRecipients()).map(Collection::stream) .orElse(Stream.empty()) .map(MailAddress::asString) @@ -202,14 +203,22 @@ class MailReferenceDTO { return EnQueueId.ofSerialized(enQueueId); } - MailImpl toMailWithMimeMessage(MimeMessage mimeMessage) throws MessagingException { + MailReference toMailReference(BlobId.Factory blobIdFactory) { + MimeMessagePartsId messagePartsId = MimeMessagePartsId.builder() + .headerBlobId(blobIdFactory.from(headerBlobId)) + .bodyBlobId(blobIdFactory.from(bodyBlobId)) + .build(); + + return new MailReference(EnQueueId.ofSerialized(enQueueId), mailMetadata(), messagePartsId); + } + + private MailImpl mailMetadata() { MailImpl.Builder builder = MailImpl.builder() .name(name) .sender(sender.map(MaybeSender::getMailSender).orElse(MaybeSender.nullSender())) .addRecipients(recipients.stream() .map(Throwing.<String, MailAddress>function(MailAddress::new).sneakyThrow()) - .toArray(MailAddress[]::new)) - .mimeMessage(mimeMessage) + .collect(Guavate.toImmutableList())) .errorMessage(errorMessage) .remoteAddr(remoteAddr) .remoteHost(remoteHost) diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java index 57d5181..3660d12 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueFactory.java @@ -30,6 +30,7 @@ import java.util.function.Function; import javax.inject.Inject; import javax.mail.internet.MimeMessage; +import org.apache.commons.lang3.tuple.Pair; import org.apache.james.blob.api.BlobId; import org.apache.james.blob.api.Store; import org.apache.james.blob.mail.MimeMessagePartsId; @@ -53,7 +54,7 @@ public class RabbitMQMailQueueFactory implements MailQueueFactory<RabbitMQMailQu private final RabbitClient rabbitClient; private final Store<MimeMessage, MimeMessagePartsId> mimeMessageStore; private final MailReferenceSerializer mailReferenceSerializer; - private final Function<MailReferenceDTO, Mail> mailLoader; + private final Function<MailReferenceDTO, Pair<EnQueueId, Mail>> mailLoader; private final MailQueueView.Factory mailQueueViewFactory; private final Clock clock; private final MailQueueItemDecoratorFactory decoratorFactory; diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/MailDTOTest.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/MailDTOTest.java index 2c98b1d..2b110fa 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/MailDTOTest.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/MailDTOTest.java @@ -85,7 +85,8 @@ class MailDTOTest { } private MailReferenceDTO mailDTO1() throws MessagingException { - return MailReferenceDTO.fromMail( + return MailReferenceDTO.fromMailReference( + new MailReference( EN_QUEUE_ID, FakeMail.builder() .name("mail-name-558") @@ -109,21 +110,22 @@ class MailDTOTest { MimeMessagePartsId.builder() .headerBlobId(BLOB_ID_FACTORY.from("210e7136-ede3-44eb-9495-3ed816d6e23b")) .bodyBlobId(BLOB_ID_FACTORY.from("ef46c026-7819-4048-b562-3a37469191ed")) - .build()); + .build())); } - private MailReferenceDTO mailDTOMin() throws MessagingException { + private MailReferenceDTO mailDTOMin() { MailImpl mail = MailImpl.builder() .name("mail-name-558") .build(); mail.setState(null); mail.setLastUpdated(null); - return MailReferenceDTO.fromMail( - EN_QUEUE_ID, - mail, - MimeMessagePartsId.builder() - .headerBlobId(BLOB_ID_FACTORY.from("210e7136-ede3-44eb-9495-3ed816d6e23b")) - .bodyBlobId(BLOB_ID_FACTORY.from("ef46c026-7819-4048-b562-3a37469191ed")) - .build()); + return MailReferenceDTO.fromMailReference( + new MailReference( + EN_QUEUE_ID, + mail, + MimeMessagePartsId.builder() + .headerBlobId(BLOB_ID_FACTORY.from("210e7136-ede3-44eb-9495-3ed816d6e23b")) + .bodyBlobId(BLOB_ID_FACTORY.from("ef46c026-7819-4048-b562-3a37469191ed")) + .build())); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
