Author: norman
Date: Sun Sep 20 16:14:15 2009
New Revision: 817049

URL: http://svn.apache.org/viewvc?rev=817049&view=rev
Log:
Start to implement marking messages as SEEN/NOT SEEN (LABS-462)

Added:
    
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/JavamailUtil.java
    
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/SetFlagsHandler.java
    labs/hupa/trunk/shared/src/main/java/org/apache/hupa/shared/rpc/SetFlag.java
Modified:
    
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/ServerModul.java
    
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/AbstractFetchMessagesHandler.java

Modified: 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/ServerModul.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/ServerModul.java?rev=817049&r1=817048&r2=817049&view=diff
==============================================================================
--- 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/ServerModul.java
 (original)
+++ 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/guice/ServerModul.java
 Sun Sep 20 16:14:15 2009
@@ -49,6 +49,7 @@
 import org.apache.hupa.server.handler.RenameFolderHandler;
 import org.apache.hupa.server.handler.ReplyMessageHandler;
 import org.apache.hupa.server.handler.SendMessageHandler;
+import org.apache.hupa.server.handler.SetFlagsHandler;
 import org.apache.hupa.server.handler.TagMessagesHandler;
 import org.apache.hupa.server.servlet.DownloadAttachmentServlet;
 import org.apache.hupa.server.servlet.UploadAttachmentServlet;
@@ -98,6 +99,7 @@
                bindHandler(CreateFolderHandler.class);
                bindHandler(TagMessagesHandler.class);
                bindHandler(GetRawMessageHandler.class);
+               bindHandler(SetFlagsHandler.class);
                bind(FileItemRegistry.class).in(Singleton.class);
                bind(IMAPStoreCache.class).to(InMemoryIMAPStoreCache.class).in(
                                Singleton.class);

Modified: 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/AbstractFetchMessagesHandler.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/AbstractFetchMessagesHandler.java?rev=817049&r1=817048&r2=817049&view=diff
==============================================================================
--- 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/AbstractFetchMessagesHandler.java
 (original)
+++ 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/AbstractFetchMessagesHandler.java
 Sun Sep 20 16:14:15 2009
