use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class GetShareNotifications method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Mailbox mbox = getRequestedMailbox(zsc);
OperationContext octxt = getOperationContext(zsc, context);
Element response = zsc.createElement(MailConstants.GET_SHARE_NOTIFICATIONS_RESPONSE);
HashSet<String> shares = new HashSet<String>();
ZimbraQueryResults zqr = null;
try {
zqr = mbox.index.search(octxt, query, SEARCH_TYPES, SortBy.DATE_DESC, 10);
while (zqr.hasNext()) {
ZimbraHit hit = zqr.getNext();
if (hit instanceof MessageHit) {
Message message = ((MessageHit) hit).getMessage();
try {
for (MPartInfo part : Mime.getParts(message.getMimeMessage())) {
String ctype = StringUtil.stripControlCharacters(part.getContentType());
if (MimeConstants.CT_XML_ZIMBRA_SHARE.equals(ctype)) {
ShareNotification sn = ShareNotification.fromMimePart(part.getMimePart());
String shareItemId = sn.getGrantorId() + ":" + sn.getItemId();
if (shares.contains(shareItemId)) {
// this notification is stale as there is
// a new one for the same share. delete
// this notification and skip to the next one.
sLog.info("deleting stale notification %s", message.getId());
mbox.delete(octxt, message.getId(), Type.MESSAGE);
continue;
}
shares.add(shareItemId);
Element share = response.addElement(sn.isRevoke() || sn.isExpire() ? MailConstants.E_REVOKE : MailConstants.E_SHARE);
if (sn.isExpire()) {
share.addAttribute(MailConstants.A_EXPIRE, true);
}
Element g = share.addUniqueElement(MailConstants.E_GRANTOR);
g.addAttribute(MailConstants.A_ID, sn.getGrantorId());
g.addAttribute(MailConstants.A_EMAIL, sn.getGrantorEmail());
g.addAttribute(MailConstants.A_NAME, sn.getGrantorName());
Element l = share.addUniqueElement(MailConstants.E_MOUNT);
l.addAttribute(MailConstants.A_ID, sn.getItemId());
l.addAttribute(MailConstants.A_NAME, sn.getItemName());
l.addAttribute(MailConstants.A_DEFAULT_VIEW, sn.getView());
l.addAttribute(MailConstants.A_RIGHTS, sn.getPermissions());
String status = (message.isUnread() ? "new" : "seen");
share.addAttribute(MailConstants.A_STATUS, status);
share.addAttribute(MailConstants.A_ID, "" + message.getId());
share.addAttribute(MailConstants.A_DATE, message.getDate());
if (sn.isRevoke() || sn.isExpire()) {
// purge revoke/expire notification upon receipt
mbox.delete(octxt, message.getId(), Type.MESSAGE);
}
}
}
} catch (IOException e) {
ZimbraLog.misc.warn("can't parse share notification", e);
} catch (MessagingException e) {
ZimbraLog.misc.warn("can't parse share notification", e);
}
}
}
} finally {
Closeables.closeQuietly(zqr);
}
return response;
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class ToXML method encodeInviteAsMP.
/** Encodes an Invite stored within a calendar item object into <m> element
* with <mp> elements.
* @param parent The Element to add the new <tt><m></tt> to.
* @param ifmt The SOAP request's context.
* @param calItem The calendar item to serialize.
* @param iid The requested item; the contained subpart will be used to
* pick the Invite out of the calendar item's blob & metadata.
* @param part If non-null, we'll serialuize this message/rfc822 subpart
* of the specified Message instead of the Message itself.
* @param maxSize The maximum amount of content to inline (<=0 is unlimited).
* @param wantHTML <tt>true</tt> to prefer HTML parts as the "body",
* <tt>false</tt> to prefer text/plain parts.
* @param neuter Whether to rename "src" attributes on HTML <img> tags.
* @param headers Extra message headers to include in the returned element.
* @param serializeType If <tt>false</tt>, always serializes as an
* <tt><m></tt> element.
* @return The newly-created <tt><m></tt> Element, which has already
* been added as a child to the passed-in <tt>parent</tt>.
* @throws ServiceException */
public static Element encodeInviteAsMP(Element parent, ItemIdFormatter ifmt, OperationContext octxt, CalendarItem calItem, String recurIdZ, ItemId iid, String part, int maxSize, boolean wantHTML, boolean neuter, Set<String> headers, boolean serializeType, boolean wantExpandGroupInfo) throws ServiceException {
int invId = iid.getSubpartId();
Invite[] invites = calItem.getInvites(invId);
boolean isPublic = calItem.isPublic();
boolean showAll = isPublic || allowPrivateAccess(octxt, calItem);
boolean wholeMessage = (part == null || part.trim().isEmpty());
Element m;
if (wholeMessage) {
// We want to return the MODIFIED_CONFLICT fields to enable conflict detection on modify.
int fields = NOTIFY_FIELDS | Change.CONFLICT;
m = encodeMessageCommon(parent, ifmt, octxt, calItem, fields, serializeType);
m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
} else {
m = parent.addElement(MailConstants.E_MSG);
m.addAttribute(MailConstants.A_ID, ifmt.formatItemId(calItem, invId));
m.addAttribute(MailConstants.A_PART, part);
}
try {
MimeMessage mm = calItem.getSubpartMessage(invId);
if (mm != null) {
if (!wholeMessage) {
MimePart mp = Mime.getMimePart(mm, part);
if (mp == null) {
throw MailServiceException.NO_SUCH_PART(part);
}
Object content = Mime.getMessageContent(mp);
if (!(content instanceof MimeMessage)) {
throw MailServiceException.NO_SUCH_PART(part);
}
mm = (MimeMessage) content;
} else {
part = "";
}
if (showAll) {
addEmails(m, Mime.parseAddressHeader(mm, "From"), EmailType.FROM);
addEmails(m, Mime.parseAddressHeader(mm, "Sender"), EmailType.SENDER);
addEmails(m, Mime.parseAddressHeader(mm, "Reply-To"), EmailType.REPLY_TO);
addEmails(m, Mime.parseAddressHeader(mm, "To"), EmailType.TO);
addEmails(m, Mime.parseAddressHeader(mm, "Cc"), EmailType.CC);
addEmails(m, Mime.parseAddressHeader(mm, "Bcc"), EmailType.BCC);
String subject = Mime.getSubject(mm);
if (subject != null) {
m.addAttribute(MailConstants.E_SUBJECT, StringUtil.stripControlCharacters(subject), Element.Disposition.CONTENT);
}
String messageID = mm.getMessageID();
if (messageID != null && !messageID.trim().isEmpty()) {
m.addAttribute(MailConstants.E_MSG_ID_HDR, StringUtil.stripControlCharacters(messageID), Element.Disposition.CONTENT);
}
if (!wholeMessage) {
m.addAttribute(MailConstants.A_SIZE, mm.getSize());
}
java.util.Date sent = mm.getSentDate();
if (sent != null) {
m.addAttribute(MailConstants.A_SENT_DATE, sent.getTime());
}
}
}
Element invElt = m.addElement(MailConstants.E_INVITE);
setCalendarItemType(invElt, calItem.getType());
encodeTimeZoneMap(invElt, calItem.getTimeZoneMap());
if (invites.length > 0) {
if (showAll) {
encodeCalendarReplies(invElt, calItem, invites[0], recurIdZ);
}
for (Invite inv : invites) {
encodeInviteComponent(invElt, ifmt, octxt, calItem, (ItemId) null, inv, NOTIFY_FIELDS, neuter);
}
}
if (mm != null && showAll) {
if (headers != null) {
for (String name : headers) {
String[] values = mm.getHeader(name);
if (values == null) {
continue;
}
for (int i = 0; i < values.length; i++) {
m.addKeyValuePair(name, values[i], MailConstants.A_HEADER, MailConstants.A_ATTRIBUTE_NAME);
}
}
}
List<MPartInfo> parts = Mime.getParts(mm, getDefaultCharset(calItem));
if (parts != null && !parts.isEmpty()) {
Set<MPartInfo> bodies = Mime.getBody(parts, wantHTML);
addParts(m, parts.get(0), bodies, part, maxSize, neuter, true, getDefaultCharset(calItem), true);
}
}
if (wantExpandGroupInfo) {
Account authedAcct = octxt.getAuthenticatedUser();
Account requestedAcct = calItem.getMailbox().getAccount();
encodeAddrsWithGroupInfo(m, requestedAcct, authedAcct);
}
} catch (IOException ex) {
throw ServiceException.FAILURE(ex.getMessage(), ex);
} catch (MessagingException ex) {
throw ServiceException.FAILURE(ex.getMessage(), ex);
}
return m;
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class SearchWrapper method addAttachmentResources.
private void addAttachmentResources(MessageHit hit, List<DavResource> children) {
try {
Message msg = hit.getMessage();
List<MPartInfo> parts = Mime.getParts(msg.getMimeMessage());
for (MPartInfo p : parts) {
String name = p.getFilename();
String ct = p.getContentType();
if (mContentType != null && ct != null && !ct.startsWith(mContentType))
continue;
if (name != null && name.length() > 0)
children.add(new Attachment(getUri() + name, getOwner(), msg.getDate(), p.getSize()));
}
} catch (Exception e) {
ZimbraLog.dav.error("can't get attachments from msg: itemid:" + hit.getItemId(), e);
}
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class ZimbraMailAdapter method getMatchingHeaderFromAllParts.
/**
* Scans all MIME parts and returns the values of any headers that
* match the given name.
*/
public Set<String> getMatchingHeaderFromAllParts(String name) throws SieveMailException {
MimeMessage msg;
Set<String> values = new HashSet<String>();
try {
msg = handler.getMimeMessage();
for (MPartInfo partInfo : Mime.getParts(msg)) {
MimePart part = partInfo.getMimePart();
values.addAll(Arrays.asList(Mime.getHeaders(part, name)));
}
} catch (Exception e) {
throw new SieveMailException("Unable to match attachment headers.", e);
}
return values;
}
Aggregations