Author: norman
Date: Fri Jul 31 12:20:01 2009
New Revision: 799586

URL: http://svn.apache.org/viewvc?rev=799586&view=rev
Log:
Fix downloading support

Modified:
    labs/hupa/src/main/java/org/apache/hupa/client/mvp/IMAPMessageView.java
    
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java

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=799586&r1=799585&r2=799586&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 Fri 
Jul 31 12:20:01 2009
@@ -31,6 +31,7 @@
 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.Window;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlowPanel;
@@ -40,6 +41,7 @@
 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;
@@ -168,11 +170,12 @@
                                link.addClickHandler(new ClickHandler() {
 
                                        public void onClick(ClickEvent event) {
-                               
-                                               
Window.open(GWT.getModuleBaseURL()+ "downloadAttachmentServlet?attachment_name="
-                                                               + a.getName() + 
"&folder_name=" + folder
-                                                               + 
"&message_uuid=" + uid + "&sessionId=" + sessionUid
-                                                               ,"_self", "");
+                                                
DOM.setElementAttribute(RootPanel.get("__download")
+                                    .getElement(), "src", 
GWT.getModuleBaseURL()
+                                    + 
"downloadAttachmentServlet?attachment_name="
+                                    + a.getName() + "&folder_name=" + folder
+                                      + "&message_uuid="
+                                    + uid + "&sessionId=" + sessionUid);
                                        }
 
                                });

Modified: 
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=799586&r1=799585&r2=799586&view=diff
==============================================================================
--- 
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
 (original)
+++ 
labs/hupa/src/main/java/org/apache/hupa/server/servlet/DownloadAttachmentServlet.java
 Fri Jul 31 12:20:01 2009
@@ -74,14 +74,22 @@
                        response.setContentType("application/download");
                        response.setHeader("Content-disposition", "attachment; 
filename="
                                        + attachmentName + "");
-
-                       OutputStream out = response.getOutputStream();
                        InputStream in = null;
-                       try {
-                               in = getInputStream((User) 
request.getSession().getAttribute(
-                                               "user"), folderName, 
Long.parseLong(message_uuid),
-                                               attachmentName);
+                       OutputStream out = response.getOutputStream();
+                       
 
+                       IMAPFolder folder = null;
+                       try {
+                               IMAPStore store = cache.get((User) 
request.getSession().getAttribute(
+                               "user"));
+                               folder = (IMAPFolder) 
store.getFolder(folderName);
+                               if (folder.isOpen() == false) {
+                                       folder.open(Folder.READ_ONLY);
+                               }
+                               Message m = 
folder.getMessageByUID(Long.parseLong(message_uuid));
+                               Object content = m.getContent();
+                               Part part  = handleMultiPart(content, 
attachmentName);
+                               in = part.getInputStream();
                                if (in != null) {
                                        byte[] buffer = new byte[4096];
                                        int bytesRead;
@@ -101,33 +109,32 @@
                                                + " for sessionId " + 
sessionId, e);
                        } finally {
                                if (in != null) {
-                                       in.close();
+                                       try {
+                                               in.close();
+                                       } catch (IOException e) {
+                                               // ignore on close
+                                       }
                                }
                                if (out != null) {
-                                       out.close();
+                                       try {
+                                               out.close();
+                                       } catch (IOException e) {
+                                               // ignore on close
+                                       }
+                               }
+                               if (folder != null) {
+                                       try {
+                                               folder.close(false);
+                                       } catch (MessagingException e) {
+                                               // ignore on close
+                                       }
                                }
 
                        }
                }
 
        }
-
-       private InputStream getInputStream(User user, String folderName,
-                       long messageUUID, String attachmentName) throws 
MessagingException,
-                       IOException {
-
-               IMAPStore store = cache.get(user);
-
-               IMAPFolder folder = (IMAPFolder) store.getFolder(folderName);
-               if (folder.isOpen() == false) {
-                       folder.open(Folder.READ_ONLY);
-               }
-               Message m = folder.getMessageByUID(messageUUID);
-               Object content = m.getContent();
-               return handleMultiPart(content, attachmentName);
-
-       }
-
+       
        /**
         * Loop over MuliPart and write the content to the Outputstream if a
         * attachment with the given name was found.
@@ -141,7 +148,7 @@
         * @throws MessagingException
         * @throws IOException
         */
-       private InputStream handleMultiPart(Object content, String 
attachmentName)
+       private Part handleMultiPart(Object content, String attachmentName)
                        throws MessagingException, IOException {
                if (content instanceof Multipart) {
                        Multipart part = (Multipart) content;
@@ -150,7 +157,7 @@
                                if (isAttachment(p)) {
                                        if 
(MimeUtility.decodeText(p.getFileName()).equals(
                                                        attachmentName)) {
-                                               return p.getInputStream();
+                                               return p;
                                        }
 
                                } else if (p.isMimeType("multipart/*")) {



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

Reply via email to