Search in sources :

Example 1 with BrowseTerm

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

the class MailboxTest method browse.

@Test
public void browse() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
    mbox.addMessage(null, new ParsedMessage("From: test1-1@sub1.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test1-2@sub1.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test1-3@sub1.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test1-4@sub1.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test2-1@sub2.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test2-2@sub2.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test2-3@sub2.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test3-1@sub3.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test3-2@sub3.zimbra.com".getBytes(), false), dopt, null);
    mbox.addMessage(null, new ParsedMessage("From: test4-1@sub4.zimbra.com".getBytes(), false), dopt, null);
    mbox.index.indexDeferredItems();
    List<BrowseTerm> terms = mbox.browse(null, Mailbox.BrowseBy.domains, null, 100);
    Assert.assertEquals("sub1.zimbra.com", terms.get(0).getText());
    Assert.assertEquals("sub2.zimbra.com", terms.get(1).getText());
    Assert.assertEquals("sub3.zimbra.com", terms.get(2).getText());
    Assert.assertEquals("sub4.zimbra.com", terms.get(3).getText());
    Assert.assertEquals("Number of expected terms", 4, terms.size());
    Assert.assertEquals(8, terms.get(0).getFreq());
    Assert.assertEquals(6, terms.get(1).getFreq());
    Assert.assertEquals(4, terms.get(2).getFreq());
    Assert.assertEquals(2, terms.get(3).getFreq());
}
Also used : BrowseTerm(com.zimbra.cs.index.BrowseTerm) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Test(org.junit.Test)

Example 2 with BrowseTerm

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

the class BrowseWrapper method browseByType.

private List<DavResource> browseByType(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.attachments, "", 0);
    for (BrowseTerm term : terms) {
        String ctype = term.getText();
        int index = ctype.indexOf('/');
        if (index != -1 || ctype.equals("message") || ctype.equals("none")) {
            continue;
        // the client still gets confused about having a slash in the
        // path even after encoding
        //ctype = ctype.substring(0,index) + "%2F" + ctype.substring(index+1);
        }
        res.add(new BrowseWrapper(generateUri(ctype), getOwner()));
    }
    res.add(new BrowseWrapper(generateUri("word"), getOwner()));
    res.add(new BrowseWrapper(generateUri("excel"), getOwner()));
    res.add(new BrowseWrapper(generateUri("ppt"), getOwner()));
    res.add(new BrowseWrapper(generateUri("pdf"), 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) ArrayList(java.util.ArrayList) Provisioning(com.zimbra.cs.account.Provisioning)

Example 3 with BrowseTerm

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

the class Browse method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Mailbox mbox = getRequestedMailbox(zsc);
    String browseBy = request.getAttribute(MailConstants.A_BROWSE_BY);
    BrowseBy parsedBrowseBy = BrowseBy.valueOf(browseBy.toLowerCase());
    String regex = request.getAttribute(MailConstants.A_REGEX, "").toLowerCase();
    int max = (int) (request.getAttributeLong(MailConstants.A_MAX_TO_RETURN, 0));
    List<BrowseTerm> terms;
    try {
        terms = mbox.browse(getOperationContext(zsc, context), parsedBrowseBy, regex, max);
    } catch (IOException e) {
        throw ServiceException.FAILURE("Failed to browse terms", e);
    }
    Element response = zsc.createElement(MailConstants.BROWSE_RESPONSE);
    for (BrowseTerm term : terms) {
        if (term instanceof DomainBrowseTerm) {
            DomainBrowseTerm domain = (DomainBrowseTerm) term;
            Element e = response.addElement(MailConstants.E_BROWSE_DATA).setText(domain.getText());
            String flags = domain.getHeaderFlags();
            if (!flags.isEmpty()) {
                e.addAttribute(MailConstants.A_BROWSE_DOMAIN_HEADER, flags);
            }
            e.addAttribute(MailConstants.A_FREQUENCY, domain.getFreq());
        } else {
            Element e = response.addElement(MailConstants.E_BROWSE_DATA).setText(term.getText());
            e.addAttribute(MailConstants.A_FREQUENCY, term.getFreq());
        }
    }
    return response;
}
Also used : BrowseBy(com.zimbra.cs.mailbox.Mailbox.BrowseBy) DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm) BrowseTerm(com.zimbra.cs.index.BrowseTerm) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) DomainBrowseTerm(com.zimbra.cs.index.DomainBrowseTerm) Element(com.zimbra.common.soap.Element) IOException(java.io.IOException)

Example 4 with BrowseTerm

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

the class MailboxIndex method getAttachmentTypes.

/**
     * Returns all attachment types from the index.
     *
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the attachment types in the index
     */
public List<BrowseTerm> getAttachmentTypes(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_ATTACHMENTS, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            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)

Example 5 with BrowseTerm

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

the class MailboxIndex method getDomains.

/**
     * Returns all domain names from the index.
     *
     * @param field Lucene field name (e.g. LuceneFields.L_H_CC)
     * @param regex matching pattern or null to match everything
     * @return {@link BrowseTerm}s which correspond to all of the domain terms stored in a given field
     */
public List<BrowseTerm> getDomains(String field, String regex) throws IOException, ServiceException {
    Pattern pattern = Strings.isNullOrEmpty(regex) ? null : Pattern.compile(regex.startsWith("@") ? regex : "@" + regex);
    List<BrowseTerm> result = new ArrayList<BrowseTerm>();
    ZimbraIndexSearcher searcher = indexStore.openSearcher();
    TermFieldEnumeration values = null;
    try {
        values = searcher.getIndexReader().getTermsForField(field, "");
        while (values.hasMoreElements()) {
            BrowseTerm term = values.nextElement();
            if (term == null) {
                break;
            }
            String text = term.getText();
            // Domains are tokenized with '@' prefix. Exclude partial domain tokens.
            if (text.startsWith("@") && text.contains(".")) {
                if (pattern == null || AccessBoundedRegex.matches(text, pattern, MAX_REGEX_ACCESSES)) {
                    result.add(new BrowseTerm(text.substring(1), term.getFreq()));
                }
            }
        }
    } 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