use of javax.naming.directory.SearchResult in project fess by codelibs.
the class LdapManager method getAttributeValueList.
protected List<Object> getAttributeValueList(final List<SearchResult> result, final String name) {
try {
for (final SearchResult srcrslt : result) {
final Attributes attrs = srcrslt.getAttributes();
final Attribute attr = attrs.get(name);
if (attr == null) {
continue;
}
final List<Object> attrList = new ArrayList<>();
for (int i = 0; i < attr.size(); i++) {
final Object attrValue = attr.get(i);
if (attrValue != null) {
attrList.add(attrValue);
}
}
return attrList;
}
return Collections.emptyList();
} catch (final NamingException e) {
throw new LdapOperationException("Failed to parse attribute values for " + name, e);
}
}
use of javax.naming.directory.SearchResult in project Lucee by lucee.
the class LDAPClient method query.
/**
* @param dn
* @param strAttributes
* @param scope
* @param startrow
* @param maxrows
* @param timeout
* @param sort
* @param sortType
* @param sortDirection
* @param start
* @param separator
* @param filter
* @return
* @throws NamingException
* @throws PageException
* @throws IOException
*/
public Query query(String strAttributes, int scope, int startrow, int maxrows, int timeout, String[] sort, int sortType, int sortDirection, String start, String separator, String filter) throws NamingException, PageException, IOException {
// strAttributes=strAttributes.trim();
boolean attEQAsterix = strAttributes.trim().equals("*");
String[] attributes = attEQAsterix ? new String[] { "name", "value" } : toStringAttributes(strAttributes, ",");
// Control
SearchControls controls = new SearchControls();
controls.setReturningObjFlag(true);
controls.setSearchScope(scope);
if (!attEQAsterix)
controls.setReturningAttributes(toStringAttributes(strAttributes, ","));
if (maxrows > 0)
controls.setCountLimit(startrow + maxrows + 1);
if (timeout > 0)
controls.setTimeLimit(timeout);
InitialLdapContext context = new InitialLdapContext(env, null);
// Search
Query qry = new QueryImpl(attributes, 0, "query");
try {
NamingEnumeration results = context.search(start, filter, controls);
// Fill result
int row = 1;
if (!attEQAsterix) {
while (results.hasMoreElements()) {
SearchResult resultRow = (SearchResult) results.next();
if (row++ < startrow)
continue;
int len = qry.addRow();
NamingEnumeration rowEnum = resultRow.getAttributes().getAll();
String dn = resultRow.getNameInNamespace();
qry.setAtEL("dn", len, dn);
while (rowEnum.hasMore()) {
Attribute attr = (Attribute) rowEnum.next();
Collection.Key key = KeyImpl.init(attr.getID());
Enumeration values = attr.getAll();
Object value;
String existing, strValue;
while (values.hasMoreElements()) {
value = values.nextElement();
strValue = Caster.toString(value, null);
existing = Caster.toString(qry.getAt(key, len, null), null);
if (!StringUtil.isEmpty(existing) && !StringUtil.isEmpty(strValue)) {
value = existing + separator + strValue;
} else if (!StringUtil.isEmpty(existing))
value = existing;
qry.setAtEL(key, len, value);
}
}
if (maxrows > 0 && len >= maxrows)
break;
}
} else {
outer: while (results.hasMoreElements()) {
SearchResult resultRow = (SearchResult) results.next();
if (row++ < startrow)
continue;
Attributes attributesRow = resultRow.getAttributes();
NamingEnumeration rowEnum = attributesRow.getIDs();
while (rowEnum.hasMoreElements()) {
int len = qry.addRow();
String name = Caster.toString(rowEnum.next());
Object value = null;
try {
value = attributesRow.get(name).get();
} catch (Exception e) {
}
qry.setAtEL("name", len, name);
qry.setAtEL("value", len, value);
if (maxrows > 0 && len >= maxrows)
break outer;
}
qry.setAtEL("name", qry.size(), "dn");
}
}
} finally {
context.close();
}
// Sort
if (sort != null && sort.length > 0) {
int order = sortDirection == SORT_DIRECTION_ASC ? Query.ORDER_ASC : Query.ORDER_DESC;
for (int i = sort.length - 1; i >= 0; i--) {
String item = sort[i];
if (item.indexOf(' ') != -1)
item = ListUtil.first(item, " ", true);
qry.sort(KeyImpl.getInstance(item), order);
// keys[i] = new SortKey(item);
}
}
return qry;
}
use of javax.naming.directory.SearchResult in project traccar by tananaev.
the class LdapProvider method isAdmin.
private boolean isAdmin(String accountName) {
if (this.adminFilter != null) {
try {
InitialDirContext context = initContext();
String searchString = adminFilter.replace(":login", accountName);
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = context.search(searchBase, searchString, searchControls);
if (results.hasMoreElements()) {
results.nextElement();
if (results.hasMoreElements()) {
Log.warning("Matched multiple users for the accountName: " + accountName);
return false;
}
return true;
}
} catch (NamingException e) {
return false;
}
}
return false;
}
use of javax.naming.directory.SearchResult in project traccar by tananaev.
the class LdapProvider method getUser.
public User getUser(String accountName) {
SearchResult ldapUser;
User user = new User();
try {
ldapUser = lookupUser(accountName);
if (ldapUser != null) {
Attribute attribute = ldapUser.getAttributes().get(idAttribute);
if (attribute != null) {
user.setLogin((String) attribute.get());
} else {
user.setLogin(accountName);
}
attribute = ldapUser.getAttributes().get(nameAttribute);
if (attribute != null) {
user.setName((String) attribute.get());
} else {
user.setName(accountName);
}
attribute = ldapUser.getAttributes().get(mailAttribute);
if (attribute != null) {
user.setEmail((String) attribute.get());
} else {
user.setEmail(accountName);
}
}
user.setAdmin(isAdmin(accountName));
} catch (NamingException e) {
user.setLogin(accountName);
user.setName(accountName);
user.setEmail(accountName);
Log.warning(e);
}
return user;
}
use of javax.naming.directory.SearchResult in project cxf by apache.
the class LdapCertificateRepo method getCertificatesFromLdap.
private List<X509Certificate> getCertificatesFromLdap(String tmpRootDN, String tmpFilter, String tmpAttrName) {
try {
List<X509Certificate> certificates = new ArrayList<>();
NamingEnumeration<SearchResult> answer = ldapSearch.searchSubTree(tmpRootDN, tmpFilter);
while (answer.hasMore()) {
SearchResult sr = answer.next();
Attributes attrs = sr.getAttributes();
Attribute attribute = attrs.get(tmpAttrName);
if (attribute != null) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream((byte[]) attribute.get()));
certificates.add(certificate);
}
}
return certificates;
} catch (CertificateException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (NamingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
Aggregations