Move method to Util class

git-svn-id: https://svn.apache.org/repos/asf/james/hupa/trunk@1522439 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java b/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java
index fb422a7..ab4c073 100644
--- a/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java
+++ b/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java
@@ -63,14 +63,11 @@
 import javax.mail.Header;

 import javax.mail.Message;

 import javax.mail.MessagingException;

-import javax.mail.Multipart;

-import javax.mail.Part;

 import javax.mail.internet.MimeMessage;

-import javax.mail.internet.MimeUtility;

 

+import org.apache.hupa.server.utils.MessageUtils;

 import org.apache.hupa.shared.data.GetMessageDetailsResultImpl;

 import org.apache.hupa.shared.data.MailHeaderImpl;

-import org.apache.hupa.shared.data.MessageAttachmentImpl;

 import org.apache.hupa.shared.data.MessageDetailsImpl;

 import org.apache.hupa.shared.domain.GetMessageDetailsAction;

 import org.apache.hupa.shared.domain.GetMessageDetailsResult;

@@ -127,12 +124,12 @@
 	        MessagingException, UnsupportedEncodingException {

 		MessageDetails mDetails = new MessageDetailsImpl();

 

-		Object con = message.getContent();

+		Object content = message.getContent();

 

 		StringBuffer sbPlain = new StringBuffer();

 		ArrayList<MessageAttachment> attachmentList = new ArrayList<MessageAttachment>();

 

-		boolean isHTML = handleParts(message, con, sbPlain, attachmentList);

+		boolean isHTML = MessageUtils.handleParts(message, content, sbPlain, attachmentList);

 

 <<<<<<< HEAD
 <<<<<<< HEAD
@@ -150,115 +147,14 @@
 

 		mDetails.setMessageAttachments(attachmentList);

 

-		for (@SuppressWarnings("unchecked")

-		Enumeration<Header> en = message.getAllHeaders(); en.hasMoreElements();) {

+		for (@SuppressWarnings("unchecked") Enumeration<Header> en = message.getAllHeaders(); en.hasMoreElements();) {

 			Header header = en.nextElement();

 			mDetails.setMailHeader(new MailHeaderImpl(header.getName(), header.getValue()));

-//			mDetails.addHeader(header.getName(), header.getValue());

 		}

 

 		return mDetails;

 	}

 

-	/**

-	 * 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 attachmentList ArrayList with attachments

-	 * @throws UnsupportedEncodingException

-	 * @throws MessagingException

-	 * @throws IOException

-	 */

-	protected boolean handleParts(MimeMessage message, Object con, StringBuffer sbPlain,

-	        ArrayList<MessageAttachment> attachmentList) throws UnsupportedEncodingException, MessagingException,

-	        IOException {

-		boolean isHTML = false;

-		if (con instanceof String) {

-			if (message.getContentType().toLowerCase().startsWith("text/html")) {

-				isHTML = true;

-			} else {

-				isHTML = false;

-			}

-			sbPlain.append((String) con);

-

-		} else if (con instanceof Multipart) {

-

-			Multipart mp = (Multipart) con;

-			String multipartContentType = mp.getContentType().toLowerCase();

-

-			String text = null;

-

-			if (multipartContentType.startsWith("multipart/alternative")) {

-				isHTML = handleMultiPartAlternative(mp, sbPlain);

-			} else {

-				for (int i = 0; i < mp.getCount(); i++) {

-					Part part = mp.getBodyPart(i);

-

-					String contentType = part.getContentType().toLowerCase();

-

-					Boolean bodyRead = sbPlain.length() > 0;

-

-					if (!bodyRead && contentType.startsWith("text/plain")) {

-						isHTML = false;

-						text = (String) part.getContent();

-					} else if (!bodyRead && contentType.startsWith("text/html")) {

-						isHTML = true;

-						text = (String) part.getContent();

-					} else if (!bodyRead && contentType.startsWith("message/rfc822")) {

-						// Extract the message and pass it

-						MimeMessage msg = (MimeMessage) part.getDataHandler().getContent();

-						isHTML = handleParts(msg, msg.getContent(), sbPlain, attachmentList);

-					} else {

-						if (part.getFileName() != null) {

-							// Inline images are not added to the attachment

-							// list

-							// TODO: improve the in-line images detection

-							if (part.getHeader("Content-ID") == null) {

-								MessageAttachment attachment = new MessageAttachmentImpl();

-								attachment.setName(MimeUtility.decodeText(part.getFileName()));

-								attachment.setContentType(part.getContentType());

-								attachment.setSize(part.getSize());

-								attachmentList.add(attachment);

-							}

-						} else {

-							isHTML = handleParts(message, part.getContent(), sbPlain, attachmentList);

-						}

-					}

-

-				}

-				if (text != null)

-					sbPlain.append(text);

-			}

-

-		}

-		return isHTML;

-	}

-

-	private boolean handleMultiPartAlternative(Multipart mp, StringBuffer sbPlain) throws MessagingException,

-	        IOException {

-		String text = null;

-		boolean isHTML = false;

-		for (int i = 0; i < mp.getCount(); i++) {

-			Part part = mp.getBodyPart(i);

-

-			String contentType = part.getContentType().toLowerCase();

-

-			// we prefer html

-			if (text == null && contentType.startsWith("text/plain")) {

-				isHTML = false;

-				text = (String) part.getContent();

-			} else if (contentType.startsWith("text/html")) {

-				isHTML = true;

-				text = (String) part.getContent();

-			}

-		}

-		sbPlain.append(text);

-		return isHTML;

-	}

-

 	protected String txtDocumentToHtml(String txt, String folderName, long uuid) {

 

 		if (txt == null || txt.length() == 0)

diff --git a/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java b/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java
index f19f2c0..2a92d52 100644
--- a/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java
+++ b/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java
@@ -73,6 +73,7 @@
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeUtility;
 
 import org.apache.commons.fileupload.FileItem;
@@ -80,6 +81,7 @@
 <<<<<<< HEAD
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
 =======
 import org.apache.hupa.server.handler.AbstractSendMessageHandler;
 >>>>>>> first commit
@@ -88,6 +90,10 @@
 >>>>>>> first commit
 =======
 >>>>>>> remove both of gwt-representer and gwt-dispatch dependencies, add license headers to all new files
+=======
+import org.apache.hupa.shared.data.MessageAttachmentImpl;
+import org.apache.hupa.shared.domain.MessageAttachment;
+>>>>>>> Move method to Util class
 
 
 
@@ -180,6 +186,104 @@
         }
         return ret;
     }
