List of usage examples for javax.mail Part getHeader
public String[] getHeader(String header_name) throws MessagingException;
From source file:org.apache.hupa.server.handler.GetMessageDetailsHandler.java
/** * Handle the parts of the given message. The method will call itself recursively to handle all nested parts * @param message the MimeMessage /*from w w w . j a va 2 s . c o m*/ * @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 MessageAttachment(); 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; }
From source file:org.apache.hupa.server.utils.MessageUtils.java
/** * Handle the parts of the given message. The method will call itself * recursively to handle all nested parts * * @param message the MimeMessage//from ww w.j a va 2s. c o m * @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(Message 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; }
From source file:org.apache.hupa.server.utils.MessageUtils.java
/** * Loop over MuliPart and write the content to the Outputstream if a * attachment with the given name was found. * * @param logger//from w w w . j ava2 s . c o m * The logger to use * @param content * Content which should checked for attachments * @param attachmentName * The attachmentname or the unique id for the searched attachment * @throws MessagingException * @throws IOException */ public static Part handleMultiPart(Log logger, Object content, String attachmentName) throws MessagingException, IOException { if (content instanceof Multipart) { Multipart part = (Multipart) content; for (int i = 0; i < part.getCount(); i++) { Part bodyPart = part.getBodyPart(i); String fileName = bodyPart.getFileName(); String[] contentId = bodyPart.getHeader("Content-ID"); if (bodyPart.isMimeType("multipart/*")) { Part p = handleMultiPart(logger, bodyPart.getContent(), attachmentName); if (p != null) return p; } else { if (contentId != null) { for (String id : contentId) { id = id.replaceAll("^.*<(.+)>.*$", "$1"); System.out.println(attachmentName + " " + id); if (attachmentName.equals(id)) return bodyPart; } } if (fileName != null) { if (cleanName(attachmentName).equalsIgnoreCase(cleanName(MimeUtility.decodeText(fileName)))) return bodyPart; } } } } else { logger.error("Unknown content: " + content.getClass().getName()); } return null; }
From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java
/** * {@inheritDoc}// w w w . j ava 2s. com * * @throws MessagingException * @throws IOException * @see org.xwiki.contrib.mail.internal.IMessageParser#parseHeaders(java.lang.Object) */ @Override public MailItem parseHeaders(Part mail) throws MessagingException, IOException { MailItem m = new MailItem(); String[] headers; String value = null; value = extractSingleHeader(mail, "Message-ID"); value = Utils.cropId(value); m.setMessageId(value); value = extractSingleHeader(mail, "In-Reply-To"); value = Utils.cropId(value); m.setReplyToId(value); value = extractSingleHeader(mail, "References"); m.setRefs(value); value = extractSingleHeader(mail, "Subject"); if (StringUtils.isBlank(value)) { value = DEFAULT_SUBJECT; } value = value.replaceAll("[\n\r]", "").replaceAll(">", ">").replaceAll("<", "<"); m.setSubject(value); // If topic is not provided, we use message subject without the beginning junk value = extractSingleHeader(mail, "Thread-Topic"); if (StringUtils.isBlank(value)) { value = m.getSubject().replaceAll("(?mi)([\\[\\(] *)?(RE|FWD?) *([-:;)\\]][ :;\\])-]*|$)|\\]+ *$", ""); } else { value = Utils.removeCRLF(value); } m.setTopic(value); // Topic Id : if none is provided, we use the message-id as topic id value = extractSingleHeader(mail, "Thread-Index"); if (!StringUtils.isBlank(value)) { value = Utils.cropId(value); } m.setTopicId(value); value = extractSingleHeader(mail, "From"); value = value.replaceAll("\"", "").replaceAll("[\n\r]", ""); m.setFrom(value); value = extractSingleHeader(mail, "Sender"); value = value.replaceAll("\"", "").replaceAll("[\n\r]", ""); m.setSender(value); value = extractSingleHeader(mail, "To"); value = value.replaceAll("\"", "").replaceAll("[\n\r]", ""); m.setTo(value); value = extractSingleHeader(mail, "CC"); value = value.replaceAll("\"", "").replaceAll("[\n\r]", ""); m.setCc(value); // process the locale, if any provided String locLang = "en"; String locCountry = "US"; String language; headers = mail.getHeader("Content-Language"); if (headers != null) { language = headers[0]; if (language != null && !language.isEmpty()) { int index = language.indexOf('.'); if (index != -1) { locLang = language.substring(0, index - 1); locCountry = language.substring(index); } } } Locale locale = new Locale(locLang, locCountry); m.setLocale(locale); String date = ""; Date decodedDate = null; headers = mail.getHeader("Date"); if (headers != null) { date = headers[0]; } // Decode the date try { logger.debug("Parsing date [" + date + "] with Javamail MailDateFormat"); decodedDate = new MailDateFormat().parse(date); } catch (ParseException e) { logger.debug("Could not parse date header " + ExceptionUtils.getRootCauseMessage(e)); decodedDate = null; } if (decodedDate == null) { try { logger.debug("Parsing date [" + date + "] with GMail parser"); decodedDate = new GMailMailDateFormat().parse(date); } catch (ParseException e) { logger.info( "Could not parse date header with GMail parser " + ExceptionUtils.getRootCauseMessage(e)); decodedDate = new Date(); logger.info("Using 'now' as date as date could not be parsed"); } } m.setDate(decodedDate); boolean firstInTopic = ("".equals(m.getReplyToId())); m.setFirstInTopic(firstInTopic); m.setOriginalMessage((Message) mail); m.setBodypart(mail.getContent()); m.setContentType(mail.getContentType().toLowerCase()); String sensitivity = "normal"; headers = mail.getHeader("Sensitivity"); if (headers != null && !headers[0].isEmpty()) { sensitivity = "normal"; } m.setSensitivity(sensitivity.toLowerCase()); String importance = "normal"; headers = mail.getHeader("Importance"); if (importance == null || importance == "") { importance = "normal"; } m.setImportance(importance.toLowerCase()); // type m.setBuiltinType("mail"); return m; }
From source file:org.xwiki.contrib.mail.internal.JavamailMessageParser.java
/** * Gets a unique value from a mail header. If header is present more than once, only first value is returned. Value * is Mime decoded, if needed. If header is not found, an empty string is returned. * /*from w w w . ja va 2 s. c o m*/ * @param part * @param name Header identifier. * @return First header value, or empty string if not found. * @throws MessagingException */ public static String extractSingleHeader(final Part part, final String name) throws MessagingException { String[] values = part.getHeader(name); if (values != null && values.length > 0) { try { return MimeUtility.decodeText(values[0]); } catch (UnsupportedEncodingException e) { return values[0]; } } return ""; }