Search in sources :

Example 1 with GalMode

use of com.zimbra.common.account.ZAttrProvisioning.GalMode in project zm-mailbox by Zimbra.

the class GalSearchControl method doLocalGalAccountSync.

private void doLocalGalAccountSync(Account galAcct) throws ServiceException {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(galAcct);
    OperationContext octxt = new OperationContext(mbox);
    GalSearchResultCallback callback = mParams.getResultCallback();
    Domain domain = mParams.getDomain();
    GalMode galMode = domain.getGalMode();
    int changeId = mParams.getGalSyncToken().getChangeId(galAcct.getId());
    // bug 46608
    // sync local resources from first datasource if galMode is ldap
    // and zimbraGalAlwaysIncludeLocalCalendarResources is set for the domain
    boolean syncLocalResources = (galMode == GalMode.ldap && domain.isGalAlwaysIncludeLocalCalendarResources());
    Set<Integer> folderIds = new HashSet<Integer>();
    String syncToken = null;
    for (DataSource ds : galAcct.getAllDataSources()) {
        if (ds.getType() != DataSourceType.gal) {
            ZimbraLog.gal.trace("skipping datasource %s: wrong type %s expected %s", ds.getName(), ds.getType(), DataSourceType.gal);
            continue;
        }
        if (galMode != null) {
            if (!(galMode.isBoth() || galMode.toString().equals(ds.getAttr(Provisioning.A_zimbraGalType)))) {
                ZimbraLog.gal.debug("skipping datasource %s: wrong zimbraGalType %s expected %s", ds.getName(), ds.getAttr(Provisioning.A_zimbraGalType), galMode.toString());
                continue;
            }
        }
        int fid = ds.getFolderId();
        DataSourceItem folderMapping = DbDataSource.getMapping(ds, fid);
        if (folderMapping.md == null) {
            ZimbraLog.gal.debug("skipping datasource %s: no folder mapping", ds.getName());
            continue;
        }
        folderIds.add(fid);
        syncToken = LdapUtil.getEarlierTimestamp(syncToken, folderMapping.md.get(GalImport.SYNCTOKEN));
        if (syncLocalResources) {
            doLocalGalAccountSync(callback, mbox, octxt, changeId, folderIds, syncToken, mParams.getLimit(), Provisioning.A_zimbraAccountCalendarUserType, "RESOURCE");
            syncLocalResources = false;
        }
    }
    if (folderIds.isEmpty()) {
        throw ServiceException.FAILURE("no gal datasource with mapped folder found", null);
    }
    if (syncToken == null) {
        throw ServiceException.FAILURE("no gal datasource with sync token found", null);
    }
    doLocalGalAccountSync(callback, mbox, octxt, changeId, folderIds, syncToken, mParams.getLimit());
    List<Integer> deleted = null;
    if (callback.getResponse() != null && !callback.getResponse().getAttributeBool(MailConstants.A_QUERY_MORE) && changeId > 0) {
        try {
            deleted = mbox.getTombstones(changeId).getAllIds();
        } catch (MailServiceException e) {
            if (MailServiceException.MUST_RESYNC == e.getCode()) {
                ZimbraLog.gal.warn("sync token too old, deleted items will not be handled", e);
            } else {
                throw e;
            }
        }
    }
    if (deleted != null) {
        for (int itemId : deleted) {
            callback.handleDeleted(new ItemId(galAcct.getId(), itemId));
        }
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) GalMode(com.zimbra.common.account.ZAttrProvisioning.GalMode) ItemId(com.zimbra.cs.service.util.ItemId) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource) Mailbox(com.zimbra.cs.mailbox.Mailbox) DataSourceItem(com.zimbra.cs.db.DbDataSource.DataSourceItem) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Domain(com.zimbra.cs.account.Domain) HashSet(java.util.HashSet)

Example 2 with GalMode

use of com.zimbra.common.account.ZAttrProvisioning.GalMode in project zm-mailbox by Zimbra.

the class GalSearchConfig method loadConfig.

