james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [01/17] james-project git commit: JAMES-2411 Get the mail inputStream in MailRepositoryStoreService
Date Tue, 12 Jun 2018 00:03:20 GMT
Repository: james-project
Updated Branches:
  refs/heads/master 65c2bb774 -> 257eae573


JAMES-2411 Get the mail inputStream in MailRepositoryStoreService


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/581a2671
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/581a2671
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/581a2671

Branch: refs/heads/master
Commit: 581a2671d7761557620272a4b425f436be4e2330
Parents: 65c2bb7
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Tue Jun 5 15:29:19 2018 +0200
Committer: benwa <btellier@linagora.com>
Committed: Tue Jun 12 06:59:33 2018 +0700

----------------------------------------------------------------------
 .../webadmin/webadmin-mailrepository/pom.xml    |  6 +++
 .../service/MailRepositoryStoreService.java     | 11 +++++
 .../service/MailRepositoryStoreServiceTest.java | 46 ++++++++++++++++++++
 3 files changed, 63 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/581a2671/server/protocols/webadmin/webadmin-mailrepository/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/pom.xml b/server/protocols/webadmin/webadmin-mailrepository/pom.xml
index 72b374a..20ffd89 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/pom.xml
+++ b/server/protocols/webadmin/webadmin-mailrepository/pom.xml
@@ -76,6 +76,12 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>javax-mail-extension</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/james-project/blob/581a2671/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java
b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java
index 7df346b..efc9a75 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/service/MailRepositoryStoreService.java
@@ -19,11 +19,13 @@
 
 package org.apache.james.webadmin.service;
 
+import java.io.InputStream;
 import java.util.List;
 import java.util.Optional;
 
 import javax.inject.Inject;
 import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 
 import org.apache.james.mailrepository.api.MailRepository;
 import org.apache.james.mailrepository.api.MailRepositoryStore;
@@ -35,6 +37,7 @@ import org.apache.james.webadmin.dto.MailDto;
 import org.apache.james.webadmin.dto.MailKey;
 import org.apache.james.webadmin.dto.MailRepositoryResponse;
 import org.apache.james.webadmin.utils.ErrorResponder;
+import org.apache.mailet.Mail;
 import org.eclipse.jetty.http.HttpStatus;
 
 import com.github.fge.lambdas.Throwing;
@@ -83,6 +86,14 @@ public class MailRepositoryStoreService {
             .map(Throwing.function(MailDto::fromMail).sneakyThrow());
     }
 
+    public Optional<InputStream> downloadMail(String url, String mailKey) throws MailRepositoryStore.MailRepositoryStoreException,
MessagingException {
+        MailRepository mailRepository = getRepository(url);
+
+        return Optional.ofNullable(mailRepository.retrieve(mailKey))
+            .map(Throwing.function(Mail::getMessage).sneakyThrow())
+            .map(Throwing.function(MimeMessage::getRawInputStream).sneakyThrow());
+    }
+
     public void deleteMail(String url, String mailKey) throws MailRepositoryStore.MailRepositoryStoreException,
MessagingException {
         getRepository(url)
             .remove(mailKey);

http://git-wip-us.apache.org/repos/asf/james-project/blob/581a2671/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/MailRepositoryStoreServiceTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/MailRepositoryStoreServiceTest.java
b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/MailRepositoryStoreServiceTest.java
index fb6fd66..bd84ae1 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/MailRepositoryStoreServiceTest.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/service/MailRepositoryStoreServiceTest.java
@@ -23,8 +23,11 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Optional;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.james.mailrepository.api.MailRepositoryStore;
 import org.apache.james.mailrepository.memory.MemoryMailRepository;
 import org.apache.james.util.streams.Limit;
@@ -126,4 +129,47 @@ public class MailRepositoryStoreServiceTest {
         assertThat(testee.listMails(FIRST_REPOSITORY, Offset.from(1), Limit.from(1)).get())
             .containsOnly(new MailKey(NAME_2));
     }
+
+    @Test
+    public void downloadMailShouldThrownWhenUnknownRepository() throws Exception {
+        when(mailRepositoryStore.get("unkown")).thenReturn(Optional.empty());
+
+        assertThatThrownBy(() -> testee.downloadMail(FIRST_REPOSITORY, NAME_1))
+            .isInstanceOf(NullPointerException.class);
+    }
+
+    @Test
+    public void dowloadMailShouldThrowWhenMailRepositoryStoreThrows() throws Exception {
+        when(mailRepositoryStore.get(FIRST_REPOSITORY))
+            .thenThrow(new MailRepositoryStore.MailRepositoryStoreException("message"));
+
+        assertThatThrownBy(() -> testee.downloadMail(FIRST_REPOSITORY, NAME_1))
+            .isInstanceOf(MailRepositoryStore.MailRepositoryStoreException.class);
+    }
+
+    @Test
+    public void dowloadMailShouldReturnEmptyWhenMailNotFound() throws Exception {
+        when(mailRepositoryStore.get(FIRST_REPOSITORY)).thenReturn(Optional.of(repository));
+
+        assertThat(testee.downloadMail(FIRST_REPOSITORY, NAME_1))
+            .isEmpty();
+    }
+
+    @Test
+    public void dowloadMailShouldReturnThInputStreamWhenMailExists() throws Exception {
+        when(mailRepositoryStore.get(FIRST_REPOSITORY)).thenReturn(Optional.of(repository));
+
+        FakeMail mail = FakeMail.builder()
+            .name(NAME_1)
+            .fileName("mail.eml")
+            .build();
+        repository.store(mail);
+
+        Optional<InputStream> downloadMail = testee.downloadMail(FIRST_REPOSITORY,
NAME_1);
+        assertThat(downloadMail).isNotEmpty();
+
+        String eml = IOUtils.toString(downloadMail.get(), StandardCharsets.UTF_8);
+        String expectedContent = IOUtils.toString(mail.getMessage().getRawInputStream(),
StandardCharsets.UTF_8);
+        assertThat(eml).isEqualTo(expectedContent);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message