Search in sources :

Example 6 with ItemIdFormatter

use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.

the class GetTag method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    List<Tag> tags = null;
    try {
        tags = mbox.getTagList(octxt);
    } catch (ServiceException e) {
        // just return no tags in the perm denied case (not considered an error here)...
        if (!e.getCode().equals(ServiceException.PERM_DENIED))
            throw e;
    }
    Element response = zsc.createElement(MailConstants.GET_TAG_RESPONSE);
    if (tags != null) {
        for (Tag tag : tags) {
            if (tag == null || tag instanceof Flag)
                continue;
            ToXML.encodeTag(response, ifmt, octxt, tag);
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Tag(com.zimbra.cs.mailbox.Tag) Flag(com.zimbra.cs.mailbox.Flag)

Example 7 with ItemIdFormatter

use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.

the class GetMsgMetadata 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);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    String ids = request.getElement(MailConstants.E_MSG).getAttribute(MailConstants.A_IDS);
    ArrayList<Integer> local = new ArrayList<Integer>();
    HashMap<String, StringBuilder> remote = new HashMap<String, StringBuilder>();
    ItemAction.partitionItems(zsc, ids, local, remote);
    Element response = zsc.createElement(MailConstants.GET_MSG_METADATA_RESPONSE);
    if (remote.size() > 0) {
        List<Element> responses = proxyRemote(request, remote, context);
        for (Element e : responses) {
            response.addElement(e);
        }
    }
    if (local.size() > 0) {
        List<Message> msgs = GetMsg.getMsgs(octxt, mbox, local, false);
        for (Message msg : msgs) {
            if (msg != null) {
                ToXML.encodeMessageSummary(response, ifmt, octxt, msg, null, SUMMARY_FIELDS);
            }
        }
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Message(com.zimbra.cs.mailbox.Message) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 8 with ItemIdFormatter

use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.

the class SoapSession method putQueuedNotifications.

/** Write a single instance of the PendingModifications structure into the
     *  passed-in <ctxt> block. */
protected void putQueuedNotifications(Mailbox mbox, QueuedNotifications ntfn, Element parent, ZimbraSoapContext zsc) {
    // create the base "notify" block:  <notify seq="6"/>
    Element eNotify = parent.addElement(ZimbraNamespace.E_NOTIFY);
    if (ntfn.getSequence() > 0) {
        eNotify.addAttribute(HeaderConstants.A_SEQNO, ntfn.getSequence());
    }
    OperationContext octxt = null;
    try {
        octxt = DocumentHandler.getOperationContext(zsc, this);
    } catch (ServiceException e) {
        ZimbraLog.session.warn("error fetching operation context for: " + zsc.getAuthtokenAccountId(), e);
        return;
    }
    boolean debug = ZimbraLog.session.isDebugEnabled();
    PendingModifications pms = ntfn.mMailboxChanges;
    RemoteNotifications rns = ntfn.mRemoteChanges;
    Element eDeleted = eNotify.addUniqueElement(ZimbraNamespace.E_DELETED);
    StringBuilder deletedIds = new StringBuilder();
    if (pms != null && pms.deleted != null && pms.deleted.size() > 0) {
        for (ModificationKey mkey : pms.deleted.keySet()) {
            addDeletedNotification(mkey, deletedIds);
        }
    }
    if (rns != null && rns.deleted != null) {
        deletedIds.append(deletedIds.length() == 0 ? "" : ",").append(rns.deleted);
    }
    boolean hasLocalCreates = pms != null && pms.created != null && !pms.created.isEmpty();
    boolean hasRemoteCreates = rns != null && rns.created != null && !rns.created.isEmpty();
    if (hasLocalCreates || hasRemoteCreates) {
        Element eCreated = eNotify.addUniqueElement(ZimbraNamespace.E_CREATED);
        if (hasLocalCreates) {
            for (MailItem item : pms.created.values()) {
                ItemIdFormatter ifmt = new ItemIdFormatter(mAuthenticatedAccountId, item.getMailbox(), false);
                try {
                    Element elem = ToXML.encodeItem(eCreated, ifmt, octxt, item, ToXML.NOTIFY_FIELDS);
                    // special-case notifications for new mountpoints in the authenticated user's mailbox
                    if (item instanceof Mountpoint && mbox == item.getMailbox()) {
                        Map<ItemId, Pair<Boolean, Element>> mountpoints = new HashMap<ItemId, Pair<Boolean, Element>>(2);
                        expandLocalMountpoint(octxt, (Mountpoint) item, eCreated.getFactory(), mountpoints);
                        expandRemoteMountpoints(octxt, zsc, mountpoints);
                        transferMountpointContents(elem, octxt, mountpoints);
                    }
                } catch (ServiceException e) {
                    ZimbraLog.session.warn("error encoding item " + item.getId(), e);
                    return;
                }
            }
            // sanity-check the returned element
            if (!eCreated.hasChildren() && debug) {
                ZimbraLog.session.debug("no serialied creates for item set: %s", pms.created.keySet());
            }
        }
        if (hasRemoteCreates) {
            if (debug) {
                ZimbraLog.session.debug("adding %d proxied creates", rns.created.size());
            }
            for (Element elt : rns.created) {
                if (encodingMatches(parent, elt)) {
                    eCreated.addElement(elt.clone().detach());
                } else {
                    ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
                }
            }
        }
    }
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    boolean hasLocalModifies = pms != null && pms.modified != null && !pms.modified.isEmpty();
    boolean hasRemoteModifies = rns != null && rns.modified != null && !rns.modified.isEmpty();
    if (hasLocalModifies || hasRemoteModifies) {
        Element eModified = eNotify.addUniqueElement(ZimbraNamespace.E_MODIFIED);
        if (hasLocalModifies) {
            for (Change chg : pms.modified.values()) {
                if (chg.why != 0 && chg.what instanceof MailItem) {
                    MailItem item = (MailItem) chg.what;
                    try {
                        Element elt = ToXML.encodeItem(eModified, ifmt, octxt, item, chg.why);
                        if (elt == null) {
                            ModificationKey mkey = new ModificationKey(item);
                            addDeletedNotification(mkey, deletedIds);
                            if (debug) {
                                ZimbraLog.session.debug("marking nonserialized item as a delete: %s", mkey);
                            }
                        }
                    } catch (ServiceException e) {
                        ZimbraLog.session.warn("error encoding item " + item.getId(), e);
                        return;
                    }
                } else if (chg.why != 0 && chg.what instanceof Mailbox) {
                    ToXML.encodeMailbox(eModified, octxt, (Mailbox) chg.what, chg.why);
                }
            }
            // sanity-check the returned element
            if (!eModified.hasChildren() && debug) {
                ZimbraLog.session.debug("no serialied modifies for item set: %s", pms.modified.keySet());
            }
        }
        if (hasRemoteModifies) {
            if (debug) {
                ZimbraLog.session.debug("adding %d proxied modifies", rns.modified.size());
            }
            for (Element elt : rns.modified) {
                if (encodingMatches(parent, elt)) {
                    eModified.addElement(elt.clone().detach());
                } else {
                    ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
                }
            }
        }
    }
    if (rns != null && rns.activities != null && !rns.activities.isEmpty()) {
        for (Element elt : rns.activities) {
            if (encodingMatches(parent, elt)) {
                eNotify.addElement(elt.clone().detach());
            } else {
                ZimbraLog.session.warn("unable to add remote notification due to mismatched SOAP protocol");
            }
        }
    }
    putExtraNotifications(ntfn, eNotify, ifmt);
    if (deletedIds == null || deletedIds.length() == 0) {
        eDeleted.detach();
    } else {
        eDeleted.addAttribute(A_ID, deletedIds.toString());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) HashMap(java.util.HashMap) Element(com.zimbra.common.soap.Element) ModificationKey(com.zimbra.cs.session.PendingModifications.ModificationKey) Change(com.zimbra.cs.session.PendingModifications.Change) ItemId(com.zimbra.cs.service.util.ItemId) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) Pair(com.zimbra.common.util.Pair)

Example 9 with ItemIdFormatter

use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.

the class SoapSession method expandLocalMountpoint.

private void expandLocalMountpoint(OperationContext octxt, Mountpoint mpt, Element.ElementFactory factory, Map<ItemId, Pair<Boolean, Element>> mountpoints) {
    // don't bother generating the subhierarchy more than once
    ItemId iidTarget = mpt.getTarget();
    if (mountpoints.containsKey(iidTarget)) {
        return;
    }
    try {
        Provisioning prov = Provisioning.getInstance();
        Account owner = prov.get(Key.AccountBy.id, mpt.getOwnerId(), octxt.getAuthToken());
        if (owner == null || owner.getId().equals(mAuthenticatedAccountId)) {
            mountpoints.put(iidTarget, new Pair<Boolean, Element>(true, null));
            return;
        }
        // if we're non-admin and it's not active
        if (Provisioning.ACCOUNT_STATUS_MAINTENANCE.equals(owner.getAccountStatus(prov)) || (!Provisioning.ACCOUNT_STATUS_ACTIVE.equals(owner.getAccountStatus(prov)) && (!octxt.isUsingAdminPrivileges() || !AccessManager.getInstance().canAccessAccount(octxt.getAuthenticatedUser(), owner)))) {
            mountpoints.put(iidTarget, new Pair<Boolean, Element>(true, null));
            return;
        }
        // handle mountpoints pointing to a different server later
        if (!Provisioning.onLocalServer(owner)) {
            mountpoints.put(iidTarget, null);
            return;
        }
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(owner);
        FolderNode remote = mbox.getFolderTree(octxt, new ItemId(mbox, mpt.getRemoteId()), false);
        if (remote != null && remote.mFolder != null && !remote.mFolder.isHidden()) {
            ItemIdFormatter ifmt = new ItemIdFormatter(octxt.getAuthenticatedUser(), mbox, false);
            if (OperationContextData.getNeedGranteeName(octxt)) {
                OperationContextData.addGranteeNames(octxt, remote);
            }
            Element subhierarchy = GetFolder.encodeFolderNode(remote, factory.createElement("ignored"), ifmt, octxt).detach();
            mountpoints.put(iidTarget, new Pair<Boolean, Element>(false, subhierarchy));
            // fault in a delegate session because there's actually something to listen on...
            getDelegateSession(mpt.getOwnerId());
        }
    } catch (ServiceException e) {
        mountpoints.put(iidTarget, new Pair<Boolean, Element>(true, null));
    }
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) FolderNode(com.zimbra.cs.mailbox.Mailbox.FolderNode) ServiceException(com.zimbra.common.service.ServiceException) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) Element(com.zimbra.common.soap.Element) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) Pair(com.zimbra.common.util.Pair)

