Search in sources :

Example 1 with ShareNotification

use of com.zimbra.common.share.ShareNotification 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;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraHit(com.zimbra.cs.index.ZimbraHit) Message(com.zimbra.cs.mailbox.Message) MessagingException(javax.mail.MessagingException) Element(com.zimbra.common.soap.Element) MessageHit(com.zimbra.cs.index.MessageHit) IOException(java.io.IOException) MPartInfo(com.zimbra.cs.mime.MPartInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) ShareNotification(com.zimbra.common.share.ShareNotification) HashSet(java.util.HashSet)

Aggregations

ShareNotification (com.zimbra.common.share.ShareNotification)1 Element (com.zimbra.common.soap.Element)1 MessageHit (com.zimbra.cs.index.MessageHit)1 ZimbraHit (com.zimbra.cs.index.ZimbraHit)1 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 Message (com.zimbra.cs.mailbox.Message)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 MPartInfo (com.zimbra.cs.mime.MPartInfo)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 MessagingException (javax.mail.MessagingException)1