protected void loadConfig(Domain domain, GalOp op) throws ServiceException {
    mRules = new LdapGalMapRules(domain, false);
    mOp = op;
    GalMode galMode = domain.getGalMode();
    if (galMode == GalMode.zimbra)
        loadZimbraConfig(domain, op, null);
    switch(op) {
        case sync:
            mUrl = domain.getMultiAttr(Provisioning.A_zimbraGalSyncLdapURL);
            mFilter = domain.getAttr(Provisioning.A_zimbraGalLdapFilter);
            mSearchBase = domain.getAttr(Provisioning.A_zimbraGalSyncLdapSearchBase, "");
            mStartTlsEnabled = domain.getBooleanAttr(Provisioning.A_zimbraGalSyncLdapStartTlsEnabled, false);
            mAuthMech = domain.getAttr(Provisioning.A_zimbraGalSyncLdapAuthMech, Provisioning.LDAP_AM_SIMPLE);
            mBindDn = domain.getAttr(Provisioning.A_zimbraGalSyncLdapBindDn);
            mBindPassword = domain.getAttr(Provisioning.A_zimbraGalSyncLdapBindPassword);
            mKerberosPrincipal = domain.getAttr(Provisioning.A_zimbraGalSyncLdapKerberos5Principal);
            mKerberosKeytab = domain.getAttr(Provisioning.A_zimbraGalSyncLdapKerberos5Keytab);
            mTimestampFormat = domain.getAttr(Provisioning.A_zimbraGalSyncTimestampFormat, LdapDateUtil.ZIMBRA_LDAP_GENERALIZED_TIME_FORMAT_LEGACY);
            mPageSize = domain.getIntAttr(Provisioning.A_zimbraGalSyncLdapPageSize, 1000);
            if (isConfigComplete())
                break;
        case search:
            mUrl = domain.getMultiAttr(Provisioning.A_zimbraGalLdapURL);
            mFilter = domain.getAttr(Provisioning.A_zimbraGalLdapFilter);
            mSearchBase = domain.getAttr(Provisioning.A_zimbraGalLdapSearchBase, "");
            mStartTlsEnabled = domain.getBooleanAttr(Provisioning.A_zimbraGalLdapStartTlsEnabled, false);
            mAuthMech = domain.getAttr(Provisioning.A_zimbraGalLdapAuthMech, Provisioning.LDAP_AM_SIMPLE);
            mBindDn = domain.getAttr(Provisioning.A_zimbraGalLdapBindDn);
            mBindPassword = domain.getAttr(Provisioning.A_zimbraGalLdapBindPassword);
            mKerberosPrincipal = domain.getAttr(Provisioning.A_zimbraGalLdapKerberos5Principal);
            mKerberosKeytab = domain.getAttr(Provisioning.A_zimbraGalLdapKerberos5Keytab);
            mTokenizeKey = domain.getAttr(Provisioning.A_zimbraGalTokenizeSearchKey);
            mPageSize = domain.getIntAttr(Provisioning.A_zimbraGalLdapPageSize, 1000);
            break;
        case autocomplete:
            mUrl = domain.getMultiAttr(Provisioning.A_zimbraGalLdapURL);
            mFilter = domain.getAttr(Provisioning.A_zimbraGalAutoCompleteLdapFilter);
            mSearchBase = domain.getAttr(Provisioning.A_zimbraGalLdapSearchBase, "");
            mStartTlsEnabled = domain.getBooleanAttr(Provisioning.A_zimbraGalLdapStartTlsEnabled, false);
            mAuthMech = domain.getAttr(Provisioning.A_zimbraGalLdapAuthMech, Provisioning.LDAP_AM_SIMPLE);
            mBindDn = domain.getAttr(Provisioning.A_zimbraGalLdapBindDn);
            mBindPassword = domain.getAttr(Provisioning.A_zimbraGalLdapBindPassword);
            mKerberosPrincipal = domain.getAttr(Provisioning.A_zimbraGalLdapKerberos5Principal);
            mKerberosKeytab = domain.getAttr(Provisioning.A_zimbraGalLdapKerberos5Keytab);
            mTokenizeKey = domain.getAttr(Provisioning.A_zimbraGalTokenizeAutoCompleteKey);
            mPageSize = domain.getIntAttr(Provisioning.A_zimbraGalLdapPageSize, 1000);
            break;
    }
    if (mFilter != null && mFilter.indexOf("(") == -1)
        mFilter = GalSearchConfig.getFilterDef(mFilter);
    mGalType = GalType.ldap;
}
Also used : LdapGalMapRules(com.zimbra.cs.account.ldap.LdapGalMapRules) GalMode(com.zimbra.common.account.ZAttrProvisioning.GalMode)