Example 10 with ItemIdFormatter

use of com.zimbra.cs.service.util.ItemIdFormatter in project zm-mailbox by Zimbra.

the class GalSearchResultCallback method reset.

public void reset(GalSearchParams params) {
    if (params.getSoapContext() != null) {
        mResponse = params.getSoapContext().createElement(params.getResponseName());
        mFormatter = new ItemIdFormatter(params.getSoapContext());
    } else {
        mResponse = Element.XMLElement.mFactory.createElement(params.getResponseName());
        mFormatter = new ItemIdFormatter();
    }
    params.setGalResult(SearchGalResult.newSearchGalResult(this));
    mIdOnly = params.isIdOnly();
    try {
        mAuthAcct = params.getAuthAccount();
    } catch (ServiceException e) {
        ZimbraLog.gal.warn("unable to get authed account", e);
    }
    mNeedCanExpand = params.getNeedCanExpand();
    mNeedIsOwner = params.getNeedIsOwner();
    mNeedIsMember = params.getNeedIsMember();
    mNeedSMIMECerts = params.getNeedSMIMECerts();
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter)

Aggregations

ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)63 Element (com.zimbra.common.soap.Element)56 Mailbox (com.zimbra.cs.mailbox.Mailbox)48 OperationContext (com.zimbra.cs.mailbox.OperationContext)46 ItemId (com.zimbra.cs.service.util.ItemId)44 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)44 Account (com.zimbra.cs.account.Account)21 ServiceException (com.zimbra.common.service.ServiceException)16 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)14 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)11 Folder (com.zimbra.cs.mailbox.Folder)11 Message (com.zimbra.cs.mailbox.Message)11 HashMap (java.util.HashMap)11 MailItem (com.zimbra.cs.mailbox.MailItem)10 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 MessagingException (javax.mail.MessagingException)7 MimeMessage (javax.mail.internet.MimeMessage)7