@@ -1,3 +1,22 @@
+/****************************************************************
+ * 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.hupa.server.handler;
 
 import java.io.IOException;
@@ -6,12 +25,10 @@
 
 import javax.mail.Address;
 import javax.mail.FetchProfile;
-import javax.mail.Flags;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Multipart;
 import javax.mail.Part;
-import javax.mail.Flags.Flag;
 import javax.mail.internet.MimeUtility;
 import javax.mail.internet.MimeMessage.RecipientType;
 import javax.servlet.http.HttpSession;
@@ -25,8 +42,8 @@
 import org.apache.hupa.shared.data.Tag;
 import org.apache.hupa.shared.data.User;
 import org.apache.hupa.shared.data.Message.IMAPFlag;
-import org.apache.hupa.shared.rpc.FetchMessagesResult;
 import org.apache.hupa.shared.rpc.FetchMessages;
+import org.apache.hupa.shared.rpc.FetchMessagesResult;
 
 import com.google.inject.Provider;
 import com.sun.mail.imap.IMAPStore;
@@ -135,25 +152,11 @@
                        msg.setReceivedDate(m.getReceivedDate());
 
                        // Add flags
-                       ArrayList<IMAPFlag> iFlags = new ArrayList<IMAPFlag>();
-                       Flags flags = m.getFlags();
-                       Flag[] systemFlags = flags.getSystemFlags();
-                       for (int a = 0; a < systemFlags.length;a++) {
-                               Flag flag = systemFlags[a];
-                               if (flag == Flag.DELETED) {
-                                       iFlags.add(IMAPFlag.DELETED);
-                               }
-                               if (flag == Flag.SEEN) {
-                                       iFlags.add(IMAPFlag.SEEN);
-                               }
-                               if (flag == Flag.RECENT) {
-                                       iFlags.add(IMAPFlag.RECENT);
-
-                               }
-                       }
+                       ArrayList<IMAPFlag> iFlags = 
JavamailUtil.convert(m.getFlags());
+                       
                  
                        ArrayList<Tag> tags = new ArrayList<Tag>();
-                       String[] userFlags = flags.getUserFlags();
+                       String[] userFlags = m.getFlags().getUserFlags();
                        for (int a = 0; a < userFlags.length;a++) {
                                String flag = userFlags[a];
                                if (flag.startsWith(Tag.PREFIX)) {

Added: 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/JavamailUtil.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/JavamailUtil.java?rev=817049&view=auto
==============================================================================
--- 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/JavamailUtil.java
 (added)
+++ 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/JavamailUtil.java
 Sun Sep 20 16:14:15 2009
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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.hupa.server.handler;
+
+import java.util.ArrayList;
+
+import javax.mail.Flags;
+import javax.mail.Flags.Flag;
+
+import org.apache.hupa.shared.data.Message.IMAPFlag;
+
+/**
+ * Util class which helps to convert from hupa internal data representation to 
javamaill classes
+ *
+ */
+public class JavamailUtil {
+       
+       /**
+        * Convert the given Flags to a ArrayList of IMAPFlags
+        * 
+        * @param flags
+        * @return imapFlags
+        */
+       public static ArrayList<IMAPFlag> convert(Flags flags) {
+               ArrayList<IMAPFlag> fList = new ArrayList<IMAPFlag>();
+               Flag[] flagArray = flags.getSystemFlags();
+               for (int i = 0; i < flagArray.length; i++) {
+                       Flag flag = flagArray[i];
+                       if (flag.equals(Flag.SEEN)) {
+                               fList.add(IMAPFlag.SEEN);
+                       } else if (flag.equals(Flag.RECENT)) {
+                               fList.add(IMAPFlag.RECENT);
+                       } else if (flag.equals(Flag.ANSWERED)) {
+                               fList.add(IMAPFlag.ANSWERED);
+                       } else if (flag.equals(Flag.DELETED)) {
+                               fList.add(IMAPFlag.DELETED);
+                       }
+               }
+               return fList;
+               
+       }
+       
+       /**
+        * Convert the given ArrayList of IMAPFlags to a Flags object
+        * 
+        * @param imapFlags
+        * @return flags
+        */
+       public static Flags convert(ArrayList<IMAPFlag> imapFlags) {
+               Flags iFlags = new Flags();
+               for ( int i = 0; i< imapFlags.size(); i++) {
+                       Flag f = null;
+                       IMAPFlag flag = imapFlags.get(i);
+                       if (flag.equals(IMAPFlag.SEEN)) {
+                               f = Flag.SEEN;
+                       } else if (flag.equals(IMAPFlag.RECENT)) {
+                               f = Flag.RECENT;
+                       } else if (flag.equals(IMAPFlag.ANSWERED)) {
+                               f = Flag.ANSWERED;
+                       } else if (flag.equals(IMAPFlag.DELETED)) {
+                               f = Flag.DELETED;
+                       }
+                       if (f != null) {
+                               iFlags.add(f);
+                       }
+               }
+               return iFlags;
+       }
+
+}

Added: 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/SetFlagsHandler.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/SetFlagsHandler.java?rev=817049&view=auto
==============================================================================
--- 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/SetFlagsHandler.java
 (added)
+++ 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/SetFlagsHandler.java
 Sun Sep 20 16:14:15 2009