Example 3 with GalMode

use of com.zimbra.common.account.ZAttrProvisioning.GalMode in project zm-mailbox by Zimbra.

the class GalSearchControl method ldapSearch.

public void ldapSearch() throws ServiceException {
    Domain domain = mParams.getDomain();
    GalMode galMode = domain.getGalMode();
    GalSearchType stype = mParams.getType();
    Provisioning prov = Provisioning.getInstance();
    if (needResources()) {
        mParams.setType(GalSearchType.resource);
        mParams.createSearchConfig(GalType.zimbra);
        try {
            prov.searchGal(mParams);
        } catch (Exception e) {
            throw ServiceException.FAILURE("ldap search failed", e);
        }
        mParams.setType(stype);
    }
    Integer ldapLimit = mParams.getLdapLimit();
    int limit;
    if (ldapLimit == null)
        limit = mParams.getLimit();
    else
        limit = ldapLimit;
    // restrict to domain config if we are not syncing, and there is no specific ldap limit set
    if (limit == 0 && GalOp.sync != mParams.getOp() && ldapLimit == null) {
        limit = domain.getGalMaxResults();
    }
    // Return the GAL definition last modified time so that clients can use it to decide if fullsync is required.
    if ((mParams.getOp() == GalOp.sync) && (mParams.getResultCallback() != null)) {
        String galLastModified = domain.getGalDefinitionLastModifiedTimeAsString();
        if (galLastModified != null) {
            mParams.getResultCallback().setGalDefinitionLastModified(galLastModified);
        }
    }
    ZimbraLog.gal.info("Using limit %d for ldapSearch", limit);
    mParams.setLimit(limit);
    if (galMode == GalMode.both) {
        // make two gal searches for 1/2 results each
        mParams.setLimit(limit / 2);
    }
    GalType type = GalType.ldap;
    if (galMode != GalMode.ldap) {
        // do zimbra gal search
        type = GalType.zimbra;
    }
    mParams.createSearchConfig(type);
    GalSyncToken galSyncToken = mParams.getGalSyncToken();
    if (galSyncToken != null) {
        mParams.setLdapTimeStamp(galSyncToken.getIntLdapTs());
        mParams.setLdapMatchCount(galSyncToken.getIntLdapMatchCount());
        mParams.setLdapHasMore(galSyncToken.intLdapHasMore());
        mParams.setMaxLdapTimeStamp(galSyncToken.getIntMaxLdapTs());
    }
    try {
        prov.searchGal(mParams);
    } catch (Exception e) {
        throw ServiceException.FAILURE("ldap search failed", e);
    }
    String resultToken = null;
    boolean intLdapHasMore = false;
    boolean extLdapHasMore = false;
    if (mParams.getResult() != null) {
        intLdapHasMore = mParams.getResult().getHadMore();
        if (mParams.getOp() == GalOp.sync) {
            resultToken = getLdapSearchResultToken(mParams.getResult(), "");
        }
    }
    if (galMode == GalMode.both) {
        // do the second query
        mParams.createSearchConfig(GalType.ldap);
        if (galSyncToken != null) {
            mParams.setLdapTimeStamp(galSyncToken.getExtLdapTs());
            mParams.setLdapMatchCount(galSyncToken.getExtLdapMatchCount());
            mParams.setLdapHasMore(galSyncToken.extLdapHasMore());
            mParams.setMaxLdapTimeStamp(galSyncToken.getExtMaxLdapTs());
        }
        try {
            prov.searchGal(mParams);
        } catch (Exception e) {
            throw ServiceException.FAILURE("ldap search failed", e);
        }
        if (mParams.getResult() != null) {
            extLdapHasMore = mParams.getResult().getHadMore();
            if (mParams.getOp() == GalOp.sync) {
                resultToken = getLdapSearchResultToken(mParams.getResult(), resultToken);
            }
        }
    }
    if (mParams.getResultCallback() != null) {
        if (mParams.getOp() == GalOp.sync) {
            mParams.getResultCallback().setNewToken(resultToken);
        }
        mParams.getResultCallback().setHasMoreResult(intLdapHasMore || extLdapHasMore);
    }
}
Also used : GalMode(com.zimbra.common.account.ZAttrProvisioning.GalMode) GalType(com.zimbra.cs.gal.GalSearchConfig.GalType) Domain(com.zimbra.cs.account.Domain) Provisioning(com.zimbra.cs.account.Provisioning) SoapFaultException(com.zimbra.common.soap.SoapFaultException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) GalSearchType(com.zimbra.soap.type.GalSearchType)

