Search in sources :

Example 6 with BrowseTerm

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

the class BrowseWrapper method browseBySender.

private List<DavResource> browseBySender(DavContext ctxt) throws IOException, ServiceException {
    ArrayList<DavResource> res = new ArrayList<DavResource>();
    String user = ctxt.getUser();
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.name, user);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    List<BrowseTerm> terms = mbox.browse(ctxt.getOperationContext(), Mailbox.BrowseBy.domains, "", 0);
    for (BrowseTerm term : terms) {
        if (term instanceof DomainBrowseTerm) {
            DomainBrowseTerm domain = (DomainBrowseTerm) term;
            if (domain.contains(DomainBrowseTerm.Field.FROM)) {
                res.add(new BrowseWrapper(generateUri(domain.getText()), getOwner()));
            }
        }
    }
    return res;
}
Also used : Account(com.zimbra.cs.account.Account) DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm) BrowseTerm(com.zimbra.cs.index.BrowseTerm) Mailbox(com.zimbra.cs.mailbox.Mailbox) DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm) ArrayList(java.util.ArrayList) Provisioning(com.zimbra.cs.account.Provisioning)

Example 7 with BrowseTerm

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

the class Mailbox method browse.

/**
     * Return a list of all the {attachments} or {doamins} or {objects} in this Mailbox, optionally with a prefix string
     * or limited by maximum number.
     *
     * @param max Maximum number of results to return.  0 means "return all results"  If more than max entries exist,
     * only the first max are returned, sorted by frequency.
     */
public List<BrowseTerm> browse(OperationContext octxt, BrowseBy browseBy, String regex, int max) throws IOException, ServiceException {
    boolean success = false;
    try {
        beginTransaction("browse", octxt);
        if (!hasFullAccess()) {
            throw ServiceException.PERM_DENIED("you do not have sufficient permissions on this mailbox");
        }
        List<BrowseTerm> result = null;
        switch(browseBy) {
            case attachments:
                result = index.getAttachmentTypes(regex);
                break;
            case domains:
                Map<String, DomainBrowseTerm> domains = new HashMap<String, DomainBrowseTerm>();
                for (BrowseTerm term : index.getDomains(LuceneFields.L_H_FROM, regex)) {
                    DomainBrowseTerm domain = domains.get(term.getText());
                    if (domain == null) {
                        domain = new DomainBrowseTerm(term);
                        domains.put(term.getText(), domain);
                    }
                    domain.addField(DomainBrowseTerm.Field.FROM);
                }
                for (BrowseTerm term : index.getDomains(LuceneFields.L_H_TO, regex)) {
                    DomainBrowseTerm domain = domains.get(term.getText());
                    if (domain == null) {
                        domain = new DomainBrowseTerm(term);
                        domains.put(term.getText(), domain);
                    }
                    domain.addField(DomainBrowseTerm.Field.TO);
                }
                for (BrowseTerm term : index.getDomains(LuceneFields.L_H_CC, regex)) {
                    DomainBrowseTerm domain = domains.get(term.getText());
                    if (domain == null) {
                        domain = new DomainBrowseTerm(term);
                        domains.put(term.getText(), domain);
                    }
                    domain.addField(DomainBrowseTerm.Field.CC);
                }
                result = new ArrayList<BrowseTerm>(domains.values());
                break;
            case objects:
                result = index.getObjects(regex);
                break;
            default:
                assert false : browseBy;
        }
        Collections.sort(result, new Comparator<BrowseTerm>() {

            @Override
            public int compare(BrowseTerm o1, BrowseTerm o2) {
                int retVal = o2.getFreq() - o1.getFreq();
                if (retVal == 0) {
                    retVal = o1.getText().compareTo(o2.getText());
                }
                return retVal;
            }
        });
        if (max > 0 && result.size() > max) {
            result = result.subList(0, max);
        }
        success = true;
        return result;
    } finally {
        endTransaction(success);
    }
}
Also used : DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm) BrowseTerm(com.zimbra.cs.index.BrowseTerm) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentLinkedHashMap(com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap) HashMap(java.util.HashMap) DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm)