@@ -0,0 +1,89 @@
+/****************************************************************
+ * 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.hupa.server.handler;
+
+import java.util.ArrayList;
+
+import javax.mail.Flags;
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.servlet.http.HttpSession;
+
+import net.customware.gwt.dispatch.server.ExecutionContext;
+import net.customware.gwt.dispatch.shared.ActionException;
+
+import org.apache.commons.logging.Log;
+import org.apache.hupa.server.IMAPStoreCache;
+import org.apache.hupa.shared.data.IMAPFolder;
+import org.apache.hupa.shared.data.User;
+import org.apache.hupa.shared.rpc.EmptyResult;
+import org.apache.hupa.shared.rpc.SetFlag;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.sun.mail.imap.IMAPStore;
+
+public class SetFlagsHandler extends AbstractSessionHandler<SetFlag, 
EmptyResult>{
+
+       @Inject
+       public SetFlagsHandler(IMAPStoreCache cache, Log logger,
+                       Provider<HttpSession> sessionProvider) {
+               super(cache, logger, sessionProvider);
+       }
+
+       @Override
+       protected EmptyResult executeInternal(SetFlag action,
+                       ExecutionContext context) throws ActionException {
+               User user = getUser(action.getSessionId());
+               IMAPFolder folder = action.getFolder();
+               ArrayList<Long> uids = action.getUids();
+               try {
+                       IMAPStore store = cache.get(user);
+
+                       com.sun.mail.imap.IMAPFolder f = 
(com.sun.mail.imap.IMAPFolder) store.getFolder(folder.getFullName());
+                       if (f.isOpen() == false) {
+                               f.open(Folder.READ_WRITE);
+                       }
+                       Message[] msgs = f.getMessagesByUID(toArray(uids));
+                       Flags flags = JavamailUtil.convert(action.getFlags());
+                       f.setFlags(msgs, flags, true);
+                       f.close(false);
+                       return new EmptyResult();
+               } catch (MessagingException e) {
+                       String errorMsg = "Error while setting flags of 
messages with uids " + uids + " for user " + user;
+                       logger.error(errorMsg,e);
+                       throw new ActionException(errorMsg,e);
+               }
+       }
+
+       public Class<SetFlag> getActionType() {
+               return SetFlag.class;
+       }
+       
+       private long[] toArray(ArrayList<Long> uids) {
+               long[] array = new long[uids.size()];
+               for (int i = 0; i < uids.size(); i++) {
+                       array[i] = uids.get(i);
+               }
+               return array;
+       }
+
+}

Added: 
labs/hupa/trunk/shared/src/main/java/org/apache/hupa/shared/rpc/SetFlag.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/shared/src/main/java/org/apache/hupa/shared/rpc/SetFlag.java?rev=817049&view=auto
==============================================================================
--- 
labs/hupa/trunk/shared/src/main/java/org/apache/hupa/shared/rpc/SetFlag.java 
(added)
+++ 
labs/hupa/trunk/shared/src/main/java/org/apache/hupa/shared/rpc/SetFlag.java 
Sun Sep 20 16:14:15 2009
@@ -0,0 +1,59 @@
+/****************************************************************
+ * 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.hupa.shared.rpc;
+
+import java.util.ArrayList;
+
+import org.apache.hupa.shared.data.IMAPFolder;
+import org.apache.hupa.shared.data.Message.IMAPFlag;
+
+public class SetFlag extends Session<EmptyResult>{
+
+       /**
+        * 
+        */
+       private static final long serialVersionUID = 662741801793895357L;
+       private ArrayList<IMAPFlag> flags;
+       private ArrayList<Long> uids;
+       private IMAPFolder folder;
+
+       public SetFlag(String session, IMAPFolder folder, ArrayList<IMAPFlag> 
flags, ArrayList<Long> uids) {
+               super(session);
+               this.flags = flags;
+               this.uids = uids;
+               this.folder = folder;
+       }
+       
+       protected SetFlag() {
+               
+       }
+       
+       public IMAPFolder getFolder() {
+               return folder;
+       }
+       
+       public ArrayList<IMAPFlag> getFlags() {
+               return flags;
+       }
+       
+       public ArrayList<Long> getUids() {
+               return uids;
+       }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to