JAMES-2578 Fix warnings use Mailet new API in james-server-queue-rabbitmq
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3c1a267f Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3c1a267f Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3c1a267f Branch: refs/heads/master Commit: 3c1a267f23997616eee50ee7b825402ebfa0d99f Parents: fc35485 Author: Gautier DI FOLCO <[email protected]> Authored: Mon Nov 5 14:08:35 2018 +0100 Committer: Gautier DI FOLCO <[email protected]> Committed: Thu Nov 8 15:53:15 2018 +0100 ---------------------------------------------------------------------- .../james/queue/rabbitmq/MailReferenceDTO.java | 22 ++++++---- .../view/cassandra/EnqueuedMailsDaoUtil.java | 34 ++++++--------- .../james/queue/rabbitmq/MailDTOTest.java | 7 ++- .../rabbitmq/RabbitMqMailQueueFactoryTest.java | 1 - .../CassandraMailQueueViewTestFactory.java | 4 -- .../src/test/resources/json/mail1.json | 46 ++++++++++++++------ 6 files changed, 64 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/MailReferenceDTO.java ---------------------------------------------------------------------- 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 e93394d..306e7f9 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 @@ -19,13 +19,13 @@ package org.apache.james.queue.rabbitmq; -import java.io.Serializable; import java.time.Instant; import java.util.Collection; import java.util.Date; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.function.Function; import java.util.stream.Stream; import javax.mail.MessagingException; @@ -35,14 +35,16 @@ import org.apache.commons.lang3.tuple.Pair; import org.apache.james.blob.mail.MimeMessagePartsId; import org.apache.james.core.MailAddress; import org.apache.james.server.core.MailImpl; -import org.apache.james.util.SerializationUtil; -import org.apache.james.util.streams.Iterators; +import org.apache.mailet.Attribute; +import org.apache.mailet.AttributeName; +import org.apache.mailet.AttributeValue; import org.apache.mailet.Mail; import org.apache.mailet.PerRecipientHeaders; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.github.fge.lambdas.Throwing; +import com.github.fge.lambdas.consumers.ThrowingBiConsumer; import com.github.steveash.guavate.Guavate; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -79,10 +81,11 @@ class MailReferenceDTO { } private static ImmutableMap<String, String> serializedAttributes(Mail mail) { - return Iterators.toStream(mail.getAttributeNames()) - .collect(Guavate.toImmutableMap( - name -> name, - name -> SerializationUtil.serialize(mail.getAttribute(name)))); + Function<Attribute, String> name = attribute -> attribute.getName().asString(); + Function<Attribute, String> value = attribute -> attribute.getValue().toJson().toString(); + return mail + .attributes() + .collect(Guavate.toImmutableMap(name, value)); } private final ImmutableList<String> recipients; @@ -202,8 +205,11 @@ class MailReferenceDTO { .map(Date::new) .ifPresent(mail::setLastUpdated); + ThrowingBiConsumer<String, String> attributeSetter = (name, value) -> + mail.setAttribute(new Attribute(AttributeName.of(name), AttributeValue.fromJsonString(value))); + attributes - .forEach((name, value) -> mail.setAttribute(name, SerializationUtil.<Serializable>deserialize(value))); + .forEach(Throwing.biConsumer(attributeSetter).sneakyThrow()); mail.addAllSpecificHeaderForRecipient(retrievePerRecipientHeaders()); http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java index 40e84bc..bab1ae7 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/EnqueuedMailsDaoUtil.java @@ -40,12 +40,10 @@ import static org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueV import static org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewModule.EnqueuedMailsTable.TIME_RANGE_START; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.Collection; import java.util.Date; @@ -65,7 +63,9 @@ import org.apache.james.queue.rabbitmq.MailQueueName; import org.apache.james.queue.rabbitmq.view.cassandra.model.BucketedSlices; import org.apache.james.queue.rabbitmq.view.cassandra.model.EnqueuedItemWithSlicingContext; import org.apache.james.server.core.MailImpl; -import org.apache.james.util.streams.Iterators; +import org.apache.mailet.Attribute; +import org.apache.mailet.AttributeName; +import org.apache.mailet.AttributeValue; import org.apache.mailet.Mail; import org.apache.mailet.PerRecipientHeaders; @@ -132,20 +132,19 @@ public class EnqueuedMailsDaoUtil { .build(); } - private static Map<String, Serializable> toAttributes(Map<String, ByteBuffer> rowAttributes) { + private static List<Attribute> toAttributes(Map<String, ByteBuffer> rowAttributes) { return rowAttributes.entrySet() .stream() - .collect(ImmutableMap.toImmutableMap( - Map.Entry::getKey, - entry -> fromByteBuffer(entry.getValue()))); + .map(entry -> new Attribute(AttributeName.of(entry.getKey()), fromByteBuffer(entry.getValue()))) + .collect(ImmutableList.toImmutableList()); } - private static Serializable fromByteBuffer(ByteBuffer byteBuffer) { + private static AttributeValue<?> fromByteBuffer(ByteBuffer byteBuffer) { try { byte[] data = new byte[byteBuffer.remaining()]; byteBuffer.get(data); ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(data)); - return (Serializable) objectInputStream.readObject(); + return AttributeValue.fromJsonString((String) objectInputStream.readObject()); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } @@ -177,20 +176,13 @@ public class EnqueuedMailsDaoUtil { } static ImmutableMap<String, ByteBuffer> toRawAttributeMap(Mail mail) { - return Iterators.toStream(mail.getAttributeNames()) - .map(name -> Pair.of(name, mail.getAttribute(name))) - .map(pair -> Pair.of(pair.getLeft(), toByteBuffer(pair.getRight()))) + return mail.attributes() + .map(attribute -> Pair.of(attribute.getName().asString(), toByteBuffer(attribute.getValue()))) .collect(ImmutableMap.toImmutableMap(Pair::getLeft, Pair::getRight)); } - private static ByteBuffer toByteBuffer(Serializable serializable) { - try { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - new ObjectOutputStream(outputStream).writeObject(serializable); - return ByteBuffer.wrap(outputStream.toByteArray()); - } catch (IOException e) { - throw new RuntimeException(e); - } + private static ByteBuffer toByteBuffer(AttributeValue<?> attributeValue) { + return ByteBuffer.wrap(attributeValue.toJson().toString().getBytes(StandardCharsets.UTF_8)); } static ImmutableMap<String, UDTValue> toHeaderMap(CassandraTypesProvider cassandraTypesProvider, http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/MailDTOTest.java ---------------------------------------------------------------------- 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 5da5655..4a8dbf3 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 @@ -31,6 +31,9 @@ import javax.mail.MessagingException; import org.apache.james.blob.api.HashBlobId; import org.apache.james.blob.mail.MimeMessagePartsId; import org.apache.james.server.core.MailImpl; +import org.apache.mailet.Attribute; +import org.apache.mailet.AttributeName; +import org.apache.mailet.AttributeValue; import org.apache.mailet.PerRecipientHeaders; import org.apache.mailet.base.MailAddressFixture; import org.apache.mailet.base.test.FakeMail; @@ -85,7 +88,7 @@ class MailDTOTest { FakeMail.builder() .recipients(MailAddressFixture.RECIPIENT1, MailAddressFixture.RECIPIENT2) .sender(MailAddressFixture.SENDER) - .attribute("att1", "value") + .attribute(new Attribute(AttributeName.of("att1"), AttributeValue.of("value"))) .errorMessage("an error") .lastUpdated(LAST_UPDATED) .remoteHost("toto.com") @@ -119,4 +122,4 @@ class MailDTOTest { .bodyBlobId(BLOB_ID_FACTORY.from("ef46c026-7819-4048-b562-3a37469191ed")) .build()); } -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java index 8a71cec..1f05da8 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMqMailQueueFactoryTest.java @@ -31,7 +31,6 @@ import java.time.Duration; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import org.apache.james.backend.rabbitmq.DockerRabbitMQ; import org.apache.james.backend.rabbitmq.RabbitMQConfiguration; import org.apache.james.backend.rabbitmq.RabbitMQExtension; import org.apache.james.blob.api.HashBlobId; http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java index 02d70e3..7eadf9c 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java @@ -23,8 +23,6 @@ import java.time.Clock; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; -import javax.mail.internet.MimeMessage; - import org.apache.james.backends.cassandra.init.CassandraTypesProvider; import org.apache.james.backends.cassandra.utils.CassandraUtils; import org.apache.james.blob.mail.MimeMessageStore; @@ -32,8 +30,6 @@ import org.apache.james.eventsourcing.eventstore.cassandra.CassandraEventStore; import org.apache.james.eventsourcing.eventstore.cassandra.EventStoreDao; import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer; import org.apache.james.blob.api.HashBlobId; -import org.apache.james.blob.api.Store; -import org.apache.james.blob.mail.MimeMessagePartsId; import org.apache.james.queue.rabbitmq.MailQueueName; import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfigurationModule; http://git-wip-us.apache.org/repos/asf/james-project/blob/3c1a267f/server/queue/queue-rabbitmq/src/test/resources/json/mail1.json ---------------------------------------------------------------------- diff --git a/server/queue/queue-rabbitmq/src/test/resources/json/mail1.json b/server/queue/queue-rabbitmq/src/test/resources/json/mail1.json index 625ed2e..b1d6d56 100644 --- a/server/queue/queue-rabbitmq/src/test/resources/json/mail1.json +++ b/server/queue/queue-rabbitmq/src/test/resources/json/mail1.json @@ -1,16 +1,34 @@ { - "recipients":["recipient1@localhost","recipient2@localhost"], - "name":"mail-name-558", - "sender":"sender@localhost", - "state":"state", - "errorMessage":"an error", - "lastUpdated":1473344752.000000000, - "attributes":{"att1":"rO0ABXQABXZhbHVl"}, - "remoteAddr":"159.221.12.145", - "remoteHost":"toto.com", - "perRecipientHeaders":{ - "recipient1@localhost":{"header":{"X-custom-header":["uedcgukrcg"]}}, - "recipient2@localhost":{"header":{"X-custom-header-2":["uedcgukrcg"]}}}, - "headerBlobId":"210e7136-ede3-44eb-9495-3ed816d6e23b", - "bodyBlobId":"ef46c026-7819-4048-b562-3a37469191ed" + "recipients": [ + "recipient1@localhost", + "recipient2@localhost" + ], + "name": "mail-name-558", + "sender": "sender@localhost", + "state": "state", + "errorMessage": "an error", + "lastUpdated": 1473344752, + "attributes": { + "att1": "{\"serializer\":\"StringSerializer\",\"value\":\"value\"}" + }, + "remoteAddr": "159.221.12.145", + "remoteHost": "toto.com", + "perRecipientHeaders": { + "recipient1@localhost": { + "header": { + "X-custom-header": [ + "uedcgukrcg" + ] + } + }, + "recipient2@localhost": { + "header": { + "X-custom-header-2": [ + "uedcgukrcg" + ] + } + } + }, + "headerBlobId": "210e7136-ede3-44eb-9495-3ed816d6e23b", + "bodyBlobId": "ef46c026-7819-4048-b562-3a37469191ed" } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
