Search in sources :

Example 1 with CalDavClient

use of com.zimbra.cs.dav.client.CalDavClient in project zm-mailbox by Zimbra.

the class CalDavDataImport method syncFolders.

private ArrayList<CalendarFolder> syncFolders() throws ServiceException, IOException, DavException, HttpException {
    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 2 with CalDavClient

use of com.zimbra.cs.dav.client.CalDavClient in project zm-mailbox by Zimbra.

the class CalDavDataImport method getClient.

protected CalDavClient getClient() throws ServiceException, IOException, DavException, HttpException {
    if (mClient == null) {
        mClient = new CalDavClient(getTargetUrl());
        mClient.setAppName(getAppName());
        mClient.setCredential(getUsername(), getDecryptedPassword(), getTargetUrl());
        mClient.setDebugEnabled(dataSource.isDebugTraceEnabled());
        mClient.login(getDefaultPrincipalUrl());
    }
    return mClient;
}
Also used : CalDavClient(com.zimbra.cs.dav.client.CalDavClient)

Example 3 with CalDavClient

use of com.zimbra.cs.dav.client.CalDavClient in project zm-mailbox by Zimbra.

the class CalDavDataImport method getRemoteItems.

private List<RemoteItem> getRemoteItems(Folder folder) throws ServiceException, IOException, DavException, HttpException {
    ZimbraLog.datasource.debug("Refresh folder %s", folder.getPath());
    DataSource ds = getDataSource();
    DataSourceItem item = DbDataSource.getMapping(ds, folder.getId());
    if (item.md == null)
        throw ServiceException.FAILURE("Mapping for folder " + folder.getPath() + " not found", null);
    // CalDAV doesn't provide delete tombstone.  in order to check for deleted appointments
    // we need to cross reference the current result with what we have from last sync
    // and check for any appointment that has disappeared since last sync.
    HashMap<String, DataSourceItem> allItems = new HashMap<String, DataSourceItem>();
    for (DataSourceItem localItem : DbDataSource.getAllMappingsInFolder(getDataSource(), folder.getId())) allItems.put(localItem.remoteId, localItem);
    ArrayList<RemoteItem> ret = new ArrayList<RemoteItem>();
    CalDavClient client = getClient();
    Collection<Appointment> appts = client.getEtags(item.remoteId);
    for (Appointment a : appts) {
        ret.add(new RemoteCalendarItem(a.href, a.etag));
        allItems.remove(a.href);
    }
    ArrayList<Integer> deletedIds = new ArrayList<Integer>();
    for (DataSourceItem deletedItem : allItems.values()) {
        // what's left in the collection are previous mapping that has disappeared.
        // we need to delete the appointments that are mapped locally.
        RemoteCalendarItem rci = new RemoteCalendarItem(deletedItem.remoteId, null);
        rci.status = Status.deleted;
        rci.itemId = deletedItem.itemId;
        ret.add(rci);
        deletedIds.add(deletedItem.itemId);
        ZimbraLog.datasource.debug("deleting: %d (%s) ", deletedItem.itemId, deletedItem.remoteId);
    }
    if (!deletedIds.isEmpty())
        DbDataSource.deleteMappings(ds, deletedIds);
    return ret;
}
Also used : Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem)

Example 4 with CalDavClient

use of com.zimbra.cs.dav.client.CalDavClient in project zm-mailbox by Zimbra.

the class CalDavDataImport method test.

