Search in sources :

Example 51 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class CalDavDataImport method pushModify.

private void pushModify(MailItem mitem) throws ServiceException, IOException, DavException {
    int itemId = mitem.getId();
    DataSource ds = getDataSource();
    DataSourceItem item = DbDataSource.getMapping(ds, itemId);
    boolean isCreate = false;
    if (item.remoteId == null) {
        // new item
        item.md = new Metadata();
        item.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
        item.remoteId = createTargetUrl(mitem);
        item.folderId = mitem.getFolderId();
        isCreate = true;
    }
    String type = item.md.get(METADATA_KEY_TYPE);
    if (METADATA_TYPE_FOLDER.equals(type)) {
        if (mitem.getType() != MailItem.Type.FOLDER) {
            ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
            return;
        }
    // detect and push rename
    } else if (METADATA_TYPE_APPOINTMENT.equals(type)) {
        if (mitem.getType() != MailItem.Type.APPOINTMENT) {
            ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
            return;
        }
        // push modified appt
        ZimbraLog.datasource.debug("pushModify: sending appointment %s", item.remoteId);
        String etag = putAppointment((CalendarItem) mitem, item);
        if (etag == null) {
            Appointment appt = mClient.getEtag(item.remoteId);
            etag = appt.etag;
        }
        item.md.put(METADATA_KEY_ETAG, etag);
        if (isCreate) {
            DbDataSource.addMapping(ds, item);
        } else {
            DbDataSource.updateMapping(ds, item);
        }
    } else {
        ZimbraLog.datasource.warn("pushModify: unrecognized item type for %d: %s", itemId, type);
        return;
    }
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Appointment(com.zimbra.cs.dav.client.CalDavClient.Appointment) Metadata(com.zimbra.cs.mailbox.Metadata) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource)

Example 52 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class CalDavDataImport method applyRemoteItem.

private MailItem applyRemoteItem(RemoteItem remoteItem, Folder where) throws ServiceException, IOException {
    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) 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)

Example 53 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class ModifyDataSource method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    ModifyDataSourceRequest req = JaxbUtil.elementToJaxb(request);
    String id = req.getId();
    if (null == id) {
        throw ServiceException.INVALID_REQUEST("missing required attribute: " + AdminConstants.E_ID, null);
    }
    Account account = prov.get(AccountBy.id, id, zsc.getAuthToken());
    defendAgainstAccountOrCalendarResourceHarvesting(account, AccountBy.id, id, zsc, Admin.R_adminLoginAs, Admin.R_adminLoginCalendarResourceAs);
    DataSourceInfo dataSource = req.getDataSource();
    Map<String, Object> attrs = dataSource.getAttrsAsOldMultimap();
    String dsId = dataSource.getId();
    DataSource ds = prov.get(account, Key.DataSourceBy.id, dsId);
    if (ds == null) {
        throw ServiceException.INVALID_REQUEST("Cannot find data source with id=" + dsId, null);
    }
    DataSourceType type = ds.getType();
    // Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
    if (isDomainAdminOnly(zsc)) {
        // yuck, can't really integrate into AdminDocumentHandler methods
        // have to check separately here
        AttributeClass klass = ModifyDataSource.getAttributeClassFromType(type);
        checkModifyAttrs(zsc, klass, attrs);
    }
    ZimbraLog.addDataSourceNameToContext(ds.getName());
    prov.modifyDataSource(account, dsId, attrs);
    Element response = zsc.createElement(AdminConstants.MODIFY_DATA_SOURCE_RESPONSE);
    return response;
}
Also used : DataSourceInfo(com.zimbra.soap.admin.type.DataSourceInfo) Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) Element(com.zimbra.common.soap.Element) ModifyDataSourceRequest(com.zimbra.soap.admin.message.ModifyDataSourceRequest) AttributeClass(com.zimbra.cs.account.AttributeClass) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource)

