{"action":"created","comment":{"url":"https://api.github.com/repos/linagora/james-project/pulls/comments/194023491","pull_request_review_id":127118244,"id":194023491,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5NDAyMzQ5MQ==","diff_hunk":"@@ -19,39 +19,208 @@\n \n package org.apache.james.webadmin.dto;\n \n+import java.io.ByteArrayInputStream;\n+import java.io.ByteArrayOutputStream;\n+import java.io.IOException;\n+import java.util.Arrays;\n+import java.util.Collections;\n+import java.util.Date;\n+import java.util.HashMap;\n import java.util.List;\n+import java.util.Map;\n import java.util.Objects;\n import java.util.Optional;\n+import java.util.function.Function;\n \n import javax.mail.MessagingException;\n+import javax.mail.internet.MimeMessage;\n \n import org.apache.james.core.MailAddress;\n+import org.apache.james.mime4j.dom.Message;\n+import org.apache.james.mime4j.stream.MimeConfig;\n+import org.apache.james.util.mime.MessageContentExtractor;\n+import org.apache.james.util.mime.MessageContentExtractor.MessageContent;\n+import org.apache.james.util.streams.Iterators;\n import org.apache.mailet.Mail;\n+import org.apache.mailet.PerRecipientHeaders;\n \n+import com.github.fge.lambdas.Throwing;\n import com.github.steveash.guavate.Guavate;\n+import com.google.common.collect.Multimap;\n \n public class MailDto {\n-    public static MailDto fromMail(Mail mail) throws MessagingException {\n+    public static MailDto fromMail(Mail mail, List<AdditionalField> additionalFields) throws MessagingException, InnaccessibleFieldException {\n         return new MailDto(mail.getName(),\n             Optional.ofNullable(mail.getSender()).map(MailAddress::asString),\n             mail.getRecipients().stream().map(MailAddress::asString).collect(Guavate.toImmutableList()),\n             Optional.ofNullable(mail.getErrorMessage()),\n-            Optional.ofNullable(mail.getState()));\n+            Optional.ofNullable(mail.getState()),\n+            Optional.ofNullable(mail.getRemoteHost()),\n+            Optional.ofNullable(mail.getRemoteAddr()),\n+            Optional.ofNullable(mail.getLastUpdated()),\n+            fetchAttributes(additionalFields, mail),\n+            fetchPerRecipientsHeaders(additionalFields, mail),\n+            fetchHeaders(additionalFields, mail),\n+            fetchBody(additionalFields, mail),\n+            fetchMessageSize(additionalFields, mail));\n+    }\n+\n+    private static Optional<Long> fetchMessageSize(List<AdditionalField> additionalFields, Mail mail) throws InnaccessibleFieldException {\n+        if (!additionalFields.contains(AdditionalField.MESSAGE_SIZE)) {\n+            return Optional.empty();\n+        }\n+        try {\n+            return Optional.of(new Long(mail.getMessageSize()));\n+        } catch (MessagingException e) {\n+            throw new InnaccessibleFieldException(AdditionalField.MESSAGE_SIZE, e);\n+        }\n+    }\n+\n+    private static Optional<String> fetchBody(List<AdditionalField> additionalFields, Mail mail) throws InnaccessibleFieldException {\n+        if (!additionalFields.contains(AdditionalField.BODY)) {\n+            return Optional.empty();\n+        }\n+\n+        try {\n+            MessageContentExtractor extractor = new MessageContentExtractor();\n+            return Optional.ofNullable(mail.getMessage())\n+                    .map(Throwing.function(MailDto::convertMessage).sneakyThrow())\n+                    .map(Throwing.function((Message message) -> extractor.extract(message)).sneakyThrow())\n+                    .flatMap(extractBody());\n+        } catch (MessagingException e) {\n+            throw new InnaccessibleFieldException(AdditionalField.BODY, e);\n+        }\n+    }\n+\n+    private static Function<? super MessageContent, Optional<String>> extractBody() {\n+        return (content) -> {\n+            Optional<String> body = content.getTextBody();\n+            if (body.isPresent()) {\n+                return body;\n+            } else {\n+                return content.getHtmlBody();\n+            }\n+        };\n+    }\n+\n+    private static Message convertMessage(MimeMessage message) throws IOException, MessagingException {\n+        ByteArrayOutputStream rawMessage = new ByteArrayOutputStream();\n+        message.writeTo(rawMessage);\n+        return Message.Builder\n+                .of()\n+                .use(MimeConfig.PERMISSIVE)\n+                .parse(new ByteArrayInputStream(rawMessage.toByteArray()))\n+                .build();\n+    }\n+\n+    private static Optional<HeadersDto> fetchHeaders(List<AdditionalField> additionalFields, Mail mail) throws InnaccessibleFieldException {\n+        if (!additionalFields.contains(AdditionalField.HEADERS)) {\n+            return Optional.empty();\n+        }\n+\n+        try {\n+            return Optional.ofNullable(mail.getMessage())\n+                    .map(Throwing.function((MimeMessage message) -> {\n+                        HeadersDto headers = new HeadersDto();\n+                        Collections\n+                            .list(message.getAllHeaders())\n+                            .forEach((header) -> headers.add(header.getName(), header.getValue()));","path":"server/protocols/webadmin/webadmin-mailrepository/src/main/java/org/apache/james/webadmin/dto/MailDto.java","position":111,"original_position":111,"commit_id":"eea60675009352d74fcd75a2296c769b6d8413ee","original_commit_id":"eea60675009352d74fcd75a2296c769b6d8413ee","user":{"login":"chibenwa","id":6928740,"node_id":"MDQ6VXNlcjY5Mjg3NDA=","avatar_url":"https://avatars1.githubusercontent.com/u/6928740?v=4","gravatar_id":"","url":"https://api.github.com/users/chibenwa","html_url":"https://github.com/chibenwa","followers_url":"https://api.github.com/users/chibenwa/followers","following_url":"https://api.github.com/users/chibenwa/following{/other_user}","gists_url":"https://api.github.com/users/chibenwa/gists{/gist_id}","starred_url":"https://api.github.com/users/chibenwa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chibenwa/subscriptions","organizations_url":"https://api.github.com/users/chibenwa/orgs","repos_url":"https://api.github.com/users/chibenwa/repos","events_url":"https://api.github.com/users/chibenwa/events{/privacy}","received_events_url":"https://api.github.com/users/chibenwa/received_events","type":"User","site_admin":false},"body":"Still some mutability around? Please do use `Steam::collect` with the `Guavate.toImmutableListMultimap()`, you gonna love it!","created_at":"2018-06-08T11:01:49Z","updated_at":"2018-06-08T11:09:34Z","html_url":"https://github.com/linagora/james-project/pull/1466#discussion_r194023491","pull_request_url":"https://api.github.com/repos/linagora/james-project/pulls/1466","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/linagora/james-project/pulls/comments/194023491"},"html":{"href":"https://github.com/linagora/james-project/pull/1466#discussion_r194023491"},"pull_request":{"href":"https://api.github.com/repos/linagora/james-project/pulls/1466"}}},"pull_request":{"url":"https://api.github.com/repos/linagora/james-project/pulls/1466","id":193579404,"node_id":"MDExOlB1bGxSZXF1ZXN0MTkzNTc5NDA0","html_url":"https://github.com/linagora/james-project/pull/1466","diff_url":"https://github.com/linagora/james-project/pull/1466.diff","patch_url":"https://github.com/linagora/james-project/pull/1466.patch","issue_url":"https://api.github.com/repos/linagora/james-project/issues/1466","number":1466,"state":"open","locked":false,"title":"JAMES-2321 WebAdmin should allow to see more details about Mails","user":{"login":"blackheaven","id":1362807,"node_id":"MDQ6VXNlcjEzNjI4MDc=","avatar_url":"https://avatars0.githubusercontent.com/u/1362807?v=4","gravatar_id":"","url":"https://api.github.com/users/blackheaven","html_url":"https://github.com/blackheaven","followers_url":"https://api.github.com/users/blackheaven/followers","following_url":"https://api.github.com/users/blackheaven/following{/other_user}","gists_url":"https://api.github.com/users/blackheaven/gists{/gist_id}","starred_url":"https://api.github.com/users/blackheaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blackheaven/subscriptions","organizations_url":"https://api.github.com/users/blackheaven/orgs","repos_url":"https://api.github.com/users/blackheaven/repos","events_url":"https://api.github.com/users/blackheaven/events{/privacy}","received_events_url":"https://api.github.com/users/blackheaven/received_events","type":"User","site_admin":false},"body":"Continuation of #1454 (for CI)","created_at":"2018-06-08T10:57:34Z","updated_at":"2018-06-08T11:09:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e899bd0fe589e03a3bc2ec463312d3b6d81b7e3d","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/linagora/james-project/pulls/1466/commits","review_comments_url":"https://api.github.com/repos/linagora/james-project/pulls/1466/comments","review_comment_url":"https://api.github.com/repos/linagora/james-project/pulls/comments{/number}","comments_url":"https://api.github.com/repos/linagora/james-project/issues/1466/comments","statuses_url":"https://api.github.com/repos/linagora/james-project/statuses/eea60675009352d74fcd75a2296c769b6d8413ee","head":{"label":"blackheaven:JAMES-2321","ref":"JAMES-2321","sha":"eea60675009352d74fcd75a2296c769b6d8413ee","user":{"login":"blackheaven","id":1362807,"node_id":"MDQ6VXNlcjEzNjI4MDc=","avatar_url":"https://avatars0.githubusercontent.com/u/1362807?v=4","gravatar_id":"","url":"https://api.github.com/users/blackheaven","html_url":"https://github.com/blackheaven","followers_url":"https://api.github.com/users/blackheaven/followers","following_url":"https://api.github.com/users/blackheaven/following{/other_user}","gists_url":"https://api.github.com/users/blackheaven/gists{/gist_id}","starred_url":"https://api.github.com/users/blackheaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blackheaven/subscriptions","organizations_url":"https://api.github.com/users/blackheaven/orgs","repos_url":"https://api.github.com/users/blackheaven/repos","events_url":"https://api.github.com/users/blackheaven/events{/privacy}","received_events_url":"https://api.github.com/users/blackheaven/received_events","type":"User","site_admin":false},"repo":{"id":136178916,"node_id":"MDEwOlJlcG9zaXRvcnkxMzYxNzg5MTY=","name":"james-project","full_name":"blackheaven/james-project","owner":{"login":"blackheaven","id":1362807,"node_id":"MDQ6VXNlcjEzNjI4MDc=","avatar_url":"https://avatars0.githubusercontent.com/u/1362807?v=4","gravatar_id":"","url":"https://api.github.com/users/blackheaven","html_url":"https://github.com/blackheaven","followers_url":"https://api.github.com/users/blackheaven/followers","following_url":"https://api.github.com/users/blackheaven/following{/other_user}","gists_url":"https://api.github.com/users/blackheaven/gists{/gist_id}","starred_url":"https://api.github.com/users/blackheaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blackheaven/subscriptions","organizations_url":"https://api.github.com/users/blackheaven/orgs","repos_url":"https://api.github.com/users/blackheaven/repos","events_url":"https://api.github.com/users/blackheaven/events{/privacy}","received_events_url":"https://api.github.com/users/blackheaven/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/blackheaven/james-project","description":"Mirror of Apache James Project","fork":true,"url":"https://api.github.com/repos/blackheaven/james-project","forks_url":"https://api.github.com/repos/blackheaven/james-project/forks","keys_url":"https://api.github.com/repos/blackheaven/james-project/keys{/key_id}","collaborators_url":"https://api.github.com/repos/blackheaven/james-project/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/blackheaven/james-project/teams","hooks_url":"https://api.github.com/repos/blackheaven/james-project/hooks","issue_events_url":"https://api.github.com/repos/blackheaven/james-project/issues/events{/number}","events_url":"https://api.github.com/repos/blackheaven/james-project/events","assignees_url":"https://api.github.com/repos/blackheaven/james-project/assignees{/user}","branches_url":"https://api.github.com/repos/blackheaven/james-project/branches{/branch}","tags_url":"https://api.github.com/repos/blackheaven/james-project/tags","blobs_url":"https://api.github.com/repos/blackheaven/james-project/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/blackheaven/james-project/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/blackheaven/james-project/git/refs{/sha}","trees_url":"https://api.github.com/repos/blackheaven/james-project/git/trees{/sha}","statuses_url":"https://api.github.com/repos/blackheaven/james-project/statuses/{sha}","languages_url":"https://api.github.com/repos/blackheaven/james-project/languages","stargazers_url":"https://api.github.com/repos/blackheaven/james-project/stargazers","contributors_url":"https://api.github.com/repos/blackheaven/james-project/contributors","subscribers_url":"https://api.github.com/repos/blackheaven/james-project/subscribers","subscription_url":"https://api.github.com/repos/blackheaven/james-project/subscription","commits_url":"https://api.github.com/repos/blackheaven/james-project/commits{/sha}","git_commits_url":"https://api.github.com/repos/blackheaven/james-project/git/commits{/sha}","comments_url":"https://api.github.com/repos/blackheaven/james-project/comments{/number}","issue_comment_url":"https://api.github.com/repos/blackheaven/james-project/issues/comments{/number}","contents_url":"https://api.github.com/repos/blackheaven/james-project/contents/{+path}","compare_url":"https://api.github.com/repos/blackheaven/james-project/compare/{base}...{head}","merges_url":"https://api.github.com/repos/blackheaven/james-project/merges","archive_url":"https://api.github.com/repos/blackheaven/james-project/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/blackheaven/james-project/downloads","issues_url":"https://api.github.com/repos/blackheaven/james-project/issues{/number}","pulls_url":"https://api.github.com/repos/blackheaven/james-project/pulls{/number}","milestones_url":"https://api.github.com/repos/blackheaven/james-project/milestones{/number}","notifications_url":"https://api.github.com/repos/blackheaven/james-project/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/blackheaven/james-project/labels{/name}","releases_url":"https://api.github.com/repos/blackheaven/james-project/releases{/id}","deployments_url":"https://api.github.com/repos/blackheaven/james-project/deployments","created_at":"2018-06-05T13:07:42Z","updated_at":"2018-06-05T13:07:58Z","pushed_at":"2018-06-08T10:59:37Z","git_url":"git://github.com/blackheaven/james-project.git","ssh_url":"git@github.com:blackheaven/james-project.git","clone_url":"https://github.com/blackheaven/james-project.git","svn_url":"https://github.com/blackheaven/james-project","homepage":null,"size":33191,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"linagora:master","ref":"master","sha":"65c2bb77460e8781dcc4ca967bf9d69946c159d2","user":{"login":"linagora","id":1230365,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEyMzAzNjU=","avatar_url":"https://avatars3.githubusercontent.com/u/1230365?v=4","gravatar_id":"","url":"https://api.github.com/users/linagora","html_url":"https://github.com/linagora","followers_url":"https://api.github.com/users/linagora/followers","following_url":"https://api.github.com/users/linagora/following{/other_user}","gists_url":"https://api.github.com/users/linagora/gists{/gist_id}","starred_url":"https://api.github.com/users/linagora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/linagora/subscriptions","organizations_url":"https://api.github.com/users/linagora/orgs","repos_url":"https://api.github.com/users/linagora/repos","events_url":"https://api.github.com/users/linagora/events{/privacy}","received_events_url":"https://api.github.com/users/linagora/received_events","type":"Organization","site_admin":false},"repo":{"id":39182136,"node_id":"MDEwOlJlcG9zaXRvcnkzOTE4MjEzNg==","name":"james-project","full_name":"linagora/james-project","owner":{"login":"linagora","id":1230365,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEyMzAzNjU=","avatar_url":"https://avatars3.githubusercontent.com/u/1230365?v=4","gravatar_id":"","url":"https://api.github.com/users/linagora","html_url":"https://github.com/linagora","followers_url":"https://api.github.com/users/linagora/followers","following_url":"https://api.github.com/users/linagora/following{/other_user}","gists_url":"https://api.github.com/users/linagora/gists{/gist_id}","starred_url":"https://api.github.com/users/linagora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/linagora/subscriptions","organizations_url":"https://api.github.com/users/linagora/orgs","repos_url":"https://api.github.com/users/linagora/repos","events_url":"https://api.github.com/users/linagora/events{/privacy}","received_events_url":"https://api.github.com/users/linagora/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/linagora/james-project","description":"Mirror of Apache James Project","fork":true,"url":"https://api.github.com/repos/linagora/james-project","forks_url":"https://api.github.com/repos/linagora/james-project/forks","keys_url":"https://api.github.com/repos/linagora/james-project/keys{/key_id}","collaborators_url":"https://api.github.com/repos/linagora/james-project/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/linagora/james-project/teams","hooks_url":"https://api.github.com/repos/linagora/james-project/hooks","issue_events_url":"https://api.github.com/repos/linagora/james-project/issues/events{/number}","events_url":"https://api.github.com/repos/linagora/james-project/events","assignees_url":"https://api.github.com/repos/linagora/james-project/assignees{/user}","branches_url":"https://api.github.com/repos/linagora/james-project/branches{/branch}","tags_url":"https://api.github.com/repos/linagora/james-project/tags","blobs_url":"https://api.github.com/repos/linagora/james-project/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/linagora/james-project/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/linagora/james-project/git/refs{/sha}","trees_url":"https://api.github.com/repos/linagora/james-project/git/trees{/sha}","statuses_url":"https://api.github.com/repos/linagora/james-project/statuses/{sha}","languages_url":"https://api.github.com/repos/linagora/james-project/languages","stargazers_url":"https://api.github.com/repos/linagora/james-project/stargazers","contributors_url":"https://api.github.com/repos/linagora/james-project/contributors","subscribers_url":"https://api.github.com/repos/linagora/james-project/subscribers","subscription_url":"https://api.github.com/repos/linagora/james-project/subscription","commits_url":"https://api.github.com/repos/linagora/james-project/commits{/sha}","git_commits_url":"https://api.github.com/repos/linagora/james-project/git/commits{/sha}","comments_url":"https://api.github.com/repos/linagora/james-project/comments{/number}","issue_comment_url":"https://api.github.com/repos/linagora/james-project/issues/comments{/number}","contents_url":"https://api.github.com/repos/linagora/james-project/contents/{+path}","compare_url":"https://api.github.com/repos/linagora/james-project/compare/{base}...{head}","merges_url":"https://api.github.com/repos/linagora/james-project/merges","archive_url":"https://api.github.com/repos/linagora/james-project/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/linagora/james-project/downloads","issues_url":"https://api.github.com/repos/linagora/james-project/issues{/number}","pulls_url":"https://api.github.com/repos/linagora/james-project/pulls{/number}","milestones_url":"https://api.github.com/repos/linagora/james-project/milestones{/number}","notifications_url":"https://api.github.com/repos/linagora/james-project/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/linagora/james-project/labels{/name}","releases_url":"https://api.github.com/repos/linagora/james-project/releases{/id}","deployments_url":"https://api.github.com/repos/linagora/james-project/deployments","created_at":"2015-07-16T07:04:59Z","updated_at":"2018-06-08T03:06:20Z","pushed_at":"2018-06-08T10:58:44Z","git_url":"git://github.com/linagora/james-project.git","ssh_url":"git@github.com:linagora/james-project.git","clone_url":"https://github.com/linagora/james-project.git","svn_url":"https://github.com/linagora/james-project","homepage":null,"size":33213,"stargazers_count":21,"watchers_count":21,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":25,"mirror_url":null,"archived":false,"open_issues_count":66,"license":null,"forks":25,"open_issues":66,"watchers":21,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/linagora/james-project/pulls/1466"},"html":{"href":"https://github.com/linagora/james-project/pull/1466"},"issue":{"href":"https://api.github.com/repos/linagora/james-project/issues/1466"},"comments":{"href":"https://api.github.com/repos/linagora/james-project/issues/1466/comments"},"review_comments":{"href":"https://api.github.com/repos/linagora/james-project/pulls/1466/comments"},"review_comment":{"href":"https://api.github.com/repos/linagora/james-project/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/linagora/james-project/pulls/1466/commits"},"statuses":{"href":"https://api.github.com/repos/linagora/james-project/statuses/eea60675009352d74fcd75a2296c769b6d8413ee"}},"author_association":"NONE"}}
{"actor":{"display_login":"chibenwa"}}