use of javax.mail.internet.MimePart in project ats-framework by Axway.
the class MimePackage method getAttachmentFileName.
/**
* Get an attachment's file name
*
* @param partIndex
* @return
* @throws PackageException
*/
@PublicAtsApi
public String getAttachmentFileName(int partIndex) throws PackageException {
// first check if there is part at this position at all
if (partIndex >= attachmentPartIndices.size()) {
throw new NoSuchMimePartException("No attachment at position '" + partIndex + "'");
}
try {
MimePart part = getPart(attachmentPartIndices.get(partIndex));
// get the attachment file name
String fileName = part.getFileName();
if (fileName == null) {
throw new PackageException("Could not determine file name for attachment at position " + partIndex);
}
return fileName;
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class AttachmentDataSource method getName.
public String getName() {
MimePart mp = null;
String name = null;
try {
mp = getMimePart();
if (mp != null) {
name = mp.getFileName();
}
} catch (Exception e) {
ZimbraLog.mailbox.error("Unable to determine the filename for contact %d, part %s.", mContact.getId(), mPartName, e);
}
return name;
}
use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class Mime method listParts.
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);
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;
}
use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class SyncFormatter method handleMessagePart.
private void handleMessagePart(UserServletContext context, MailItem item) throws IOException, ServiceException, MessagingException, UserServletException {
if (!(item instanceof Message))
throw UserServletException.notImplemented("can only handle messages");
Message message = (Message) item;
MimePart mp = getMimePart(message, context.getPart());
if (mp != null) {
String contentType = mp.getContentType();
if (contentType == null)
contentType = MimeConstants.CT_APPLICATION_OCTET_STREAM;
sendbackOriginalDoc(mp, contentType, context.req, context.resp);
return;
}
context.resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "part not found");
}
use of javax.mail.internet.MimePart in project zm-mailbox by Zimbra.
the class TestMain method doConversion.
private static boolean doConversion(MimeMessage mm, Writer out, TnefToICalendar converter, File tnefFile, boolean debug) throws ServiceException, IOException, MessagingException {
boolean successful = false;
// Find the TNEF part.
TNEFPartFinder finder = new TNEFPartFinder();
finder.accept(mm);
MimePart tnefPart = finder.getTNEFPart();
if (tnefPart == null)
throw ServiceException.FAILURE("No TNEF part in the message!", null);
if (tnefFile != null) {
FileOutputStream wout = null;
try {
byte[] bytes = ByteUtil.getContent(tnefPart.getInputStream(), tnefPart.getSize());
wout = new FileOutputStream(tnefFile);
wout.write(bytes);
wout.flush();
} finally {
if (wout != null) {
wout.close();
}
}
}
if (debug) {
// Test text part extraction.
String desc = getPlainText(mm);
if (desc != null) {
System.out.println("<<< TEXT PART BEGIN >>>");
System.out.println(desc);
System.out.println("<<< TEXT PART END >>>");
}
}
if (converter == null)
throw ServiceException.FAILURE("No converter!", null);
TestContentHandler icalGen = new TestContentHandler(debug);
successful = converter.convert(mm, tnefPart.getInputStream(), icalGen);
if (successful) {
for (ZVCalendar cal : icalGen.getCals()) {
cal.toICalendar(out);
}
}
out.flush();
return successful;
}
Aggregations