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);
}
}
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());
}
}
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)));
}
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;
}
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);
}
Aggregations