Author: dongxu Date: Thu Sep 12 05:04:08 2013 New Revision: 1522439 URL: http://svn.apache.org/r1522439 Log: Move method to Util class
Modified: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java Modified: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java URL: http://svn.apache.org/viewvc/james/hupa/trunk/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java?rev=1522439&r1=1522438&r2=1522439&view=diff ============================================================================== --- james/hupa/trunk/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java (original) +++ james/hupa/trunk/server/src/main/java/org/apache/hupa/server/service/GetMessageDetailsServiceImpl.java Thu Sep 12 05:04:08 2013 @@ -63,14 +63,11 @@ import javax.mail.Flags.Flag; 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 @@ public class GetMessageDetailsServiceImp 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 @@ public class GetMessageDetailsServiceImp 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) Modified: james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java URL: http://svn.apache.org/viewvc/james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java?rev=1522439&r1=1522438&r2=1522439&view=diff ============================================================================== --- james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java (original) +++ james/hupa/trunk/server/src/main/java/org/apache/hupa/server/utils/MessageUtils.java Thu Sep 12 05:04:08 2013 @@ -73,6 +73,7 @@ import javax.mail.Part; 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 @@ import org.apache.commons.logging.Log; <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD +<<<<<<< HEAD ======= import org.apache.hupa.server.handler.AbstractSendMessageHandler; >>>>>>> first commit @@ -88,6 +90,10 @@ import org.apache.hupa.server.handler.Ab >>>>>>> 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 @@ public class MessageUtils { } 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 --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org