use of javax.mail.internet.MimePartDataSource in project zm-mailbox by Zimbra.
the class ZMimeParser method endPart.
/** Records the endpoint of a MIME part. Stores both the byte offset of
* the end of the part as well as the line count of the part body. If
* the part being ended was a multipart preamble and was of nonzero
* length, associates the part with its parent appropriately. */
private PartInfo endPart(long end, boolean clean, long lineStart) {
PartInfo pinfo = parts.remove(parts.size() - 1);
ZMimePart mp = pinfo.part;
if (pinfo.location == PartLocation.PREAMBLE) {
long bodyEnd = Math.max(pinfo.bodyStart, lineStart), length = lineStart - pinfo.bodyStart;
SharedInputStream bodyStream = (SharedInputStream) sis.newStream(pinfo.bodyStart, bodyEnd);
if (!clean && !parts.isEmpty()) {
PartInfo pcurrent = currentPart();
String enc = pcurrent.part.getEncoding();
if (enc != null && !ZMimeBodyPart.RAW_ENCODINGS.contains(enc)) {
// supposedly-encoded multipart and no boundary hit -- defer decoding and parsing
pcurrent.multi.setDataSource(new MimePartDataSource(pcurrent.part));
// don't save this as a preamble!
length = 0;
}
}
if (length > 0) {
try {
if (length > MAXIMUM_HEADER_LENGTH) {
// constrain preamble length to some reasonable value (64K)
bodyStream = (SharedInputStream) bodyStream.newStream(0, length = MAXIMUM_HEADER_LENGTH);
}
// save preamble to *parent* multipart
String preamble = new String(ByteUtil.readInput((InputStream) bodyStream, (int) length, (int) length), charset);
currentPart().multi.setPreamble(preamble);
} catch (IOException ioe) {
}
}
} else {
long bodyEnd = Math.max(pinfo.bodyStart, end), length = end - pinfo.bodyStart;
SharedInputStream bodyStream = (SharedInputStream) sis.newStream(pinfo.bodyStart, bodyEnd);
mp.endPart(bodyStream, length, lineNumber - pinfo.firstLine);
}
return currentPart();
}
use of javax.mail.internet.MimePartDataSource in project zm-mailbox by Zimbra.
the class CreateContact method parseAttachment.
private static Attachment parseAttachment(Element elt, String name, ZimbraSoapContext zsc, OperationContext octxt, Contact existing) throws ServiceException {
// check for uploaded attachment
String attachId = elt.getAttribute(MailConstants.A_ATTACHMENT_ID, null);
if (attachId != null) {
if (Contact.isSMIMECertField(name)) {
elt.setText(parseCertificate(elt, name, zsc, octxt, existing));
return null;
} else {
Upload up = FileUploadServlet.fetchUpload(zsc.getAuthtokenAccountId(), attachId, zsc.getAuthToken());
UploadDataSource uds = new UploadDataSource(up);
return new Attachment(new DataHandler(uds), name, (int) up.getSize());
}
}
int itemId = (int) elt.getAttributeLong(MailConstants.A_ID, -1);
String part = elt.getAttribute(MailConstants.A_PART, null);
if (itemId != -1 || (part != null && existing != null)) {
MailItem item = itemId == -1 ? existing : getRequestedMailbox(zsc).getItemById(octxt, itemId, MailItem.Type.UNKNOWN);
try {
if (item instanceof Contact) {
Contact contact = (Contact) item;
if (part != null && !part.equals("")) {
try {
int partNum = Integer.parseInt(part) - 1;
if (partNum >= 0 && partNum < contact.getAttachments().size()) {
Attachment att = contact.getAttachments().get(partNum);
return new Attachment(att.getDataHandler(), name, att.getSize());
}
} catch (NumberFormatException nfe) {
}
throw ServiceException.INVALID_REQUEST("invalid contact part number: " + part, null);
} else {
VCard vcf = VCard.formatContact(contact);
return new Attachment(vcf.getFormatted().getBytes("utf-8"), "text/x-vcard; charset=utf-8", name, vcf.fn + ".vcf");
}
} else if (item instanceof Message) {
Message msg = (Message) item;
if (part != null && !part.equals("")) {
try {
MimePart mp = Mime.getMimePart(msg.getMimeMessage(), part);
if (mp == null) {
throw MailServiceException.NO_SUCH_PART(part);
}
DataSource ds = new MimePartDataSource(mp);
return new Attachment(new DataHandler(ds), name);
} catch (MessagingException me) {
throw ServiceException.FAILURE("error parsing blob", me);
}
} else {
DataSource ds = new MessageDataSource(msg);
return new Attachment(new DataHandler(ds), name, (int) msg.getSize());
}
} else if (item instanceof Document) {
Document doc = (Document) item;
if (part != null && !part.equals("")) {
throw MailServiceException.NO_SUCH_PART(part);
}
DataSource ds = new DocumentDataSource(doc);
return new Attachment(new DataHandler(ds), name, (int) doc.getSize());
}
} catch (IOException ioe) {
throw ServiceException.FAILURE("error attaching existing item data", ioe);
} catch (MessagingException e) {
throw ServiceException.FAILURE("error attaching existing item data", e);
}
}
return null;
}
Aggregations