Author: norman
Date: Tue Jul 21 17:37:39 2009
New Revision: 796402

URL: http://svn.apache.org/viewvc?rev=796402&view=rev
Log:
Fix DownloadAttachmentServlet so it work on macosx too

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=796402&r1=796401&r2=796402&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 17:37:39 2009
@@ -165,7 +165,7 @@
                        link.addClickHandler(new ClickHandler() {
 
                                public void onClick(ClickEvent event) {
-                                        
DOM.setElementAttribute(RootPanel.get("__download")
+                                       
DOM.setElementAttribute(RootPanel.get("__download")
                              .getElement(), "src", GWT.getModuleBaseURL()
                              + "downloadAttachmentServlet?attachment_name="
                              + a.getName() + "&folder_name=" + folder

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=796402&r1=796401&r2=796402&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
 Tue Jul 21 17:37:39 2009
@@ -20,6 +20,7 @@
 package org.apache.hupa.server.servlet;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 
 import javax.mail.Folder;
@@ -42,7 +43,6 @@
 
 public class DownloadAttachmentServlet extends HttpServlet {
 
-
        /**
         * 
         */
@@ -51,61 +51,92 @@
        private Log logger;
 
        @Inject
-       public DownloadAttachmentServlet(IMAPStoreCache cache,Log logger) {
+       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 (Exception e) {
-               logger.error("Error while downloading attachment " + 
attachmentName +" of message "+  messageUUID + " for user " + user,e );
-        }
-    }
-
-    /**
-     * 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 {
+
+       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.setContentType("application/download");
+               response.setHeader("Content-disposition", "attachment; 
filename="
+                               + attachmentName + "");
+
+               OutputStream out = response.getOutputStream();
+               InputStream in = null;
+               try {
+                       in = getInputStream(user, pass, folderName, Long
+                                       .parseLong(message_uuid), 
attachmentName);
+
+                       if (in != null) {
+                               byte[] buffer = new byte[4096];
+                               int bytesRead;
+                               int bytesComplete = 0;
+                               while ((bytesRead = in.read(buffer)) != -1) {
+                                       bytesComplete = bytesComplete + 
bytesRead;
+                                       out.write(buffer, 0, bytesRead); // 
write
+                               }
+                               out.flush();
+                               response.setContentLength(bytesComplete);
+                       } else {
+                               response.setContentLength(0);
+                       }
+               } catch (Exception e) {
+                       logger.error("Error while downloading attachment " + 
attachmentName
+                                       + " of message " + message_uuid + " for 
user " + user, e);
+               } finally {
+                       if (in != null) {
+                               in.close();
+                       }
+                       if (out != null) {
+                               out.close();
+                       }
+
+               }
+
+       }
+
+       private InputStream getInputStream(String user, String pass,
+                       String folderName, long messageUUID, String 
attachmentName)
+                       throws MessagingException, IOException {
+
+               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();
+               return handleMultiPart(content, attachmentName);
+
+       }
+
+       /**
+        * 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 InputStream handleMultiPart(Object content, String 
attachmentName)
+                       throws MessagingException, IOException {
                if (content instanceof Multipart) {
                        Multipart part = (Multipart) content;
                        for (int i = 0; i < part.getCount(); i++) {
@@ -113,28 +144,25 @@
                                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
+                                               return p.getInputStream();
                                        }
 
                                } else if (p.isMimeType("multipart/*")) {
-                                       handleMultiPart(out, p.getContent(), 
attachmentName);
+                                       return handleMultiPart(p.getContent(), 
attachmentName);
                                }
                        }
                }
+               return null;
        }
-    
-    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;
-    }
 
+       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;
+       }
 
 }
-



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

Reply via email to