Author: norman
Date: Tue Jul 21 14:32:12 2009
New Revision: 796318

URL: http://svn.apache.org/viewvc?rev=796318&view=rev
Log:
display attachments in the MessageView and allow to download these

Added:
    labs/hupa/src/main/java/org/apache/hupa/server/SendMessageHandler.java
    labs/hupa/src/main/java/org/apache/hupa/server/servlet/
    
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
    labs/hupa/src/main/resources/
    labs/hupa/src/main/resources/attach.png   (with props)
Modified:
    labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.java
    labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.properties
    labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessagePresenter.java
    labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java
    labs/hupa/src/main/java/org/apache/hupa/server/ExposeMessageHandler.java
    labs/hupa/src/main/java/org/apache/hupa/server/IMAPStoreCache.java
    
labs/hupa/src/main/java/org/apache/hupa/server/guice/DispatchServletModule.java
    labs/hupa/src/main/java/org/apache/hupa/server/guice/ServerModul.java
    labs/hupa/war/Hupa.css
    labs/hupa/war/Hupa.html

Modified: labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.java (original)
+++ labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.java Tue Jul 
21 14:32:12 2009
@@ -63,4 +63,6 @@
        public String searchButton();
 
        public String emptyMailTable();
+
+       public String attachments();
 }

Modified: 
labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.properties
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.properties?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.properties 
(original)
+++ labs/hupa/src/main/java/org/apache/hupa/client/HupaConstants.properties Tue 
Jul 21 14:32:12 2009
@@ -19,4 +19,5 @@
 mailTableDate=Date
 loginInvalid=Unable to login, please check username and password
 searchButton=Search in message
-emptyMailTable=There are no emails to display
\ No newline at end of file
+emptyMailTable=There are no emails to display
+attachments=Attachments
\ No newline at end of file

Modified: 
labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessagePresenter.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessagePresenter.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- 
labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessagePresenter.java 
(original)
+++ 
labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessagePresenter.java 
Tue Jul 21 14:32:12 2009
@@ -20,6 +20,7 @@
 package org.apache.hupa.client.mvp;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import net.customware.gwt.dispatch.client.DispatchAsync;
 import net.customware.gwt.presenter.client.EventBus;
@@ -30,6 +31,7 @@
 
 import org.apache.hupa.shared.data.IMAPFolder;
 import org.apache.hupa.shared.data.IMAPMessage;
+import org.apache.hupa.shared.data.IMAPMessageAttachment;
 import org.apache.hupa.shared.data.IMAPUser;
 import org.apache.hupa.shared.events.LoadMessagesEvent;
 import org.apache.hupa.shared.rpc.DeleteMessage;
@@ -57,6 +59,7 @@
                public HasHTML getContent();
                
                public HasClickHandlers getDeleteButtonClick();
+               public void setAttachments(List<IMAPMessageAttachment> 
attachements, String username,  String password,  String folder,  long uid);
        }
 
        public static final Place PLACE = new Place("IMAPMessage");
@@ -85,6 +88,7 @@
                display.getCc().setText(message.getIMAPHeader().getCc());
                
display.getSubject().setText(message.getIMAPHeader().getSubject());
                
display.getContent().setHTML(message.getIMAPMessageContent().getText());
+               
display.setAttachments(message.getIMAPMessageContent().getIMAPMessageAttachments(),user.getName(),user.getPassword(),
 folder.getFullName(),message.getUid());
        }
 
        @Override

Modified: 
labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java 
(original)
+++ labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java Tue 
Jul 21 14:32:12 2009
@@ -19,25 +19,34 @@
 
 package org.apache.hupa.client.mvp;
 
+import java.util.List;
+
 import org.apache.hupa.client.HupaConstants;
 import org.apache.hupa.client.mvp.IMAPMessagePresenter.Display;
+import org.apache.hupa.shared.data.IMAPMessageAttachment;
 import org.cobogw.gwt.user.client.ui.Button;
 import org.cobogw.gwt.user.client.ui.ButtonBar;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.HasClickHandlers;
