james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [03/17] james-project git commit: JAMES-2411 Add the capability to download the eml itself in the MailRepository GET endpoint
Date Tue, 12 Jun 2018 00:03:22 GMT
JAMES-2411 Add the capability to download the eml itself in the MailRepository GET endpoint


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

Branch: refs/heads/master
Commit: 4d3cda34928214b4dca5aa44e1061236dba3016a
Parents: 581a267
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Wed Jun 6 11:42:37 2018 +0200
Committer: benwa <btellier@linagora.com>
Committed: Tue Jun 12 07:00:46 2018 +0700

----------------------------------------------------------------------
 .../org/apache/james/webadmin/Constants.java    |  1 +
 .../apache/james/webadmin/WebAdminUtils.java    |  4 ++
 .../webadmin/routes/MailRepositoriesRoutes.java | 66 ++++++++++++++------
 .../routes/MailRepositoriesRoutesTest.java      | 52 +++++++++++++++
 4 files changed, 104 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/4d3cda34/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Constants.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Constants.java
b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Constants.java
index 70cfbbb..8c54f94 100644
--- a/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Constants.java
+++ b/server/protocols/webadmin/webadmin-core/src/main/java/org/apache/james/webadmin/Constants.java
@@ -24,5 +24,6 @@ public interface Constants {
     String SEPARATOR = "/";
     String EMPTY_BODY = "";
     String JSON_CONTENT_TYPE = "application/json";
+    String RFC822_CONTENT_TYPE = "message/rfc822";
 
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4d3cda34/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
index f4f95ff..7f294ad 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/WebAdminUtils.java
@@ -54,4 +54,8 @@ public class WebAdminUtils {
             .setPort(port.getValue());
     }
 
+    public static RequestSpecBuilder buildRequestSpecificationWithPortOnly(Port port) {
+        return new RequestSpecBuilder()
+            .setPort(port.getValue());
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/4d3cda34/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
index dfe1fcf..2600812 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/routes/MailRepositoriesRoutes.java
@@ -19,11 +19,13 @@
 
 package org.apache.james.webadmin.routes;
 
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Optional;
+import java.util.function.Supplier;
 
 import javax.inject.Inject;
 import javax.mail.MessagingException;
@@ -42,6 +44,7 @@ import org.apache.james.util.streams.Offset;
 import org.apache.james.webadmin.Constants;
 import org.apache.james.webadmin.Routes;
 import org.apache.james.webadmin.dto.ExtendedMailRepositoryResponse;
+import org.apache.james.webadmin.dto.MailDto;
 import org.apache.james.webadmin.dto.TaskIdDto;
 import org.apache.james.webadmin.service.MailRepositoryStoreService;
 import org.apache.james.webadmin.service.ReprocessingAllMailsTask;
@@ -60,6 +63,7 @@ import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 import io.swagger.jaxrs.PATCH;
+import spark.HaltException;
 import spark.Request;
 import spark.Service;
 
@@ -177,25 +181,49 @@ public class MailRepositoriesRoutes implements Routes {
         @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "Not found - Could not retrieve
the given mail.")
     })
     public void defineGetMail() {
-        service.get(MAIL_REPOSITORIES + "/:encodedUrl/mails/:mailKey", (request, response)
-> {
-            String url = decodedRepositoryUrl(request);
-            String mailKey = request.params("mailKey");
-            try {
-                return repositoryStoreService.retrieveMail(url, mailKey)
-                    .orElseThrow(() -> ErrorResponder.builder()
-                        .statusCode(HttpStatus.NOT_FOUND_404)
-                        .type(ErrorResponder.ErrorType.NOT_FOUND)
-                        .message("Could not retrieve " + mailKey)
-                        .haltError());
-            } catch (MailRepositoryStore.MailRepositoryStoreException | MessagingException
e) {
-                throw ErrorResponder.builder()
-                    .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500)
-                    .type(ErrorResponder.ErrorType.SERVER_ERROR)
-                    .cause(e)
-                    .message("Error while retrieving mail")
-                    .haltError();
-            }
-        }, jsonTransformer);
+
+        service.get(MAIL_REPOSITORIES + "/:encodedUrl/mails/:mailKey", Constants.JSON_CONTENT_TYPE,
+            (request, response) -> getMailAsJson(decodedRepositoryUrl(request), request.params("mailKey")),
jsonTransformer);
+
+        service.get(MAIL_REPOSITORIES + "/:encodedUrl/mails/:mailKey", Constants.RFC822_CONTENT_TYPE,
(request, response) -> {
+            response.type(Constants.RFC822_CONTENT_TYPE);
+            return getMailAsEml(decodedRepositoryUrl(request), request.params("mailKey"));
+        });
+    }
+
+    private InputStream getMailAsEml(String url, String mailKey) {
+        try {
+            return repositoryStoreService.downloadMail(url, mailKey)
+                .orElseThrow(mailNotFoundError(mailKey));
+        } catch (MailRepositoryStore.MailRepositoryStoreException | MessagingException e)
{
+            throw internalServerError(e);
+        }
+    }
+
+    private MailDto getMailAsJson(String url, String mailKey) {
+        try {
+            return repositoryStoreService.retrieveMail(url, mailKey)
+                .orElseThrow(mailNotFoundError(mailKey));
+        } catch (MailRepositoryStore.MailRepositoryStoreException | MessagingException e)
{
+            throw internalServerError(e);
+        }
+    }
+
+    private Supplier<HaltException> mailNotFoundError(String mailKey) {
+        return () -> ErrorResponder.builder()
+            .statusCode(HttpStatus.NOT_FOUND_404)
+            .type(ErrorResponder.ErrorType.NOT_FOUND)
+            .message("Could not retrieve " + mailKey)
+            .haltError();
+    }
+
+    private HaltException internalServerError(Exception e) {
+        return ErrorResponder.builder()
+            .statusCode(HttpStatus.INTERNAL_SERVER_ERROR_500)
+            .type(ErrorResponder.ErrorType.SERVER_ERROR)
+            .cause(e)
+            .message("Error while retrieving mail")
+            .haltError();
     }
 
     @GET

