use of javax.naming.directory.Attributes in project perun by CESNET.
the class ExtSourceLdap method getGroupSubjects.
public List<Map<String, String>> getGroupSubjects(Map<String, String> attributes) throws InternalErrorException {
NamingEnumeration<SearchResult> results = null;
List<String> ldapGroupSubjects = new ArrayList<String>();
// Get the LDAP group name
String ldapGroupName = attributes.get(GroupsManager.GROUPMEMBERSQUERY_ATTRNAME);
// Get optional filter for members filtering
String filter = attributes.get(GroupsManager.GROUPMEMBERSFILTER_ATTRNAME);
try {
log.trace("LDAP External Source: searching for group subjects [{}]", ldapGroupName);
String attrName;
if (getAttributes().containsKey("memberAttribute")) {
attrName = (String) getAttributes().get("memberAttribute");
} else {
// Default value
attrName = "uniqueMember";
}
List<String> retAttrs = new ArrayList<String>();
retAttrs.add(attrName);
String[] retAttrsArray = retAttrs.toArray(new String[retAttrs.size()]);
Attributes attrs = getContext().getAttributes(ldapGroupName, retAttrsArray);
Attribute ldapAttribute = null;
// Get the list of returned groups, should be only one
if (attrs.get(attrName) != null) {
// Get the attribute which holds group subjects
ldapAttribute = attrs.get(attrName);
}
if (ldapAttribute != null) {
// Get the DNs of the subjects
for (int i = 0; i < ldapAttribute.size(); i++) {
String ldapSubjectDN = (String) ldapAttribute.get(i);
ldapGroupSubjects.add(ldapSubjectDN);
log.trace("LDAP External Source: found group subject [{}].", ldapSubjectDN);
}
}
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
// If attribute filter not exists, use optional default filter from extSource definition
if (filter == null)
filter = filteredQuery;
// Now query LDAP again and search for each subject
for (String ldapSubjectName : ldapGroupSubjects) {
subjects.addAll(this.querySource(filter, ldapSubjectName, 0));
}
return subjects;
} catch (NamingException e) {
log.error("LDAP exception during running query '{}'", ldapGroupName);
throw new InternalErrorException("Entry '" + ldapGroupName + "' was not found in LDAP.", e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (Exception e) {
log.error("LDAP exception during closing result, while running query '{}'", ldapGroupName);
throw new InternalErrorException(e);
}
}
}
use of javax.naming.directory.Attributes in project perun by CESNET.
the class ExtSourceLdap method querySource.
/**
* Query LDAP using query in defined base. Results can be limited to the maxResults.
*
* @param query
* @param base
* @param maxResults
* @return List of Map of the LDAP attribute names and theirs values
* @throws InternalErrorException
*/
protected List<Map<String, String>> querySource(String query, String base, int maxResults) throws InternalErrorException {
NamingEnumeration<SearchResult> results = null;
List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
try {
// If query is null, then we are finding object by the base
if (query == null) {
log.trace("search base [{}]", base);
// TODO jmena atributu spise prijimiat pres vstupni parametr metody
Attributes ldapAttributes = getContext().getAttributes(base);
if (ldapAttributes.size() > 0) {
Map<String, String> attributes = this.getSubjectAttributes(ldapAttributes);
if (!attributes.isEmpty()) {
subjects.add(attributes);
}
}
} else {
log.trace("search string [{}]", query);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// Set timeout to 5s
controls.setTimeLimit(5000);
if (maxResults > 0) {
controls.setCountLimit(maxResults);
}
if (base == null)
base = "";
results = getContext().search(base, query, controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Map<String, String> subjectAttributes = this.getSubjectAttributes(attributes);
if (!subjectAttributes.isEmpty()) {
subjects.add(subjectAttributes);
}
}
}
log.trace("Returning [{}] subjects", subjects.size());
return subjects;
} catch (NamingException e) {
log.error("LDAP exception during running query '{}'", query);
throw new InternalErrorException("LDAP exception during running query: " + query + ".", e);
} finally {
try {
if (results != null) {
results.close();
}
} catch (Exception e) {
log.error("LDAP exception during closing result, while running query '{}'", query);
throw new InternalErrorException(e);
}
}
}
use of javax.naming.directory.Attributes in project cloudstack by apache.
the class OpenLdapUserManagerImpl method createUser.
protected LdapUser createUser(final SearchResult result) throws NamingException {
final Attributes attributes = result.getAttributes();
final String username = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getUsernameAttribute());
final String email = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getEmailAttribute());
final String firstname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getFirstnameAttribute());
final String lastname = LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getLastnameAttribute());
final String principal = result.getNameInNamespace();
String domain = principal.replace("cn=" + LdapUtils.getAttributeValue(attributes, _ldapConfiguration.getCommonNameAttribute()) + ",", "");
domain = domain.replace("," + _ldapConfiguration.getBaseDn(), "");
domain = domain.replace("ou=", "");
boolean disabled = isUserDisabled(result);
return new LdapUser(username, email, firstname, lastname, principal, domain, disabled);
}
use of javax.naming.directory.Attributes in project jmeter by apache.
the class LDAPExtSampler method writeSearchResult.
private void writeSearchResult(final SearchResult sr, final XMLBuffer xmlb) throws NamingException {
final Attributes attrs = sr.getAttributes();
final int size = attrs.size();
final ArrayList<Attribute> sortedAttrs = new ArrayList<>(size);
// $NON-NLS-1$
xmlb.openTag("searchresult");
// $NON-NLS-1$
xmlb.tag("dn", sr.getName());
// $NON-NLS-1$
xmlb.tag("returnedattr", Integer.toString(size));
// $NON-NLS-1$
xmlb.openTag("attributes");
try {
for (NamingEnumeration<? extends Attribute> en = attrs.getAll(); en.hasMore(); ) {
final Attribute attr = en.next();
sortedAttrs.add(attr);
}
sortAttributes(sortedAttrs);
for (final Attribute attr : sortedAttrs) {
StringBuilder sb = new StringBuilder();
if (attr.size() == 1) {
sb.append(getWriteValue(attr.get()));
} else {
final ArrayList<String> sortedVals = new ArrayList<>(attr.size());
boolean first = true;
for (NamingEnumeration<?> ven = attr.getAll(); ven.hasMore(); ) {
final Object value = getWriteValue(ven.next());
sortedVals.add(value.toString());
}
Collections.sort(sortedVals);
for (final String value : sortedVals) {
if (first) {
first = false;
} else {
// $NON-NLS-1$
sb.append(", ");
}
sb.append(value);
}
}
xmlb.tag(attr.getID(), sb);
}
} finally {
// $NON-NLS-1$
xmlb.closeTag("attributes");
// $NON-NLS-1$
xmlb.closeTag("searchresult");
}
}
use of javax.naming.directory.Attributes in project jmeter by apache.
the class LDAPExtSampler method getUserAttributes.
/***************************************************************************
* Collect all the values from the table (Arguments), using this create the
* Attributes, this will create the Attributes for the User
* defined TestCase for Add Test
*
* @return The Attributes
**************************************************************************/
private Attributes getUserAttributes() {
Attributes attrs = new BasicAttributes(true);
Attribute attr;
for (JMeterProperty jMeterProperty : getArguments()) {
Argument item = (Argument) jMeterProperty.getObjectValue();
attr = attrs.get(item.getName());
if (attr == null) {
attr = getBasicAttribute(item.getName(), item.getValue());
} else {
attr.add(item.getValue());
}
attrs.put(attr);
}
return attrs;
}
Aggregations