Example 4 with GalMode

use of com.zimbra.common.account.ZAttrProvisioning.GalMode in project zm-mailbox by Zimbra.

the class GalSearchControl method generateSearchQuery.

private void generateSearchQuery(Account galAcct) throws ServiceException {
    String query = mParams.getQuery();
    String searchByDn = mParams.getSearchEntryByDn();
    GalSearchType type = mParams.getType();
    StringBuilder searchQuery = new StringBuilder();
    if (searchByDn != null) {
        searchQuery.append("#dn:(" + searchByDn + ")");
    } else if (!Strings.isNullOrEmpty(query)) {
        searchQuery.append("contact:\"");
        // escape quotes
        searchQuery.append(query.replace("\"", "\\\""));
        searchQuery.append("\" AND");
    }
    GalSearchQueryCallback queryCallback = mParams.getExtraQueryCallback();
    if (queryCallback != null) {
        String extraQuery = queryCallback.getMailboxSearchQuery();
        if (extraQuery != null) {
            ZimbraLog.gal.debug("extra search query: " + extraQuery);
            searchQuery.append(" (").append(extraQuery).append(") AND");
        }
    }
    GalMode galMode = mParams.getDomain().getGalMode();
    boolean first = true;
    for (DataSource ds : galAcct.getAllDataSources()) {
        if (ds.getType() != DataSourceType.gal)
            continue;
        String galType = ds.getAttr(Provisioning.A_zimbraGalType);
        if (galMode == GalMode.ldap && galType.compareTo("zimbra") == 0)
            continue;
        if (galMode == GalMode.zimbra && galType.compareTo("ldap") == 0)
            continue;
        if (first)
            searchQuery.append("(");
        else
            searchQuery.append(" OR");
        first = false;
        searchQuery.append(" inid:").append(ds.getFolderId());
    }
    if (!first)
        searchQuery.append(")");
    switch(type) {
        case resource:
            searchQuery.append(" AND #zimbraAccountCalendarUserType:RESOURCE");
            break;
        case group:
            searchQuery.append(" AND #type:group");
            break;
        case account:
            searchQuery.append(" AND !(#zimbraAccountCalendarUserType:RESOURCE)");
            break;
        case all:
            break;
    }
    ZimbraLog.gal.debug("query: %s", searchQuery);
    mParams.parseSearchParams(mParams.getRequest(), searchQuery.toString());
}
Also used : GalMode(com.zimbra.common.account.ZAttrProvisioning.GalMode) GalSearchType(com.zimbra.soap.type.GalSearchType) DbDataSource(com.zimbra.cs.db.DbDataSource) DataSource(com.zimbra.cs.account.DataSource)

Aggregations

GalMode (com.zimbra.common.account.ZAttrProvisioning.GalMode)4 DataSource (com.zimbra.cs.account.DataSource)2 Domain (com.zimbra.cs.account.Domain)2 DbDataSource (com.zimbra.cs.db.DbDataSource)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 GalSearchType (com.zimbra.soap.type.GalSearchType)2 ServiceException (com.zimbra.common.service.ServiceException)1 SoapFaultException (com.zimbra.common.soap.SoapFaultException)1 Provisioning (com.zimbra.cs.account.Provisioning)1 LdapGalMapRules (com.zimbra.cs.account.ldap.LdapGalMapRules)1 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)1 GalType (com.zimbra.cs.gal.GalSearchConfig.GalType)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ItemId (com.zimbra.cs.service.util.ItemId)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1