protected static void putInfo(Element response, ZimbraQueryResults results) { List<QueryInfo> qinfo = results.getResultInfo(); if (qinfo.size() > 0) { Element qinfoElt = response.addElement(MailConstants.E_INFO); for (QueryInfo inf : qinfo) { inf.toXml(qinfoElt); } } }
@Override public Element handle(Element request, Map<String, Object> context) throws ServiceException { ZimbraSoapContext zsc = getZimbraSoapContext(context); Mailbox mbox = getRequestedMailbox(zsc); Account account = getRequestedAccount(zsc); OperationContext octxt = getOperationContext(zsc, context); SearchRequest req = JaxbUtil.elementToJaxb(request); if (Objects.firstNonNull(req.getWarmup(), false)) { mbox.index.getIndexStore().warmup(); return zsc.createElement(MailConstants.SEARCH_RESPONSE); } SearchParams params = SearchParams.parse(req, zsc, account.getPrefMailInitialSearch()); if (params.getLocale() == null) { params.setLocale(mbox.getAccount().getLocale()); } if (params.inDumpster() && params.getTypes().contains(MailItem.Type.CONVERSATION)) { throw ServiceException.INVALID_REQUEST("cannot search for conversations in dumpster", null); } if (LC.calendar_cache_enabled.booleanValue()) { List<String> apptFolderIds = getFolderIdListIfSimpleAppointmentsQuery(params, zsc); if (apptFolderIds != null) { Account authAcct = getAuthenticatedAccount(zsc); Element response = zsc.createElement(MailConstants.SEARCH_RESPONSE); runSimpleAppointmentQuery(response, params, octxt, zsc, authAcct, mbox, apptFolderIds); return response; } } ZimbraQueryResults results = mbox.index.search(zsc.getResponseProtocol(), octxt, params); try { // create the XML response Element Element response = zsc.createElement(MailConstants.SEARCH_RESPONSE); // must use results.getSortBy() because the results might have ignored our sortBy // request and used something else... response.addAttribute(MailConstants.A_SORTBY, results.getSortBy().toString()); putHits(zsc, octxt, response, results, params); return response; } finally { Closeables.closeQuietly(results); } }
@Override public boolean hasNext() { if (results == null) { return false; } try { return results.hasNext(); } catch (ServiceException e) { ZimbraLog.misc.warn("caught exception", e); return false; } }
/** * initialize the Pop3Mailbox, without keeping a reference to either the Mailbox object or any of * the Message objects in the inbox. */ Pop3Mailbox(Mailbox mbox, Account acct, String query) throws ServiceException { id = mbox.getId(); numDeleted = 0; opContext = new OperationContext(acct); deleteOption = acct.getPrefPop3DeleteOption(); if (Strings.isNullOrEmpty(query)) { Set<Integer> folderIds = acct.isPrefPop3IncludeSpam() ? ImmutableSet.of(Mailbox.ID_FOLDER_INBOX, Mailbox.ID_FOLDER_SPAM) : Collections.singleton(Mailbox.ID_FOLDER_INBOX); String dateConstraint = acct.getAttr(Provisioning.A_zimbraPrefPop3DownloadSince); Date popSince = dateConstraint == null ? null : DateUtil.parseGeneralizedTime(dateConstraint); messages = mbox.openPop3Folder(opContext, folderIds, popSince); for (Pop3Message p3m : messages) { totalSize += p3m.getSize(); } } else { ZimbraQueryResults results = null; messages = new ArrayList<Pop3Message>(500); try { results = mbox.index.search(opContext, query, POP3_TYPES, SortBy.DATE_DESC, 500); while (results.hasNext()) { ZimbraHit hit = results.getNext(); if (hit instanceof MessageHit) { MessageHit mh = (MessageHit) hit; Message msg = mh.getMessage(); if (!msg.isTagged(Flag.FlagInfo.POPPED)) { totalSize += msg.getSize(); messages.add(new Pop3Message(msg)); } } } } finally { Closeables.closeQuietly(results); } } }
@Override public MailItem next() { if (results == null) { return null; } try { ZimbraHit hit = results.getNext(); if (hit != null) { return hit.getMailItem(); } } catch (ServiceException e) { ZimbraLog.misc.warn("caught exception", e); } return null; }
private void putHits( ZimbraSoapContext zsc, OperationContext octxt, Element el, ZimbraQueryResults results, SearchParams params) throws ServiceException { if (params.getInlineRule() == ExpandResults.HITS) { // "hits" is not a valid value for Search... params.setInlineRule(ExpandResults.NONE); } ResultsPager pager = ResultsPager.create(results, params); if (params.getCursor() != null) { if (params.getCursor().isIncludeOffset()) { long offset = pager.getCursorOffset(); if (offset >= 0) { el.addAttribute(MailConstants.A_QUERY_OFFSET, offset); } } } else { el.addAttribute(MailConstants.A_QUERY_OFFSET, params.getOffset()); } SearchResponse resp = new SearchResponse(zsc, octxt, el, params); resp.setIncludeMailbox(false); resp.setSortOrder(pager.getSortOrder()); while (pager.hasNext() && resp.size() < params.getLimit()) { ZimbraHit hit = pager.getNextHit(); resp.add(hit); } resp.addHasMore(pager.hasNext()); resp.add(results.getResultInfo()); }
@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; }