use of com.zimbra.cs.index.ZimbraHit in project zm-mailbox by Zimbra.
the class TestUtil method searchForHits.
public static List<ZimbraHit> searchForHits(Mailbox mbox, String query, Set<MailItem.Type> types) throws Exception {
List<ZimbraHit> hits = Lists.newArrayList();
ZimbraQueryResults r = mbox.index.search(new OperationContext(mbox), query, types, SortBy.DATE_DESC, 100);
while (r.hasNext()) {
hits.add(r.getNext());
}
Closeables.closeQuietly(r);
return hits;
}
use of com.zimbra.cs.index.ZimbraHit in project zm-mailbox by Zimbra.
the class ScheduleInbox method getAppointmentsByUids.
@Override
public java.util.Collection<DavResource> getAppointmentsByUids(DavContext ctxt, List<String> hrefs) throws ServiceException, DavException {
List<DavResource> result = new ArrayList<DavResource>();
if (!DavResource.isSchedulingEnabled()) {
return result;
}
Account target = null;
Provisioning prov = Provisioning.getInstance();
if (ctxt.getActingAsDelegateFor() != null) {
target = prov.getAccountByName(ctxt.getActingAsDelegateFor());
}
String query = "is:invite is:unread inid:" + getId() + " after:\"-1month\" ";
Mailbox mbox = getMailbox(ctxt);
ZimbraQueryResults zqr = null;
try {
zqr = mbox.index.search(ctxt.getOperationContext(), query, SEARCH_TYPES, SortBy.DATE_ASC, 100);
while (zqr.hasNext()) {
ZimbraHit hit = zqr.getNext();
if (hit instanceof MessageHit) {
Message msg = ((MessageHit) hit).getMessage();
if (target == null && msg.getCalendarIntendedFor() != null) {
continue;
}
if (!msg.isInvite() || !msg.hasCalendarItemInfos()) {
continue;
}
/* Bug 40567. hide replies to avoid them being deleted by CalDAV clients.
* TODO: An alternative approach would be to show them but when they are "deleted", flag them as
* absent from the scheduling inbox.
*/
if ("REPLY".equals(msg.getCalendarItemInfo(0).getInvite().getMethod())) {
continue;
}
if (target != null) {
if (msg.getCalendarIntendedFor() == null) {
continue;
}
Account apptRcpt = prov.getAccountByName(msg.getCalendarIntendedFor());
if (apptRcpt == null || !apptRcpt.getId().equals(target.getId())) {
continue;
}
}
DavResource rs = UrlNamespace.getResourceFromMailItem(ctxt, msg);
if (rs != null) {
String href = UrlNamespace.getRawResourceUrl(rs);
if (hrefs == null)
result.add(rs);
else {
boolean found = false;
for (String ref : hrefs) {
if (HttpUtil.urlUnescape(ref).equals(href)) {
result.add(rs);
found = true;
break;
}
}
if (!found)
result.add(new DavResource.InvalidResource(href, getOwner()));
}
}
}
}
} catch (Exception e) {
ZimbraLog.dav.error("can't search: uri=" + getUri(), e);
} finally {
Closeables.closeQuietly(zqr);
}
return result;
}
use of com.zimbra.cs.index.ZimbraHit in project zm-mailbox by Zimbra.
the class Search method putHits.
private void putHits(ZimbraSoapContext zsc, OperationContext octxt, Element el, ZimbraQueryResults results, SearchParams params) throws ServiceException {
if (params.getInlineRule() == ExpandResults.HITS || params.getInlineRule() == ExpandResults.FIRST_MSG || params.getInlineRule() == ExpandResults.HITS_OR_FIRST_MSG || params.getInlineRule() == ExpandResults.UNREAD || params.getInlineRule() == ExpandResults.UNREAD_FIRST || params.getInlineRule() == ExpandResults.U_OR_FIRST_MSG || params.getInlineRule() == ExpandResults.U1_OR_FIRST_MSG) {
// these are not valid values for Search (according to soap.txt)
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());
boolean expand;
ExpandResults expandValue = params.getInlineRule();
int hitNum = 0;
while (pager.hasNext() && resp.size() < params.getLimit()) {
hitNum++;
ZimbraHit hit = pager.getNextHit();
if (hit instanceof MessageHit) {
/*
* Determine whether or not to expand MessageHits.
* This logic used to be in SearchResponse.isInlineExpand, but was moved
* to the handler classes because in some cases
* the decision to expand any particular hit is dependent on
* other hits (see SearchConv)
*/
if (expandValue == ExpandResults.NONE) {
expand = false;
} else if (expandValue == ExpandResults.ALL) {
expand = true;
} else if (expandValue == ExpandResults.FIRST) {
expand = params.getOffset() > 0 ? false : hitNum == 1;
} else {
expand = expandValue.matches(hit.getParsedItemID());
}
resp.add(hit, expand);
} else {
resp.add(hit);
}
}
resp.addHasMore(pager.hasNext());
resp.add(results.getResultInfo());
}
use of com.zimbra.cs.index.ZimbraHit in project zm-mailbox by Zimbra.
the class SearchConv method putHits.
/**
* This will only work for messages. That's OK since we force
* GROUP_BY_MESSAGE here.
*
* @param octxt operation context
* @param el SOAP container to put response data in
* @param msgs list of messages in this conversation
* @param results set of HITS for messages in this conversation which
* matches the search
* @param offset offset in conversation to start at
* @param limit number to return
* @return whether there are more messages in the conversation past
* the specified limit
* @throws ServiceException
*/
private boolean putHits(OperationContext octxt, ItemIdFormatter ifmt, SearchResponse resp, List<Message> msgs, ZimbraQueryResults results, SearchParams params, Conversation conv) throws ServiceException {
int offset = params.getOffset();
int limit = params.getLimit();
int size = msgs.size() <= limit + offset ? msgs.size() - offset : limit;
if (size > 0) {
// Array of ZimbraHit ptrs for matches, 1 entry for every message
// we might return from conv. NULL means no ZimbraHit presumably b/c the message didn't match the search.
// Note that the match for msgs[i] is matched[i-offset]!!!!
ZimbraHit[] matched = new ZimbraHit[size];
// For each hit, see if the hit message is in this conv (msgs).
while (results.hasNext()) {
ZimbraHit hit = results.getNext();
// since only they are getting returned.
for (int i = offset; i < offset + size; i++) {
if (hit.getParsedItemID().equals(new ItemId(msgs.get(i)))) {
matched[i - offset] = hit;
break;
}
}
}
ExpandResults expand = params.getInlineRule();
/* Build a boolean array of which messages should be expanded.
* This consolidates logic from SearchParams.isInlineExpand and the main message loop below
*/
// Okay, we've built the matched[] array. Now iterate through all the messages, and put the message or
// the MATCHED entry into the result
boolean[] expandMsgs = determineExpandedMessages(expand, matched, msgs, conv, offset, size);
for (int i = offset; i < offset + size; i++) {
boolean expandMsg = expandMsgs[i];
if (matched[i - offset] != null) {
resp.add(matched[i - offset], expandMsg);
} else {
Message msg = msgs.get(i);
//boolean inline = expand == ExpandResults.ALL || expand.matches(msg);
addMessageMiss(msg, resp.toElement(), octxt, ifmt, expandMsg, params);
}
}
}
return offset + size < msgs.size();
}
use of com.zimbra.cs.index.ZimbraHit in project zm-mailbox by Zimbra.
the class ImapSessionManager method loadVirtualFolder.
/** Fetches the messages contained within a search folder. When a search
* folder is IMAP-visible, it appears in folder listings, is SELECTable
* READ-ONLY, and appears to have all matching messages as its contents.
* If it is not visible, it will be completely hidden from all IMAP
* commands.
* @param octxt Encapsulation of the authenticated user.
* @param search The search folder being exposed. */
private static List<ImapMessage> loadVirtualFolder(OperationContext octxt, SearchFolder search) throws ServiceException {
List<ImapMessage> i4list = new ArrayList<ImapMessage>();
Set<MailItem.Type> types = ImapFolder.getTypeConstraint(search);
if (types.isEmpty()) {
return i4list;
}
SearchParams params = new SearchParams();
params.setQueryString(search.getQuery());
params.setIncludeTagDeleted(true);
params.setTypes(types);
params.setSortBy(SortBy.DATE_ASC);
params.setChunkSize(1000);
params.setFetchMode(SearchParams.Fetch.IMAP);
Mailbox mbox = search.getMailbox();
try {
ZimbraQueryResults zqr = mbox.index.search(SoapProtocol.Soap12, octxt, params);
try {
for (ZimbraHit hit = zqr.getNext(); hit != null; hit = zqr.getNext()) {
i4list.add(hit.getImapMessage());
}
} finally {
zqr.close();
}
} catch (ServiceException e) {
throw e;
} catch (Exception e) {
throw ServiceException.FAILURE("failure opening search folder", e);
}
return i4list;
}
Aggregations