@Override
public void test() throws ServiceException {
    mClient = new CalDavClient(getTargetUrl());
    mClient.setAppName(getAppName());
    mClient.setCredential(getUsername(), getDecryptedPassword(), getTargetUrl());
    mClient.setDebugEnabled(dataSource.isDebugTraceEnabled());
    try {
        mClient.login(getDefaultPrincipalUrl());
    } catch (Exception x) {
        throw ServiceException.FAILURE(x.getMessage(), x);
    }
}
Also used : CalDavClient(com.zimbra.cs.dav.client.CalDavClient) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) DavException(com.zimbra.cs.dav.DavException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 5 with CalDavClient

use of com.zimbra.cs.dav.client.CalDavClient in project zm-mailbox by Zimbra.

the class CalDavDataImport method applyRemoteItem.

private MailItem applyRemoteItem(RemoteItem remoteItem, Folder where) throws ServiceException, IOException, HttpException {
    if (!(remoteItem instanceof RemoteCalendarItem)) {
        ZimbraLog.datasource.warn("applyRemoteItem: not a calendar item: %s", remoteItem);
        return null;
    }
    RemoteCalendarItem item = (RemoteCalendarItem) remoteItem;
    DataSource ds = getDataSource();
    DataSourceItem dsItem = DbDataSource.getReverseMapping(ds, item.href);
    OperationContext octxt = new OperationContext(mbox);
    MailItem mi = null;
    boolean isStale = false;
    boolean isCreate = false;
    if (dsItem.md == null && item.status != Status.deleted) {
        dsItem.md = new Metadata();
        dsItem.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
    }
    if (dsItem.itemId == 0) {
        isStale = true;
        isCreate = true;
    } else {
        String etag = dsItem.md.get(METADATA_KEY_ETAG, null);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (MailServiceException.NoSuchItemException se) {
            ZimbraLog.datasource.warn("applyRemoteItem: calendar item not found: ", remoteItem);
        }
        if (item.etag == null) {
            ZimbraLog.datasource.warn("No Etag returned for item %s", item.href);
            isStale = true;
        } else if (etag == null) {
            ZimbraLog.datasource.warn("Empty etag for item %d", dsItem.itemId);
            isStale = true;
        } else {
            isStale = !item.etag.equals(etag);
        }
        if (mi == null)
            isStale = true;
    }
    if (item.status == Status.deleted) {
        ZimbraLog.datasource.debug("Deleting appointment %s", item.href);
        try {
            mi = mbox.getItemById(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            mi = null;
        }
        try {
            mbox.delete(octxt, item.itemId, MailItem.Type.UNKNOWN);
        } catch (ServiceException se) {
            ZimbraLog.datasource.warn("Error deleting remotely deleted item %d (%s)", item.itemId, dsItem.remoteId);
        }
    } else if (isStale) {
        ZimbraLog.datasource.debug("Updating stale appointment %s", item.href);
        ZCalendar.ZVCalendar vcalendar;
        SetCalendarItemData main = new SetCalendarItemData();
        SetCalendarItemData[] exceptions = null;
        CalDavClient client = null;
        try {
            client = getClient();
        } catch (DavException e) {
            throw ServiceException.FAILURE("error creating CalDAV client", e);
        }
        Appointment appt = client.getCalendarData(new Appointment(item.href, item.etag));
        if (appt.data == null) {
            ZimbraLog.datasource.warn("No appointment found at " + item.href);
            return null;
        }
        dsItem.md.put(METADATA_KEY_ETAG, appt.etag);
        try {
            vcalendar = ZCalendar.ZCalendarBuilder.build(appt.data);
            List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, vcalendar, true);
            if (invites.size() > 1)
                exceptions = new SetCalendarItemData[invites.size() - 1];
            int pos = 0;
            boolean first = true;
            for (Invite i : invites) {
                if (first) {
                    main.invite = i;
                    first = false;
                } else {
                    SetCalendarItemData scid = new SetCalendarItemData();
                    scid.invite = i;
                    exceptions[pos++] = scid;
                }
            }
        } catch (Exception e) {
            ZimbraLog.datasource.warn("Error parsing appointment ", e);
            return null;
        }
        mi = mbox.setCalendarItem(octxt, where.getId(), 0, null, main, exceptions, null, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
        dsItem.itemId = mi.getId();
        dsItem.folderId = mi.getFolderId();
        if (isCreate) {
            DbDataSource.addMapping(ds, dsItem);
        } else {
            DbDataSource.updateMapping(ds, dsItem);
        }
    } else {
        ZimbraLog.datasource.debug("Appointment up to date %s", item.href);
        try {
            mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
        } catch (NoSuchItemException se) {
            // item not found.  delete the mapping so it can be downloaded again if needed.
            ArrayList<Integer> deletedIds = new ArrayList<Integer>();
            deletedIds.add(dsItem.itemId);
            DbDataSource.deleteMappings(ds, deletedIds);
        }
    }
    return mi;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) DavException(com.zimbra.cs.dav.DavException) Metadata(com.zimbra.cs.mailbox.Metadata) ArrayList(java.util.ArrayList) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) CalDavClient(com.zimbra.cs.dav.client.CalDavClient) ServiceException(com.zimbra.common.service.ServiceException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) DavException(com.zimbra.cs.dav.DavException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) SetCalendarItemData(com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData) MailItem(com.zimbra.cs.mailbox.MailItem) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) ArrayList(java.util.ArrayList) List(java.util.List) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite)

Aggregations

CalDavClient (com.zimbra.cs.dav.client.CalDavClient)5 ServiceException (com.zimbra.common.service.ServiceException)3 DataSource (com.zimbra.cs.account.DataSource)3 DbDataSource (com.zimbra.cs.db.DbDataSource)3 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)3 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)3 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)3 ArrayList (java.util.ArrayList)3 DavException (com.zimbra.cs.dav.DavException)2 Appointment (com.zimbra.cs.dav.client.CalDavClient.Appointment)2 Metadata (com.zimbra.cs.mailbox.Metadata)2 OperationContext (com.zimbra.cs.mailbox.OperationContext)2 IOException (java.io.IOException)2 HttpException (org.apache.http.HttpException)2 DavObject (com.zimbra.cs.dav.client.DavObject)1 Folder (com.zimbra.cs.mailbox.Folder)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 SetCalendarItemData (com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData)1 Invite (com.zimbra.cs.mailbox.calendar.Invite)1 HashMap (java.util.HashMap)1