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