Example usage for javax.naming.directory Attribute size

List of usage examples for javax.naming.directory Attribute size

Introduction

In this page you can find the example usage for javax.naming.directory Attribute size.

Prototype

int size();

Source Link

Document

Retrieves the number of values in this attribute.

Usage

From source file:org.pentaho.di.trans.steps.mailvalidator.MailValidation.java

/**
 * verify if there is a mail server registered to the domain name. and return the email servers count
 *//*from  ww  w.  j  av a 2  s  .c  o  m*/
public static int mailServersCount(String hostName) throws NamingException {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    DirContext ictx = new InitialDirContext(env);
    Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
    Attribute attr = attrs.get("MX");
    if (attr == null) {
        return (0);
    }
    return (attr.size());
}

From source file:org.pentaho.di.trans.steps.mailvalidator.MailValidation.java

private static ArrayList<String> getMX(String hostName) throws NamingException {
    // Perform a DNS lookup for MX records in the domain
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    DirContext ictx = new InitialDirContext(env);
    Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
    Attribute attr = attrs.get("MX");

    // if we don't have an MX record, try the machine itself
    if ((attr == null) || (attr.size() == 0)) {
        attrs = ictx.getAttributes(hostName, new String[] { "A" });
        attr = attrs.get("A");
        if (attr == null) {
            throw new NamingException(BaseMessages.getString(PKG, "MailValidator.NoMatchName", hostName));
        }//w  ww  . j a  va 2s  . com
    }

    // Huzzah! we have machines to try. Return them as an array list
    // NOTE: We SHOULD take the preference into account to be absolutely
    // correct. This is left as an exercise for anyone who cares.
    ArrayList<String> res = new ArrayList<String>();
    NamingEnumeration<?> en = attr.getAll();

    while (en.hasMore()) {
        String x = (String) en.next();
        String[] f = x.split(" ");
        if (f[1].endsWith(".")) {
            f[1] = f[1].substring(0, (f[1].length() - 1));
        }
        res.add(f[1]);
    }
    return res;
}

From source file:org.springframework.ldap.support.LdapUtils.java

/**
 * Iterate through all the values of the specified Attribute calling back to
 * the specified callbackHandler./*ww w . j  a  v a2  s  .  co  m*/
 * @param attribute the Attribute to work with; not <code>null</code>.
 * @param callbackHandler the callbackHandler; not <code>null</code>.
 * @since 1.3
 */
public static void iterateAttributeValues(Attribute attribute, AttributeValueCallbackHandler callbackHandler) {
    Assert.notNull(attribute, "Attribute must not be null");
    Assert.notNull(callbackHandler, "callbackHandler must not be null");

    for (int i = 0; i < attribute.size(); i++) {
        try {
            callbackHandler.handleAttributeValue(attribute.getID(), attribute.get(i), i);
        } catch (javax.naming.NamingException e) {
            throw LdapUtils.convertLdapException(e);
        }
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static List<LDAPUser> getIdPUserList() {
    List<LDAPUser> users = new ArrayList<LDAPUser>();
    NamingEnumeration results = null;
    DirContext ctx;//from w w w.  j a  v  a 2s . c o  m
    ResourceBundle rb = ResourceBundle.getBundle("ldap");
    try {
        ctx = getMainAuthContext();
        String attrs[] = { "uniqueMember" };
        Attribute userGAttr = ctx.getAttributes(rb.getString("usersGroup"), attrs).get("uniqueMember");
        for (int i = 0; i < userGAttr.size(); i++) {
            String userDN = (String) userGAttr.get(i);
            users.add(getUser(userDN.substring(userDN.indexOf("=") + 1, userDN.indexOf(","))));

        }

    } catch (NamingException ex) {
        _log.error(ex);
    }

    return users;
}

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);/*  www. jav  a  2 s . co m*/
                }
            }
        }
    }
    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);// ww w.jav a2  s . c o  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 {/* ww w  . ja  va2 s  .com*/
        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: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;/*from  ww w.  ja  v a2  s  . c  o  m*/
    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: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);
        }/* w ww.j  av a 2 s.c o m*/
    } 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:org.iplantc.persondir.support.ldap.AttributesMapperImpl.java

@Override
public Object mapFromAttributes(Attributes attributes) throws NamingException {
    final int attributeCount = attributes.size();
    final Map<String, Object> mapOfAttrValues = this.createAttributeMap(attributeCount);

    for (final NamingEnumeration<? extends Attribute> attributesEnum = attributes.getAll(); attributesEnum
            .hasMore();) {/* w w  w. j  a  v a  2 s  . c  om*/
        final Attribute attribute = attributesEnum.next();

        if (!this.ignoreNull || attribute.size() > 0) {
            final String attrName = attribute.getID();
            final String key = this.getAttributeKey(attrName);

            final NamingEnumeration<?> valuesEnum = attribute.getAll();
            final List<?> values = this.getAttributeValues(valuesEnum);

            mapOfAttrValues.put(key, values);
        }
    }

    return mapOfAttrValues;
}