use of com.zimbra.cs.mailbox.DeliveryOptions in project zm-mailbox by Zimbra.
the class ArchiveFormatter method addData.
private void addData(UserServletContext context, Folder fldr, Map<Object, Folder> fmap, Set<MailItem.Type> types, Resolve r, boolean timestamp, ArchiveInputStream ais, ArchiveInputEntry aie, List<ServiceException> errs) throws ServiceException {
try {
int defaultFldr;
Mailbox mbox = fldr.getMailbox();
String dir, file;
String name = aie.getName();
int idx = name.lastIndexOf('/');
MailItem newItem = null, oldItem;
OperationContext oc = context.opContext;
BufferedReader reader;
MailItem.Type type, view;
if (idx == -1) {
file = name;
dir = "";
} else {
file = name.substring(idx + 1);
dir = name.substring(0, idx + 1);
if (!dir.startsWith("/"))
dir = '/' + dir;
}
if (file.length() == 0) {
return;
} else if (file.endsWith(".csv") || file.endsWith(".vcf")) {
defaultFldr = Mailbox.ID_FOLDER_CONTACTS;
type = MailItem.Type.CONTACT;
view = MailItem.Type.CONTACT;
} else if (file.endsWith(".eml")) {
defaultFldr = Mailbox.ID_FOLDER_INBOX;
type = MailItem.Type.MESSAGE;
view = MailItem.Type.MESSAGE;
} else if (file.endsWith(".ics")) {
if (dir.startsWith("Tasks/")) {
defaultFldr = Mailbox.ID_FOLDER_TASKS;
type = MailItem.Type.TASK;
view = MailItem.Type.TASK;
} else {
defaultFldr = Mailbox.ID_FOLDER_CALENDAR;
type = MailItem.Type.APPOINTMENT;
view = MailItem.Type.APPOINTMENT;
}
} else if (file.endsWith(".wiki")) {
defaultFldr = Mailbox.ID_FOLDER_NOTEBOOK;
type = MailItem.Type.WIKI;
view = MailItem.Type.WIKI;
} else {
defaultFldr = Mailbox.ID_FOLDER_BRIEFCASE;
type = MailItem.Type.DOCUMENT;
view = MailItem.Type.DOCUMENT;
}
if (types != null && !types.contains(type)) {
return;
}
if (dir.equals("")) {
if (fldr.getPath().equals("/")) {
fldr = mbox.getFolderById(oc, defaultFldr);
}
if (fldr.getDefaultView() != MailItem.Type.UNKNOWN && fldr.getDefaultView() != view && !((view == MailItem.Type.DOCUMENT || view == MailItem.Type.WIKI) && (fldr.getDefaultView() == MailItem.Type.DOCUMENT || fldr.getDefaultView() == MailItem.Type.WIKI))) {
throw FormatterServiceException.INVALID_TYPE(view.toString(), fldr.getPath());
}
} else {
String s = fldr.getPath();
if (!s.endsWith("/"))
s += '/';
if (dir.startsWith(s))
dir = dir.substring(s.length());
fldr = createPath(context, fmap, fldr.getPath() + dir, view);
}
switch(type) {
case APPOINTMENT:
case TASK:
boolean continueOnError = context.ignoreAndContinueOnError();
boolean preserveExistingAlarms = context.preserveAlarms();
InputStream is = ais.getInputStream();
try {
if (aie.getSize() <= LC.calendar_ics_import_full_parse_max_size.intValue()) {
List<ZVCalendar> icals = ZCalendarBuilder.buildMulti(is, UTF8);
ImportInviteVisitor visitor = new ImportInviteVisitor(oc, fldr, preserveExistingAlarms);
Invite.createFromCalendar(context.targetAccount, null, icals, true, continueOnError, visitor);
} else {
ZICalendarParseHandler handler = new IcsImportParseHandler(oc, context.targetAccount, fldr, continueOnError, preserveExistingAlarms);
ZCalendarBuilder.parse(is, UTF8, handler);
}
} finally {
is.close();
}
break;
case CONTACT:
if (file.endsWith(".csv")) {
reader = new BufferedReader(new InputStreamReader(ais.getInputStream(), UTF8));
ImportContacts.ImportCsvContacts(oc, context.targetMailbox, new ItemId(fldr), ContactCSV.getContacts(reader, null));
} else {
List<VCard> cards = VCard.parseVCard(new String(readArchiveEntry(ais, aie), UTF8));
if (cards == null || cards.size() == 0 || (cards.size() == 1 && cards.get(0).fields.isEmpty())) {
addError(errs, FormatterServiceException.MISSING_VCARD_FIELDS(name));
return;
}
for (VCard vcf : cards) {
if (vcf.fields.isEmpty())
continue;
mbox.createContact(oc, vcf.asParsedContact(), fldr.getId(), null);
}
}
break;
case DOCUMENT:
case WIKI:
String creator = context.getAuthAccount() == null ? null : context.getAuthAccount().getName();
try {
oldItem = mbox.getItemByPath(oc, file, fldr.getId());
if (oldItem.getType() != type) {
addError(errs, FormatterServiceException.MISMATCHED_TYPE(name));
} else if (r == Resolve.Replace) {
mbox.delete(oc, oldItem.getId(), type);
throw MailServiceException.NO_SUCH_ITEM(oldItem.getId());
} else if (r != Resolve.Skip) {
newItem = mbox.addDocumentRevision(oc, oldItem.getId(), creator, oldItem.getName(), null, ais.getInputStream());
}
} catch (NoSuchItemException e) {
if (type == MailItem.Type.WIKI) {
newItem = mbox.createWiki(oc, fldr.getId(), file, creator, null, ais.getInputStream());
} else {
newItem = mbox.createDocument(oc, fldr.getId(), file, null, creator, null, ais.getInputStream());
}
}
if (newItem != null) {
if (timestamp)
mbox.setDate(oc, newItem.getId(), type, aie.getModTime());
}
break;
case MESSAGE:
int flags = aie.isUnread() ? Flag.BITMASK_UNREAD : 0;
DeliveryOptions opt = new DeliveryOptions().setFolderId(fldr.getId()).setNoICal(true).setFlags(flags);
mbox.addMessage(oc, ais.getInputStream(), (int) aie.getSize(), timestamp ? aie.getModTime() : ParsedMessage.DATE_HEADER, opt, null);
break;
}
} catch (Exception e) {
if (e instanceof MailServiceException && ((MailServiceException) e).getCode() == MailServiceException.QUOTA_EXCEEDED)
throw (MailServiceException) e;
else
addError(errs, FormatterServiceException.UNKNOWN_ERROR(aie.getName(), e));
}
}
use of com.zimbra.cs.mailbox.DeliveryOptions in project zm-mailbox by Zimbra.
the class LuceneQueryOperationTest method notClauses.
@Test
public void notClauses() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
mbox.addMessage(null, new ParsedMessage("From: test1@zimbra.com".getBytes(), false), dopt, null);
Message msg2 = mbox.addMessage(null, new ParsedMessage("From: test2@zimbra.com".getBytes(), false), dopt, null);
Message msg3 = mbox.addMessage(null, new ParsedMessage("From: test3@zimbra.com".getBytes(), false), dopt, null);
MailboxTestUtil.index(mbox);
SearchParams params = new SearchParams();
params.setQueryString("-from:(test1 zimbra.com)");
params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
params.setSortBy(SortBy.NONE);
ZimbraQuery query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
ZimbraQueryResults results = query.execute();
List<Integer> expecteds = Lists.newArrayList();
List<Integer> matches = Lists.newArrayList();
Assert.assertTrue(results.hasNext());
matches.add(results.getNext().getItemId());
Assert.assertTrue(results.hasNext());
matches.add(results.getNext().getItemId());
Assert.assertFalse(results.hasNext());
expecteds.add(msg2.getId());
expecteds.add(msg3.getId());
Collections.sort(matches);
Collections.sort(expecteds);
Assert.assertEquals("Match Item ID", expecteds.get(0), matches.get(0));
Assert.assertEquals("Match Item ID", expecteds.get(1), matches.get(1));
results.close();
}
use of com.zimbra.cs.mailbox.DeliveryOptions in project zm-mailbox by Zimbra.
the class LuceneQueryOperationTest method andClauses.
@Test
public void andClauses() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
Message msg1 = mbox.addMessage(null, new ParsedMessage("From: test1@zimbra.com".getBytes(), false), dopt, null);
mbox.addMessage(null, new ParsedMessage("From: test2@zimbra.com".getBytes(), false), dopt, null);
mbox.addMessage(null, new ParsedMessage("From: test3@zimbra.com".getBytes(), false), dopt, null);
MailboxTestUtil.index(mbox);
SearchParams params = new SearchParams();
params.setQueryString("from:test1 from:zimbra.com -from:vmware.com");
params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
params.setSortBy(SortBy.NONE);
ZimbraQuery query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
ZimbraQueryResults results = query.execute();
Assert.assertTrue(results.hasNext());
Assert.assertEquals(msg1.getId(), results.getNext().getItemId());
Assert.assertFalse(results.hasNext());
results.close();
}
use of com.zimbra.cs.mailbox.DeliveryOptions in project zm-mailbox by Zimbra.
the class IntersectionQueryOperationTest method optimize.
@Test
public void optimize() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
mbox.addMessage(null, new ParsedMessage("From: test1@zimbra.com".getBytes(), false), dopt, null);
mbox.addMessage(null, new ParsedMessage("From: test2@zimbra.com".getBytes(), false), dopt, null);
mbox.addMessage(null, new ParsedMessage("From: test3@zimbra.com".getBytes(), false), dopt, null);
MailboxTestUtil.index(mbox);
SearchParams params = new SearchParams();
// wildcard
params.setQueryString("in:inbox from:none*");
params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
params.setSortBy(SortBy.NONE);
ZimbraQuery query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
// this wildcard expansion results in no hits
Assert.assertEquals("ZQ: Q(IN:Inbox) && Q(from:none*[*])", query.toString());
// then intersection of something and no hits is always no hits
Assert.assertEquals("((from:none*) AND IN:/Inbox )", query.toQueryString());
ZimbraQueryResults results = query.execute();
Assert.assertFalse(results.hasNext());
results.close();
params = new SearchParams();
// stop-word
params.setQueryString("in:inbox content:the");
params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
params.setSortBy(SortBy.NONE);
query = new ZimbraQuery(new OperationContext(mbox), SoapProtocol.Soap12, mbox, params);
Assert.assertEquals("ZQ: Q(IN:Inbox) && Q(l.content:)", query.toString());
Assert.assertEquals("", query.toQueryString());
}
use of com.zimbra.cs.mailbox.DeliveryOptions in project zm-mailbox by Zimbra.
the class SyncTest method conversations.
@Test
public void conversations() throws Exception {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
mbox.beginTrackingSync();
// message and reply in inbox
int msgId = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setConversationId(-msgId);
Message msg = mbox.addMessage(null, MailboxTestUtil.generateMessage("Re: test subject"), dopt, null);
int convId = msg.getConversationId();
Set<Integer> expectedDeletes = Sets.newHashSet(convId, msgId, msg.getId());
// initial sync
Element request = new Element.XMLElement(MailConstants.SYNC_REQUEST);
Map<String, Object> context = new HashMap<String, Object>();
context.put(SoapEngine.ZIMBRA_CONTEXT, new ZimbraSoapContext(AuthProvider.getAuthToken(acct), acct.getId(), SoapProtocol.Soap12, SoapProtocol.Soap12));
Element response = new Sync().handle(request, context);
String token = response.getAttribute(MailConstants.A_TOKEN);
Element rootFolder = response.getElement(MailConstants.E_FOLDER);
List<Element> subFolders = rootFolder.listElements(MailConstants.E_FOLDER);
boolean found = true;
for (Element subFolder : subFolders) {
if (subFolder.getAttributeInt(MailConstants.A_ID, 0) == Mailbox.ID_FOLDER_CONVERSATIONS) {
Element conversations = subFolder.getElement(MailConstants.E_CONV);
String ids = conversations.getAttribute(MailConstants.A_IDS);
String[] convIds = ids.split(",");
Assert.assertEquals(1, convIds.length);
Assert.assertEquals(convId + "", convIds[0]);
break;
}
}
Assert.assertTrue("found expected converation", found);
// delete original conv
mbox.delete(null, convId, MailItem.Type.CONVERSATION);
// new conv
msgId = mbox.addMessage(null, MailboxTestUtil.generateMessage("test subject 2"), MailboxTest.STANDARD_DELIVERY_OPTIONS, null).getId();
dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setConversationId(-msgId);
convId = mbox.addMessage(null, MailboxTestUtil.generateMessage("Re: test subject 2"), dopt, null).getConversationId();
// delta sync
request = new Element.XMLElement(MailConstants.SYNC_REQUEST);
request.addAttribute(MailConstants.A_TOKEN, token);
response = new Sync().handle(request, context);
Element conv = response.getElement(MailConstants.E_CONV);
Assert.assertEquals(convId, conv.getAttributeInt(MailConstants.A_ID));
Element deleted = response.getElement(MailConstants.E_DELETED);
String ids = deleted.getAttribute(MailConstants.A_IDS);
String[] deletedIds = ids.split(",");
Assert.assertEquals(3, deletedIds.length);
for (String delete : deletedIds) {
Assert.assertTrue("id " + delete + " deleted", expectedDeletes.remove(Integer.valueOf(delete)));
}
Assert.assertTrue("all expected ids deleted", expectedDeletes.isEmpty());
}
Aggregations