Example usage for javax.naming.ldap LdapName LdapName

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

Introduction

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

Prototype

public LdapName(List<Rdn> rdns) 

Source Link

Document

Constructs an LDAP name given its parsed RDN components.

Usage

From source file:fi.laverca.Pkcs7.java

/**
 * Convenience method. Equivalent to calling getSignerCert and
 * then parsing out the CN from the certificate's Subject field.
 * @return Signer CN or null if there's a problem.
 *//*from www. ja  v a 2s .c  o  m*/
public String getSignerCn() {
    try {
        X509Certificate signerCert = this.getSignerCert();
        String dn = signerCert.getSubjectX500Principal().getName();

        String cn = null;
        try {
            LdapName ldapDn = new LdapName(dn);
            List<Rdn> rdns = ldapDn.getRdns();
            for (Rdn r : rdns) {
                if ("CN".equals(r.getType())) {
                    cn = r.getValue().toString();
                }
            }
        } catch (InvalidNameException e) {
            log.warn("Invalid name", e);
        }

        return cn;
    } catch (Throwable t) {
        log.error("Failed to get signer CN: " + t.getMessage());
        return null;
    }
}

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

@Transactional(readOnly = true)
@Override/*  ww w .j a  v  a2  s . 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:fr.mtlx.odm.TestSessionImpl.java

@Test(expected = NameNotFoundException.class)
public void testLookupUnknowDn() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=foo,ou=personnes");

    session.getOperations(Person.class).lookup(dn);
}

From source file:fr.mtlx.odm.TestSessionImpl.java

@Test
@Ignore//from  w w  w  .j a v a2s  .c  om
public void testLookupGroupOfNames() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=prod,ou=groupes");

    GroupOfNames entry = session.getOperations(GroupOfNames.class).lookup(dn);

    assertNotNull(entry);

    assertTrue(entry.getDn().equals(dn));

    assertThat(entry.getCommonName(), is("prod"));

    assertThat(entry.getMembers().size(), is(2));
}

From source file:edu.kit.scc.ldap.LdapPosixGroupDao.java

/**
 * Inserts a new POSIX group into the LDAP directory.
 * /*  w w  w. j  a v a  2s .  c o  m*/
 * @param group the {@link PosixGroup} to insert
 * @return the {@link PosixGroup} inserted
 */
public PosixGroup insertGroup(PosixGroup group) {
    if (group.commonName == null || group.gidNumber == null) {
        log.warn("PosixGroup has missing mandatory attributes");
        return null;
    }

    BasicAttribute posixGroupBasicAttribute = new BasicAttribute("objectclass");
    posixGroupBasicAttribute.add("posixGroup");

    Attributes posixGroupAttributes = new BasicAttributes();
    posixGroupAttributes.put(posixGroupBasicAttribute);
    posixGroupAttributes.put("cn", group.getCommonName());
    posixGroupAttributes.put("gidNumber", String.valueOf(group.getGidNumber()));

    if (group.getUserPassword() != null) {
        posixGroupAttributes.put("userPassword", group.getUserPassword());
    }
    if (group.getDescription() != null) {
        posixGroupAttributes.put("description", group.getDescription());
    }
    LdapName newGroupDn = LdapUtils.emptyLdapName();
    try {
        newGroupDn = new LdapName(groupBase);
        newGroupDn.add("cn=" + group.getCommonName());
        log.debug("Insert {}", newGroupDn.toString());
        ldapTemplate.bind(newGroupDn, null, posixGroupAttributes);

        return group;
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.getMessage());
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.getMessage());
    }
    return null;
}

From source file:org.apache.archiva.redback.common.ldap.connection.ConfigurableLdapConnectionFactory.java

public LdapName getBaseDnLdapName() throws LdapException {
    try {//from w  w  w  .ja  v a 2 s.  c  o m
        return new LdapName(baseDn);
    } catch (InvalidNameException e) {
        throw new LdapException("The base DN is not a valid name.", e);
    }
}

From source file:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testIsPersistent() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=alex,ou=personnes");

    Object entry = session.getOperations(Person.class).lookup(dn);

    assertTrue(session.isPersistent(entry));
}

From source file:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testLookupGroupOfPersons() throws InvalidNameException, NameNotFoundException {
    Name dn = new LdapName("cn=alex,ou=personnes");

    session.getOperations(Person.class).lookup(dn);

    dn = new LdapName("cn=prod,ou=groupes");

    GroupOfPersons entry = session.getOperations(GroupOfPersons.class).lookup(dn);

    assertNotNull(entry);// w ww.j av a 2  s .c  o m

    assertTrue(entry.getDn().equals(dn));

    assertThat(entry.getCommonName(), is("prod"));

    assertFalse(entry.getMembers().isEmpty());

    assertTrue(Iterables.any(entry.getMembers(), new Predicate<Person>() {
        @Override
        public boolean apply(Person entry) {
            return entry != null;
        }

    }));
}

