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