List of usage examples for javax.mail.internet MimeMultipart getPreamble
public synchronized String getPreamble() throws MessagingException
From source file:com.mirth.connect.connectors.http.HttpMessageConverter.java
private static void processContent(DonkeyElement contentElement, Object content, ContentType contentType, boolean parseMultipart, BinaryContentTypeResolver resolver) throws DonkeyElementException, MessagingException, IOException { if (resolver == null) { resolver = defaultResolver;// w w w . j a v a 2s. c o m } if (parseMultipart && content instanceof MimeMultipart) { contentElement.setAttribute("multipart", "yes"); MimeMultipart multipart = (MimeMultipart) content; String boundary = contentType.getParameter("boundary"); if (StringUtils.isNotBlank(boundary)) { contentElement.setAttribute("boundary", boundary); } if (StringUtils.isNotEmpty(multipart.getPreamble())) { contentElement.addChildElement("Preamble", multipart.getPreamble()); } for (int partIndex = 0; partIndex < multipart.getCount(); partIndex++) { BodyPart part = multipart.getBodyPart(partIndex); DonkeyElement partElement = contentElement.addChildElement("Part"); DonkeyElement headersElement = partElement.addChildElement("Headers"); ContentType partContentType = contentType; for (Enumeration<javax.mail.Header> en = part.getAllHeaders(); en.hasMoreElements();) { javax.mail.Header header = en.nextElement(); headersElement.addChildElement(header.getName(), header.getValue()); if (header.getName().equalsIgnoreCase("Content-Type")) { try { partContentType = ContentType.parse(header.getValue()); } catch (RuntimeException e) { } } } processContent(partElement.addChildElement("Content"), part.getContent(), partContentType, true, resolver); } } else { contentElement.setAttribute("multipart", "no"); String charset = getDefaultHttpCharset( contentType.getCharset() != null ? contentType.getCharset().name() : null); // Call the resolver to determine if the content should be Base64 encoded if (resolver.isBinaryContentType(contentType)) { contentElement.setAttribute("encoding", "Base64"); byte[] contentByteArray = null; if (content instanceof StreamSource) { StreamSource streamSource = (StreamSource) content; if (streamSource.getInputStream() != null) { contentByteArray = IOUtils.toByteArray(streamSource.getInputStream()); } else if (streamSource.getReader() != null) { contentByteArray = IOUtils.toString(streamSource.getReader()).getBytes(charset); } } else if (content instanceof InputStream) { contentByteArray = IOUtils.toByteArray((InputStream) content); } else if (content instanceof byte[]) { contentByteArray = (byte[]) content; } if (contentByteArray == null) { contentByteArray = (content != null ? content.toString() : "").getBytes(charset); } contentElement.setTextContent(new String(Base64Util.encodeBase64(contentByteArray), "US-ASCII")); } else { String contentText = null; if (content instanceof StreamSource) { StreamSource streamSource = (StreamSource) content; if (streamSource.getInputStream() != null) { contentText = IOUtils.toString(streamSource.getInputStream(), charset); } else if (streamSource.getReader() != null) { contentText = IOUtils.toString(streamSource.getReader()); } } else if (content instanceof InputStream) { contentText = IOUtils.toString((InputStream) content, charset); } else if (content instanceof byte[]) { contentText = new String((byte[]) content, charset); } if (contentText == null) { contentText = content != null ? content.toString() : ""; } contentElement.setTextContent(contentText); } } }
From source file:com.zimbra.cs.mime.Mime.java
private static List<MPartInfo> listParts(MimePart root, String defaultCharset) throws MessagingException, IOException { List<MPartInfo> parts = new ArrayList<MPartInfo>(); LinkedList<MPartInfo> queue = new LinkedList<MPartInfo>(); queue.add(generateMPartInfo(root, null, "", 0)); MimeMultipart emptyMultipart = null; while (!queue.isEmpty()) { MPartInfo mpart = queue.removeFirst(); MimePart mp = mpart.getMimePart(); parts.add(mpart);/*w ww . j a v a 2 s . c o m*/ String cts = mpart.mContentType; boolean isMultipart = cts.startsWith(MimeConstants.CT_MULTIPART_PREFIX); boolean isMessage = !isMultipart && cts.equals(MimeConstants.CT_MESSAGE_RFC822); if (isMultipart) { // IMAP part numbering is screwy: top-level multipart doesn't get a number String prefix = mpart.mPartName.length() > 0 ? (mpart.mPartName + '.') : ""; if (mp instanceof MimeMessage) { mpart.mPartName = prefix + "TEXT"; } MimeMultipart multi = getMultipartContent(mp, cts); if (multi != null) { if (multi.getCount() == 0 && LC.mime_promote_empty_multipart.booleanValue()) { if (emptyMultipart == null) { emptyMultipart = multi; } if (MimeConstants.CT_MULTIPART_APPLEDOUBLE.equalsIgnoreCase(getContentType(mp))) { ZimbraLog.misc.debug( "appledouble with no children; assuming it is malformed and really applefile"); mpart.mContentType = mpart.mContentType.replace(MimeConstants.CT_MULTIPART_APPLEDOUBLE, MimeConstants.CT_APPLEFILE); } } mpart.mChildren = new ArrayList<MPartInfo>(multi.getCount()); for (int i = 1; i <= multi.getCount(); i++) { mpart.mChildren .add(generateMPartInfo((MimePart) multi.getBodyPart(i - 1), mpart, prefix + i, i)); } queue.addAll(0, mpart.mChildren); } } else if (isMessage) { MimeMessage mm = getMessageContent(mp); if (mm != null) { MPartInfo child = generateMPartInfo(mm, mpart, mpart.mPartName, 0); queue.addFirst(child); mpart.mChildren = Arrays.asList(child); } } else { // nothing to do at this stage } } if (emptyMultipart != null && parts.size() == 1) { String text = emptyMultipart.getPreamble(); if (!StringUtil.isNullOrEmpty(text)) { ZimbraLog.misc .debug("single multipart with no children. promoting the preamble into a single text part"); parts.remove(0); MPartInfo mpart = new MPartInfo(); ZMimeBodyPart mp = new ZMimeBodyPart(); mp.setText(text, defaultCharset); mpart.mPart = mp; mpart.mContentType = mp.getContentType(); mpart.mDisposition = ""; mpart.mPartName = "1"; parts.add(mpart); } } return parts; }
From source file:org.apache.jmeter.protocol.mail.sampler.MailReaderSampler.java
private void appendMultiPart(SampleResult child, StringBuilder cdata, MimeMultipart mmp) throws MessagingException, IOException { String preamble = mmp.getPreamble(); if (preamble != null) { cdata.append(preamble);/* w ww. j av a 2s . c o m*/ } child.setResponseData(cdata.toString(), child.getDataEncodingNoDefault()); int count = mmp.getCount(); for (int j = 0; j < count; j++) { BodyPart bodyPart = mmp.getBodyPart(j); final Object bodyPartContent = bodyPart.getContent(); final String contentType = bodyPart.getContentType(); SampleResult sr = new SampleResult(); sr.setSampleLabel("Part: " + j); sr.setContentType(contentType); sr.setDataEncoding(RFC_822_DEFAULT_ENCODING); sr.setEncodingAndType(contentType); sr.sampleStart(); if (bodyPartContent instanceof InputStream) { sr.setResponseData(IOUtils.toByteArray((InputStream) bodyPartContent)); } else if (bodyPartContent instanceof MimeMultipart) { appendMultiPart(sr, cdata, (MimeMultipart) bodyPartContent); } else { sr.setResponseData(bodyPartContent.toString(), sr.getDataEncodingNoDefault()); } sr.setResponseOK(); if (sr.getEndTime() == 0) {// not been set by any child samples sr.sampleEnd(); } child.addSubResult(sr); } }