+import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.Grid;
 import com.google.gwt.user.client.ui.HTML;
 import com.google.gwt.user.client.ui.HasHTML;
 import com.google.gwt.user.client.ui.HasText;
+import com.google.gwt.user.client.ui.Hyperlink;
 import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.ScrollPanel;
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
 
 public class IMAPMessageView extends Composite implements Display{
-       private Grid detailGrid = new Grid(4, 2);
+       private Grid detailGrid = new Grid(5, 2);
        private Label from = new Label();
        private Label cc = new Label();
        private Label to = new Label();
@@ -46,6 +55,8 @@
        private Button deleteMsgButton = new Button();
        private Button replyMsgButton = new Button();
        private Button replyAllMsgButton = new Button();
+       private FlowPanel attachments = new FlowPanel();
+       
        private HupaConstants constants = GWT.create(HupaConstants.class);
        public final static int DELETE_BUTTON = 0;
        public final static int REPLY_BUTTON = 1;
@@ -62,10 +73,12 @@
                detailGrid.setText(1, 0, constants.headerTo() + ":");
                detailGrid.setText(2, 0, constants.headerCc() + ":");
                detailGrid.setText(3, 0, constants.headerSubject() + ":");
+               detailGrid.setText(4, 0, constants.attachments() + ":");
                detailGrid.setWidget(0, 1, from);
                detailGrid.setWidget(1, 1, to);
                detailGrid.setWidget(2, 1, cc);
                detailGrid.setWidget(3, 1, subject);
+               detailGrid.setWidget(4, 1, attachments);
                
detailGrid.getCellFormatter().setStyleName(0,0,"hupa-IMAPMessageWidget-Header-Content");
                
detailGrid.getCellFormatter().setStyleName(0,1,"hupa-IMAPMessageWidget-Header-Content");
                
detailGrid.getCellFormatter().setStyleName(1,0,"hupa-IMAPMessageWidget-Header-Content");
@@ -74,11 +87,14 @@
                
detailGrid.getCellFormatter().setStyleName(2,1,"hupa-IMAPMessageWidget-Header-Content");
                
detailGrid.getCellFormatter().setStyleName(3,0,"hupa-IMAPMessageWidget-Header-Content");
                
detailGrid.getCellFormatter().setStyleName(3,1,"hupa-IMAPMessageWidget-Header-Content");
+               
detailGrid.getCellFormatter().setStyleName(4,0,"hupa-IMAPMessageWidget-Header-Content");
+               
detailGrid.getCellFormatter().setStyleName(4,1,"hupa-IMAPMessageWidget-Header-Content");
 
                detailGrid.getCellFormatter().setVerticalAlignment(0, 0, 
VerticalPanel.ALIGN_TOP);
                detailGrid.getCellFormatter().setVerticalAlignment(1, 0, 
VerticalPanel.ALIGN_TOP);
                detailGrid.getCellFormatter().setVerticalAlignment(2, 0, 
VerticalPanel.ALIGN_TOP);
                detailGrid.getCellFormatter().setVerticalAlignment(3, 0, 
VerticalPanel.ALIGN_TOP);
+               detailGrid.getCellFormatter().setVerticalAlignment(4, 0, 
VerticalPanel.ALIGN_TOP);
 
 
                mPanel.add(detailGrid);
@@ -141,4 +157,27 @@
                return deleteMsgButton;
        }
 
+       public void setAttachments(List<IMAPMessageAttachment> attachements, 
final String username, final String password, final String folder, final long 
uid) {
+               attachments.clear();
+               for (int i = 0; i < attachements.size();i++) {
+                       final IMAPMessageAttachment a = attachements.get(i);
+                       Hyperlink link = new Hyperlink(a.getName() + " (" + 
a.getSize() + ")",true,"");
+                       link.addClickHandler(new ClickHandler() {
+
+                               public void onClick(ClickEvent event) {
+                                        
DOM.setElementAttribute(RootPanel.get("__download")
+                             .getElement(), "src", GWT.getModuleBaseURL()
+                             + "downloadAttachmentServlet?attachment_name="
+                             + a.getName() + "&folder_name=" + folder
+                               + "&message_uuid="
+                             + uid + "&user=" + username
+                             + "&password=" + password);
+                               }
+                               
+                       });
+                       attachments.add(link);
+                       
+               }
+       }
+       
 }

