You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2019/11/29 02:00:28 UTC
[james-project] 02/06: JAMES-2989 JMAP Messages carrying a strong
typing Preview
This is an automated email from the ASF dual-hosted git repository.
rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 9a178a1969104003d1e2d5832b728a9c7e086595
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Tue Nov 26 11:21:17 2019 +0700
JAMES-2989 JMAP Messages carrying a strong typing Preview
---
.../apache/james/jmap/draft/model/PreviewDTO.java | 68 ++++++++++++++++++++++
.../draft/model/message/view/MessageFullView.java | 17 +++---
.../model/message/view/MessageFullViewFactory.java | 11 ++--
.../message/view/MessageFullViewFactoryTest.java | 44 +++++++-------
4 files changed, 107 insertions(+), 33 deletions(-)
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java
new file mode 100644
index 0000000..6754db7
--- /dev/null
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/PreviewDTO.java
@@ -0,0 +1,68 @@
+/****************************************************************
+ * 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.jmap.draft.model;
+
+import java.util.Objects;
+
+import org.apache.james.jmap.api.preview.Preview;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+public class PreviewDTO {
+
+ public static PreviewDTO from(Preview preview) {
+ return new PreviewDTO(preview.getValue());
+ }
+
+ @VisibleForTesting
+ public static PreviewDTO of(String value) {
+ return new PreviewDTO(value);
+ }
+
+ private final String value;
+
+ private PreviewDTO(String value) {
+ Preconditions.checkNotNull(value);
+
+ this.value = value;
+ }
+
+ @JsonValue
+ public String getValue() {
+ return value;
+ }
+
+ @Override
+ public final boolean equals(Object o) {
+ if (o instanceof PreviewDTO) {
+ PreviewDTO that = (PreviewDTO) o;
+
+ return Objects.equals(this.value, that.value);
+ }
+ return false;
+ }
+
+ @Override
+ public final int hashCode() {
+ return Objects.hash(value);
+ }
+}
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
index 01ab3a3..00d8005 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullView.java
@@ -25,12 +25,14 @@ import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
+import org.apache.james.jmap.api.preview.Preview;
import org.apache.james.jmap.draft.methods.JmapResponseWriterImpl;
import org.apache.james.jmap.draft.model.Attachment;
import org.apache.james.jmap.draft.model.BlobId;
import org.apache.james.jmap.draft.model.Emailer;
import org.apache.james.jmap.draft.model.Keywords;
import org.apache.james.jmap.draft.model.Number;
+import org.apache.james.jmap.draft.model.PreviewDTO;
import org.apache.james.mailbox.model.MailboxId;
import org.apache.james.mailbox.model.MessageId;
@@ -39,7 +41,6 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -53,7 +54,7 @@ public class MessageFullView extends MessageHeaderView {
@JsonPOJOBuilder(withPrefix = "")
public static class Builder extends MessageHeaderView.Builder<MessageFullView.Builder> {
- private String preview;
+ private Preview preview;
private Optional<String> textBody = Optional.empty();
private Optional<String> htmlBody = Optional.empty();
private final ImmutableList.Builder<Attachment> attachments;
@@ -65,7 +66,7 @@ public class MessageFullView extends MessageHeaderView {
attachedMessages = ImmutableMap.builder();
}
- public Builder preview(String preview) {
+ public Builder preview(Preview preview) {
this.preview = preview;
return this;
}
@@ -98,13 +99,13 @@ public class MessageFullView extends MessageHeaderView {
return new MessageFullView(id, blobId, threadId, mailboxIds, Optional.ofNullable(inReplyToMessageId),
hasAttachment, headers, Optional.ofNullable(from),
- to.build(), cc.build(), bcc.build(), replyTo.build(), subject, date, size, preview, textBody, htmlBody, attachments, attachedMessages,
+ to.build(), cc.build(), bcc.build(), replyTo.build(), subject, date, size, PreviewDTO.from(preview), textBody, htmlBody, attachments, attachedMessages,
keywords.orElse(Keywords.DEFAULT_VALUE));
}
public void checkState(ImmutableList<Attachment> attachments, ImmutableMap<BlobId, SubMessage> attachedMessages) {
super.checkState();
- Preconditions.checkState(!Strings.isNullOrEmpty(preview), "'preview' is mandatory");
+ Preconditions.checkState(preview != null, "'preview' is mandatory");
Preconditions.checkState(areAttachedMessagesKeysInAttachments(attachments, attachedMessages), "'attachedMessages' keys must be in 'attachements'");
}
}
@@ -126,7 +127,7 @@ public class MessageFullView extends MessageHeaderView {
}
private final boolean hasAttachment;
- private final String preview;
+ private final PreviewDTO preview;
private final Optional<String> textBody;
private final Optional<String> htmlBody;
private final ImmutableList<Attachment> attachments;
@@ -148,7 +149,7 @@ public class MessageFullView extends MessageHeaderView {
String subject,
Instant date,
Number size,
- String preview,
+ PreviewDTO preview,
Optional<String> textBody,
Optional<String> htmlBody,
ImmutableList<Attachment> attachments,
@@ -167,7 +168,7 @@ public class MessageFullView extends MessageHeaderView {
return hasAttachment;
}
- public String getPreview() {
+ public PreviewDTO getPreview() {
return preview;
}
diff --git a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
index 430910d..c246ea5 100644
--- a/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
+++ b/server/protocols/jmap-draft/src/main/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactory.java
@@ -30,10 +30,10 @@ import java.util.Set;
import javax.inject.Inject;
import javax.mail.internet.SharedInputStream;
+import org.apache.james.jmap.api.preview.Preview;
import org.apache.james.jmap.draft.model.Attachment;
import org.apache.james.jmap.draft.model.BlobId;
import org.apache.james.jmap.draft.model.Keywords;
-import org.apache.james.jmap.draft.model.MessagePreviewGenerator;
import org.apache.james.jmap.draft.utils.HtmlTextExtractor;
import org.apache.james.mailbox.BlobManager;
import org.apache.james.mailbox.MessageUid;
@@ -55,15 +55,13 @@ import com.google.common.collect.Sets;
public class MessageFullViewFactory implements MessageViewFactory<MessageFullView> {
private final BlobManager blobManager;
- private final MessagePreviewGenerator messagePreview;
private final MessageContentExtractor messageContentExtractor;
private final HtmlTextExtractor htmlTextExtractor;
@Inject
- public MessageFullViewFactory(BlobManager blobManager, MessagePreviewGenerator messagePreview, MessageContentExtractor messageContentExtractor,
+ public MessageFullViewFactory(BlobManager blobManager, MessageContentExtractor messageContentExtractor,
HtmlTextExtractor htmlTextExtractor) {
this.blobManager = blobManager;
- this.messagePreview = messagePreview;
this.messageContentExtractor = messageContentExtractor;
this.htmlTextExtractor = htmlTextExtractor;
}
@@ -79,7 +77,10 @@ public class MessageFullViewFactory implements MessageViewFactory<MessageFullVie
Optional<String> htmlBody = messageContent.getHtmlBody();
Optional<String> mainTextContent = mainTextContent(messageContent);
Optional<String> textBody = computeTextBodyIfNeeded(messageContent, mainTextContent);
- String preview = messagePreview.compute(mainTextContent);
+
+ Preview preview = mainTextContent.map(Preview::compute)
+ .orElse(Preview.NO_BODY);
+
return MessageFullView.builder()
.id(message.getMessageId())
.blobId(BlobId.of(blobManager.toBlobId(message.getMessageId())))
diff --git a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
index c1c3082..60aefee 100644
--- a/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
+++ b/server/protocols/jmap-draft/src/test/java/org/apache/james/jmap/draft/model/message/view/MessageFullViewFactoryTest.java
@@ -35,13 +35,14 @@ import java.util.Optional;
import javax.mail.Flags;
import org.apache.commons.lang3.StringUtils;
+import org.apache.james.jmap.api.preview.Preview;
import org.apache.james.jmap.draft.model.Attachment;
import org.apache.james.jmap.draft.model.BlobId;
import org.apache.james.jmap.draft.model.Emailer;
import org.apache.james.jmap.draft.model.Keyword;
import org.apache.james.jmap.draft.model.Keywords;
-import org.apache.james.jmap.draft.model.MessagePreviewGenerator;
import org.apache.james.jmap.draft.model.Number;
+import org.apache.james.jmap.draft.model.PreviewDTO;
import org.apache.james.jmap.draft.model.message.view.MessageFullViewFactory.MetaDataWithContent;
import org.apache.james.jmap.draft.utils.HtmlTextExtractor;
import org.apache.james.jmap.draft.utils.JsoupHtmlTextExtractor;
@@ -87,7 +88,6 @@ class MessageFullViewFactoryTest {
void setUp() throws Exception {
HtmlTextExtractor htmlTextExtractor = new JsoupHtmlTextExtractor();
- MessagePreviewGenerator messagePreview = new MessagePreviewGenerator();
MessageContentExtractor messageContentExtractor = new MessageContentExtractor();
InMemoryIntegrationResources resources = InMemoryIntegrationResources.defaultResources();
@@ -106,7 +106,7 @@ class MessageFullViewFactoryTest {
.build(ClassLoaderUtils.getSystemResourceAsSharedStream("fullMessage.eml")),
session);
- messageFullViewFactory = new MessageFullViewFactory(resources.getBlobManager(), messagePreview, messageContentExtractor, htmlTextExtractor);
+ messageFullViewFactory = new MessageFullViewFactory(resources.getBlobManager(), messageContentExtractor, htmlTextExtractor);
}
@Test
@@ -132,7 +132,7 @@ class MessageFullViewFactoryTest {
softly.assertThat(actual.getSubject()).isEqualTo("Full message");
softly.assertThat(actual.getDate()).isEqualTo("2016-06-07T14:23:37Z");
softly.assertThat(actual.isHasAttachment()).isEqualTo(true);
- softly.assertThat(actual.getPreview()).isEqualTo("blabla bloblo");
+ softly.assertThat(actual.getPreview()).isEqualTo(PreviewDTO.of("blabla bloblo"));
softly.assertThat(actual.getTextBody()).isEqualTo(Optional.of("/blabla/\n*bloblo*\n"));
softly.assertThat(actual.getHtmlBody()).isEqualTo(Optional.of("<i>blabla</i>\n<b>bloblo</b>\n"));
softly.assertThat(actual.getAttachments()).hasSize(1);
@@ -171,7 +171,7 @@ class MessageFullViewFactoryTest {
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
assertThat(testee)
.extracting(MessageFullView::getPreview, MessageFullView::getSize, MessageFullView::getSubject, MessageFullView::getHeaders, MessageFullView::getDate)
- .containsExactly("(Empty)", Number.ZERO, "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
+ .containsExactly(PreviewDTO.of("(Empty)"), Number.ZERO, "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
}
@Test
@@ -250,7 +250,7 @@ class MessageFullViewFactoryTest {
.subject("test subject")
.date(Instant.parse("2015-07-14T12:30:42.000Z"))
.size(headers.length())
- .preview("(Empty)")
+ .preview(Preview.from("(Empty)"))
.textBody(Optional.of(""))
.htmlBody(Optional.empty())
.keywords(keywords)
@@ -301,7 +301,7 @@ class MessageFullViewFactoryTest {
.subject("test subject")
.date(Instant.parse("2012-02-03T14:30:42.000Z"))
.size(headers.length())
- .preview("(Empty)")
+ .preview(Preview.from("(Empty)"))
.textBody(Optional.of(""))
.htmlBody(Optional.empty())
.keywords(keywords)
@@ -350,7 +350,7 @@ class MessageFullViewFactoryTest {
.subject("test subject")
.date(Instant.parse("2012-02-03T14:30:42.000Z"))
.size(headers.length())
- .preview("(Empty)")
+ .preview(Preview.from("(Empty)"))
.textBody(Optional.of(""))
.htmlBody(Optional.empty())
.keywords(keywords)
@@ -421,11 +421,11 @@ class MessageFullViewFactoryTest {
String body300 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
+ "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
+ "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999";
- String expectedPreview = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
+ PreviewDTO expectedPreview = PreviewDTO.of("0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
+ "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
- + "00000000001111111111222222222233333333334444444444555555";
+ + "00000000001111111111222222222233333333334444444444555555");
assertThat(body300.length()).isEqualTo(300);
- assertThat(expectedPreview.length()).isEqualTo(256);
+ assertThat(expectedPreview.getValue().length()).isEqualTo(256);
String mail = headers + "\n" + body300;
MetaDataWithContent testMail = MetaDataWithContent.builder()
.uid(MessageUid.of(2))
@@ -438,7 +438,8 @@ class MessageFullViewFactoryTest {
.messageId(TestMessageId.of(2))
.build();
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
- assertThat(testee.getPreview()).isEqualTo(expectedPreview);
+ assertThat(testee.getPreview())
+ .isEqualTo(expectedPreview);
}
@Test
@@ -638,7 +639,7 @@ class MessageFullViewFactoryTest {
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
assertThat(testee)
.extracting(MessageFullView::getPreview, MessageFullView::getSize, MessageFullView::getSubject, MessageFullView::getHeaders, MessageFullView::getDate)
- .containsExactly("(Empty)", Number.fromLong(1010L), "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
+ .containsExactly(PreviewDTO.of("(Empty)"), Number.fromLong(1010L), "", ImmutableMap.of("MIME-Version", "1.0"), INTERNAL_DATE);
}
@Test
@@ -659,7 +660,8 @@ class MessageFullViewFactoryTest {
.build();
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
- assertThat(testee.getPreview()).isEqualTo("my HTML message");
+ assertThat(testee.getPreview())
+ .isEqualTo(PreviewDTO.of("my HTML message"));
assertThat(testee.getTextBody()).hasValue("my HTML message");
assertThat(testee.getHtmlBody()).hasValue("my <b>HTML</b> message");
}
@@ -680,7 +682,7 @@ class MessageFullViewFactoryTest {
.build();
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
- assertThat(testee.getPreview()).isEqualTo(MessagePreviewGenerator.NO_BODY);
+ assertThat(testee.getPreview()).isEqualTo(PreviewDTO.of("(Empty)"));
assertThat(testee.getHtmlBody()).contains("");
assertThat(testee.getTextBody()).isEmpty();
}
@@ -712,7 +714,8 @@ class MessageFullViewFactoryTest {
.build();
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
- assertThat(testee.getPreview()).isEqualTo(expected);
+ assertThat(testee.getPreview())
+ .isEqualTo(PreviewDTO.of(expected));
}
@Test
@@ -734,7 +737,8 @@ class MessageFullViewFactoryTest {
.build();
MessageFullView testee = messageFullViewFactory.fromMetaDataWithContent(testMail);
- assertThat(testee.getPreview()).isEqualTo("My plain text");
+ assertThat(testee.getPreview())
+ .isEqualTo(PreviewDTO.of("My plain text"));
}
@Test
@@ -755,7 +759,7 @@ class MessageFullViewFactoryTest {
assertThat(testee)
.extracting(MessageFullView::getPreview, MessageFullView::getHtmlBody, MessageFullView::getTextBody)
- .containsExactly(MessagePreviewGenerator.NO_BODY, Optional.empty(), Optional.of(""));
+ .containsExactly(PreviewDTO.of("(Empty)"), Optional.empty(), Optional.of(""));
}
@Test
@@ -779,7 +783,7 @@ class MessageFullViewFactoryTest {
assertThat(testee)
.extracting(MessageFullView::getPreview, MessageFullView::getHtmlBody, MessageFullView::getTextBody)
- .containsExactly(MessagePreviewGenerator.NO_BODY, Optional.of("<html><body></body></html>"), Optional.empty());
+ .containsExactly(PreviewDTO.of("(Empty)"), Optional.of("<html><body></body></html>"), Optional.empty());
}
@Test
@@ -815,7 +819,7 @@ class MessageFullViewFactoryTest {
assertThat(testee)
.extracting(MessageFullView::getPreview, MessageFullView::getHtmlBody, MessageFullView::getTextBody)
- .containsExactly("My plain message", Optional.of("<html></html>"), Optional.of("My plain message"));
+ .containsExactly(PreviewDTO.of("My plain message"), Optional.of("<html></html>"), Optional.of("My plain message"));
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org