james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [3/3] james-project git commit: JAMES-1900 Full support for header folding in ES indexing
Date Fri, 05 May 2017 01:59:14 GMT
JAMES-1900 Full support for header folding in ES indexing


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

Branch: refs/heads/master
Commit: 900fd9d36242e424dab9a29c295cac342f8f08ba
Parents: f34b6fe
Author: benwa <btellier@linagora.com>
Authored: Thu May 4 15:02:11 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Fri May 5 08:58:11 2017 +0700

----------------------------------------------------------------------
 .../elasticsearch/json/HeaderCollection.java    | 48 ++++++++++++--------
 .../json/HeaderCollectionTest.java              | 32 +++++++++++++
 2 files changed, 60 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/900fd9d3/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollection.java
----------------------------------------------------------------------
diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollection.java
b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollection.java
index 6125ebf..eef245c 100644
--- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollection.java
+++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollection.java
@@ -19,12 +19,17 @@
 
 package org.apache.james.mailbox.elasticsearch.json;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ImmutableMultimap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Multimap;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 import org.apache.james.mailbox.store.search.SearchUtil;
 import org.apache.james.mime4j.codec.DecodeMonitor;
 import org.apache.james.mime4j.codec.DecoderUtil;
@@ -37,19 +42,21 @@ import org.apache.james.mime4j.util.MimeUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.time.ZonedDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.Optional;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Multimap;
 
 public class HeaderCollection {
 
+    private static String sanitizeHeaderField(String headerName) {
+        return DecoderUtil.decodeEncodedWords(
+            MimeUtil.unfold(headerName),
+            DecodeMonitor.SILENT);
+    }
+
     public static class Builder {
 
         // Some sent e-mail have this form : Wed,  3 Jun 2015 09:05:46 +0000 (UTC)
@@ -83,9 +90,10 @@ public class HeaderCollection {
         public Builder add(Field field) {
             Preconditions.checkNotNull(field);
             String headerName = field.getName().toLowerCase(Locale.US);
-            String headerValue = field.getBody();
-            headers.put(headerName, DecoderUtil.decodeEncodedWords(headerValue, DecodeMonitor.SILENT));
-            handleSpecificHeader(headerName, headerValue);
+            String sanitizedValue = sanitizeHeaderField(field.getBody());
+
+            headers.put(headerName, sanitizedValue);
+            handleSpecificHeader(headerName, sanitizedValue);
             return this;
         }
 
@@ -121,7 +129,7 @@ public class HeaderCollection {
 
         private void manageAddressField(String headerName, String headerValue) {
             LenientAddressParser.DEFAULT
-                .parseAddressList(MimeUtil.unfold(headerValue))
+                .parseAddressList(headerValue)
                 .stream()
                 .flatMap(this::convertAddressToMailboxStream)
                 .map((mailbox) -> new EMailer(SearchUtil.getDisplayAddress(mailbox) ,
mailbox.getAddress()))

http://git-wip-us.apache.org/repos/asf/james-project/blob/900fd9d3/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollectionTest.java
----------------------------------------------------------------------
diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollectionTest.java
b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollectionTest.java
index 88efd00..d12cca8 100644
--- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollectionTest.java
+++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/json/HeaderCollectionTest.java
@@ -86,6 +86,16 @@ public class HeaderCollectionTest {
     }
 
     @Test
+    public void getHeadersShouldDecodeValues() {
+        HeaderCollection headerCollection = HeaderCollection.builder()
+            .add(new FieldImpl("From", "=?UTF-8?B?RnLDqWTDqXJpYyBNQVJUSU4=?= <fmartin@linagora.com>,
Graham CROSMARIE <gcrosmarie@linagora.com>"))
+            .build();
+
+        assertThat(headerCollection.getHeaders().get("from"))
+            .containsExactly("Frédéric MARTIN <fmartin@linagora.com>, Graham CROSMARIE
<gcrosmarie@linagora.com>");
+    }
+
+    @Test
     public void addressWithTwoDisplayNamesOnTheSameFieldShouldBeRetrieved() {
         HeaderCollection headerCollection = HeaderCollection.builder()
             .add(new FieldImpl("From", "Christophe Hamerling <chri.hamerling@linagora.com>,
Graham CROSMARIE <grah.crosmarie@linagora.com>"))
@@ -94,7 +104,29 @@ public class HeaderCollectionTest {
         assertThat(headerCollection.getFromAddressSet())
             .containsOnly(new EMailer("Christophe Hamerling", "chri.hamerling@linagora.com"),
                 new EMailer("Graham CROSMARIE", "grah.crosmarie@linagora.com"));
+    }
+
+    @Test
+    public void foldedFromHeaderShouldBeSupported() {
+        HeaderCollection headerCollection = HeaderCollection.builder()
+            .add(new FieldImpl("From", "Christophe Hamerling <chri.hamerling@linagora.com>,\r\n"
+
+                " Graham CROSMARIE <grah.crosmarie@linagora.com>"))
+            .build();
+
+        assertThat(headerCollection.getFromAddressSet())
+            .containsOnly(new EMailer("Christophe Hamerling", "chri.hamerling@linagora.com"),
+                new EMailer("Graham CROSMARIE", "grah.crosmarie@linagora.com"));
+    }
+
+    @Test
+    public void foldedHeaderShouldBeSupported() {
+        HeaderCollection headerCollection = HeaderCollection.builder()
+            .add(new FieldImpl("From", "Christophe Hamerling <chri.hamerling@linagora.com>,\r\n"
+
+                " Graham CROSMARIE <grah.crosmarie@linagora.com>"))
+            .build();
 
+        assertThat(headerCollection.getHeaders().get("from"))
+            .containsOnly("Christophe Hamerling <chri.hamerling@linagora.com>, Graham
CROSMARIE <grah.crosmarie@linagora.com>");
     }
 
     @Test


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