Search in sources :

Example 11 with ZimbraQueryResults

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

the class ContactQueryTest method search.

@Test
public void search() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "Michael");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "michael.smith@zimbra.com");
    mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    fields.put(ContactConstants.A_firstName, "Jonathan");
    fields.put(ContactConstants.A_lastName, "Smith");
    fields.put(ContactConstants.A_email, "jonathan.smith@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"Jon Smith\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue("Expected some hits", results.hasNext());
    Assert.assertEquals("Hit ItemId not as expected", contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 12 with ZimbraQueryResults

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

the class ContactQueryTest method wildcard.

@Test
public void wildcard() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put(ContactConstants.A_firstName, "First*");
    fields.put(ContactConstants.A_lastName, "Las*t");
    fields.put(ContactConstants.A_email, "first.last@zimbra.com");
    Contact contact = mbox.createContact(null, new ParsedContact(fields), Mailbox.ID_FOLDER_CONTACTS, null);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "contact:\"First\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"First*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
    results = mbox.index.search(new OperationContext(mbox), "contact:\"Las*t\"", EnumSet.of(MailItem.Type.CONTACT), SortBy.NONE, 100);
    Assert.assertTrue(results.hasNext());
    Assert.assertEquals(contact.getId(), results.getNext().getItemId());
    results.close();
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ParsedContact(com.zimbra.cs.mime.ParsedContact) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Contact(com.zimbra.cs.mailbox.Contact) ParsedContact(com.zimbra.cs.mime.ParsedContact) Test(org.junit.Test)

Example 13 with ZimbraQueryResults

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

the class TextQueryTest method wildcardExpandedToNone.

@Test
public void wildcardExpandedToNone() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    ZimbraQueryResults results = mbox.index.search(new OperationContext(mbox), "none*", EnumSet.of(MailItem.Type.MESSAGE), SortBy.NONE, 100);
    Assert.assertFalse(results.hasNext());
    results = mbox.index.search(new OperationContext(mbox), "from:none* AND subject:none*", EnumSet.of(MailItem.Type.MESSAGE), SortBy.NONE, 100);
    Assert.assertFalse(results.hasNext());
    results = mbox.index.search(new OperationContext(mbox), "from:none* OR subject:none*", EnumSet.of(MailItem.Type.MESSAGE), SortBy.NONE, 100);
    Assert.assertFalse(results.hasNext());
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) Test(org.junit.Test)

Example 14 with ZimbraQueryResults

use of com.zimbra.cs.index.ZimbraQueryResults 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;
}
Also used : ZimbraHit(com.zimbra.cs.index.ZimbraHit) SearchParams(com.zimbra.cs.index.SearchParams) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) ServiceException(com.zimbra.common.service.ServiceException) MailboxInMaintenanceException(com.zimbra.cs.mailbox.MailServiceException.MailboxInMaintenanceException)

Example 15 with ZimbraQueryResults

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

the class GetShareNotifications 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);
    Element response = zsc.createElement(MailConstants.GET_SHARE_NOTIFICATIONS_RESPONSE);
    HashSet<String> shares = new HashSet<String>();
    ZimbraQueryResults zqr = null;
    try {
        zqr = mbox.index.search(octxt, query, SEARCH_TYPES, SortBy.DATE_DESC, 10);
        while (zqr.hasNext()) {
            ZimbraHit hit = zqr.getNext();
            if (hit instanceof MessageHit) {
                Message message = ((MessageHit) hit).getMessage();
                try {
                    for (MPartInfo part : Mime.getParts(message.getMimeMessage())) {
                        String ctype = StringUtil.stripControlCharacters(part.getContentType());
                        if (MimeConstants.CT_XML_ZIMBRA_SHARE.equals(ctype)) {
                            ShareNotification sn = ShareNotification.fromMimePart(part.getMimePart());
                            String shareItemId = sn.getGrantorId() + ":" + sn.getItemId();
                            if (shares.contains(shareItemId)) {
                                // this notification is stale as there is
                                // a new one for the same share.  delete
                                // this notification and skip to the next one.
                                sLog.info("deleting stale notification %s", message.getId());
                                mbox.delete(octxt, message.getId(), Type.MESSAGE);
                                continue;
                            }
                            shares.add(shareItemId);
                            Element share = response.addElement(sn.isRevoke() || sn.isExpire() ? MailConstants.E_REVOKE : MailConstants.E_SHARE);
                            if (sn.isExpire()) {
                                share.addAttribute(MailConstants.A_EXPIRE, true);
                            }
                            Element g = share.addUniqueElement(MailConstants.E_GRANTOR);
                            g.addAttribute(MailConstants.A_ID, sn.getGrantorId());
                            g.addAttribute(MailConstants.A_EMAIL, sn.getGrantorEmail());
                            g.addAttribute(MailConstants.A_NAME, sn.getGrantorName());
                            Element l = share.addUniqueElement(MailConstants.E_MOUNT);
                            l.addAttribute(MailConstants.A_ID, sn.getItemId());
                            l.addAttribute(MailConstants.A_NAME, sn.getItemName());
                            l.addAttribute(MailConstants.A_DEFAULT_VIEW, sn.getView());
                            l.addAttribute(MailConstants.A_RIGHTS, sn.getPermissions());
                            String status = (message.isUnread() ? "new" : "seen");
                            share.addAttribute(MailConstants.A_STATUS, status);
                            share.addAttribute(MailConstants.A_ID, "" + message.getId());
                            share.addAttribute(MailConstants.A_DATE, message.getDate());
                            if (sn.isRevoke() || sn.isExpire()) {
                                // purge revoke/expire notification upon receipt
                                mbox.delete(octxt, message.getId(), Type.MESSAGE);
                            }
                        }
                    }
                } catch (IOException e) {
                    ZimbraLog.misc.warn("can't parse share notification", e);
                } catch (MessagingException e) {
                    ZimbraLog.misc.warn("can't parse share notification", e);
                }
            }
        }
    } finally {
        Closeables.closeQuietly(zqr);
    }
    return response;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) ZimbraHit(com.zimbra.cs.index.ZimbraHit) Message(com.zimbra.cs.mailbox.Message) MessagingException(javax.mail.MessagingException) Element(com.zimbra.common.soap.Element) MessageHit(com.zimbra.cs.index.MessageHit) IOException(java.io.IOException) MPartInfo(com.zimbra.cs.mime.MPartInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ZimbraQueryResults(com.zimbra.cs.index.ZimbraQueryResults) ShareNotification(com.zimbra.common.share.ShareNotification) HashSet(java.util.HashSet)

Aggregations

ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)22 Mailbox (com.zimbra.cs.mailbox.Mailbox)13 ZimbraHit (com.zimbra.cs.index.ZimbraHit)11 OperationContext (com.zimbra.cs.mailbox.OperationContext)11 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)9 SearchParams (com.zimbra.cs.index.SearchParams)8 Element (com.zimbra.common.soap.Element)6 Account (com.zimbra.cs.account.Account)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 HashMap (java.util.HashMap)4 MessageHit (com.zimbra.cs.index.MessageHit)3 SortBy (com.zimbra.cs.index.SortBy)3 Contact (com.zimbra.cs.mailbox.Contact)3 Message (com.zimbra.cs.mailbox.Message)3 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)3 ZMailbox (com.zimbra.client.ZMailbox)2