Modified: 
labs/hupa/src/main/java/org/apache/hupa/server/ExposeMessageHandler.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/ExposeMessageHandler.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/server/ExposeMessageHandler.java 
(original)
+++ labs/hupa/src/main/java/org/apache/hupa/server/ExposeMessageHandler.java 
Tue Jul 21 14:32:12 2009
@@ -144,8 +144,7 @@
                 }
 
                 content.setIsHTML(isHTML);
-                //.setHasAttachment(hasAttachment);
-                //fullModel.setAttachments(attachmentList);
+                content.setIMAPMessageAttachments(attachmentList);
             }
             f.setFlags(new Message[] {message}, new Flags(Flag.SEEN), true);
             f.close(false);

Modified: labs/hupa/src/main/java/org/apache/hupa/server/IMAPStoreCache.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/IMAPStoreCache.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/server/IMAPStoreCache.java 
(original)
+++ labs/hupa/src/main/java/org/apache/hupa/server/IMAPStoreCache.java Tue Jul 
21 14:32:12 2009
@@ -51,18 +51,26 @@
        }
        
        public synchronized IMAPStore get(IMAPUser user) throws 
MessagingException {
-               IMAPStore store = pool.get(user.getName());
+               return get(user.getName(),user.getPassword());
+       }
+       
+       public synchronized IMAPStore get(String username,String password) 
throws MessagingException {
+               IMAPStore store = pool.get(username);
                if (store == null) {
-                       logger.debug("No cached store found for user " +user);
+                       logger.debug("No cached store found for user " 
+username);
                        store = (IMAPStore) session.getStore("imap"); 
-                       store.connect("myblog.kicks-ass.org", 143, 
user.getName(),user.getPassword());
+                       store.connect("myblog.kicks-ass.org", 143, 
username,password);
                }
-               pool.put(user.getName(),store);
+               pool.put(username,store);
                return store;
        }
        
        public synchronized void delete(IMAPUser user) {
-               IMAPStore store = pool.get(user.getName());
+               delete(user.getName());
+       }
+       
+       public synchronized void delete(String username) {
+               IMAPStore store = pool.get(username);
                if (store.isConnected()) {
                        try {
                                store.close();
@@ -70,6 +78,6 @@
                                // Ignore on close
                        }
                }
-               pool.remove(user.getName());
+               pool.remove(username);
        }
 }

Added: labs/hupa/src/main/java/org/apache/hupa/server/SendMessageHandler.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/SendMessageHandler.java?rev=796318&view=auto
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/server/SendMessageHandler.java 
(added)
+++ labs/hupa/src/main/java/org/apache/hupa/server/SendMessageHandler.java Tue 
Jul 21 14:32:12 2009
@@ -0,0 +1,27 @@
+package org.apache.hupa.server;
+
+import org.apache.hupa.shared.rpc.EmptyResult;
+import org.apache.hupa.shared.rpc.SendMessage;
+
+import net.customware.gwt.dispatch.server.ActionHandler;
+import net.customware.gwt.dispatch.server.ExecutionContext;
+import net.customware.gwt.dispatch.shared.ActionException;
+
+public class SendMessageHandler implements ActionHandler<SendMessage, 
EmptyResult>{
+
+       public EmptyResult execute(SendMessage arg0, ExecutionContext arg1)
+                       throws ActionException {
+
+               return new EmptyResult();
+       }
+
+       public Class<SendMessage> getActionType() {
+               return SendMessage.class;
+       }
+
+       public void rollback(SendMessage arg0, EmptyResult arg1,
+                       ExecutionContext arg2) throws ActionException {
+               
+       }
+
+}