From source file:com.alfaariss.oa.util.idmapper.jndi.JNDIMapper.java

/**
 * @see com.alfaariss.oa.api.idmapper.IIDMapper#remap(java.lang.String)
 *///from   ww w  . j a v  a2s  . c  o  m
public String remap(String id) throws OAException {
    if (id == null)
        throw new IllegalArgumentException("Could not remap: NULL");

    String sReturn = null;
    DirContext oDirContext = null;
    try {
        try {
            oDirContext = new InitialDirContext(_htJNDIEnvironment);
        } catch (NamingException e) {
            _logger.error("Could not create the connection: " + _htJNDIEnvironment, e);
            throw new OAException(SystemErrors.ERROR_RESOURCE_CONNECT);
        }

        try {
            if (_sMapperAttribute == null) {//must be null, otherwise you can't do the inverse
                Name nameLdap = new LdapName(id);
                if (_sIDAttribute != null)
                    return getAttributes(oDirContext, _sIDAttribute, nameLdap);

                _logger.error("Can't remap: no id attribute name configured");
                throw new OAException(SystemErrors.ERROR_RESOURCE_CONNECT);
            }

            sReturn = searchAttributes(oDirContext, _sMapperAttribute, _sIDAttribute, id);
        } catch (InvalidNameException e) {
            _logger.debug("Supplied id isn't a valid LdapName: " + id);
        }

    } catch (OAException e) {
        throw e;
    } catch (Exception e) {
        _logger.fatal("Could not remap id: " + id, e);
        throw new OAException(SystemErrors.ERROR_INTERNAL);
    } finally {
        if (oDirContext != null) {
            try {
                oDirContext.close();
            } catch (NamingException e) {
                _logger.error("Could not close Dir Context after searching for mapped id: " + id, e);
            }
        }
    }
    return sReturn;
}

From source file:edu.kit.scc.ldap.LdapPosixUserDao.java

/**
 * Inserts a new POSIX user into the LDAP directory.
 * //from w w w.ja va 2  s .  co m
 * @param posixUser the {@link PosixUser} to insert
 * @return the {@link PosixUser} inserted
 */
public PosixUser insertUser(PosixUser posixUser) {
    if (posixUser.commonName == null || posixUser.gidNumber == null || posixUser.homeDirectory == null
            || posixUser.surName == null || posixUser.uid == null || posixUser.uidNumber == null) {
        log.warn("PosixUser has missing mandatory attributes");
        return null;
    }

    BasicAttribute personBasicAttribute = new BasicAttribute("objectclass");
    personBasicAttribute.add("extensibleObject");
    personBasicAttribute.add("inetOrgPerson");
    personBasicAttribute.add("organizationalPerson");
    personBasicAttribute.add("person");
    personBasicAttribute.add("posixAccount");

    Attributes personAttributes = new BasicAttributes();
    personAttributes.put(personBasicAttribute);
    personAttributes.put("cn", posixUser.getCommonName());
    personAttributes.put("sn", posixUser.getSurName());
    personAttributes.put("uid", posixUser.getUid());
    personAttributes.put("uidNumber", String.valueOf(posixUser.getUidNumber()));
    personAttributes.put("gidNumber", String.valueOf(posixUser.getGidNumber()));
    personAttributes.put("homeDirectory", posixUser.getHomeDirectory());

    if (posixUser.getUniqueIdentifier() != null) {
        personAttributes.put("uniqueIdentifier", posixUser.getUniqueIdentifier());
    }
    if (posixUser.getDescription() != null) {
        personAttributes.put("description", posixUser.getDescription());
    }
    if (posixUser.getGecos() != null) {
        personAttributes.put("gecos", posixUser.getGecos());
    }
    if (posixUser.getLoginShell() != null) {
        personAttributes.put("loginShell", posixUser.getLoginShell());
    }
    if (posixUser.getUserPassword() != null) {
        personAttributes.put("userPassword", posixUser.getUserPassword());
    }
    if (posixUser.getGivenName() != null) {
        personAttributes.put("givenName", posixUser.getGivenName());
    }
    if (posixUser.getMail() != null) {
        personAttributes.put("mail", posixUser.getMail());
    }

    LdapName newUserDn = LdapUtils.emptyLdapName();
    try {
        newUserDn = new LdapName(userBase);
        newUserDn.add("uid=" + posixUser.getUid());
        log.debug("Insert {}", newUserDn.toString());
        ldapTemplate.bind(newUserDn, null, personAttributes);

        return posixUser;
    } catch (InvalidNameException ex) {
        log.error("ERROR {}", ex.toString());
        // ex.printStackTrace();
    } catch (NameAlreadyBoundException ex) {
        log.error("ERROR {}", ex.toString());
    }
    return null;
}