use of javax.naming.NamingEnumeration in project uPortal by Jasig.
the class LDAPGroupStore method searchForEntities.
public EntityIdentifier[] searchForEntities(String query, int method, Class type) throws GroupsException {
if (type != group && type != iperson)
return new EntityIdentifier[0];
// Guarantee that LDAP injection is prevented by replacing LDAP special characters
// with escaped versions of the character
query = LdapEncoder.filterEncode(query);
ArrayList ids = new ArrayList();
switch(method) {
case STARTS_WITH:
query = query + "*";
break;
case ENDS_WITH:
query = "*" + query;
break;
case CONTAINS:
query = "*" + query + "*";
break;
}
query = namefield + "=" + query;
DirContext context = getConnection();
NamingEnumeration userlist = null;
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
sc.setReturningAttributes(new String[] { keyfield });
try {
userlist = context.search(usercontext, query, sc);
ArrayList keys = new ArrayList();
processLdapResults(userlist, keys);
String[] k = (String[]) keys.toArray(new String[0]);
for (int i = 0; i < k.length; i++) {
ids.add(new EntityIdentifier(k[i], iperson));
}
return (EntityIdentifier[]) ids.toArray(new EntityIdentifier[0]);
} catch (NamingException nex) {
throw new GroupsException("LDAPGroupStore: Unable to perform filter " + query, nex);
}
}
use of javax.naming.NamingEnumeration in project uPortal by Jasig.
the class SimpleLdapSecurityContext method getAttributeValue.
/*--------------------- Helper methods ---------------------*/
/**
* Return a single value of an attribute from possibly multiple values, grossly ignoring
* anything else. If there are no values, then return an empty string.
*
* @param attrs LDAP query results
* @param attribute LDAP attribute we are interested in
* @return a single value of the attribute
*/
private String getAttributeValue(Attributes attrs, int attribute) throws NamingException {
NamingEnumeration values = null;
String aValue = "";
if (!isAttribute(attribute))
return aValue;
Attribute attrib = attrs.get(attributes[attribute]);
if (attrib != null) {
for (values = attrib.getAll(); values.hasMoreElements(); ) {
aValue = (String) values.nextElement();
// take only the first attribute value
break;
}
}
return aValue;
}
use of javax.naming.NamingEnumeration in project geode by apache.
the class SocketCreator method reverseDNS.
/**
* This method uses JNDI to look up an address in DNS and return its name
*
* @param addr
*
* @return the host name associated with the address or null if lookup isn't possible or there is
* no host name for this address
*/
public static String reverseDNS(InetAddress addr) {
byte[] addrBytes = addr.getAddress();
// reverse the address suitable for reverse lookup
String lookup = "";
for (int index = addrBytes.length - 1; index >= 0; index--) {
lookup = lookup + (addrBytes[index] & 0xff) + '.';
}
lookup += "in-addr.arpa";
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attrs = ctx.getAttributes(lookup, new String[] { "PTR" });
for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements(); ) {
Attribute attr = (Attribute) ae.next();
for (Enumeration vals = attr.getAll(); vals.hasMoreElements(); ) {
Object elem = vals.nextElement();
if ("PTR".equals(attr.getID()) && elem != null) {
return elem.toString();
}
}
}
ctx.close();
} catch (Exception e) {
// ignored
}
return null;
}
use of javax.naming.NamingEnumeration in project geode by apache.
the class ContextJUnitTest method clearContext.
/**
* Removes all entries from the specified context, including subcontexts.
*
* @param context context to clear
*/
private void clearContext(Context context) throws NamingException {
for (NamingEnumeration e = context.listBindings(""); e.hasMoreElements(); ) {
Binding binding = (Binding) e.nextElement();
if (binding.getObject() instanceof Context) {
clearContext((Context) binding.getObject());
}
context.unbind(binding.getName());
}
}
use of javax.naming.NamingEnumeration in project geode by apache.
the class ContextJUnitTest method verifyListBindings.
private void verifyListBindings(Context c, String name, Object obj1, Object obj2) throws NamingException {
boolean datasourceFoundFlg = false;
boolean o2FoundFlg = false;
boolean datasourceO1FoundFlg = false;
boolean datasourceNullFoundFlg = false;
// List bindings for the specified context
for (NamingEnumeration en = c.listBindings(name); en.hasMore(); ) {
Binding b = (Binding) en.next();
if (b.getName().equals("datasource")) {
assertEquals(b.getObject(), dataSourceContext);
datasourceFoundFlg = true;
Context nextCon = (Context) b.getObject();
for (NamingEnumeration en1 = nextCon.listBindings(""); en1.hasMore(); ) {
Binding b1 = (Binding) en1.next();
if (b1.getName().equals("sub41")) {
assertEquals(b1.getObject(), obj1);
datasourceO1FoundFlg = true;
} else if (b1.getName().equals("sub43")) {
// check for null object
assertNull(b1.getObject());
datasourceNullFoundFlg = true;
}
}
} else if (b.getName().equals("sub42")) {
assertEquals(b.getObject(), obj2);
o2FoundFlg = true;
}
}
if (!(datasourceFoundFlg && o2FoundFlg && datasourceO1FoundFlg && datasourceNullFoundFlg)) {
fail();
}
}
Aggregations