Example usage for javax.naming.directory Attributes get

List of usage examples for javax.naming.directory Attributes get

Introduction

In this page you can find the example usage for javax.naming.directory Attributes get.

Prototype

Attribute get(String attrID);

Source Link

Document

Retrieves the attribute with the given attribute id from the attribute set.

Usage

From source file:net.spfbl.core.Reverse.java

private static TreeSet<String> getIPv4Set(String host) throws NamingException {
    TreeSet<String> ipSet = new TreeSet<String>();
    Attributes atributes = Server.getAttributesDNS(host, new String[] { "A" });
    if (atributes != null) {
        Attribute attribute = atributes.get("A");
        if (attribute != null) {
            for (int index = 0; index < attribute.size(); index++) {
                String ip = (String) attribute.get(index);
                if (SubnetIPv4.isValidIPv4(ip)) {
                    ip = SubnetIPv4.normalizeIPv4(ip);
                    ipSet.add(ip);/*from   w  ww. j a v  a 2s  . com*/
                }
            }
        }
    }
    return ipSet;
}

From source file:net.spfbl.core.Reverse.java

private static TreeSet<String> getIPv6Set(String host) throws NamingException {
    TreeSet<String> ipSet = new TreeSet<String>();
    Attributes atributes = Server.getAttributesDNS(host, new String[] { "AAAA" });
    if (atributes != null) {
        Attribute attribute = atributes.get("AAAA");
        if (attribute != null) {
            for (int index = 0; index < attribute.size(); index++) {
                String ip = (String) attribute.get(index);
                if (SubnetIPv6.isValidIPv6(ip)) {
                    ip = SubnetIPv6.normalizeIPv6(ip);
                    ipSet.add(ip);//from  w  w w  .j  a v  a  2 s.co  m
                }
            }
        }
    }
    return ipSet;
}

From source file:net.spfbl.core.Reverse.java

public static TreeSet<String> getPointerSet(String host) throws NamingException {
    if (host == null) {
        return null;
    } else {/*  w w  w.jav  a  2s .c  o m*/
        TreeSet<String> reverseSet = new TreeSet<String>();
        if (Subnet.isValidIP(host)) {
            if (SubnetIPv4.isValidIPv4(host)) {
                host = getHostReverse(host, "in-addr.arpa");
            } else if (SubnetIPv6.isValidIPv6(host)) {
                host = getHostReverse(host, "ip6.arpa");
            }
        }
        Attributes atributes = Server.getAttributesDNS(host, new String[] { "PTR" });
        if (atributes != null) {
            Attribute attribute = atributes.get("PTR");
            if (attribute != null) {
                for (int index = 0; index < attribute.size(); index++) {
                    host = (String) attribute.get(index);
                    if (host != null) {
                        host = host.trim();
                        if (host.endsWith(".")) {
                            int endIndex = host.length() - 1;
                            host = host.substring(0, endIndex);
                        }
                        if (Domain.isHostname(host)) {
                            host = Domain.normalizeHostname(host, true);
                            reverseSet.add(host);
                        }
                    }
                }
            }
        }
        return reverseSet;
    }
}

From source file:com.adito.activedirectory.ActiveDirectoryUserDatabase.java

protected static String getAttributeValue(Attributes attributes, String attributeName) throws NamingException {
    if (attributes == null) {
        return null;
    }//from  www. j  ava  2  s .co m
    Attribute attribute = attributes.get(attributeName);
    return attribute == null ? "" : (String) attribute.get();
}

From source file:net.spfbl.core.Reverse.java

public static ArrayList<String> getMXSet(String host) throws NamingException {
    TreeMap<Integer, TreeSet<String>> mxMap = new TreeMap<Integer, TreeSet<String>>();
    Attributes atributes = Server.getAttributesDNS(host, new String[] { "MX" });
    if (atributes == null || atributes.size() == 0) {
        atributes = Server.getAttributesDNS(host, new String[] { "CNAME" });
        Attribute attribute = atributes.get("CNAME");
        if (attribute != null) {
            String cname = (String) attribute.get(0);
            return getMXSet(cname);
        }//from  w w  w .  j  a  v a 2 s . c om
    } else {
        Attribute attribute = atributes.get("MX");
        if (attribute != null) {
            for (int index = 0; index < attribute.size(); index++) {
                try {
                    String mx = (String) attribute.get(index);
                    int space = mx.indexOf(' ');
                    String value = mx.substring(0, space);
                    int priority = Integer.parseInt(value);
                    mx = mx.substring(space + 1);
                    int last = mx.length() - 1;
                    TreeSet<String> mxSet = mxMap.get(priority);
                    if (mxSet == null) {
                        mxSet = new TreeSet<String>();
                        mxMap.put(priority, mxSet);
                    }
                    if (Subnet.isValidIP(mx.substring(0, last))) {
                        mxSet.add(Subnet.normalizeIP(mx.substring(0, last)));
                    } else if (Domain.isHostname(mx)) {
                        mxSet.add(Domain.normalizeHostname(mx, true));
                    }
                } catch (NumberFormatException ex) {
                }
            }
        }
    }
    ArrayList<String> mxList = new ArrayList<String>();
    if (mxMap.isEmpty()) {
        // https://tools.ietf.org/html/rfc5321#section-5
        mxList.add(Domain.normalizeHostname(host, true));
    } else {
        for (int priority : mxMap.keySet()) {
            TreeSet<String> mxSet = mxMap.get(priority);
            for (String mx : mxSet) {
                if (!mxList.contains(mx)) {
                    mxList.add(mx);
                }
            }
        }
    }
    return mxList;
}

