You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by no...@apache.org on 2009/09/04 20:59:28 UTC

svn commit: r811516 - /labs/hupa/trunk/server/src/main/java/org/apache/hupa/server/handler/GetMessageDetailsHandler.java

Author: norman
Date: Fri Sep  4 18:59:28 2009
New Revision: 811516

URL: http://svn.apache.org/viewvc?rev=811516&view=rev
Log:
Make sure parts with message/rfc822 are handled 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=811516&r1=811515&r2=811516&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 Fri Sep  4 18:59:28 2009
@@ -19,10 +19,13 @@
 
 package org.apache.hupa.server.handler;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 
 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;
@@ -46,37 +49,46 @@
 import com.google.inject.Provider;
 import com.sun.mail.imap.IMAPStore;
 
-public class GetMessageDetailsHandler extends AbstractSessionHandler<GetMessageDetails, GetMessageDetailsResult>{
+public class GetMessageDetailsHandler extends
+		AbstractSessionHandler<GetMessageDetails, GetMessageDetailsResult> {
 
 	@Inject
-	public GetMessageDetailsHandler(IMAPStoreCache cache, Log logger,Provider<HttpSession> sProvider) {
-		super(cache,logger,sProvider);
+	public GetMessageDetailsHandler(IMAPStoreCache cache, Log logger,
+			Provider<HttpSession> sProvider) {
+		super(cache, logger, sProvider);
 	}
-	
+
 	/*
 	 * (non-Javadoc)
-	 * @see org.apache.hupa.server.handler.AbstractSessionHandler#executeInternal(org.apache.hupa.shared.rpc.Session, net.customware.gwt.dispatch.server.ExecutionContext)
+	 * 
+	 * @see
+	 * org.apache.hupa.server.handler.AbstractSessionHandler#executeInternal
+	 * (org.apache.hupa.shared.rpc.Session,
+	 * net.customware.gwt.dispatch.server.ExecutionContext)
 	 */
-	public GetMessageDetailsResult executeInternal(GetMessageDetails action, ExecutionContext arg1)
-			throws ActionException {
-			return new GetMessageDetailsResult(exposeMessage(getUser(action.getSessionId()), action.getFolder(), action.getUid()));
+	public GetMessageDetailsResult executeInternal(GetMessageDetails action,
+			ExecutionContext arg1) throws ActionException {
+		return new GetMessageDetailsResult(exposeMessage(getUser(action
+				.getSessionId()), action.getFolder(), action.getUid()));
 	}
 
 	/*
 	 * (non-Javadoc)
+	 * 
 	 * @see net.customware.gwt.dispatch.server.ActionHandler#getActionType()
 	 */
 	public Class<GetMessageDetails> getActionType() {
 		return GetMessageDetails.class;
 	}
-	
-	protected MessageDetails exposeMessage(User user, IMAPFolder folder, long uid) throws ActionException {
+
+	protected MessageDetails exposeMessage(User user, IMAPFolder folder,
+			long uid) throws ActionException {
 		IMAPStore store = null;
 		try {
 			store = cache.get(user);
 
-
-			com.sun.mail.imap.IMAPFolder f = (com.sun.mail.imap.IMAPFolder)store.getFolder(folder.getFullName());
+			com.sun.mail.imap.IMAPFolder f = (com.sun.mail.imap.IMAPFolder) store
+					.getFolder(folder.getFullName());
 
 			if (f.isOpen() == false) {
 				f.open(com.sun.mail.imap.IMAPFolder.READ_WRITE);
@@ -84,83 +96,101 @@
 			MimeMessage message = (MimeMessage) f.getMessageByUID(uid);
 			MessageDetails mDetails = new MessageDetails();
 			mDetails.setUid(uid);
-			
-			boolean bodyFound = false;
-            boolean isHTML = false;
-            Object con = message.getContent();
-
-            if (con instanceof String) {
-            	if (message.getContentType().startsWith("text/html")) {
-            		isHTML = true;
-            	} else {
-            		isHTML = false;
-            	}
-                bodyFound = true;
-                mDetails.setText((String)con);
-                mDetails.setIsHTML(isHTML);   
-
-            } else if (con instanceof Multipart) {
-                Multipart mp = (Multipart) con;
-                ArrayList<MessageAttachment> attachmentList = new ArrayList<MessageAttachment>();
-                StringBuffer sbPlain = new StringBuffer();
-                StringBuffer sbHTML = new StringBuffer();
-                for (int i = 0; i < mp.getCount(); i++) {
-                    Part part = mp.getBodyPart(i);
-
-                    String contentType = part.getContentType().toLowerCase();
-                    System.out.println("c: " + contentType);
-
-                    if ( bodyFound == false && contentType.startsWith("text/plain") && isHTML == false) {
-                        //sbPlain.append(StringEscapeUtils.escapeHtml((String) part.getContent()).replaceAll("\n", "<br>"));
-                        sbPlain.append((String) part.getContent());
-                    	bodyFound = true;
-                    } else if (bodyFound == false && contentType.startsWith("text/html")) {
-                        isHTML = true;
-                        /*
-                        if (sbHTML.length() > 0) {
-                            sbHTML.append("<HR/>");
-                        }
-                        */
-                        bodyFound = true;
-                        sbHTML.append((String) part.getContent());
-
-                    } else {
-
-                        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);
-                        }
-                    }
-
-                }
-                if (isHTML) {
-                	mDetails.setText(sbHTML.toString());
-                } else {
-                	mDetails.setText(sbPlain.toString());
-                }
-
-                mDetails.setIsHTML(isHTML);
-                mDetails.setMessageAttachments(attachmentList);
-            }
-            
-            mDetails.setRawHeader(message.getAllHeaders().toString());
-			
-            f.setFlags(new Message[] {message}, new Flags(Flag.SEEN), true);
-            f.close(false);
 
-        
+			boolean isHTML = false;
+			Object con = message.getContent();
+
+			StringBuffer sbPlain = new StringBuffer();
+			ArrayList<MessageAttachment> attachmentList = new ArrayList<MessageAttachment>();
+
+			handleParts(message, con, sbPlain, isHTML, attachmentList);
+
+			mDetails.setText(sbPlain.toString());
+
+			mDetails.setIsHTML(isHTML);
+			mDetails.setMessageAttachments(attachmentList);
+
+			mDetails.setRawHeader(message.getAllHeaders().toString());
+
+			f.setFlags(new Message[] { message }, new Flags(Flag.SEEN), true);
+			f.close(false);
 
 			return mDetails;
 		} catch (Exception e) {
-			logger.error("Unable to expose msg for user " + user + " in folder "+ folder + " with uid "+ uid, e);
-			throw new ActionException("Unable to expose msg for user " + user + " in folder "+ folder + " with uid "
-					+ uid);
+			logger.error("Unable to expose msg for user " + user
+					+ " in folder " + folder + " with uid " + uid, e);
+			throw new ActionException("Unable to expose msg for user " + user
+					+ " in folder " + folder + " with uid " + uid);
+
+		}
+	}
+
+	/**
+	 * Handle the parts of the given message. The method will call itself recursively to handle all nested parts
+	 * @param message the MimeMessage 
+	 * @param con the current processing Content
+	 * @param sbPlain the StringBuffer to fill with text
+	 * @param isHTML identicate if the message is HTML
+	 * @param attachmentList ArrayList with attachments
+	 * @throws UnsupportedEncodingException
+	 * @throws MessagingException
+	 * @throws IOException
+	 */
+	private void handleParts(MimeMessage message, Object con,
+			StringBuffer sbPlain, boolean isHTML,
+			ArrayList<MessageAttachment> attachmentList)
+			throws UnsupportedEncodingException, MessagingException,
+			IOException {
+		if (con instanceof String) {
+			if (message.getContentType().startsWith("text/html")) {
+				isHTML = true;
+			} else {
+				isHTML = false;
+			}
+			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);
+
+				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());
+
+						attachmentList.add(attachment);
+					} else {
+						handleParts(message, part, sbPlain, isHTML,
+								attachmentList);
+					}
+				}
+
+			}
 
-		} 
+		}
 	}
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org