+    
+    /**
+     * Handle the parts of the given message. The method will call itself
+     * recursively to handle all nested parts
+     * 
+     * @param message the MimeMessage
+     * @param content the current processing Content
+     * @param sbPlain the StringBuffer to fill with text
+     * @param attachmentList ArrayList with attachments
+     * @throws UnsupportedEncodingException
+     * @throws MessagingException
+     * @throws IOException
+     */
+    public static boolean handleParts(MimeMessage message, Object content, StringBuffer sbPlain,
+            ArrayList<MessageAttachment> attachmentList) throws UnsupportedEncodingException, MessagingException,
+            IOException {
+        boolean isHTML = false;
+        if (content instanceof String) {
+            if (message.getContentType().toLowerCase().startsWith("text/html")) {
+                isHTML = true;
+            } else {
+                isHTML = false;
+            }
+            sbPlain.append((String) content);
+
+        } else if (content instanceof Multipart) {
+
+            Multipart mp = (Multipart) content;
+            String multipartContentType = mp.getContentType().toLowerCase();
+
+            String text = null;
+
+            if (multipartContentType.startsWith("multipart/alternative")) {
+                isHTML = handleMultiPartAlternative(mp, sbPlain);
+            } else {
+                for (int i = 0; i < mp.getCount(); i++) {
+                    Part part = mp.getBodyPart(i);
+
+                    String contentType = part.getContentType().toLowerCase();
+
+                    Boolean bodyRead = sbPlain.length() > 0;
+
+                    if (!bodyRead && contentType.startsWith("text/plain")) {
+                        isHTML = false;
+                        text = (String) part.getContent();
+                    } else if (!bodyRead && contentType.startsWith("text/html")) {
+                        isHTML = true;
+                        text = (String) part.getContent();
+                    } else if (!bodyRead && contentType.startsWith("message/rfc822")) {
+                        // Extract the message and pass it
+                        MimeMessage msg = (MimeMessage) part.getDataHandler().getContent();
+                        isHTML = handleParts(msg, msg.getContent(), sbPlain, attachmentList);
+                    } else {
+                        if (part.getFileName() != null) {
+                            // Inline images are not added to the attachment
+                            // list
+                            // TODO: improve the in-line images detection
+                            if (part.getHeader("Content-ID") == null) {
+                                MessageAttachment attachment = new MessageAttachmentImpl();
+                                attachment.setName(MimeUtility.decodeText(part.getFileName()));
+                                attachment.setContentType(part.getContentType());
+                                attachment.setSize(part.getSize());
+                                attachmentList.add(attachment);
+                            }
+                        } else {
+                            isHTML = handleParts(message, part.getContent(), sbPlain, attachmentList);
+                        }
+                    }
+
+                }
+                if (text != null)
+                    sbPlain.append(text);
+            }
+
+        }
+        return isHTML;
+    }
+    
+    private static boolean handleMultiPartAlternative(Multipart mp, StringBuffer sbPlain) throws MessagingException, IOException {
+        String text = null;
+        boolean isHTML = false;
+        for (int i = 0; i < mp.getCount(); i++) {
+            Part part = mp.getBodyPart(i);
+
+            String contentType = part.getContentType().toLowerCase();
+
+            // we prefer html
+            if (text == null && contentType.startsWith("text/plain")) {
+                isHTML = false;
+                text = (String) part.getContent();
+            } else if (contentType.startsWith("text/html")) {
+                isHTML = true;
+                text = (String) part.getContent();
+            }
+        }
+        sbPlain.append(text);
+        return isHTML;
+    }
 
     /**
      * Loop over MuliPart and write the content to the Outputstream if a