Search in sources :

Example 1 with UploadDataSource

use of com.zimbra.cs.service.UploadDataSource 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;
}
Also used : Message(com.zimbra.cs.mailbox.Message) MessagingException(javax.mail.MessagingException) Upload(com.zimbra.cs.service.FileUploadServlet.Upload) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) MimePartDataSource(javax.mail.internet.MimePartDataSource) UploadDataSource(com.zimbra.cs.service.UploadDataSource) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) DataSource(javax.activation.DataSource) MailItem(com.zimbra.cs.mailbox.MailItem) MessageDataSource(com.zimbra.cs.mailbox.MessageDataSource) MimePartDataSource(javax.mail.internet.MimePartDataSource) DocumentDataSource(com.zimbra.cs.mailbox.DocumentDataSource) MimePart(javax.mail.internet.MimePart) UploadDataSource(com.zimbra.cs.service.UploadDataSource) VCard(com.zimbra.cs.service.formatter.VCard)

Example 2 with UploadDataSource

use of com.zimbra.cs.service.UploadDataSource in project zm-mailbox by Zimbra.

the class ParseMimeMessage method attachUpload.

private static void attachUpload(MimeMultipart mmp, Upload up, String contentID, ParseMessageContext ctxt, ContentType ctypeOverride, String contentDescription, String contentDisposition) throws ServiceException, MessagingException {
    // make sure we haven't exceeded the max size
    ctxt.incrementSize("upload " + up.getName(), (long) (up.getSize() * 1.33));
    // scan upload for viruses
    StringBuffer info = new StringBuffer();
    UploadScanner.Result result = UploadScanner.accept(up, info);
    if (result == UploadScanner.REJECT) {
        throw MailServiceException.UPLOAD_REJECTED(up.getName(), info.toString());
    } else if (result == UploadScanner.ERROR) {
        throw MailServiceException.SCAN_ERROR(up.getName());
    }
    String filename = up.getName();
    // create the part and override the DataSource's default ctype, if required
    MimeBodyPart mbp = new ForceBase64MimeBodyPart();
    UploadDataSource uds = new UploadDataSource(up);
    if (ctypeOverride != null && !ctypeOverride.equals("")) {
        uds.setContentType(ctypeOverride);
    }
    mbp.setDataHandler(new DataHandler(uds));
    // set headers -- ctypeOverride non-null has magical properties that I'm going to regret tomorrow
    ContentType ctype = ctypeOverride;
    ContentDisposition cdisp;
    if (Part.INLINE.equalsIgnoreCase(contentDisposition)) {
        cdisp = new ContentDisposition(Part.INLINE, ctxt.use2231);
    } else {
        cdisp = new ContentDisposition(Part.ATTACHMENT, ctxt.use2231);
    }
    if (ctype == null) {
        ctype = new ContentType(up.getContentType() == null ? MimeConstants.CT_APPLICATION_OCTET_STREAM : up.getContentType());
        ctype.cleanup().setParameter("name", filename);
        cdisp.setParameter("filename", filename);
    }
    mbp.setHeader("Content-Type", ctype.setCharset(ctxt.defaultCharset).toString());
    mbp.setHeader("Content-Disposition", cdisp.setCharset(ctxt.defaultCharset).toString());
    if (contentDescription != null) {
        mbp.setHeader("Content-Description", contentDescription);
    }
    if (ctype.getContentType().equals(MimeConstants.CT_APPLICATION_PDF)) {
        mbp.setHeader("Content-Transfer-Encoding", "base64");
    }
    mbp.setContentID(contentID);
    // add to the parent part
    mmp.addBodyPart(mbp);
}
Also used : ContentType(com.zimbra.common.mime.ContentType) ContentDisposition(com.zimbra.common.mime.ContentDisposition) UploadDataSource(com.zimbra.cs.service.UploadDataSource) DataHandler(javax.activation.DataHandler) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

UploadDataSource (com.zimbra.cs.service.UploadDataSource)2 DataHandler (javax.activation.DataHandler)2 ContentDisposition (com.zimbra.common.mime.ContentDisposition)1 ContentType (com.zimbra.common.mime.ContentType)1 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)1 Contact (com.zimbra.cs.mailbox.Contact)1 Attachment (com.zimbra.cs.mailbox.Contact.Attachment)1 Document (com.zimbra.cs.mailbox.Document)1 DocumentDataSource (com.zimbra.cs.mailbox.DocumentDataSource)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 Message (com.zimbra.cs.mailbox.Message)1 MessageDataSource (com.zimbra.cs.mailbox.MessageDataSource)1 ParsedContact (com.zimbra.cs.mime.ParsedContact)1 Upload (com.zimbra.cs.service.FileUploadServlet.Upload)1 VCard (com.zimbra.cs.service.formatter.VCard)1 IOException (java.io.IOException)1 DataSource (javax.activation.DataSource)1 MessagingException (javax.mail.MessagingException)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimePart (javax.mail.internet.MimePart)1