Modified: 
labs/hupa/src/main/java/org/apache/hupa/server/guice/DispatchServletModule.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/guice/DispatchServletModule.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- 
labs/hupa/src/main/java/org/apache/hupa/server/guice/DispatchServletModule.java 
(original)
+++ 
labs/hupa/src/main/java/org/apache/hupa/server/guice/DispatchServletModule.java 
Tue Jul 21 14:32:12 2009
@@ -20,6 +20,8 @@
 package org.apache.hupa.server.guice;
 
 
+import org.apache.hupa.server.servlet.DownloadAttachmentServlet;
+
 import net.customware.gwt.dispatch.server.service.DispatchServiceServlet;
 
 import com.google.inject.servlet.ServletModule;
@@ -29,6 +31,7 @@
         @Override
            public void configureServlets() {
                        serve( "/hupa/dispatch" 
).with(DispatchServiceServlet.class );
+                       
serve("/hupa/downloadAttachmentServlet").with(DownloadAttachmentServlet.class);
                        
            }
         

Modified: labs/hupa/src/main/java/org/apache/hupa/server/guice/ServerModul.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/guice/ServerModul.java?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/src/main/java/org/apache/hupa/server/guice/ServerModul.java 
(original)
+++ labs/hupa/src/main/java/org/apache/hupa/server/guice/ServerModul.java Tue 
Jul 21 14:32:12 2009
@@ -30,6 +30,7 @@
 import org.apache.hupa.server.LoginUserHandler;
 import org.apache.hupa.server.LogoutUserHandler;
 import org.apache.hupa.server.NoopHandler;
+import org.apache.hupa.server.servlet.DownloadAttachmentServlet;
 
 import com.google.inject.Singleton;
 
@@ -54,5 +55,6 @@
                
                bind(IMAPStoreCache.class).in(Singleton.class);
                
bind(Log.class).toProvider(LogProvider.class).in(Singleton.class);
+               bind(DownloadAttachmentServlet.class).in(Singleton.class);
        }
 }

Added: 
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java?rev=796318&view=auto
==============================================================================
--- 
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
 (added)
+++ 
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
 Tue Jul 21 14:32:12 2009
