Search in sources :

Example 11 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class TestItemCache method cacheHit.

/**
     * Re-gets the same message 10 times and makes sure we don't hit the database.
     */
@Test
public void cacheHit() throws Exception {
    ZimbraLog.test.debug("Starting %s", testInfo.getMethodName());
    TestUtil.addMessage(mMbox, String.format("%s-%s", PREFIX, "missive in inbox"));
    TestUtil.addMessage(mMbox, String.format("%s-%s", PREFIX, "2nd missive in inbox"));
    List<MailItem> messages = mMbox.getItemList(null, MailItem.Type.MESSAGE, Mailbox.ID_FOLDER_INBOX);
    assertNotNull("List returned by mMbox.getItemList", messages);
    assertEquals("Expected number of messages in the inbox", 2, messages.size());
    Message msg = (Message) messages.get(0);
    mMbox.getItemById(null, msg.getId(), msg.getType());
    int prepareCount = ZimbraPerf.getPrepareCount();
    for (int i = 1; i <= 10; i++) {
        mMbox.getItemById(null, msg.getId(), msg.getType());
    }
    prepareCount = ZimbraPerf.getPrepareCount() - prepareCount;
    assertEquals("Detected unexpected SQL statements.", 0, prepareCount);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Message(com.zimbra.cs.mailbox.Message) Test(org.junit.Test)

Example 12 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class PendingModifications method deserialize.

@SuppressWarnings("unchecked")
public static PendingModifications deserialize(Mailbox mbox, byte[] data) throws IOException, ClassNotFoundException, ServiceException {
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInputStream ois = new ObjectInputStream(bis);
    PendingModifications pms = new PendingModifications();
    pms.changedTypes = (Set<Type>) ois.readObject();
    LinkedHashMap<ModificationKeyMeta, String> metaCreated = (LinkedHashMap<ModificationKeyMeta, String>) ois.readObject();
    if (metaCreated != null) {
        pms.created = new LinkedHashMap<ModificationKey, MailItem>();
        Iterator<Entry<ModificationKeyMeta, String>> iter = metaCreated.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<ModificationKeyMeta, String> entry = iter.next();
            Metadata meta = new Metadata(entry.getValue());
            MailItem.UnderlyingData ud = new MailItem.UnderlyingData();
            ud.deserialize(meta);
            MailItem item = MailItem.constructItem(mbox, ud, true);
            if (item instanceof Folder) {
                Folder folder = ((Folder) item);
                folder.setParent(mbox.getFolderById(null, folder.getFolderId()));
            }
            ModificationKey key = new ModificationKey(entry.getKey().accountId, entry.getKey().itemId);
            pms.created.put(key, item);
        }
    }
    Map<ModificationKeyMeta, ChangeMeta> metaModified = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
    pms.modified = getOriginal(mbox, metaModified);
    Map<ModificationKeyMeta, ChangeMeta> metaDeleted = (Map<ModificationKeyMeta, ChangeMeta>) ois.readObject();
    pms.deleted = getOriginal(mbox, metaDeleted);
    return pms;
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) LinkedHashMap(java.util.LinkedHashMap) Type(com.zimbra.cs.mailbox.MailItem.Type) MailItem(com.zimbra.cs.mailbox.MailItem) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ObjectInputStream(java.io.ObjectInputStream)

Example 13 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class PendingModifications method getSerializedBytes.

public byte[] getSerializedBytes() throws IOException {
    // assemble temporary created, modified, deleted with Metadata
    LinkedHashMap<ModificationKeyMeta, String> metaCreated = null;
    Map<ModificationKeyMeta, ChangeMeta> metaModified = null;
    Map<ModificationKeyMeta, ChangeMeta> metaDeleted = null;
    if (created != null) {
        metaCreated = new LinkedHashMap<ModificationKeyMeta, String>();
        Iterator<Entry<ModificationKey, MailItem>> iter = created.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<ModificationKey, MailItem> entry = iter.next();
            ModificationKeyMeta keyMeta = new ModificationKeyMeta(entry.getKey().getAccountId(), entry.getKey().getItemId());
            MailItem item = entry.getValue();
            Metadata meta = item.serializeUnderlyingData();
            metaCreated.put(keyMeta, meta.toString());
        }
    }
    metaModified = getSerializable(modified);
    metaDeleted = getSerializable(deleted);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(changedTypes);
    oos.writeObject(metaCreated);
    oos.writeObject(metaModified);
    oos.writeObject(metaDeleted);
    oos.flush();
    oos.close();
    return bos.toByteArray();
}
Also used : Metadata(com.zimbra.cs.mailbox.Metadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Entry(java.util.Map.Entry) MailItem(com.zimbra.cs.mailbox.MailItem)

Example 14 with MailItem

use of com.zimbra.cs.mailbox.MailItem 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 15 with MailItem

use of com.zimbra.cs.mailbox.MailItem in project zm-mailbox by Zimbra.

the class TestDocumentServer method tearDown.

@After
public void tearDown() throws Exception {
    // Delete documents.
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    for (MailItem item : mbox.getItemList(null, MailItem.Type.DOCUMENT)) {
        if (item.getName().contains(NAME_PREFIX)) {
            mbox.delete(null, item.getId(), item.getType());
        }
    }
    cleanUp();
    TestUtil.deleteAccountIfExists(USER_NAME);
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) After(org.junit.After)

Aggregations

MailItem (com.zimbra.cs.mailbox.MailItem)74 Mailbox (com.zimbra.cs.mailbox.Mailbox)36 ServiceException (com.zimbra.common.service.ServiceException)30 Folder (com.zimbra.cs.mailbox.Folder)23 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)23 Message (com.zimbra.cs.mailbox.Message)19 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)17 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)16 OperationContext (com.zimbra.cs.mailbox.OperationContext)15 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)14 Element (com.zimbra.common.soap.Element)13 CalendarItem (com.zimbra.cs.mailbox.CalendarItem)13 Account (com.zimbra.cs.account.Account)12 Document (com.zimbra.cs.mailbox.Document)12 ItemId (com.zimbra.cs.service.util.ItemId)11 HashMap (java.util.HashMap)11 ZMailbox (com.zimbra.client.ZMailbox)9 Contact (com.zimbra.cs.mailbox.Contact)9 HashSet (java.util.HashSet)9