private void searchGal(String syncToken, SearchGalResult result, boolean fetchGroupMembers) throws ServiceException { ZimbraLog.gal.debug("searchGal: " + syncToken); DataSource ds = getDataSource(); GalSearchParams params = new GalSearchParams(ds); params.setGalResult(result); params.setToken(syncToken); params.setQuery("*"); for (String attr : ZIMBRA_ATTRS) params.getConfig().getRules().add(attr + "=" + attr); params.getConfig().getRules().setFetchGroupMembers(fetchGroupMembers); params.getConfig().getRules().setNeedSMIMECerts(true); Provisioning.getInstance().searchGal(params); }
@Override public Collection<AddressObject> match(DavContext ctxt, AddressbookCollection folder) { // search the folder for #key:val where key is mName and val is mTextMatch.mText. // boolean ignoreCase = mCollation.equals(DavElements.ASCII); ArrayList<AddressObject> result = new ArrayList<AddressObject>(); StringBuilder search = new StringBuilder(); if (mNegate) search.append("!"); AttrMapping mapping = sAttrMappings.get(mEnclosingFilter.getName().toUpperCase()); if (mapping == null) return result; String[] attrs = mapping.getAttrs(); search.append("("); boolean first = true; for (String key : attrs) { if (!first) search.append(" OR "); search.append("#").append(key).append(":"); if (mMatch == MatchType.contains || mMatch == MatchType.ends_with) search.append("*"); search.append(mText); if (mMatch == MatchType.contains || mMatch == MatchType.starts_with) search.append("*"); first = false; } search.append(")"); String filter = search.toString(); ZimbraLog.dav.debug("Search Filter: %s", filter); ZimbraQueryResults zqr = null; try { Mailbox mbox = ctxt.getTargetMailbox(); if (mbox == null) { ZimbraLog.dav.debug("Can't get target mailbox for %s", ctxt.getUser()); return result; } zqr = mbox.index.search( ctxt.getOperationContext(), filter, EnumSet.of(MailItem.Type.CONTACT), SortBy.NAME_ASC, 100); while (zqr.hasNext()) { ZimbraHit hit = zqr.getNext(); if (hit instanceof ContactHit) { result.add(new AddressObject(ctxt, ((ContactHit) hit).getContact())); } } } catch (Exception e) { ZimbraLog.dav.warn("can't get target mailbox", e); return result; } finally { Closeables.closeQuietly(zqr); } boolean includeGal = true; if (includeGal) { StringBuilder query = new StringBuilder(); if (mMatch == MatchType.contains || mMatch == MatchType.ends_with) query.append("*"); query.append(mText); if (mMatch == MatchType.contains || mMatch == MatchType.starts_with) query.append("*"); ZimbraLog.dav.debug("Gal query: %s", query.toString()); GalSearchParams params = new GalSearchParams(ctxt.getAuthAccount()); params.setType(GalSearchType.account); params.setQuery(query.toString()); params.setResultCallback(new Callback(ctxt, result, params)); GalSearchControl gal = new GalSearchControl(params); try { gal.search(); } catch (ServiceException e) { if (ServiceException.PERM_DENIED.equals(e.getCode())) ZimbraLog.dav.debug( "Can't get gal search result for %s (%s)", ctxt.getUser(), e.getMessage()); else ZimbraLog.dav.error("Can't get gal search result for %s", ctxt.getUser()); } } return result; }