List of usage examples for javax.mail.internet MimePart getHeader
public String getHeader(String name, String delimiter) throws MessagingException;
From source file:com.zimbra.cs.imap.ImapMessage.java
static void serializeStructure(PrintStream ps, MimeMessage root, boolean extensions) throws IOException, MessagingException { LinkedList<LinkedList<MPartInfo>> queue = new LinkedList<LinkedList<MPartInfo>>(); LinkedList<MPartInfo> level = new LinkedList<MPartInfo>(); level.add(Mime.getParts(root).get(0)); queue.add(level);// w w w . j ava2 s. co m boolean pop = false; while (!queue.isEmpty()) { level = queue.getLast(); if (level.isEmpty()) { queue.removeLast(); pop = true; continue; } MPartInfo mpi = level.getFirst(); MimePart mp = mpi.getMimePart(); boolean hasChildren = mpi.getChildren() != null && !mpi.getChildren().isEmpty(); // we used to force unset charsets on text/plain parts to US-ASCII, but that always seemed unwise... ContentType ctype = new ContentType(mp.getHeader("Content-Type", null)) .setContentType(mpi.getContentType()); String primary = nATOM(ctype.getPrimaryType()), subtype = nATOM(ctype.getSubType()); if (!pop) ps.write('('); if (primary.equals("\"MULTIPART\"")) { if (!pop) { // 7.4.2: "Multiple parts are indicated by parenthesis nesting. Instead of a body type // as the first element of the parenthesized list, there is a sequence of one // or more nested body structures. The second element of the parenthesized // list is the multipart subtype (mixed, digest, parallel, alternative, etc.)." if (!hasChildren) { ps.print("NIL"); } else { queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren())); continue; } } ps.write(' '); ps.print(subtype); if (extensions) { // 7.4.2: "Extension data follows the multipart subtype. Extension data is never // returned with the BODY fetch, but can be returned with a BODYSTRUCTURE // fetch. Extension data, if present, MUST be in the defined order. The // extension data of a multipart body part are in the following order: // body parameter parenthesized list, body disposition, body language, // body location" ps.write(' '); nparams(ps, ctype); ps.write(' '); ndisposition(ps, mp.getHeader("Content-Disposition", null)); ps.write(' '); nlist(ps, mp.getContentLanguage()); ps.write(' '); nstring(ps, mp.getHeader("Content-Location", null)); } } else { if (!pop) { // 7.4.2: "The basic fields of a non-multipart body part are in the following order: // body type, body subtype, body parameter parenthesized list, body id, body // description, body encoding, body size." String cte = mp.getEncoding(); cte = (cte == null || cte.trim().equals("") ? "7bit" : cte); aSTRING(ps, ctype.getPrimaryType()); ps.write(' '); aSTRING(ps, ctype.getSubType()); ps.write(' '); nparams(ps, ctype); ps.write(' '); nstring(ps, mp.getContentID()); ps.write(' '); nstring2047(ps, mp.getDescription()); ps.write(' '); aSTRING(ps, cte); ps.write(' '); ps.print(Math.max(mp.getSize(), 0)); } boolean rfc822 = primary.equals("\"MESSAGE\"") && subtype.equals("\"RFC822\""); if (rfc822) { // 7.4.2: "A body type of type MESSAGE and subtype RFC822 contains, immediately // after the basic fields, the envelope structure, body structure, and // size in text lines of the encapsulated message." if (!pop) { if (!hasChildren) { ps.print(" NIL NIL"); } else { MimeMessage mm = (MimeMessage) mpi.getChildren().get(0).getMimePart(); ps.write(' '); serializeEnvelope(ps, mm); ps.write(' '); queue.addLast(new LinkedList<MPartInfo>(mpi.getChildren())); continue; } } ps.write(' '); ps.print(getLineCount(mp)); } else if (primary.equals("\"TEXT\"")) { // 7.4.2: "A body type of type TEXT contains, immediately after the basic fields, the // size of the body in text lines. Note that this size is the size in its // content transfer encoding and not the resulting size after any decoding." ps.write(' '); ps.print(getLineCount(mp)); } if (extensions) { // 7.4.2: "Extension data follows the basic fields and the type-specific fields // listed above. Extension data is never returned with the BODY fetch, // but can be returned with a BODYSTRUCTURE fetch. Extension data, if // present, MUST be in the defined order. The extension data of a // non-multipart body part are in the following order: body MD5, body // disposition, body language, body location" ps.write(' '); nstring(ps, mp.getContentMD5()); ps.write(' '); ndisposition(ps, mp.getHeader("Content-Disposition", null)); ps.write(' '); nlist(ps, mp.getContentLanguage()); ps.write(' '); nstring(ps, mp.getHeader("Content-Location", null)); } } ps.write(')'); level.removeFirst(); pop = false; } }
From source file:com.zimbra.cs.mime.Mime.java
/** Determines the "primary/subtype" part of a Part's Content-Type * header. Uses a permissive, RFC2231-capable parser, and defaults * as indicated. */// w w w . ja v a2 s . co m public static final String getContentType(MimePart mp, String ctdefault) { try { String cthdr = mp.getHeader("Content-Type", null); if (cthdr == null || cthdr.trim().isEmpty()) return ctdefault; return getContentType(cthdr); } catch (MessagingException e) { ZimbraLog.extensions .warn("could not fetch part's content-type; defaulting to " + MimeConstants.CT_DEFAULT, e); return MimeConstants.CT_DEFAULT; } }
From source file:com.zimbra.cs.mime.Mime.java
/** * Returns the decoded and unfolded value for the given header name. If * multiple headers with the same name exist, returns the first one. * If the header does not exist, returns <tt>null</tt>. *///from www. ja v a2 s .c o m public static String getHeader(MimePart part, String headerName) { try { String value = part.getHeader(headerName, null); if (value == null || value.isEmpty()) { return null; } try { value = MimeUtility.decodeText(value); } catch (UnsupportedEncodingException e) { } value = MimeUtility.unfold(value); return value; } catch (MessagingException e) { sLog.debug("Unable to get header '%s'", headerName, e); return null; } }
From source file:com.zimbra.cs.mime.Mime.java
public static void repairTransferEncoding(MimePart mp) throws MessagingException { if (isZimbraJavaMailShim(mp)) { return;/*from w w w . j a v a2 s.c o m*/ } String cte = mp.getHeader("Content-Transfer-Encoding", null); String ct = getContentType(mp); if (cte != null && (!TRANSFER_ENCODINGS.contains(cte.toLowerCase().trim()) || ct.startsWith(MimeConstants.CT_MULTIPART_PREFIX) || ct.equals(MimeConstants.CT_MESSAGE_RFC822))) mp.removeHeader("Content-Transfer-Encoding"); }
From source file:com.zimbra.cs.mime.Mime.java
public static String getFilename(MimePart mp) { String name = null;/*from w w w .j a va 2 s . co m*/ // first, check the Content-Disposition header for the "filename" parameter try { String cdisp = mp.getHeader("Content-Disposition", null); if (cdisp != null) { // will also catch (legal, but uncommon) RFC 2231 encoded filenames // (things like filename*=UTF-8''%E3%82%BD%E3%83%AB%E3%83%86%E3%82%A3.rtf) name = new ContentDisposition(cdisp).getParameter("filename"); } } catch (MessagingException me) { } // if we didn't find anything, check the Content-Type header for the "name" parameter if (name == null) { try { String ctype = mp.getHeader("Content-Type", null); if (ctype != null) { // will also catch (legal, but uncommon) RFC 2231 encoded filenames // (things like name*=UTF-8''%E3%82%BD%E3%83%AB%E3%83%86%E3%82%A3.rtf) name = new ContentType(ctype).getParameter("name"); } } catch (MessagingException me) { } } if (name == null) { return null; } name = StringUtil.sanitizeFilename(name); // catch (illegal, but less common) character entities if (name.indexOf("&#") != -1 && name.indexOf(';') != -1) { return expandNumericCharacterReferences(name); } return name; }
From source file:com.zimbra.cs.service.mail.ToXML.java
private static Element addPart(VisitPhase phase, Element parent, Element root, MPartInfo mpi, Set<MPartInfo> bodies, String prefix, int maxSize, boolean neuter, boolean excludeCalendarParts, String defaultCharset, boolean swallowContentExceptions, MsgContent wantContent) throws ServiceException { if (phase == VisitPhase.POSTVISIT) { return null; }//w w w. java 2 s. com String ctype = StringUtil.stripControlCharacters(mpi.getContentType()); if (excludeCalendarParts && MimeConstants.CT_TEXT_CALENDAR.equalsIgnoreCase(ctype)) { // A true calendar part has "method" parameter in the content type. Otherwise it's just an attachment // that happens to be a .ics file. try { ContentType ct = new ContentType(mpi.getMimePart().getContentType()); if (ct.getParameter("method") != null) { return null; } } catch (MessagingException e) { } } Element el = parent.addNonUniqueElement(MailConstants.E_MIMEPART); MimePart mp = mpi.getMimePart(); String part = mpi.getPartName(); part = prefix + (prefix.isEmpty() || part.isEmpty() ? "" : ".") + part; el.addAttribute(MailConstants.A_PART, part); String fname = Mime.getFilename(mp); if (MimeConstants.CT_XML_ZIMBRA_SHARE.equals(ctype)) { // the <shr> share info goes underneath the top-level <m> Element shr = root.addNonUniqueElement(MailConstants.E_SHARE_NOTIFICATION); try { addContent(shr, mpi, maxSize, defaultCharset); } catch (IOException e) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing share XML", e); } else { LOG.warn("error writing body part", e); } } catch (MessagingException e) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing share XML", e); } } } else if (MimeConstants.CT_XML_ZIMBRA_DL_SUBSCRIPTION.equals(ctype)) { // the <dlSubs> dl subscription info goes underneath the top-level <m> Element dlSubs = root.addNonUniqueElement(MailConstants.E_DL_SUBSCRIPTION_NOTIFICATION); try { addContent(dlSubs, mpi, maxSize, defaultCharset); } catch (IOException e) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing DL subscription", e); } else { LOG.warn("error writing body part", e); } } catch (MessagingException e) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing DL subscription", e); } } } else if (MimeConstants.CT_TEXT_ENRICHED.equals(ctype)) { // we'll be replacing text/enriched with text/html ctype = MimeConstants.CT_TEXT_HTML; } else if (fname != null && (MimeConstants.CT_APPLICATION_OCTET_STREAM.equals(ctype) || MimeConstants.CT_APPLICATION_TNEF.equals(ctype))) { String guess = MimeDetect.getMimeDetect().detect(fname); if (guess != null) { ctype = guess; } } el.addAttribute(MailConstants.A_CONTENT_TYPE, ctype); if (mpi.isMultipart()) { return el; // none of the below stuff is relevant for a multipart, so just return now... } // figure out attachment size try { el.addAttribute(MailConstants.A_SIZE, Mime.getSize(mp)); } catch (Exception e) { // don't put out size if we get exception ZimbraLog.mailbox.warn("Unable to determine MIME part size: %s", e.getMessage()); } // figure out attachment disposition try { String disp = mp.getHeader("Content-Disposition", null); if (disp != null) { ContentDisposition cdisp = new ContentDisposition(MimeUtility.decodeText(disp)); el.addAttribute(MailConstants.A_CONTENT_DISPOSITION, StringUtil.stripControlCharacters(cdisp.getDisposition())); } } catch (MessagingException e) { } catch (UnsupportedEncodingException e) { } // figure out attachment name try { if (fname == null && MimeConstants.CT_MESSAGE_RFC822.equals(ctype)) { // "filename" for attached messages is the Subject Object content = Mime.getMessageContent(mp); if (content instanceof MimeMessage) { fname = Mime.getSubject((MimeMessage) content); } } if (!Strings.isNullOrEmpty(fname)) { el.addAttribute(MailConstants.A_CONTENT_FILENAME, StringUtil.stripControlCharacters(fname)); } } catch (MessagingException me) { } catch (IOException ioe) { } // figure out content-id (used in displaying attached images) String cid = mpi.getContentID(); if (cid != null) { el.addAttribute(MailConstants.A_CONTENT_ID, StringUtil.stripControlCharacters(cid)); } // figure out content-location (used in displaying attached images) try { String cl = mp.getHeader("Content-Location", null); if (cl != null) { el.addAttribute(MailConstants.A_CONTENT_LOCATION, StringUtil.stripControlCharacters(cl)); } } catch (MessagingException e) { } // include the part's content if this is the displayable "memo part", // or if it was requested to include all parts if (bodies == null || bodies.contains(mpi)) { if (bodies != null) { el.addAttribute(MailConstants.A_BODY, true); } try { addContent(el, mpi, maxSize, neuter, defaultCharset, wantContent); } catch (IOException e) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing part content", e); } else { LOG.warn("error writing body part", e); } } catch (MessagingException me) { if (!swallowContentExceptions) { throw ServiceException.FAILURE("error serializing part content", me); } } } return el; }