Example 54 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class ModifyDataSource method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Account account = getRequestedAccount(zsc);
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    //boolean wipeOutOldData = false;
    Element eDataSource = CreateDataSource.getDataSourceElement(request);
    DataSourceType type = DataSourceType.fromString(eDataSource.getName());
    String id = eDataSource.getAttribute(MailConstants.A_ID);
    DataSource ds = prov.get(account, Key.DataSourceBy.id, id);
    if (ds == null) {
        throw ServiceException.INVALID_REQUEST("Unable to find data source with id=" + id, null);
    }
    Map<String, Object> dsAttrs = new HashMap<String, Object>();
    String value = eDataSource.getAttribute(MailConstants.A_NAME, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceName, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_IS_ENABLED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_IS_ENABLED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_FOLDER, null);
    if (value != null) {
        Mailbox mbox = getRequestedMailbox(zsc);
        CreateDataSource.validateFolderId(account, mbox, eDataSource, type);
        dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_HOST, null);
    if (value != null && !value.equals(ds.getHost())) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceHost, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PORT, null);
    if (value != null)
        dsAttrs.put(Provisioning.A_zimbraDataSourcePort, value);
    value = eDataSource.getAttribute(MailConstants.A_DS_CONNECTION_TYPE, null);
    if (value != null)
        dsAttrs.put(Provisioning.A_zimbraDataSourceConnectionType, value);
    value = eDataSource.getAttribute(MailConstants.A_DS_USERNAME, null);
    if (value != null && !value.equals(ds.getUsername())) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceUsername, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_PASSWORD, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourcePassword, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_OAUTH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthToken, value);
        dsAttrs.put(Provisioning.A_zimbraDataSourceAuthMechanism, ZAttrProvisioning.DataSourceAuthMechanism.XOAUTH2.name());
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_ID, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientId, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_SECRET, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientSecret, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshToken, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN_URL, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshTokenUrl, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_LEAVE_ON_SERVER, null);
    if (value != null) {
        if (type != DataSourceType.pop3) {
            String msg = String.format("%s only allowed for %s data sources", MailConstants.A_DS_LEAVE_ON_SERVER, MailConstants.E_DS_POP3);
            throw ServiceException.INVALID_REQUEST(msg, null);
        }
        boolean newValue = eDataSource.getAttributeBool(MailConstants.A_DS_LEAVE_ON_SERVER);
        if (newValue != ds.leaveOnServer()) {
            dsAttrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, LdapUtil.getLdapBooleanString(newValue));
            Mailbox mbox = getRequestedMailbox(zsc);
            DbPop3Message.deleteUids(mbox, ds.getId());
            DbDataSource.deleteAllMappings(ds);
        }
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_POLLING_INTERVAL, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourcePollingInterval, value);
    }
    // import class
    String importClass = eDataSource.getAttribute(MailConstants.A_DS_IMPORT_CLASS, DataSourceManager.getDefaultImportClass(type));
    if (importClass != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceImportClassName, importClass);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_ENABLED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_ENABLED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_HOST, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpHost, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PORT, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpPort, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_CONNECTION_TYPE, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpConnectionType, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_AUTH_REQUIRED, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthRequired, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_AUTH_REQUIRED)));
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_USERNAME, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthUsername, value);
    }
    value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PASSWORD, null);
    if (value != null) {
        dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthPassword, value);
    }
    if (eDataSource.getAttributeBool(MailConstants.A_DS_TEST, false)) {
        TestDataSource.testDataSourceConnection(prov, eDataSource, type, account);
    }
    processCommonOptionalAttrs(dsAttrs, eDataSource);
    prov.modifyDataSource(account, id, dsAttrs);
    Element response = zsc.createElement(MailConstants.MODIFY_DATA_SOURCE_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) ZAttrProvisioning(com.zimbra.common.account.ZAttrProvisioning) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource) DbDataSource(com.zimbra.cs.db.DbDataSource)

Example 55 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class ImportData method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Account account = getRequestedAccount(zsc);
    for (Element elem : request.listElements()) {
        DataSource ds;
        String name, id = elem.getAttribute(MailConstants.A_ID, null);
        if (id != null) {
            ds = prov.get(account, Key.DataSourceBy.id, id);
            if (ds == null) {
                throw ServiceException.INVALID_REQUEST("Could not find Data Source with id " + id, null);
            }
        } else if ((name = elem.getAttribute(MailConstants.A_NAME, null)) != null) {
            ds = prov.get(account, Key.DataSourceBy.name, name);
            if (ds == null) {
                throw ServiceException.INVALID_REQUEST("Could not find Data Source with name " + name, null);
            }
        } else {
            throw ServiceException.INVALID_REQUEST("must specify either 'id' or 'name'", null);
        }
        ZimbraLog.addDataSourceNameToContext(ds.getName());
        DataSourceManager.asyncImportData(ds);
    }
    Element response = zsc.createElement(MailConstants.IMPORT_DATA_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource)

Aggregations

DataSource (com.zimbra.cs.account.DataSource)68 Account (com.zimbra.cs.account.Account)29 HashMap (java.util.HashMap)19 Provisioning (com.zimbra.cs.account.Provisioning)18 DbDataSource (com.zimbra.cs.db.DbDataSource)17 ServiceException (com.zimbra.common.service.ServiceException)15 Element (com.zimbra.common.soap.Element)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)11 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)11 ArrayList (java.util.ArrayList)11 ZDataSource (com.zimbra.client.ZDataSource)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 DataSourceType (com.zimbra.soap.admin.type.DataSourceType)7 HashSet (java.util.HashSet)7 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)6 ZMailbox (com.zimbra.client.ZMailbox)5 LdapDataSource (com.zimbra.cs.account.ldap.entry.LdapDataSource)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)5 Test (org.junit.Test)5