Author: norman
Date: Sun Sep  6 18:09:09 2009
New Revision: 811850

URL: http://svn.apache.org/viewvc?rev=811850&view=rev
Log:
Handle multipart/alternatives correctly

Modified:
    
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java

Modified: 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java
URL: 
http://svn.apache.org/viewvc/labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java?rev=811850&r1=811849&r2=811850&view=diff
==============================================================================
--- 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java
 (original)
+++ 
labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java
 Sun Sep  6 18:09:09 2009
@@ -150,47 +150,75 @@
                        sbPlain.append((String) con);
 
                } else if (con instanceof Multipart) {
-                       Multipart mp = (Multipart) con;
-
-                       for (int i = 0; i < mp.getCount(); i++) {
-                               Part part = mp.getBodyPart(i);
 
-                               String contentType = 
part.getContentType().toLowerCase();
-                               System.out.println("c: " + contentType);
+                       Multipart mp = (Multipart) con;
+                       String multipartContentType = 
mp.getContentType().toLowerCase();
+                       System.out.println("mc: " + multipartContentType);
 
-                               if (contentType.startsWith("text/plain")) {
-                                       isHTML = false;
-                                       sbPlain.append((String) 
part.getContent());
-                               } else if (contentType.startsWith("text/html")) 
{
-                                       isHTML = true;
-                                       sbPlain.append((String) 
part.getContent());
-
-                               } else if 
(contentType.startsWith("message/rfc822")) {
-                                       // Extract the message and pass it
-                                       MimeMessage msg = (MimeMessage) 
part.getDataHandler()
-                                                       .getContent();
-                                       handleParts(msg, msg.getContent(), 
sbPlain, isHTML,
-                                                       attachmentList);
-
-                               } else {
-
-                                       if (part.getFileName() != null) {
-                                               MessageAttachment attachment = 
new MessageAttachment();
-                                               
attachment.setName(MimeUtility.decodeText(part
-                                                               
.getFileName()));
-                                               
attachment.setContentType(part.getContentType());
-                                               
attachment.setSize(part.getSize());
+                       if 
(multipartContentType.startsWith("multipart/alternative")) {
+                               handleMultiPartAlternative(mp, sbPlain, isHTML);
+                       } else {
+                               for (int i = 0; i < mp.getCount(); i++) {
+                                       Part part = mp.getBodyPart(i);
+
+                                       String contentType = 
part.getContentType().toLowerCase();
+                                       System.out.println("c: " + contentType);
+
+                                       if 
(contentType.startsWith("text/plain")) {
+                                               isHTML = false;
+                                       } else if 
(contentType.startsWith("text/html")) {
+                                               isHTML = true;
+                                               sbPlain.append((String) 
part.getContent());
+
+                                       } else if 
(contentType.startsWith("message/rfc822")) {
+                                               // Extract the message and pass 
it
+                                               MimeMessage msg = (MimeMessage) 
part.getDataHandler()
+                                                               .getContent();
+                                               handleParts(msg, 
msg.getContent(), sbPlain, isHTML,
+                                                               attachmentList);
 
-                                               attachmentList.add(attachment);
                                        } else {
-                                               handleParts(message, part, 
sbPlain, isHTML,
-                                                               attachmentList);
+
+                                               if (part.getFileName() != null) 
{
+                                                       MessageAttachment 
attachment = new MessageAttachment();
+                                                       
attachment.setName(MimeUtility.decodeText(part
+                                                                       
.getFileName()));
+                                                       
attachment.setContentType(part.getContentType());
+                                                       
attachment.setSize(part.getSize());
+
+                                                       
attachmentList.add(attachment);
+                                               } else {
+                                                       handleParts(message, 
part, sbPlain, isHTML,
+                                                                       
attachmentList);
+                                               }
                                        }
-                               }
 
+                               }
                        }
 
                }
        }
+       
+       private void handleMultiPartAlternative(Multipart mp,StringBuffer 
sbPlain, boolean isHTML) throws MessagingException, IOException {
+               String text = null;
+               for (int i = 0; i < mp.getCount(); i++) {
+                       Part part = mp.getBodyPart(i);
+                       
+                       String contentType = 
part.getContentType().toLowerCase();
+                       System.out.println("c: " + contentType);
+
+                       if (contentType.startsWith("text/plain")) {
+                               // we prefer plain text of html.. Does this 
make sense for a webmail client ?
+                               isHTML = false;
+                               text = (String) part.getContent();
+                               break;
+
+                       } else if (contentType.startsWith("text/html")) {
+                               isHTML = true;
+                               text = (String) part.getContent();
+                       } 
+               }
+               sbPlain.append(text);
+       }
 
 }



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

Reply via email to