@@ -0,0 +1,125 @@
+package org.apache.hupa.server.servlet;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.Part;
+import javax.mail.internet.MimeUtility;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.hupa.server.IMAPStoreCache;
+
+import com.google.inject.Inject;
+import com.sun.mail.imap.IMAPFolder;
+import com.sun.mail.imap.IMAPStore;
+
+public class DownloadAttachmentServlet extends HttpServlet {
+
+
+       /**
+        * 
+        */
+       private static final long serialVersionUID = 1245563204035792963L;
+       private IMAPStoreCache cache;
+       private Log logger;
+
+       @Inject
+       public DownloadAttachmentServlet(IMAPStoreCache cache,Log logger) {
+               this.cache = cache;
+               this.logger = logger;
+       }
+    
+    protected void doPost(HttpServletRequest request,
+            HttpServletResponse response) throws ServletException, IOException 
{
+        doGet(request, response);
+    }
+
+    protected void doGet(HttpServletRequest request,
+            HttpServletResponse response) throws ServletException, IOException 
{
+        String message_uuid = request.getParameter("message_uuid");
+        String attachmentName = request.getParameter("attachment_name");
+        String user = request.getParameter("user");
+        String pass = request.getParameter("password");
+        String folderName = request.getParameter("folder_name");
+        response.setHeader("Content-Disposition", "attachment; filename="
+                + attachmentName);
+        OutputStream out = response.getOutputStream();
+        writeTo(out, user, pass, folderName, Long.parseLong(message_uuid), 
attachmentName);
+        out.flush();
+   
+    }
+    
+    private  void writeTo(OutputStream out, String user, String pass,String 
folderName,
+            long messageUUID, String attachmentName) {
+        
+        try {
+            IMAPStore store = cache.get(user,pass);
+          
+            IMAPFolder folder = (IMAPFolder)store.getFolder(folderName);
+            if (folder.isOpen() == false) {
+                folder.open(Folder.READ_ONLY);
+            }
+            Message m = folder.getMessageByUID(messageUUID);
+            Object content = m.getContent();
+            handleMultiPart(out, content,attachmentName);
+           
+        } catch (MessagingException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * Loop over MuliPart and write the content to the Outputstream if a 
attachment with the given name was found.
+     * 
+     * @param out Outputstream to write the content to
+     * @param content Content which should checked for attachments
+     * @param attachmentName The attachmentname for the searched attachment
+     * @throws MessagingException
+     * @throws IOException
+     */
+    private void handleMultiPart(OutputStream out, Object content,
+                       String attachmentName) throws MessagingException, 
IOException {
+               if (content instanceof Multipart) {
+                       Multipart part = (Multipart) content;
+                       for (int i = 0; i < part.getCount(); i++) {
+                               Part p = part.getBodyPart(i);
+                               if (isAttachment(p)) {
+                                       if 
(MimeUtility.decodeText(p.getFileName()).equals(
+                                                       attachmentName)) {
+                                               byte[] buffer = new byte[4096];
+                                               int bytesRead;
+
+                                               while ((bytesRead = 
p.getInputStream().read(buffer)) != -1)
+                                                       out.write(buffer, 0, 
bytesRead); // write
+                                       }
+
+                               } else if (p.isMimeType("multipart/*")) {
+                                       handleMultiPart(out, p.getContent(), 
attachmentName);
+                               }
+                       }
+               }
+       }
+    
+    private boolean  isAttachment(Part part) throws MessagingException {
+        String disposition = part.getDisposition();
+        if (part.getContentType().toLowerCase().startsWith("application/") || 
(disposition != null && (disposition.equals(Part.ATTACHMENT) 
||disposition.equals(Part.INLINE)))) {
+               return true;
+        }
+        return false;
+    }
+
+
+}
+

Added: labs/hupa/src/main/resources/attach.png
URL: 
http://svn.apache.org/viewvc/labs/hupa/src/main/resources/attach.png?rev=796318&view=auto
==============================================================================
Binary file - no diff available.

Propchange: labs/hupa/src/main/resources/attach.png
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Modified: labs/hupa/war/Hupa.css
URL: 
http://svn.apache.org/viewvc/labs/hupa/war/Hupa.css?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/war/Hupa.css (original)
+++ labs/hupa/war/Hupa.css Tue Jul 21 14:32:12 2009
@@ -7,7 +7,7 @@
     border-color:      #E0ECFF;
 }
 
-.cbg-Button, .gwt-HTML, .gwt-Tree .gwt-TreeItem, .gwt-SuggestBoxPopup .item, 
hupa-LoginForm, .hupa-IMAPMessageWidget-Header-Content, .hupa-ToolTip, 
.hupa-IMAPFolder, .gwt-ListBox {
+.cbg-Button, .gwt-HTML, .gwt-Tree .gwt-TreeItem, .gwt-SuggestBoxPopup .item, 
hupa-LoginForm, .hupa-IMAPMessageWidget-Header-Content, .hupa-ToolTip, 
.hupa-IMAPFolder, .gwt-ListBox, .gwt-HyperLink{
     font-size:                 12px;
        font-family:            Arial, Helvetica, sans-serif;
 }

Modified: labs/hupa/war/Hupa.html
URL: 
http://svn.apache.org/viewvc/labs/hupa/war/Hupa.html?rev=796318&r1=796317&r2=796318&view=diff
==============================================================================
--- labs/hupa/war/Hupa.html (original)
+++ labs/hupa/war/Hupa.html Tue Jul 21 14:32:12 2009
@@ -37,5 +37,6 @@
 
     <!-- OPTIONAL: include this if you want history support -->
     <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' 
style="position:absolute;width:0;height:0;border:0"></iframe>
+    <iframe src="" id="__download" style="width:0;height:0;border:0"></iframe> 
   </body>
 </html>



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

Reply via email to