Example 8 with BrowseTerm

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

the class MailboxIndex method existsInContacts.

/**
     * Returns true if any of the specified email addresses exists in contacts, otherwise false.
     */
public boolean existsInContacts(Collection<InternetAddress> addrs) throws IOException {
    Set<MailItem.Type> types = EnumSet.of(MailItem.Type.CONTACT);
    if (getDeferredCount(types) > 0) {
        try {
            indexDeferredItems(types, new BatchStatus(), false);
        } catch (ServiceException e) {
            ZimbraLog.index.error("Failed to index deferred items", e);
        }
    }
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    try {
        for (InternetAddress addr : addrs) {
            if (!Strings.isNullOrEmpty(addr.getAddress())) {
                String lcAddr = addr.getAddress().toLowerCase();
                TermFieldEnumeration values = null;
                try {
                    values = searcher.getIndexReader().getTermsForField(LuceneFields.L_CONTACT_DATA, lcAddr);
                    if (values.hasMoreElements()) {
                        BrowseTerm term = values.nextElement();
                        if (term != null && lcAddr.equals(term.getText())) {
                            ZimbraLog.index.debug("Contact = %s present in indexed items", lcAddr);
                            return true;
                        }
                    }
                } finally {
                    Closeables.closeQuietly(values);
                }
            }
        }
        return false;
    } finally {
        Closeables.closeQuietly(searcher);
    }
}
Also used : Type(com.zimbra.cs.mailbox.MailItem.Type) InternetAddress(com.zimbra.common.mime.InternetAddress) BrowseTerm(com.zimbra.cs.index.BrowseTerm) ServiceException(com.zimbra.common.service.ServiceException) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Example 9 with BrowseTerm

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

the class MailboxIndex method getObjects.

/**
     * Returns all objects (e.g. PO, etc) from the index.
     *
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the objects in the index
     */
public List<BrowseTerm> getObjects(String regex) throws IOException, ServiceException {
    Pattern pattern = Strings.isNullOrEmpty(regex) ? null : Pattern.compile(regex);
    List<BrowseTerm> result = new ArrayList<BrowseTerm>();
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    TermFieldEnumeration values = null;
    try {
        values = searcher.getIndexReader().getTermsForField(LuceneFields.L_OBJECTS, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            if (term == null) {
                break;
            }
            if (pattern == null || AccessBoundedRegex.matches(term.getText(), pattern, MAX_REGEX_ACCESSES)) {
                result.add(term);
            }
        }
    } finally {
        Closeables.closeQuietly(values);
        Closeables.closeQuietly(searcher);
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) BrowseTerm(com.zimbra.cs.index.BrowseTerm) TermFieldEnumeration(com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration) ArrayList(java.util.ArrayList) ZimbraIndexSearcher(com.zimbra.cs.index.ZimbraIndexSearcher)

Aggregations

BrowseTerm (com.zimbra.cs.index.BrowseTerm)9 ArrayList (java.util.ArrayList)5 DomainBrowseTerm (com.zimbra.cs.index.DomainBrowseTerm)4 TermFieldEnumeration (com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration)4 ZimbraIndexSearcher (com.zimbra.cs.index.ZimbraIndexSearcher)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)3 Pattern (java.util.regex.Pattern)3 Account (com.zimbra.cs.account.Account)2 Provisioning (com.zimbra.cs.account.Provisioning)2 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1 InternetAddress (com.zimbra.common.mime.InternetAddress)1 ServiceException (com.zimbra.common.service.ServiceException)1 Element (com.zimbra.common.soap.Element)1 Type (com.zimbra.cs.mailbox.MailItem.Type)1 BrowseBy (com.zimbra.cs.mailbox.Mailbox.BrowseBy)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1