Search in sources :

Example 11 with SearchParams

use of com.zimbra.cs.index.SearchParams in project zm-mailbox by Zimbra.

the class Search method handle.

@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);
    fixBooleanRecipients(request);
    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);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) SearchRequest(com.zimbra.soap.mail.message.SearchRequest) SearchParams(com.zimbra.cs.index.SearchParams) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMailbox(com.zimbra.client.ZMailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults)

Example 12 with SearchParams

use of com.zimbra.cs.index.SearchParams in project zm-mailbox by Zimbra.

the class SearchConv method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    OperationContext octxt = getOperationContext(zsc, context);
    ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
    SearchConvRequest req = JaxbUtil.elementToJaxb(request);
    boolean nest = ZmBoolean.toBool(req.getNestMessages(), false);
    Account acct = getRequestedAccount(zsc);
    SearchParams params = SearchParams.parse(req, zsc, acct.getPrefMailInitialSearch());
    // append (conv:(convid)) onto the beginning of the queryStr
    ItemId cid = new ItemId(req.getConversationId(), zsc);
    params.setQueryString("conv:\"" + cid.toString(ifmt) + "\" (" + params.getQueryString() + ')');
    // force to group-by-message
    params.setTypes(EnumSet.of(MailItem.Type.MESSAGE));
    Element response = null;
    if (cid.belongsTo(mbox)) {
        // local
        ZimbraQueryResults results = mbox.index.search(zsc.getResponseProtocol(), octxt, params);
        try {
            response = zsc.createElement(MailConstants.SEARCH_CONV_RESPONSE);
            response.addAttribute(MailConstants.A_QUERY_OFFSET, Integer.toString(params.getOffset()));
            SortBy sort = results.getSortBy();
            response.addAttribute(MailConstants.A_SORTBY, sort.toString());
            List<Message> msgs = mbox.getMessagesByConversation(octxt, cid.getId(), sort, -1);
            if (msgs.isEmpty() && zsc.isDelegatedRequest()) {
                throw ServiceException.PERM_DENIED("you do not have sufficient permissions");
            }
            // filter out IMAP \Deleted messages from the message lists
            Conversation conv = mbox.getConversationById(octxt, cid.getId());
            if (conv.isTagged(Flag.FlagInfo.DELETED)) {
                List<Message> raw = msgs;
                msgs = new ArrayList<Message>();
                for (Message msg : raw) {
                    if (!msg.isTagged(Flag.FlagInfo.DELETED)) {
                        msgs.add(msg);
                    }
                }
            }
            Element container = nest ? ToXML.encodeConversationSummary(response, ifmt, octxt, conv, CONVERSATION_FIELD_MASK) : response;
            SearchResponse builder = new SearchResponse(zsc, octxt, container, params);
            builder.setAllRead(conv.getUnreadCount() == 0);
            boolean more = putHits(octxt, ifmt, builder, msgs, results, params, conv);
            response.addAttribute(MailConstants.A_QUERY_MORE, more);
            // call me AFTER putHits since some of the <info> is generated by the getting of the hits!
            builder.add(results.getResultInfo());
        } finally {
            Closeables.closeQuietly(results);
        }
        return response;
    } else {
        // remote
        try {
            Element proxyRequest = zsc.createElement(MailConstants.SEARCH_CONV_REQUEST);
            Account target = Provisioning.getInstance().get(AccountBy.id, cid.getAccountId(), zsc.getAuthToken());
            if (target != null) {
                params.setInlineRule(params.getInlineRule().toLegacyExpandResults(target.getServer()));
            }
            params.encodeParams(proxyRequest);
            proxyRequest.addAttribute(MailConstants.A_NEST_MESSAGES, nest);
            proxyRequest.addAttribute(MailConstants.A_CONV_ID, cid.toString());
            // okay, lets run the search through the query parser -- this has the side-effect of
            // re-writing the query in a format that is OK to proxy to the other server -- since the
            // query has an "AND conv:remote-conv-id" part, the query parser will figure out the right
            // format for us.  TODO somehow make this functionality a bit more exposed in the
            // ZimbraQuery APIs...
            String rewrittenQueryString = mbox.getRewrittenQueryString(octxt, params);
            proxyRequest.addAttribute(MailConstants.E_QUERY, rewrittenQueryString, Element.Disposition.CONTENT);
            // proxy to remote account
            response = proxyRequest(proxyRequest, context, target.getId());
            return response.detach();
        } catch (SoapFaultException e) {
            throw ServiceException.FAILURE("SoapFaultException: ", e);
        }
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) SearchParams(com.zimbra.cs.index.SearchParams) Message(com.zimbra.cs.mailbox.Message) ItemIdFormatter(com.zimbra.cs.service.util.ItemIdFormatter) SortBy(com.zimbra.cs.index.SortBy) Element(com.zimbra.common.soap.Element) Conversation(com.zimbra.cs.mailbox.Conversation) ItemId(com.zimbra.cs.service.util.ItemId) SoapFaultException(com.zimbra.common.soap.SoapFaultException) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) SearchConvRequest(com.zimbra.soap.mail.message.SearchConvRequest)

Aggregations

SearchParams (com.zimbra.cs.index.SearchParams)12 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)8 Mailbox (com.zimbra.cs.mailbox.Mailbox)7 Element (com.zimbra.common.soap.Element)5 Account (com.zimbra.cs.account.Account)4 ZimbraHit (com.zimbra.cs.index.ZimbraHit)4 OperationContext (com.zimbra.cs.mailbox.OperationContext)4 ServiceException (com.zimbra.common.service.ServiceException)3 ItemId (com.zimbra.cs.service.util.ItemId)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 ZMailbox (com.zimbra.client.ZMailbox)2 SoapFaultException (com.zimbra.common.soap.SoapFaultException)2 ContactHit (com.zimbra.cs.index.ContactHit)2 SortBy (com.zimbra.cs.index.SortBy)2 Conversation (com.zimbra.cs.mailbox.Conversation)2 Message (com.zimbra.cs.mailbox.Message)2 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)2 ArrayList (java.util.ArrayList)2 GalContact (com.zimbra.cs.account.GalContact)1 GuestAccount (com.zimbra.cs.account.GuestAccount)1