Search in sources :

Example 86 with Attributes

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;
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes)

Example 87 with Attributes

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);
        }
    }
}
Also used : BasicAttributes(javax.naming.directory.BasicAttributes) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) BasicAttributes(javax.naming.directory.BasicAttributes) Attributes(javax.naming.directory.Attributes) IOException(java.io.IOException)

Example 88 with Attributes

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));
}
Also used : Attribute(javax.naming.directory.Attribute) Hashtable(java.util.Hashtable) InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) DirContext(javax.naming.directory.DirContext) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 89 with Attributes

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());
    }
}
Also used : Attribute(javax.naming.directory.Attribute) NameNotFoundException(javax.naming.NameNotFoundException) Element(net.sf.ehcache.Element) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException)

Example 90 with Attributes

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));
}
Also used : UnknownHostException(java.net.UnknownHostException) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingEnumeration(javax.naming.NamingEnumeration) InetAddress(java.net.InetAddress)

Aggregations

Attributes (javax.naming.directory.Attributes)252 Attribute (javax.naming.directory.Attribute)135 SearchResult (javax.naming.directory.SearchResult)87 NamingException (javax.naming.NamingException)84 BasicAttributes (javax.naming.directory.BasicAttributes)72 ArrayList (java.util.ArrayList)61 BasicAttribute (javax.naming.directory.BasicAttribute)56 SearchControls (javax.naming.directory.SearchControls)55 DirContext (javax.naming.directory.DirContext)49 NamingEnumeration (javax.naming.NamingEnumeration)44 Test (org.junit.Test)34 InitialDirContext (javax.naming.directory.InitialDirContext)32 LdapContext (javax.naming.ldap.LdapContext)29 HashMap (java.util.HashMap)25 InitialLdapContext (javax.naming.ldap.InitialLdapContext)24 Hashtable (java.util.Hashtable)20 HashSet (java.util.HashSet)18 Map (java.util.Map)17 IOException (java.io.IOException)16 Identity (org.olat.core.id.Identity)16