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());
}
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;
}
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;
}
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;
}
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;
}
Aggregations