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 no...@apache.org on 2011/06/05 14:17:28 UTC

svn commit: r1132377 - in /james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator: AbstractHeaderComparator.java HeaderMailboxComparator.java SentDateComparator.java

Author: norman
Date: Sun Jun  5 12:17:27 2011
New Revision: 1132377

URL: http://svn.apache.org/viewvc?rev=1132377&view=rev
Log:
Share some more code for the comparators which are used for Search sorting. See MAILBOX-78

Added:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/AbstractHeaderComparator.java
Modified:
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java

Added: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/AbstractHeaderComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/AbstractHeaderComparator.java?rev=1132377&view=auto
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/AbstractHeaderComparator.java (added)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/AbstractHeaderComparator.java Sun Jun  5 12:17:27 2011
@@ -0,0 +1,42 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.james.mailbox.store.mail.model.Header;
+import org.apache.james.mailbox.store.mail.model.Message;
+
+
+public abstract class AbstractHeaderComparator implements Comparator<Message<?>>{
+
+    protected String getHeaderValue(String headerName, Message<?> message) {
+        final List<Header> headers = message.getHeaders();
+        for (Header header:headers) {
+            final String name = header.getFieldName();
+            if (headerName.equalsIgnoreCase(name)) {
+                final String value = header.getValue();
+                return value.toLowerCase(Locale.US);
+            }
+        }
+        return "";
+    }
+}

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java?rev=1132377&r1=1132376&r2=1132377&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/HeaderMailboxComparator.java Sun Jun  5 12:17:27 2011
@@ -1,9 +1,25 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
 package org.apache.james.mailbox.store.search.comparator;
 
 import java.util.Comparator;
-import java.util.List;
 
-import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mime4j.field.address.Address;
 import org.apache.james.mime4j.field.address.AddressList;
@@ -11,7 +27,7 @@ import org.apache.james.mime4j.field.add
 import org.apache.james.mime4j.field.address.Mailbox;
 import org.apache.james.mime4j.field.address.MailboxList;
 
-public class HeaderMailboxComparator implements Comparator<Message<?>>{
+public class HeaderMailboxComparator extends AbstractHeaderComparator{
 
     private final String headerName;
 
@@ -33,46 +49,39 @@ public class HeaderMailboxComparator imp
     
     @Override
     public int compare(Message<?> o1, Message<?> o2) {
-        String mailbox1 = getMailbox(o1, headerName);
-        String mailbox2 = getMailbox(o2, headerName);
+        String mailbox1 = getMailbox(headerName, o1);
+        String mailbox2 = getMailbox(headerName, o2);
 
         return mailbox1.compareTo(mailbox2);
     }
     
     
-    private String getMailbox(Message<?> message, String headerName) {
-        final List<Header> headers = message.getHeaders();
-        for (Header header:headers) {
-            final String name = header.getFieldName();
-            if (headerName.equalsIgnoreCase(name)) {
-                final String value = header.getValue();
-                try {
-                    AddressList aList = AddressList.parse(value);
-                    for (int i = 0; i < aList.size(); i++) {
-                        Address address = aList.get(i);
-                        if (address instanceof Mailbox) {
-                            Mailbox m = (Mailbox) address;
-                            String mailboxName = m.getName();
-                            if (mailboxName == null) {
-                                mailboxName ="";
-                            }
-                            return mailboxName;
-                        } else if (address instanceof Group) {
-                            MailboxList mList = ((Group) address).getMailboxes();
-                            for (int a = 0; a < mList.size(); ) {
-                                String mailboxName = mList.get(a).getName();
-                                if (mailboxName == null) {
-                                    mailboxName ="";
-                                }
-                                return mailboxName;                         
-                            }
+    private String getMailbox(String headerName, Message<?> message) {
+        try {
+            AddressList aList = AddressList.parse(getHeaderValue(headerName, message));
+            for (int i = 0; i < aList.size(); i++) {
+                Address address = aList.get(i);
+                if (address instanceof Mailbox) {
+                    Mailbox m = (Mailbox) address;
+                    String mailboxName = m.getName();
+                    if (mailboxName == null) {
+                        mailboxName ="";
+                    }
+                    return mailboxName;
+                } else if (address instanceof Group) {
+                    MailboxList mList = ((Group) address).getMailboxes();
+                    for (int a = 0; a < mList.size(); ) {
+                        String mailboxName = mList.get(a).getName();
+                        if (mailboxName == null) {
+                            mailboxName ="";
                         }
+                        return mailboxName;                         
                     }
-
-                } catch (org.apache.james.mime4j.field.address.parser.ParseException e) {
-                    return "";
                 }
             }
+
+        } catch (org.apache.james.mime4j.field.address.parser.ParseException e) {
+            return "";
         }
         return "";
     }

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java?rev=1132377&r1=1132376&r2=1132377&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java (original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/SentDateComparator.java Sun Jun  5 12:17:27 2011
@@ -21,9 +21,7 @@ package org.apache.james.mailbox.store.s
 import java.io.StringReader;
 import java.util.Comparator;
 import java.util.Date;
-import java.util.List;
 
-import org.apache.james.mailbox.store.mail.model.Header;
 import org.apache.james.mailbox.store.mail.model.Message;
 import org.apache.james.mime4j.field.datetime.DateTime;
 import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
@@ -33,7 +31,7 @@ import org.apache.james.mime4j.field.dat
  * {@link Comparator} which works like stated in RFC5256 2.2 Sent Date
  *
  */
-public class SentDateComparator implements Comparator<Message<?>>{
+public class SentDateComparator extends AbstractHeaderComparator {
 
 
 
@@ -60,23 +58,15 @@ public class SentDateComparator implemen
     }
     
     private Date getSentDate(Message<?> message) {
-        final List<Header> headers = message.getHeaders();
-        for (Header header:headers) {
-            final String name = header.getFieldName();
-            if ("Date".equalsIgnoreCase(name)) {
-                final String value = header.getValue();
-                    final StringReader reader = new StringReader(value);
-                    try {
-                        DateTime dateTime = new DateTimeParser(reader).parseAll();
-                        return dateTime.getDate();
-                    } catch (ParseException e) {
-                        // if we can not parse the date header we should break here and use the internaldate as fallback
-                        break;
-                    }
-                
-            }
+        final String value = getHeaderValue("Date", message);
+        final StringReader reader = new StringReader(value);
+        try {
+            DateTime dateTime = new DateTimeParser(reader).parseAll();
+            return dateTime.getDate();
+        } catch (ParseException e) {
+            // if we can not parse the date header we should use the internaldate as fallback
+            return message.getInternalDate();
         }
-        return message.getInternalDate();
     }
     
     public static Comparator<Message<?>> sentDate(boolean reverse){



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