http://git-wip-us.apache.org/repos/asf/james-project/blob/4d3cda34/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
index f3945bb..308c9de 100644
--- a/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-mailrepository/src/test/java/org/apache/james/webadmin/routes/MailRepositoriesRoutesTest.java
@@ -35,9 +35,11 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 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.metrics.api.NoopMetricFactory;
@@ -46,6 +48,7 @@ import org.apache.james.queue.api.ManageableMailQueue;
 import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory;
 import org.apache.james.queue.memory.MemoryMailQueueFactory;
 import org.apache.james.task.MemoryTaskManager;
+import org.apache.james.webadmin.Constants;
 import org.apache.james.webadmin.WebAdminServer;
 import org.apache.james.webadmin.WebAdminUtils;
 import org.apache.james.webadmin.service.ClearMailRepositoryTask;
@@ -65,6 +68,7 @@ import org.junit.Test;
 import com.google.common.collect.ImmutableList;
 import com.jayway.restassured.RestAssured;
 import com.jayway.restassured.http.ContentType;
+import com.jayway.restassured.parsing.Parser;
 
 public class MailRepositoriesRoutesTest {
 
@@ -466,6 +470,54 @@ public class MailRepositoriesRoutesTest {
     }
 
     @Test
+    public void downloadingAMailShouldReturnTheEml() throws Exception {
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecificationWithPortOnly(webAdminServer.getPort())
+                .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES)
+                .build();
+        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.JSON);
+
+        when(mailRepositoryStore.get(URL_MY_REPO)).thenReturn(Optional.of(mailRepository));
+
+        String name = NAME_1;
+        FakeMail mail = FakeMail.builder()
+            .name(name)
+            .fileName("mail.eml")
+            .build();
+        mailRepository.store(mail);
+
+        String expectedContent = IOUtils.toString(mail.getMessage().getRawInputStream(),
StandardCharsets.UTF_8);
+        given()
+            .accept(Constants.RFC822_CONTENT_TYPE)
+        .when()
+            .get(URL_ESCAPED_MY_REPO + "/mails/" + name)
+        .then()
+            .statusCode(HttpStatus.OK_200)
+            .contentType(Constants.RFC822_CONTENT_TYPE)
+            .content(is(expectedContent));
+    }
+
+    @Test
+    public void downloadingAMailShouldFailWhenUnknown() throws Exception {
+        RestAssured.requestSpecification = WebAdminUtils.buildRequestSpecificationWithPortOnly(webAdminServer.getPort())
+                .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES)
+                .build();
+        RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.JSON);
+
+        when(mailRepositoryStore.get(URL_MY_REPO)).thenReturn(Optional.of(mailRepository));
+
+        String name = "name";
+        given()
+            .accept(Constants.RFC822_CONTENT_TYPE)
+        .when()
+            .get(URL_ESCAPED_MY_REPO + "/mails/" + name)
+        .then()
+            .statusCode(HttpStatus.NOT_FOUND_404)
+            .body("statusCode", is(404))
+            .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType()))
+            .body("message", is("Could not retrieve " + name));
+    }
+
+    @Test
     public void deletingAMailShouldRemoveIt() throws Exception {
         when(mailRepositoryStore.get(URL_MY_REPO)).thenReturn(Optional.of(mailRepository));
 


---------------------------------------------------------------------
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