From source file:com.seyren.core.security.ldap.LdapUserManagement.java

@Override
public String[] autoCompleteUsers(String name) {
    List<String> users = new ArrayList<String>();
    try {//from   ww  w .  j a v a 2s.c  om
        DirContext readOnlyContext = contextSource.getReadOnlyContext();
        SearchControls ctls = new SearchControls();
        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String[] attrIDs = { USERNAME };
        ctls.setReturningAttributes(attrIDs);
        NamingEnumeration<SearchResult> results = readOnlyContext.search("", "(sAMAccountName=" + name + "*)",
                ctls);
        while (results.hasMore()) {
            SearchResult rslt = results.next();
            Attributes attrs = rslt.getAttributes();
            if (attrs.get(USERNAME) != null) {
                users.add((String) attrs.get(USERNAME).get());
            }
        }
    } catch (NamingException e) {

    }
    return users.toArray(new String[users.size()]);
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.ldap.GrailsLdapRoleMapper.java

/**
 * {@inheritDoc}//from   ww w. j  a  v  a2s  . c o m
 * @see org.springframework.ldap.core.AttributesMapper#mapFromAttributes(javax.naming.directory.Attributes)
 */
public Object mapFromAttributes(final Attributes attributes) throws NamingException {
    Attribute roleAttr = attributes.get(_groupRoleAttributeName);

    NamingEnumeration<?> ne = roleAttr.getAll();
    // assert ne.hasMore();
    Object group = ne.next();
    String role = group.toString();

    return new GrantedAuthorityImpl(_rolePrefix + role.toUpperCase());
}

From source file:fedora.server.security.servletfilters.ldap.FilterLdap.java

private static Boolean comparePassword(Attributes attributes, String password, String passwordAttribute)
        throws PasswordComparisonException {
    String m = "- comparePassword() ";
    log.debug(m + ">");
    Boolean rc = null;/*ww w.  ja va  2s  .c om*/
    try {
        log.debug(m + "looking for return attribute==" + passwordAttribute);
        Attribute attribute = attributes.get(passwordAttribute);
        if (attribute == null) {
            log.error(m + "null object");
        } else {
            int size = attribute.size();
            log.debug(m + "object with n==" + size);
            for (int j = 0; j < size; j++) {
                Object o = attribute.get(j);
                if (password.equals(o.toString())) {
                    log.debug(m + "compares true");
                    if (rc == null) {
                        log.debug(m + "1st comp:  authenticate");
                        rc = Boolean.TRUE;
                    } else {
                        log.error(m + "dup comp:  keep previous rc==" + rc);
                    }
                } else {
                    log.debug(m + "compares false, -un-authenticate");
                    if (rc == null) {
                        log.debug(m + "1st comp (fyi)");
                    } else {
                        log.error(m + "dup comp (fyi)");
                    }
                    rc = Boolean.FALSE;
                }
            }
        }
    } catch (Throwable th) {
        log.error(m + "resetting to null rc==" + rc + th.getMessage());
        throw new PasswordComparisonException("in ldap servlet filter", th);
    } finally {
        log.debug(m + "< " + rc);
    }
    return rc;
}

From source file:io.gravitee.management.idp.ldap.lookup.LdapIdentityLookup.java

private String attributeValue(Attributes attrs, String attributeId) throws NamingException {
    Attribute attr = attrs.get(attributeId);
    if (attr != null) {
        return (String) attr.get();
    }//  ww w  .  j av a2s.  c  o  m

    return null;
}

From source file:org.eurekastreams.server.persistence.mappers.ldap.AttributesToDisplayNameSuffixTransformer.java

/**
 * Transform the input Attributes to the display name suffix, or null if not found or not applicable.
 * /*from  w  ww. j  a v a  2 s .  c o m*/
 * @param inAttributes
 *            The Attributes to transform to the display name suffix
 * @return Transformed object.
 */
public String transform(final Attributes inAttributes) {
    String result = null;

    try {
        Attribute attribute = inAttributes.get(attributeName);
        log.debug("Matching attribute(with brackets added): [" + attributeName + " = " + attribute.get() + "]");
        if (attribute != null && attribute.get().toString().matches(regularExpression)) {
            log.debug("Matched - result (with brackets added): [" + displayNameSuffix + "]");
            result = displayNameSuffix;
        }
    } catch (Exception ex) {
        log.error("Error transforming Attributes to displayNameSuffix: ", ex);
        result = null;
    }

    return result;
}