Example usage for javax.naming.ldap LdapName size

List of usage examples for javax.naming.ldap LdapName size

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName size.

Prototype

public int size() 

Source Link

Document

Retrieves the number of components in this LDAP name.

Usage

From source file:ConstructLdapName.java

public static void main(String args[]) {
    String name = "cn=Mango, ou=Fruits, o=Food";
    try {/*from w  w w  . j ava2s.  c o m*/
        LdapName dn = new LdapName(name);
        System.out.println(dn + " has " + dn.size() + " RDNs: ");
        for (int i = 0; i < dn.size(); i++) {
            System.out.println(dn.get(i));
        }
    } catch (InvalidNameException e) {
        System.out.println("Cannot parse name: " + name);
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {/*from www .ja  v a  2 s  .  co  m*/
        LdapName one = new LdapName("cn=Abc Def, ou=People, o=JNDITutorial");
        LdapName two = new LdapName("cn=Abc Def");
        LdapName three = new LdapName("o=JNDITutorial");
        LdapName four = new LdapName("");

        System.out.println(one.equals(two));
        System.out.println(one.startsWith(three));
        System.out.println(one.endsWith(two));
        System.out.println(one.startsWith(four));
        System.out.println(one.endsWith(four));
        System.out.println(one.endsWith(three));
        System.out.println(one.isEmpty());
        System.out.println(four.isEmpty());
        System.out.println(four.size() == 0);
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:CompareLdapNames.java

public static void main(String args[]) {
    try {/*from   w w w  .jav a 2s.  c  o m*/
        LdapName one = new LdapName("cn=Vincent Ryan, ou=People, o=JNDITutorial");
        LdapName two = new LdapName("cn=Vincent Ryan");
        LdapName three = new LdapName("o=JNDITutorial");
        LdapName four = new LdapName("");

        System.out.println(one.equals(two)); // false
        System.out.println(one.startsWith(three)); // true
        System.out.println(one.endsWith(two)); // true
        System.out.println(one.startsWith(four)); // true
        System.out.println(one.endsWith(four)); // true
        System.out.println(one.endsWith(three)); // false
        System.out.println(one.isEmpty()); // false
        System.out.println(four.isEmpty()); // true
        System.out.println(four.size() == 0); // true
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:edu.acu.cs.spring.security.cas.userdetails.GrantedAuthorityFromMemberOfAssertionAttributeUserDetailsService.java

private void convertObjectAndAddGrantedAuthorityToList(final Object o,
        final List<GrantedAuthority> grantedAuthorities) {
    if (o instanceof String) {
        final String memberOfString = (String) o;
        try {/*from www .  j a  v  a 2s  . com*/
            LdapName name = new LdapName(memberOfString);
            if (name.size() > 0) {
                String value = name.getRdn(name.size() - 1).getValue().toString();
                if (this.convertToUpperCase) {
                    value = value.toUpperCase();
                }
                if (this.convertSpacesToUnderscores) {
                    value = value.replace(' ', '_');
                }
                grantedAuthorities.add(new SimpleGrantedAuthority(rolePrefix + value));
            }
        } catch (InvalidNameException e) {
            logger.warn("Couldn't convert \"" + memberOfString + "\" to an LdapName", e);
        }
    }
}

From source file:jenkins.security.plugins.ldap.FromUserRecordLDAPGroupMembershipStrategy.java

@Override
public GrantedAuthority[] getGrantedAuthorities(LdapUserDetails ldapUser) {
    List<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
    Attributes attributes = ldapUser.getAttributes();
    final String attributeName = getAttributeName();
    Attribute attribute = attributes == null ? null : attributes.get(attributeName);
    if (attribute != null) {
        try {//  ww  w  . j a  v  a 2s. c  o  m
            for (Object value : Collections.list(attribute.getAll())) {
                String groupName = String.valueOf(value);
                try {
                    LdapName dn = new LdapName(groupName);
                    groupName = String.valueOf(dn.getRdn(dn.size() - 1).getValue());
                } catch (InvalidNameException e) {
                    LOGGER.log(Level.FINEST, "Expected a Group DN but found: {0}", groupName);
                }
                result.add(new GrantedAuthorityImpl(groupName));
            }
        } catch (NamingException e) {
            LogRecord lr = new LogRecord(Level.FINE,
                    "Failed to retrieve member of attribute ({0}) from LDAP user details");
            lr.setThrown(e);
            lr.setParameters(new Object[] { attributeName });
            LOGGER.log(lr);
        }

    }
    return result.toArray(new GrantedAuthority[result.size()]);
}

From source file:org.apache.syncope.core.sync.LDAPDomainSyncActions.java

@Override
public <T extends AbstractAttributableTO> SyncDelta beforeCreate(SyncResultsHandler srh, SyncDelta sd, T t)
        throws JobExecutionException {

    if (!ObjectClass.ACCOUNT_NAME.equals(sd.getObject().getObjectClass().toString())) {
        if (t != null) {
            LOG.debug("CREATION OF A NEW USER");
            String rdn = "/";
            ConnectorObject conn = sd.getObject();

            try {
                LdapName dn = new LdapName(conn.getAttributeByName(Name.NAME).getValue().toString()
                        .replace("[", "").replace("]", ""));
                if (dn.size() == 4) {
                    rdn = dn.getRdn(2).getValue().toString();
                } else {
                    rdn = "/";
                }/* w w w . ja  v  a 2  s .com*/
            } catch (InvalidNameException ex) {
                LOG.error("ERROR CONSTRUCTING LDAP DN FROM NAME ATTRIBUTE: ".concat(ex.getMessage()));
            }
            //Creation of new attribute to assign to new user in Syncope
            AttributeTO domain = new AttributeTO();
            domain.setSchema("domain");
            domain.addValue(rdn);
            t.addAttribute(domain);
        } else {
            LOG.error("SUBJECT OF SYNCHRONIZATION IS NULL");
        }
    }
    return sd;
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoCertificateService.java

public Map<String, String> parseDN(String dnString) throws InvalidNameException {
    Map<String, String> toReturn = new HashMap<>();
    LdapName ldapName = new LdapName(dnString);
    if (log.isDebugEnabled()) {
        log.debug("Parsing DN: " + dnString);
        log.debug("ldapNames size:" + ldapName.size());
    }//from w w  w. j  a v  a2s.  c o  m
    for (Rdn rdn : ldapName.getRdns()) {
        if (rdn.getValue() instanceof String) {
            if (log.isDebugEnabled()) {
                log.debug("RDN: '" + rdn.getType() + "' has a String value");
            }
            toReturn.put(rdn.getType(), (String) rdn.getValue());
        } else if (rdn.getValue() instanceof byte[]) {
            if (log.isDebugEnabled()) {
                log.debug("RDN: '" + rdn.getType() + "' has a binary value");
            }
            toReturn.put(rdn.getType(), new String((byte[]) rdn.getValue()));
        }
    }
    return toReturn;
}

From source file:org.apache.syncope.core.sync.LDAPDomainSyncActions.java

@Transactional(readOnly = true)
@Override/*from   ww w.j av  a  2s.c o m*/
public <T extends AbstractAttributableTO, K extends AbstractAttributableMod> SyncDelta beforeUpdate(
        SyncResultsHandler srh, SyncDelta sd, T t, K k) throws JobExecutionException {

    if (!ObjectClass.ACCOUNT_NAME.equals(sd.getObject().getObjectClass().toString())) {

        if (t != null) {
            SyncopeUser user = userDAO.find(t.getId());

            if (user != null && !user.isSuspended()) {

                ConnectorObject conn = sd.getObject();
                // Get dn of current user to be updated on Syncope 
                LdapName dnOnSyncope = resolveDnOnSyncope(user, srh);
                try {
                    LdapName dn = new LdapName(conn.getAttributeByName(Name.NAME).getValue().toString()
                            .replace("[", "").replace("]", ""));
                    // Check if dn on Syncope and dn on Ldap are the same, if so returns
                    if (dnOnSyncope.compareTo(dn) != 0) {
                        String rdn;
                        if (dn.size() == 4) {
                            rdn = dn.getRdn(2).getValue().toString();
                        } else {
                            rdn = "/";
                        }
                        //Creation of new attribute to assign to new user in Syncope
                        AttributeMod attr = new AttributeMod();
                        attr.setSchema("domain");
                        attr.addValueToBeAdded(rdn);
                        k.addAttributeToBeUpdated(attr);
                    } else {
                        LOG.info("NO CHANGES APPLIED TO DOMAIN ATTRIBUTE");
                        return sd;
                    }
                } catch (InvalidNameException ex) {
                    LOG.error("ERROR CONSTRUCTING LDAP DN FROM NAME ATTRIBUTE: ".concat(ex.getMessage()));
                }
            } else {
                LOG.error("USER WITH ID: " + t.getId() + " DOESN'T EXIST OR IS SUSPENDED ON SYNCOPE ");
            }
        } else {
            LOG.error("SUBJECT OF SYNCHRONIZATION IS NULL");
        }
    }
    return sd;
}

From source file:ldap.SearchUtility.java

/**
 * This is a utility method for turning a list of LdapNames into a list
 * of Strings, being the final RDN value.  E.g.
 * ou=Computer Networks Division,ou=COMPUTERS AND INFORMATION GROUP,ou=DIRECTORS OFFICE AREA,ou=isac,dc=isro,dc=dos,c=au
 * would become simply "Computer Networks Division".
 * @param elementNames/* w ww  . j ava 2 s  .  c  o  m*/
 * @return a list of the same order and size as that passed in, but giving only the final String RDN value.
 */
public List<String> convertLdapNamesToStrings(List<LdapName> elementNames) {
    List<String> elements = new ArrayList<String>(elementNames.size());
    for (LdapName name : elementNames)
        elements.add(name.getRdn(name.size() - 1).getValue().toString());
    return elements;
}

From source file:ldap.SearchUtility.java

/**
 * This parses the name, and adds 'fake' attributes corresponding to the structure types list.
 * @param entry/*from  ww w .  j ava  2s  .co m*/
 */
private void fillInSyntheticAttributes(Entry entry) {
    LdapName name = entry.getName();

    int size = name.size();
    int maxTypeSize = structureTypes.size();
    //for (int i=Config.SEARCH_BASE_DN_SIZE-1; i<size; i++)
    for (int i = LdapConstants.ldapSearchBaseDnSize - 1; i < size; i++) {
        String value = name.getRdn(i).getValue().toString();
        //int typeIndex = i - Config.SEARCH_BASE_DN_SIZE + 1;
        int typeIndex = i - LdapConstants.ldapSearchBaseDnSize + 1;
        if (typeIndex < maxTypeSize) {
            String typeName = structureTypes.get(typeIndex);
            if (entry.get(typeName) == null)
                entry.put(typeName, value);
            else
                logger.info("WARNING: Type Name '" + typeName + "' clashes with existing attribute - skipping");
        } else // if we're here, we've run out of defined elements.
        {
            logger.info("WARNING: Type elements out of range in name " + name);
        }

    }
}