use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class SendMsgTest method deleteDraft.
@Test
public void deleteDraft() throws Exception {
Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// first, add draft message
MimeMessage mm = new MimeMessage(JMSession.getSmtpSession(acct));
mm.setText("foo");
ParsedMessage pm = new ParsedMessage(mm, false);
int draftId = mbox.saveDraft(null, pm, Mailbox.ID_AUTO_INCREMENT).getId();
// then send a message referencing the draft
Element request = new Element.JSONElement(MailConstants.SEND_MSG_REQUEST);
Element m = request.addElement(MailConstants.E_MSG).addAttribute(MailConstants.A_DRAFT_ID, draftId).addAttribute(MailConstants.E_SUBJECT, "dinner appt");
m.addUniqueElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "foo bar");
m.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, ToXML.EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
new SendMsg().handle(request, ServiceTestUtil.getRequestContext(acct));
// finally, verify that the draft is gone
try {
mbox.getMessageById(null, draftId);
Assert.fail("draft message not deleted");
} catch (NoSuchItemException nsie) {
}
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class ItemActionTest method deleteConversation.
@Test
public void deleteConversation() throws Exception {
Account acct = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
acct.setMailThreadingAlgorithm(MailThreadingAlgorithm.subject);
// setup: add the root message
ParsedMessage pm = MailboxTestUtil.generateMessage("test subject");
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
int rootId = mbox.addMessage(null, pm, dopt, null).getId();
// add additional messages
pm = MailboxTestUtil.generateMessage("Re: test subject");
Message draft = mbox.saveDraft(null, pm, Mailbox.ID_AUTO_INCREMENT, rootId + "", MailSender.MSGTYPE_REPLY, null, null, 0);
Message parent = mbox.getMessageById(null, rootId);
Assert.assertEquals(parent.getConversationId(), draft.getConversationId());
pm = MailboxTestUtil.generateMessage("Re: test subject");
Message draft2 = mbox.saveDraft(null, pm, Mailbox.ID_AUTO_INCREMENT);
parent = mbox.getMessageById(null, rootId);
Assert.assertEquals(parent.getConversationId(), draft2.getConversationId());
MailItem.TargetConstraint tcon = new MailItem.TargetConstraint(mbox, MailItem.TargetConstraint.INCLUDE_TRASH);
ItemId iid = new ItemId(mbox, Mailbox.ID_FOLDER_TRASH);
// trash the conversation
ItemActionHelper.MOVE(null, mbox, SoapProtocol.Soap12, Arrays.asList(parent.getId(), draft.getId(), draft2.getId()), MailItem.Type.MESSAGE, tcon, iid);
parent = mbox.getMessageById(null, parent.getId());
draft = mbox.getMessageById(null, draft.getId());
draft2 = mbox.getMessageById(null, draft2.getId());
Assert.assertEquals(parent.getFolderId(), Mailbox.ID_FOLDER_TRASH);
Assert.assertEquals(draft.getFolderId(), Mailbox.ID_FOLDER_TRASH);
Assert.assertEquals(draft2.getFolderId(), Mailbox.ID_FOLDER_TRASH);
ItemActionHelper.HARD_DELETE(null, mbox, SoapProtocol.Soap12, Collections.singletonList(parent.getConversationId()), MailItem.Type.CONVERSATION, tcon);
Exception ex = null;
try {
mbox.getMessageById(null, parent.getId());
} catch (Exception e) {
ex = e;
Assert.assertTrue(e instanceof NoSuchItemException);
}
Assert.assertNotNull(ex);
ex = null;
try {
mbox.getMessageById(null, draft.getId());
} catch (Exception e) {
ex = e;
Assert.assertTrue(e instanceof NoSuchItemException);
}
Assert.assertNotNull(ex);
ex = null;
try {
mbox.getMessageById(null, draft2.getId());
} catch (Exception e) {
ex = e;
Assert.assertTrue(e instanceof NoSuchItemException);
}
Assert.assertNotNull(ex);
ex = null;
try {
mbox.getConversationById(null, draft2.getConversationId());
} catch (Exception e) {
ex = e;
Assert.assertTrue(e instanceof NoSuchItemException);
}
Assert.assertNotNull(ex);
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class TagActionTest method delete.
@Test
public void delete() throws Exception {
Account acct = Provisioning.getInstance().getAccountByName("test@zimbra.com");
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// create the tag
Element request = new Element.XMLElement(MailConstants.CREATE_TAG_REQUEST);
request.addUniqueElement(MailConstants.E_TAG).addAttribute(MailConstants.A_COLOR, 4).addAttribute(MailConstants.A_NAME, "test");
Element response = new CreateTag().handle(request, ServiceTestUtil.getRequestContext(acct));
int tagId = response.getElement(MailConstants.E_TAG).getAttributeInt(MailConstants.A_ID);
try {
mbox.getTagById(null, tagId);
} catch (ServiceException e) {
Assert.fail("tag not created: " + e);
}
// delete the tag
request = new Element.XMLElement(MailConstants.TAG_ACTION_REQUEST);
request.addUniqueElement(MailConstants.E_ACTION).addAttribute(MailConstants.A_OPERATION, ItemAction.OP_HARD_DELETE).addAttribute(MailConstants.A_ID, tagId);
new TagAction().handle(request, ServiceTestUtil.getRequestContext(acct));
try {
mbox.getTagById(null, tagId);
Assert.fail("tag not deleted");
} catch (NoSuchItemException e) {
}
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class CalDavDataImport method syncFolders.
private ArrayList<CalendarFolder> syncFolders() throws ServiceException, IOException, DavException {
ArrayList<CalendarFolder> ret = new ArrayList<CalendarFolder>();
DataSource ds = getDataSource();
OperationContext octxt = new OperationContext(mbox);
HashMap<String, DataSourceItem> allFolders = getAllFolderMappings(ds);
Folder rootFolder = null;
try {
rootFolder = mbox.getFolderById(octxt, getRootFolderId(ds));
} catch (NoSuchItemException e) {
// folder may be deleted. delete the datasource
ZimbraLog.datasource.info("Folder %d was deleted. Deleting data source %s.", getRootFolderId(ds), ds.getName());
mbox.getAccount().deleteDataSource(ds.getId());
// return empty array
return new ArrayList<CalendarFolder>(0);
}
List<Integer> deleted = new ArrayList<Integer>();
int lastSync = (int) rootFolder.getLastSyncDate();
if (lastSync > 0) {
for (int itemId : mbox.getTombstones(lastSync).getAllIds()) deleted.add(itemId);
}
CalDavClient client = getClient();
Map<String, DavObject> calendars = client.getCalendars();
for (String name : calendars.keySet()) {
DavObject obj = calendars.get(name);
String ctag = obj.getPropertyText(DavElements.E_GETCTAG);
String url = obj.getHref();
DataSourceItem f = allFolders.get(url);
if (f == null)
f = new DataSourceItem(0, 0, url, null);
CalendarFolder cf = new CalendarFolder(f.itemId);
Folder folder = null;
if (f.itemId != 0) {
// check if the folder was deleted
if (deleted.contains(f.itemId)) {
allFolders.remove(url);
DbDataSource.deleteMapping(ds, f.itemId);
DbDataSource.deleteAllMappingsInFolder(ds, f.itemId);
deleteRemoteFolder(url);
continue;
}
// check if the folder is valid
try {
folder = mbox.getFolderById(octxt, f.itemId);
} catch (ServiceException se) {
if (se.getCode() != MailServiceException.NO_SUCH_FOLDER) {
throw se;
}
f.itemId = 0;
}
}
if (f.itemId == 0) {
try {
// check if we can use the folder
folder = mbox.getFolderByName(octxt, rootFolder.getId(), name);
if (folder.getDefaultView() != MailItem.Type.APPOINTMENT) {
name = name + " (" + getDataSource().getName() + ")";
folder = null;
}
} catch (MailServiceException.NoSuchItemException e) {
}
if (folder == null) {
Folder.FolderOptions fopt = new Folder.FolderOptions();
fopt.setDefaultView(MailItem.Type.APPOINTMENT).setFlags(DEFAULT_FOLDER_FLAGS).setColor(getDefaultColor());
folder = mbox.createFolder(octxt, name, rootFolder.getId(), fopt);
}
f.itemId = folder.getId();
f.folderId = folder.getFolderId();
f.md = new Metadata();
f.md.put(METADATA_KEY_TYPE, METADATA_TYPE_FOLDER);
if (ctag != null) {
f.md.put(METADATA_KEY_CTAG, ctag);
}
f.remoteId = url;
cf.id = f.itemId;
mbox.setSyncDate(octxt, folder.getId(), mbox.getLastChangeID());
DbDataSource.addMapping(ds, f);
} else if (f.md == null) {
ZimbraLog.datasource.warn("syncFolders: empty metadata for item %d", f.itemId);
f.folderId = folder.getFolderId();
f.remoteId = url;
f.md = new Metadata();
f.md.put(METADATA_KEY_TYPE, METADATA_TYPE_FOLDER);
if (ctag != null)
f.md.put(METADATA_KEY_CTAG, ctag);
DbDataSource.addMapping(ds, f);
} else if (ctag != null) {
String oldctag = f.md.get(METADATA_KEY_CTAG, null);
if (ctag.equals(oldctag)) {
cf.ctagMatched = true;
} else {
f.md.put(METADATA_KEY_CTAG, ctag);
DbDataSource.updateMapping(ds, f);
}
}
String fname = folder.getName();
if (!fname.equals(name)) {
ZimbraLog.datasource.warn("renaming folder %s to %s", fname, name);
try {
mbox.rename(octxt, f.itemId, MailItem.Type.FOLDER, name, folder.getFolderId());
} catch (ServiceException e) {
ZimbraLog.datasource.warn("folder rename failed", e);
}
}
allFolders.remove(url);
ret.add(cf);
}
if (!allFolders.isEmpty()) {
// handle deleted folders
ArrayList<Integer> fids = new ArrayList<Integer>();
int[] fidArray = new int[allFolders.size()];
int i = 0;
for (DataSourceItem f : allFolders.values()) {
Folder folder = mbox.getFolderById(octxt, f.itemId);
if (folder != null && folder.getDefaultView() != MailItem.Type.APPOINTMENT && folder.getDefaultView() != MailItem.Type.TASK) {
continue;
}
fids.add(f.itemId);
fidArray[i++] = f.itemId;
DbDataSource.deleteAllMappingsInFolder(ds, f.itemId);
}
if (!fids.isEmpty()) {
DbDataSource.deleteMappings(ds, fids);
try {
mbox.delete(octxt, fidArray, MailItem.Type.FOLDER, null);
} catch (ServiceException e) {
ZimbraLog.datasource.warn("folder delete failed", e);
}
}
}
mbox.setSyncDate(octxt, rootFolder.getId(), mbox.getLastChangeID());
return ret;
}
use of com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException in project zm-mailbox by Zimbra.
the class Collection method createItem.
// create Document at the URI
public DavResource createItem(DavContext ctxt, String name) throws DavException, IOException {
Mailbox mbox = null;
try {
mbox = getMailbox(ctxt);
} catch (ServiceException e) {
throw new DavException("cannot get mailbox", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
FileUploadServlet.Upload upload = ctxt.getUpload();
String ctype = upload.getContentType();
if (ctype != null && (ctype.startsWith(DavProtocol.VCARD_CONTENT_TYPE) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY) || ctype.startsWith(MimeConstants.CT_TEXT_VCARD_LEGACY2))) {
// vCard MIME types such as text/x-vcard or text/directory.
return createVCard(ctxt, name);
}
String author = ctxt.getAuthAccount().getName();
try {
// add a revision if the resource already exists
MailItem item = mbox.getItemByPath(ctxt.getOperationContext(), ctxt.getPath());
if (item.getType() != MailItem.Type.DOCUMENT && item.getType() != MailItem.Type.WIKI) {
throw new DavException("no DAV resource for " + item.getType(), HttpServletResponse.SC_NOT_ACCEPTABLE, null);
}
Document doc = mbox.addDocumentRevision(ctxt.getOperationContext(), item.getId(), author, name, null, upload.getInputStream());
return new Notebook(ctxt, doc);
} catch (ServiceException e) {
if (!(e instanceof NoSuchItemException))
throw new DavException("cannot get item ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
}
// create
try {
Document doc = mbox.createDocument(ctxt.getOperationContext(), mId, name, ctype, author, null, upload.getInputStream());
Notebook notebook = new Notebook(ctxt, doc);
notebook.mNewlyCreated = true;
return notebook;
} catch (ServiceException se) {
throw new DavException("cannot create ", HttpServletResponse.SC_INTERNAL_SERVER_ERROR, se);
}
}
Aggregations