use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.
the class TestImap method testStoreInvalidSystemFlag.
@Test
public void testStoreInvalidSystemFlag() throws Exception {
ZMailbox mbox = TestUtil.getZMailbox(USER);
mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", "", System.currentTimeMillis(), simpleMessage("foo"), true);
MailboxInfo info = connection.select("INBOX");
Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
assertEquals(1, data.size());
Iterator<Long> it = data.keySet().iterator();
Long seq = it.next();
storeInvalidFlag("\\Bulk", seq);
storeInvalidFlag("\\Unread", seq);
storeInvalidFlag("\\Forwarded", seq);
}
use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.
the class TestImap method testStoreTags.
@Test
public void testStoreTags() throws Exception {
ZMailbox mbox = TestUtil.getZMailbox(USER);
List<ZTag> tags = mbox.getAllTags();
assertTrue(tags == null || tags.size() == 0);
String tagName = "T1";
ZTag tag = mbox.getTag(tagName);
if (tag == null) {
tag = mbox.createTag(tagName, Color.blue);
}
tags = mbox.getAllTags();
assertTrue(tags != null && tags.size() == 1);
assertEquals("T1", tags.get(0).getName());
String folderName = "newfolder1";
ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", "", System.currentTimeMillis(), simpleMessage("foo2"), true);
MailboxInfo info = connection.select("INBOX");
assertTrue("INBOX does not contain expected flag " + tagName, info.getFlags().isSet(tagName));
Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
assertEquals(2, data.size());
Iterator<Long> it = data.keySet().iterator();
Long seq = it.next();
assertTrue("flag not set on first message", data.get(seq).getFlags().isSet(tagName));
seq = it.next();
assertFalse("flag unexpectedly set on second message", data.get(seq).getFlags().isSet(tagName));
connection.store(seq + "", "+FLAGS", tagName);
data = connection.fetch(seq + "", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertTrue("flag not set after STORE in INBOX", data.get(seq).getFlags().isSet(tagName));
mbox.addMessage(folder.getId(), "u", "", System.currentTimeMillis(), simpleMessage("bar"), true);
info = connection.select(folderName);
assertFalse(folderName + " contains unexpected flag " + tagName, info.getFlags().isSet(tagName));
data = connection.fetch("*", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertFalse("flag unexpectedly set on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
connection.store(seq + "", "+FLAGS", tagName);
data = connection.fetch(seq + "", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
info = connection.select(folderName);
assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
String tagName2 = "T2";
connection.store(seq + "", "+FLAGS", tagName2);
data = connection.fetch(seq + "", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName2));
info = connection.select(folderName);
assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
assertTrue("new tag not set in new folder", info.getFlags().isSet(tagName2));
//should not have created T2 as a visible tag
tags = mbox.getAllTags();
assertTrue(tags != null && tags.size() == 1);
assertEquals("T1", tags.get(0).getName());
String tagName3 = "T3";
connection.store(seq + "", "FLAGS", tagName3);
data = connection.fetch(seq + "", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertFalse("flag unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName));
assertFalse("flag unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName2));
assertTrue("flag not set after STORE on message in " + folderName, data.get(seq).getFlags().isSet(tagName3));
info = connection.select(folderName);
assertTrue("new tag not set in new folder", info.getFlags().isSet(tagName3));
assertFalse("old tag unexpectedly set in new folder", info.getFlags().isSet(tagName));
assertFalse("old tag unexpectedly set in new folder", info.getFlags().isSet(tagName2));
//should not have created T2 or T3 as a visible tag
tags = mbox.getAllTags();
assertTrue(tags != null && tags.size() == 1);
assertEquals("T1", tags.get(0).getName());
connection.store(seq + "", "-FLAGS", tagName3);
data = connection.fetch(seq + "", "FLAGS");
assertEquals(1, data.size());
seq = data.keySet().iterator().next();
assertTrue("flags unexpectedly set after STORE on message in " + folderName, data.get(seq).getFlags().isEmpty());
info = connection.select("INBOX");
assertTrue("old tag not set in new folder", info.getFlags().isSet(tagName));
assertFalse("new tag unexpectedly set in new folder", info.getFlags().isSet(tagName2));
}
use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.
the class ImapAppender method findUids.
private List<Long> findUids(String seq, final MessageInfo mi) throws IOException, MessagingException {
final List<Long> uids = new ArrayList<Long>(1);
Map<Long, MessageData> mds = connection.uidFetch(seq, "(RFC822.SIZE ENVELOPE)");
for (MessageData md : mds.values()) {
if (matches(mi, md)) {
uids.add(md.getUid());
}
}
return uids;
}
use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.
the class ImapFolderSync method handleFetch.
private void handleFetch(MessageData md, Map<Long, MessageData> flagsByUid, int folderId, boolean storeSync, boolean restorePurged) throws ServiceException, IOException {
long uid = md.getUid();
if (uid == -1) {
throw new MailException("Missing UID in FETCH response");
}
MessageData flagsData = flagsByUid.get(uid);
remoteFolder.debug("Found new IMAP message with uid %d", uid);
// Parse the message data
Date date = flagsData.getInternalDate();
Long receivedDate = date != null ? date.getTime() : null;
int zflags = SyncUtil.imapToZimbraFlags(flagsData.getFlags());
MessageContent mc = getContent(md);
Message msg;
try {
ParsedMessage pm = mc.getParsedMessage(receivedDate, mailbox.attachmentsIndexingEnabled());
if (pm == null) {
remoteFolder.warn("Empty message body for UID %d. Must be ignored.", uid);
return;
}
pm.setDataSourceId(ds.getId());
if (restorePurged) {
try {
refetchPurgedMsgsInConversation(pm);
} catch (ServiceException e) {
// refetching error should not halt import
ZimbraLog.datasource.error("error refetching purged message", e);
}
}
msg = imapSync.addMessage(null, pm, mc.getSize(), folderId, zflags, mc.getDeliveryContext());
} finally {
mc.cleanup();
}
if (msg != null && msg.getFolderId() == folderId) {
storeImapMessage(uid, msg.getId(), zflags, storeSync);
stats.msgsAddedLocally++;
} else {
// Message was filtered and discarded or moved to another folder.
// This can only happen for messages fetched from INBOX which is
// always imported first. Mark remote message for deletion and
// do not create a local tracker. If message was moved to another
// folder we will append it to the remote folder when we sync
// that folder.
addDeletedUid(uid);
}
}
use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.
the class ImapFolderSync method fetchMessages.
private void fetchMessages(String seq) throws ServiceException, IOException {
final Map<Long, MessageData> flagsByUid = connection.uidFetch(seq, "(FLAGS INTERNALDATE)");
removeDeleted(flagsByUid);
final Set<Long> uidSet = flagsByUid.keySet();
if (uidSet.isEmpty())
return;
FetchResponseHandler handler = new FetchResponseHandler() {
@Override
public void handleFetchResponse(MessageData md) throws Exception {
long uid = md.getUid();
IOExceptionHandler.getInstance().trackSyncItem(mailbox, uid);
try {
handleFetch(md, flagsByUid, true);
clearError(uid);
} catch (OutOfMemoryError e) {
Zimbra.halt("Out of memory", e);
} catch (Exception e) {
if (!IOExceptionHandler.getInstance().isRecoverable(mailbox, uid, "Exception syncing UID " + uid + " in folder " + remoteFolder.getPath(), e)) {
syncFailed("Fetch failed for uid " + uid, e);
SyncErrorManager.incrementErrorCount(ds, remoteId(uid));
}
}
uidSet.remove(uid);
}
};
// Try fetching group of messages first
LOG.debug("Fetching messages for sequence: " + seq);
try {
connection.uidFetch(getSequence(uidSet), "BODY.PEEK[]", handler);
} catch (CommandFailedException e) {
String msg = "UID FETCH failed: " + e.toString();
checkCanContinue(msg, e);
LOG.warn(msg, e);
}
if (uidSet.isEmpty())
return;
LOG.info("Fetching remaining messages one at a time for UIDs: " + uidSet);
for (long uid : getOrderedUids(uidSet)) {
try {
LOG.info("Fetching message for uid: " + uid);
MessageData md = connection.uidFetch(uid, "BODY.PEEK[]");
if (md == null) {
// FLAGS returned data for UID but BODY.PEEK[] is not; server error; provide more meaningful error than NPE
throw ServiceException.FAILURE("Server returned no response for UID FETCH " + uid + " BODY.PEEK[]", null);
}
handler.handleFetchResponse(md);
} catch (Exception e) {
String msg = "Error while fetching message for UID " + uid;
checkCanContinue(msg, e);
LOG.warn(msg, e);
}
}
if (!uidSet.isEmpty()) {
LOG.error("Unable to fetch messages for uids: " + uidSet);
}
}
Aggregations