use of javax.naming.directory.Attributes in project directory-ldap-api by apache.
the class AttributeUtils method toAttributes.
/**
* Converts an {@link Entry} to an {@link Attributes}.
*
* @param entry
* the {@link Entry} to convert
* @return
* the equivalent {@link Attributes}
*/
public static Attributes toAttributes(Entry entry) {
if (entry != null) {
Attributes attributes = new BasicAttributes(true);
// Looping on attributes
for (Iterator<Attribute> attributeIterator = entry.iterator(); attributeIterator.hasNext(); ) {
Attribute entryAttribute = attributeIterator.next();
attributes.put(toJndiAttribute(entryAttribute));
}
return attributes;
}
return null;
}
use of javax.naming.directory.Attributes in project directory-ldap-api by apache.
the class LdifAttributesReader method parseAttributes.
/**
* A method which parses a ldif string and returns a list of Attributes.
*
* @param ldif The ldif string
* @return A list of Attributes, or an empty List
* @throws LdapLdifException If something went wrong
*/
public Attributes parseAttributes(String ldif) throws LdapLdifException {
lines = new ArrayList<String>();
position = 0;
LOG.debug(I18n.msg(I18n.MSG_13407_STARTS_PARSING_LDIF));
if (Strings.isEmpty(ldif)) {
return new BasicAttributes(true);
}
StringReader strIn = new StringReader(ldif);
reader = new BufferedReader(strIn);
try {
readLines();
Attributes attributes = parseAttributes();
if (LOG.isDebugEnabled()) {
if (attributes == null) {
LOG.debug(I18n.msg(I18n.MSG_13401_PARSED_NO_ENTRY));
} else {
LOG.debug(I18n.msg(I18n.MSG_13402_PARSED_ONE_ENTRY));
}
}
return attributes;
} catch (LdapLdifException ne) {
LOG.error(I18n.err(I18n.ERR_13403_CANNOT_PARSE_LDIF_BUFFER, ne.getLocalizedMessage()));
throw new LdapLdifException(I18n.err(I18n.ERR_13442_ERROR_PARSING_LDIF_BUFFER), ne);
} finally {
try {
reader.close();
} catch (IOException ioe) {
throw new LdapLdifException(I18n.err(I18n.ERR_13450_CANNOT_CLOSE_FILE), ioe);
}
}
}
use of javax.naming.directory.Attributes in project goodies by sonatype.
the class LdapTestEnvironmentTest method smoke.
@Test
public void smoke() throws Exception {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:" + getLdapServer().getPort() + "/o=sonatype");
env.put(Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN);
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// Let's open a connection on this partition
InitialContext initialContext = new InitialLdapContext(env, null);
// We should be able to read it
DirContext appRoot = (DirContext) initialContext.lookup("");
assertThat(appRoot, notNullValue());
// Let's get the entry associated to the top level
Attributes attributes = appRoot.getAttributes("");
assertThat(attributes, notNullValue());
assertThat((String) attributes.get("o").get(), equalTo("sonatype"));
Attribute attribute = attributes.get("objectClass");
assertThat(attribute, notNullValue());
assertThat(attribute.contains("top"), is(true));
assertThat(attribute.contains("organization"), is(true));
}
use of javax.naming.directory.Attributes in project LogHub by fbacchella.
the class NameResolver method resolve.
@Override
public boolean resolve(Event event, String query, String destination) throws ProcessorException {
Element cacheElement = hostCache.get(query);
if (cacheElement != null && !cacheElement.isExpired()) {
Object o = cacheElement.getObjectValue();
// Set only if it was not a negative cache
if (o != null) {
event.put(destination, o);
}
return true;
}
try {
Attributes attrs = localContext.get().getAttributes(query, new String[] { type });
for (NamingEnumeration<? extends Attribute> ae = attrs.getAll(); ae.hasMoreElements(); ) {
Attribute attr = (Attribute) ae.next();
if (attr.getID() != type) {
continue;
}
Object o = attr.getAll().next();
if (o != null) {
String value = attr.getAll().next().toString();
value = value.substring(0, value.length() - 1).intern();
hostCache.put(new Element(query, value));
event.put(destination, value);
}
}
return true;
} catch (IllegalArgumentException e) {
throw event.buildException("can't setup resolver for '" + query + "':" + e.getCause().getMessage());
} catch (NameNotFoundException ex) {
// Expected failure from DNS, don't care
// But keep a short negative cache to avoid flooding
Element e = new Element(query, null);
e.setTimeToLive(timeout * 5);
hostCache.put(e);
return false;
} catch (NamingException e) {
throw event.buildException("unresolvable name '" + query + "': " + e.getMessage());
}
}
use of javax.naming.directory.Attributes in project BiglyBT by BiglySoftware.
the class DNSUtilsImpl method getAllByName.
@Override
public List<InetAddress> getAllByName(DNSUtils.DNSDirContext context, String host) throws UnknownHostException {
System.out.println("Lookup for " + host);
List<InetAddress> result = new ArrayList<>();
try {
String[] attributes = new String[] { "A", "AAAA" };
Attributes attrs = ((DNSDirContextImpl) context).ctx.getAttributes(host, attributes);
if (attrs != null) {
for (String a : attributes) {
Attribute attr = attrs.get(a);
if (attr != null) {
NamingEnumeration values = attr.getAll();
while (values.hasMore()) {
Object value = values.next();
if (value instanceof String) {
try {
result.add(InetAddress.getByName((String) value));
} catch (Throwable e) {
}
}
}
}
}
}
} catch (Throwable e) {
}
if (result.size() > 0) {
return (result);
}
throw (new UnknownHostException(host));
}
Aggregations