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 bt...@apache.org on 2019/05/15 07:42:11 UTC

[james-project] 05/05: JAMES-2756 MessageSearcher should decode value before searching

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit f31eedcbcf26a5e0089a421f405829540907f93a
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Fri May 10 18:10:18 2019 +0700

    JAMES-2756 MessageSearcher should decode value before searching
---
 .../store/search/SimpleMessageSearchIndexTest.java |  5 --
 .../mailbox/store/search/MessageSearches.java      | 53 ++++++++++++++--------
 2 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndexTest.java b/mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndexTest.java
index c383e35..48ffb16 100644
--- a/mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndexTest.java
+++ b/mailbox/scanning-search/src/test/java/org/apache/james/mailbox/store/search/SimpleMessageSearchIndexTest.java
@@ -238,9 +238,4 @@ public class SimpleMessageSearchIndexTest extends AbstractMessageSearchIndexTest
     @Override
     public void searchWithTextShouldReturnMailsWhenHtmlBodyMatchesWithStemming() throws Exception {
     }
-
-    @Ignore("JAMES-2756 SimpleMessageSearchIndex doesn't decode header values before searching")
-    @Override
-    public void addressShouldReturnTheRightUidOfTheMessageContainingUTF8EncodingToHeaderName() {
-    }
 }
diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
index 6f98523..5ff778a 100644
--- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
+++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/search/MessageSearches.java
@@ -421,37 +421,54 @@ public class MessageSearches implements Iterable<SimpleMessageSearchIndex.Search
      */
     private boolean matchesAddress(SearchQuery.AddressOperator operator, String headerName,
                                    MailboxMessage message) throws MailboxException, IOException {
-        String text = operator.getAddress().toUpperCase(Locale.US);
+        String text = operator.getAddress();
         List<Header> headers = ResultUtils.createHeaders(message);
         for (Header header : headers) {
             String name = header.getName();
             if (headerName.equalsIgnoreCase(name)) {
                 String value = header.getValue();
-                AddressList aList = LenientAddressParser.DEFAULT.parseAddressList(value);
-                for (Address address : aList) {
-                    if (address instanceof Mailbox) {
-                        if (AddressFormatter.DEFAULT.encode((Mailbox) address).toUpperCase(Locale.US)
-                            .contains(text)) {
-                            return true;
-                        }
-                    } else if (address instanceof Group) {
-                        MailboxList mList = ((Group) address).getMailboxes();
-                        for (Mailbox mailbox : mList) {
-                            if (AddressFormatter.DEFAULT.encode(mailbox).toUpperCase(Locale.US)
-                                .contains(text)) {
-                                return true;
-                            }
-                        }
-                    }
+                AddressList addressList = LenientAddressParser.DEFAULT.parseAddressList(value);
+                if (matchesAddress(addressList, text)) {
+                    return true;
                 }
 
                 // Also try to match against raw header now
-                return value.toUpperCase(Locale.US).contains(text);
+                return value.toUpperCase(Locale.US).contains(text.toUpperCase(Locale.US));
             }
         }
         return false;
     }
 
+    private boolean matchesAddress(AddressList addressList, String valueToMatch) {
+        for (Address address : addressList) {
+            if (address instanceof Mailbox) {
+                if (doesMailboxContains((Mailbox) address, valueToMatch)) {
+                    return true;
+                }
+            } else if (address instanceof Group) {
+                MailboxList mList = ((Group) address).getMailboxes();
+                for (Mailbox mailbox : mList) {
+                    if (doesMailboxContains(mailbox, valueToMatch)) {
+                        return true;
+                    }
+                }
+            }
+        }
+
+        return false;
+    }
+
+    private boolean doesMailboxContains(Mailbox mailbox, String searchedText) {
+        String mailboxAsString = encodeAndUnscramble(mailbox);
+        return mailboxAsString.toUpperCase(Locale.US)
+            .contains(searchedText.toUpperCase(Locale.US));
+    }
+
+    private String encodeAndUnscramble(Mailbox mailbox) {
+        return MimeUtil.unscrambleHeaderValue(
+            AddressFormatter.DEFAULT.encode(mailbox));
+    }
+
     private boolean exists(String headerName, MailboxMessage message) throws MailboxException, IOException {
         List<Header> headers = ResultUtils.createHeaders(message);
 


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