Search in sources :

Example 16 with MPartInfo

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

Example 17 with MPartInfo

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>&lt;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>&lt;m></tt> element.
     * @return The newly-created <tt>&lt;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;
}
Also used : Account(com.zimbra.cs.account.Account) MessagingException(javax.mail.MessagingException) Date(java.util.Date) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) MPartInfo(com.zimbra.cs.mime.MPartInfo) MimeMessage(javax.mail.internet.MimeMessage) MimePart(javax.mail.internet.MimePart) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Example 18 with MPartInfo

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);
    }
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) Message(com.zimbra.cs.mailbox.Message)

Example 19 with MPartInfo

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;
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) MimeMessage(javax.mail.internet.MimeMessage) SieveMailException(org.apache.jsieve.mail.SieveMailException) MimePart(javax.mail.internet.MimePart) MessagingException(javax.mail.MessagingException) SieveMailException(org.apache.jsieve.mail.SieveMailException) SieveException(org.apache.jsieve.exception.SieveException) AddressException(javax.mail.internet.AddressException) ServiceException(com.zimbra.common.service.ServiceException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

MPartInfo (com.zimbra.cs.mime.MPartInfo)19 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 IOException (java.io.IOException)8 MimeMessage (javax.mail.internet.MimeMessage)8 MessagingException (javax.mail.MessagingException)6 Account (com.zimbra.cs.account.Account)5 Message (com.zimbra.cs.mailbox.Message)5 InputStream (java.io.InputStream)5 MimePart (javax.mail.internet.MimePart)5 Test (org.junit.Test)5 Element (com.zimbra.common.soap.Element)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 OperationContext (com.zimbra.cs.mailbox.OperationContext)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ServiceException (com.zimbra.common.service.ServiceException)3 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)3 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)2 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)2 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)2 RemoteDataInfo (com.zimbra.cs.service.FeedManager.RemoteDataInfo)2