Search in sources :

Example 46 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class CalendarItem method createBlob.

/**
     * The Blob for the appointment/task is currently single Mime multipart/digest which has
     * each invite's MimeMessage stored as a part.
     *
     * @param invPm
     * @param firstInvite
     * @throws ServiceException
     */
private MailboxBlob createBlob(ParsedMessage invPm, Invite firstInvite) throws ServiceException {
    // Create blob only if there's an attachment or DESCRIPTION is too big to be stored in metadata.
    if (!firstInvite.hasAttachment() && (invPm == null || firstInvite.descInMeta())) {
        // If we're not going to create a blob after all, we must at least save the metadata to db.
        // It's weird that the db update is implicitly required of this method, but that's the way
        // it is, unfortunately.  If we're creating a blob, the implicit db update is done by
        // storeUpdatedBlob() call.  (see below)
        saveMetadata();
        return null;
    }
    try {
        // create the toplevel multipart/digest...
        MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
        MimeMultipart mmp = new ZMimeMultipart("digest");
        mm.setContent(mmp);
        // add the invite
        MimeBodyPart mbp = new ZMimeBodyPart();
        mbp.setDataHandler(new DataHandler(new PMDataSource(invPm)));
        mbp.addHeader("invId", Integer.toString(firstInvite.getMailItemId()));
        mmp.addBodyPart(mbp);
        mm.saveChanges();
        return storeUpdatedBlob(mm);
    } catch (MessagingException e) {
        throw ServiceException.FAILURE("MessagingException " + e, e);
    } catch (IOException e) {
        throw ServiceException.FAILURE("IOException " + e, e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 47 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class Contact method decodeMetadata.

@Override
void decodeMetadata(Metadata meta) throws ServiceException {
    Metadata metaAttrs;
    if (meta.containsKey(Metadata.FN_FIELDS)) {
        // new version: fields are in their own subhash
        super.decodeMetadata(meta);
        metaAttrs = meta.getMap(Metadata.FN_FIELDS);
        MetadataList mlAttach = meta.getList(Metadata.FN_ATTACHMENTS, true);
        if (mlAttach != null) {
            attachments = new ArrayList<Attachment>(mlAttach.size());
            for (int i = 0; i < mlAttach.size(); i++) {
                Metadata attachMeta = mlAttach.getMap(i);
                String fieldName = attachMeta.get(Attachment.FN_FIELD);
                String partName = attachMeta.get(Attachment.FN_PART);
                int size = (int) attachMeta.getLong(Attachment.FN_SIZE);
                DataHandler dh = new DataHandler(new AttachmentDataSource(this, partName));
                Attachment attachment = new Attachment(dh, fieldName, size);
                attachment.setPartName(partName);
                attachments.add(attachment);
            }
        }
    } else {
        // version 8 or earlier; metadata is just the fields
        metaAttrs = meta;
    }
    fields = new HashMap<String, String>();
    for (Map.Entry<String, ?> entry : metaAttrs.asMap().entrySet()) {
        fields.put(entry.getKey(), entry.getValue().toString());
    }
}
Also used : CustomMetadataList(com.zimbra.cs.mailbox.MailItem.CustomMetadata.CustomMetadataList) DataHandler(javax.activation.DataHandler) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class Contact method reanalyze.

@Override
void reanalyze(Object data, long newSize) throws ServiceException {
    if (!(data instanceof ParsedContact)) {
        throw ServiceException.FAILURE("cannot reanalyze non-ParsedContact object", null);
    }
    ParsedContact pc = (ParsedContact) data;
    markItemModified(Change.CONTENT | Change.DATE | Change.FLAGS);
    fields = pc.getFields();
    if (fields == null || fields.isEmpty()) {
        throw ServiceException.INVALID_REQUEST("contact must have fields", null);
    }
    attachments = pc.getAttachments();
    if (attachments != null && attachments.size() > 0) {
        for (Attachment attach : attachments) {
            //refresh the data handler. the content rev changes which can change the blob filename/locator.
            //not safe to keep old data handler that may point to old blob
            attach.mDataHandler = new DataHandler(new AttachmentDataSource(this, attach.mPartName));
        }
    }
    mData.unsetFlag(Flag.FlagInfo.ATTACHED);
    if (pc.hasAttachment()) {
        mData.setFlag(Flag.FlagInfo.ATTACHED);
    }
    saveData(new DbMailItem(mMailbox).setSender(getFileAsString(fields)));
}
Also used : ParsedContact(com.zimbra.cs.mime.ParsedContact) DbMailItem(com.zimbra.cs.db.DbMailItem) DataHandler(javax.activation.DataHandler)

Example 49 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class ParsedContact method generateMimeMessage.

private static MimeMessage generateMimeMessage(List<Attachment> attachments) throws MessagingException {
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession());
    MimeMultipart multi = new ZMimeMultipart("mixed");
    int part = 1;
    for (Attachment attach : attachments) {
        ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT);
        cdisp.setParameter("filename", attach.getFilename()).setParameter("field", attach.getName());
        MimeBodyPart bp = new ZMimeBodyPart();
        // it gets called before setting Content-Type and CTE headers.
        try {
            bp.setDataHandler(new DataHandler(new ByteArrayDataSource(attach.getContent(), attach.getContentType())));
        } catch (IOException e) {
            throw new MessagingException("could not generate mime part content", e);
        }
        bp.addHeader("Content-Disposition", cdisp.toString());
        bp.addHeader("Content-Type", attach.getContentType());
        bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT);
        multi.addBodyPart(bp);
        attach.setPartName(Integer.toString(part++));
    }
    mm.setContent(multi);
    mm.saveChanges();
    return mm;
}
Also used : ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) Attachment(com.zimbra.cs.mailbox.Contact.Attachment) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) ContentDisposition(com.zimbra.common.mime.ContentDisposition) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 50 with DataHandler

