use of javax.naming.directory.Attributes in project rabbitmq-java-client by rabbitmq.
the class DnsSrvRecordAddressResolver method lookupSrvRecords.
protected List<SrvRecord> lookupSrvRecords(String service, String dnsUrls) throws IOException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", dnsUrls);
List<SrvRecord> records = new ArrayList<SrvRecord>();
try {
DirContext ctx = new InitialDirContext(env);
Attributes attributes = ctx.getAttributes(service, new String[] { "SRV" });
NamingEnumeration<?> servers = attributes.get("srv").getAll();
while (servers.hasMore()) {
records.add(mapSrvRecord((String) servers.next()));
}
} catch (NamingException e) {
throw new IOException("Error during DNS SRV query", e);
}
return records;
}
use of javax.naming.directory.Attributes in project Lucee by lucee.
the class AbsDefaultHostnameVerifier method extractCN.
static String extractCN(final String subjectPrincipal) throws SSLException {
if (subjectPrincipal == null) {
return null;
}
try {
final LdapName subjectDN = new LdapName(subjectPrincipal);
final List<Rdn> rdns = subjectDN.getRdns();
for (int i = rdns.size() - 1; i >= 0; i--) {
final Rdn rds = rdns.get(i);
final Attributes attributes = rds.toAttributes();
final Attribute cn = attributes.get("cn");
if (cn != null) {
try {
final Object value = cn.get();
if (value != null) {
return value.toString();
}
} catch (NoSuchElementException ignore) {
} catch (NamingException ignore) {
}
}
}
return null;
} catch (InvalidNameException e) {
throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
}
}
use of javax.naming.directory.Attributes 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.Attributes in project cxf by apache.
the class LdapClaimsHandler method retrieveClaimValues.
public ProcessedClaimCollection retrieveClaimValues(ClaimCollection claims, ClaimsParameters parameters) {
String user = null;
boolean useLdapLookup = false;
Principal principal = parameters.getPrincipal();
if (principal instanceof KerberosPrincipal) {
KerberosPrincipal kp = (KerberosPrincipal) principal;
StringTokenizer st = new StringTokenizer(kp.getName(), "@");
user = st.nextToken();
} else if (principal instanceof X500Principal) {
X500Principal x500p = (X500Principal) principal;
LOG.warning("Unsupported principal type X500: " + x500p.getName());
return new ProcessedClaimCollection();
} else if (principal != null) {
user = principal.getName();
if (user == null) {
LOG.warning("User must not be null");
return new ProcessedClaimCollection();
}
useLdapLookup = LdapUtils.isDN(user);
} else {
LOG.warning("Principal is null");
return new ProcessedClaimCollection();
}
if (LOG.isLoggable(Level.FINEST)) {
LOG.finest("Retrieve claims for user " + user);
}
Map<String, Attribute> ldapAttributes = null;
if (useLdapLookup) {
AttributesMapper<Map<String, Attribute>> mapper = new AttributesMapper<Map<String, Attribute>>() {
public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException {
Map<String, Attribute> map = new HashMap<>();
NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
while (attrEnum.hasMore()) {
Attribute att = attrEnum.next();
map.put(att.getID(), att);
}
return map;
}
};
ldapAttributes = ldap.lookup(user, mapper);
} else {
List<String> searchAttributeList = new ArrayList<>();
for (Claim claim : claims) {
String claimType = claim.getClaimType().toString();
if (getClaimsLdapAttributeMapping().keySet().contains(claimType)) {
searchAttributeList.add(getClaimsLdapAttributeMapping().get(claimType));
} else {
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Unsupported claim: " + claimType);
}
}
}
String[] searchAttributes = searchAttributeList.toArray(new String[searchAttributeList.size()]);
if (this.userBaseDn != null) {
ldapAttributes = LdapUtils.getAttributesOfEntry(ldap, this.userBaseDn, this.getObjectClass(), this.getUserNameAttribute(), user, searchAttributes);
}
if (this.userBaseDNs != null && (ldapAttributes == null || ldapAttributes.isEmpty())) {
for (String userBase : userBaseDNs) {
ldapAttributes = LdapUtils.getAttributesOfEntry(ldap, userBase, this.getObjectClass(), this.getUserNameAttribute(), user, searchAttributes);
if (ldapAttributes != null && !ldapAttributes.isEmpty()) {
// User found
break;
}
}
}
}
if (ldapAttributes == null || ldapAttributes.isEmpty()) {
// No result
if (LOG.isLoggable(Level.INFO)) {
LOG.info("User '" + user + "' not found");
}
return new ProcessedClaimCollection();
}
ProcessedClaimCollection claimsColl = new ProcessedClaimCollection();
for (Claim claim : claims) {
ProcessedClaim c = processClaim(claim, ldapAttributes, principal);
if (c != null) {
// c.setIssuer(issuer);
// c.setOriginalIssuer(originalIssuer);
// c.setNamespace(namespace);
claimsColl.add(c);
}
}
return claimsColl;
}
use of javax.naming.directory.Attributes in project cxf by apache.
the class DefaultHostnameVerifier method extractCN.
static String extractCN(final String subjectPrincipal) throws SSLException {
if (subjectPrincipal == null) {
return null;
}
try {
final LdapName subjectDN = new LdapName(subjectPrincipal);
final List<Rdn> rdns = subjectDN.getRdns();
for (int i = rdns.size() - 1; i >= 0; i--) {
final Rdn rds = rdns.get(i);
final Attributes attributes = rds.toAttributes();
final Attribute cn = attributes.get("cn");
if (cn != null) {
try {
final Object value = cn.get();
if (value != null) {
return value.toString();
}
} catch (NoSuchElementException ignore) {
//
} catch (NamingException ignore) {
//
}
}
}
return null;
} catch (InvalidNameException e) {
throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
}
}
Aggregations