Search in sources :

Example 6 with NoSuchItemException

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) {
    }
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Element(com.zimbra.common.soap.Element) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 7 with NoSuchItemException

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);
}
Also used : Account(com.zimbra.cs.account.Account) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) ItemId(com.zimbra.cs.service.util.ItemId) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Test(org.junit.Test)

Example 8 with NoSuchItemException

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) {
    }
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Element(com.zimbra.common.soap.Element) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) Test(org.junit.Test)

Example 9 with NoSuchItemException

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;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ArrayList(java.util.ArrayList) Metadata(com.zimbra.cs.mailbox.Metadata) Folder(com.zimbra.cs.mailbox.Folder) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavObject(com.zimbra.cs.dav.client.DavObject)

Example 10 with NoSuchItemException

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);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DavException(com.zimbra.cs.dav.DavException) Document(com.zimbra.cs.mailbox.Document) FileUploadServlet(com.zimbra.cs.service.FileUploadServlet) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)

Aggregations

NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)50 Mailbox (com.zimbra.cs.mailbox.Mailbox)21 ServiceException (com.zimbra.common.service.ServiceException)20 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)15 MailItem (com.zimbra.cs.mailbox.MailItem)11 Test (org.junit.Test)11 Account (com.zimbra.cs.account.Account)10 OperationContext (com.zimbra.cs.mailbox.OperationContext)10 ItemId (com.zimbra.cs.service.util.ItemId)10 Folder (com.zimbra.cs.mailbox.Folder)9 MimeMessage (javax.mail.internet.MimeMessage)8 Element (com.zimbra.common.soap.Element)7 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)7 IOException (java.io.IOException)7 AccountServiceException (com.zimbra.cs.account.AccountServiceException)6 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)6 ArrayList (java.util.ArrayList)6 DbMailItem (com.zimbra.cs.db.DbMailItem)5 DbTag (com.zimbra.cs.db.DbTag)5 Document (com.zimbra.cs.mailbox.Document)5