use of javax.activation.DataHandler in project zm-mailbox by Zimbra.

the class SpamHandler method sendReport.

private void sendReport(SpamReport sr) throws ServiceException, MessagingException {
    Config config = Provisioning.getInstance().getConfig();
    String isSpamString = sr.isSpam ? config.getSpamReportTypeSpam() : config.getSpamReportTypeHam();
    SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
    Mailbox mbox = MailboxManager.getInstance().getMailboxById(sr.mailboxId);
    Message msg = mbox.getMessageById(null, sr.messageId);
    MimeMultipart mmp = new ZMimeMultipart("mixed");
    MimeBodyPart infoPart = new ZMimeBodyPart();
    infoPart.setHeader("Content-Description", "Zimbra spam classification report");
    String body = String.format("Classified-By: %s\r\n" + "Classified-As: %s\r\n" + "Action: %s\r\n" + "Source-Folder: %s\r\n" + "Destination-Folder: %s\r\n" + "Destination-Mailbox: %s\r\n", Strings.nullToEmpty(sr.accountName), isSpamString, Strings.nullToEmpty(sr.action), Strings.nullToEmpty(sr.sourceFolder), Strings.nullToEmpty(sr.destFolder), Strings.nullToEmpty(sr.destAccountName));
    infoPart.setText(body);
    mmp.addBodyPart(infoPart);
    MailboxBlob blob = msg.getBlob();
    MimeBodyPart mbp = new ZMimeBodyPart();
    mbp.setDataHandler(new DataHandler(new MailboxBlobDataSource(blob)));
    mbp.setHeader("Content-Type", MimeConstants.CT_MESSAGE_RFC822);
    mbp.setHeader("Content-Disposition", Part.ATTACHMENT);
    mmp.addBodyPart(mbp);
    out.setContent(mmp);
    out.addHeader(config.getSpamReportSenderHeader(), sr.accountName);
    out.addHeader(config.getSpamReportTypeHeader(), isSpamString);
    if (config.isSmtpSendAddOriginatingIP() && sr.origIp != null)
        out.addHeader(MailSender.X_ORIGINATING_IP, MailSender.formatXOrigIpHeader(sr.origIp));
    out.setRecipient(javax.mail.Message.RecipientType.TO, sr.reportRecipient);
    out.setEnvelopeFrom(config.getSpamReportEnvelopeFrom());
    out.setSubject(config.getSpamTrainingSubjectPrefix() + " " + sr.accountName + ": " + isSpamString);
    Transport.send(out);
    ZimbraLog.misc.info("Sent " + sr);
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) MimeMessage(javax.mail.internet.MimeMessage) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MailboxBlobDataSource(com.zimbra.cs.mime.MailboxBlobDataSource) Config(com.zimbra.cs.account.Config) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) DataHandler(javax.activation.DataHandler) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

DataHandler (javax.activation.DataHandler)180 Exchange (org.apache.camel.Exchange)39 MimeBodyPart (javax.mail.internet.MimeBodyPart)38 FileDataSource (javax.activation.FileDataSource)33 Test (org.junit.Test)33 DataSource (javax.activation.DataSource)32 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)29 IOException (java.io.IOException)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 InputStream (java.io.InputStream)25 MessagingException (javax.mail.MessagingException)25 MimeMultipart (javax.mail.internet.MimeMultipart)25 MimeMessage (javax.mail.internet.MimeMessage)23 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Message (org.apache.camel.Message)21 OMElement (org.apache.axiom.om.OMElement)17 Processor (org.apache.camel.Processor)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 File (java.io.File)13 PipedInputStream (java.io.PipedInputStream)13