JAMES-1804 Introduce type for Set<EMailer>
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java
index 0ed87ac..6fff269 100644
--- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailer.java
@@ -22,9 +22,10 @@
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
-public class EMailer {
+public class EMailer implements Serializable {
private final String name;
private final String address;
@@ -45,6 +46,11 @@
}
@Override
+ public String serialize() {
+ return Joiner.on(" ").join(name, address);
+ }
+
+ @Override
public boolean equals(Object o) {
if (o instanceof EMailer) {
EMailer otherEMailer = (EMailer) o;
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java
new file mode 100644
index 0000000..7679310
--- /dev/null
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/EMailers.java
@@ -0,0 +1,52 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mailbox.elasticsearch.json;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.base.Preconditions;
+
+public class EMailers implements Serializable {
+
+ public static EMailers from(Set<EMailer> emailers) {
+ Preconditions.checkNotNull(emailers, "'emailers' is mandatory");
+ return new EMailers(emailers);
+ }
+
+ private final Set<EMailer> emailers;
+
+ private EMailers(Set<EMailer> emailers) {
+ this.emailers = emailers;
+ }
+
+ @JsonValue
+ public Set<EMailer> getEmailers() {
+ return emailers;
+ }
+
+ @Override
+ public String serialize() {
+ return emailers.stream()
+ .map(EMailer::serialize)
+ .collect(Collectors.joining(" "));
+ }
+}
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java
index 5b1cec1..d36b1ec 100644
--- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/IndexableMessage.java
@@ -68,11 +68,11 @@
private void copyHeaderFields(HeaderCollection headerCollection, ZonedDateTime internalDate) {
this.headers = headerCollection.getHeaders();
this.subjects = headerCollection.getSubjectSet();
- this.from = headerCollection.getFromAddressSet();
- this.to = headerCollection.getToAddressSet();
- this.replyTo = headerCollection.getReplyToAddressSet();
- this.cc = headerCollection.getCcAddressSet();
- this.bcc = headerCollection.getBccAddressSet();
+ this.from = EMailers.from(headerCollection.getFromAddressSet());
+ this.to = EMailers.from(headerCollection.getToAddressSet());
+ this.replyTo = EMailers.from(headerCollection.getReplyToAddressSet());
+ this.cc = EMailers.from(headerCollection.getCcAddressSet());
+ this.bcc = EMailers.from(headerCollection.getBccAddressSet());
this.sentDate = DateResolutionFormater.DATE_TIME_FOMATTER.format(headerCollection.getSentDate().orElse(internalDate));
}
@@ -119,11 +119,11 @@
private boolean isAnswered;
private String[] userFlags;
private Multimap<String, String> headers;
- private Set<EMailer> from;
- private Set<EMailer> to;
- private Set<EMailer> cc;
- private Set<EMailer> bcc;
- private Set<EMailer> replyTo;
+ private EMailers from;
+ private EMailers to;
+ private EMailers cc;
+ private EMailers bcc;
+ private EMailers replyTo;
private Set<String> subjects;
private String sentDate;
private List<Property> properties;
@@ -216,27 +216,27 @@
}
@JsonProperty(JsonMessageConstants.FROM)
- public Set<EMailer> getFrom() {
+ public EMailers getFrom() {
return from;
}
@JsonProperty(JsonMessageConstants.TO)
- public Set<EMailer> getTo() {
+ public EMailers getTo() {
return to;
}
@JsonProperty(JsonMessageConstants.CC)
- public Set<EMailer> getCc() {
+ public EMailers getCc() {
return cc;
}
@JsonProperty(JsonMessageConstants.BCC)
- public Set<EMailer> getBcc() {
+ public EMailers getBcc() {
return bcc;
}
@JsonProperty(JsonMessageConstants.REPLY_TO)
- public Set<EMailer> getReplyTo() {
+ public EMailers getReplyTo() {
return replyTo;
}
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java
new file mode 100644
index 0000000..92915df
--- /dev/null
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/Serializable.java
@@ -0,0 +1,25 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mailbox.elasticsearch.json;
+
+public interface Serializable {
+
+ String serialize();
+}
diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java
new file mode 100644
index 0000000..2ff133b
--- /dev/null
+++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/EMailersTest.java
@@ -0,0 +1,66 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+
+package org.apache.james.mailbox.elasticsearch.json;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.Test;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableSet;
+
+public class EMailersTest {
+
+ @Test
+ public void fromShouldThrowWhenSetIsNull() {
+ assertThatThrownBy(() -> EMailers.from(null))
+ .isInstanceOf(NullPointerException.class)
+ .hasMessage("'emailers' is mandatory");
+ }
+
+ @Test
+ public void serializeShouldReturnEmptyWhenEmptySet() {
+ EMailers eMailers = EMailers.from(ImmutableSet.of());
+
+ assertThat(eMailers.serialize()).isEmpty();
+ }
+
+ @Test
+ public void serializeShouldNotJoinWhenOneElement() {
+ EMailer emailer = new EMailer("name", "address");
+ EMailers eMailers = EMailers.from(ImmutableSet.of(emailer));
+
+ assertThat(eMailers.serialize()).isEqualTo(emailer.serialize());
+ }
+
+ @Test
+ public void serializeShouldJoinWhenMultipleElements() {
+ EMailer emailer = new EMailer("name", "address");
+ EMailer emailer2 = new EMailer("name2", "address2");
+ EMailer emailer3 = new EMailer("name3", "address3");
+
+ String expected = Joiner.on(" ").join(emailer.serialize(), emailer2.serialize(), emailer3.serialize());
+
+ EMailers eMailers = EMailers.from(ImmutableSet.of(emailer, emailer2, emailer3));
+
+ assertThat(eMailers.serialize()).isEqualTo(expected);
+ }
+}