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:ldap.Entry.java

public Entry(String DN, Attribute[] atts) throws InvalidNameException {
    this(new LdapName(DN), makeAtts(atts));
}

From source file:ch.bfh.unicert.certimport.Main.java

/**
 * Create a certificate fot the given CSV record
 *
 * @param record the record to parse//  w  w w .  jav a2s.c om
 * @throws InvalidNameException
 */
private static void createCertificate(CSVRecord record) throws InvalidNameException {

    int recordid = Integer.parseInt(record.get(0));
    String pemCert = record.get(1);
    String institution = record.get(2);
    int revoked = Integer.parseInt(record.get(3));
    if (revoked == 1) {
        System.out.println("Certficate " + recordid + " is revoked. Looking for next certificate...");
        return;
    }

    String studyBranch = record.get(5);
    String uniqueId = record.get(6);
    String mail = record.get(8);

    CertificateFactory cf;
    X509Certificate cert;
    try {
        cf = CertificateFactory.getInstance("X.509");
        cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(pemCert.getBytes()));
    } catch (CertificateException ex) {
        logger.log(Level.SEVERE, "Not able to read certificate for record {0}, exception: {1}",
                new Object[] { recordid, ex });
        return;
    }

    DSAPublicKey pubKey = (DSAPublicKey) cert.getPublicKey();

    String commonName = cert.getSubjectDN().getName();

    LdapName ln = new LdapName(cert.getSubjectX500Principal().toString());

    for (Rdn rdn : ln.getRdns()) {
        if (rdn.getType().equalsIgnoreCase("CN")) {
            commonName = (String) rdn.getValue();
            break;
        } else if (rdn.getType().equalsIgnoreCase("UID")) {
            uniqueId = (String) rdn.getValue();
            break;
        } else if (rdn.getType().equalsIgnoreCase("OU")) {
            studyBranch = (String) rdn.getValue();
            break;
        }
    }

    IdentityData idData = new IdentityData(commonName, uniqueId, institution, studyBranch, null, null, null,
            null, null, "SwitchAAI", null);

    try {
        Certificate certificate = issuer.createClientCertificate(idData, keystorePath, pubKey, 10, "UniVote",
                new String[] { "Voter" }, uniBoardWSDLurl, uniBoardUrl, section);
        counter++;
        System.out.println("Certificate published for " + recordid + ". Count " + counter + " of 6424");
    } catch (CertificateCreationException ex) {
        logger.log(Level.SEVERE, "Not able to create certificate for record {0}, exception: {1}",
                new Object[] { recordid, ex });
    }
}

From source file:ldap.Entry.java

public Entry(String DN) throws InvalidNameException {
    super(true);
    name = new LdapName(DN);
}

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 {//w  w  w.  j  ava  2s . co 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:fi.laverca.Pkcs1.java

/**
 * Get the signer CN. /*from  w  w  w  . j a  v  a 2s . c  o m*/
 * <p>Equivalent to calling getSignerCert and
 * then parsing out the CN from the certificate's Subject field.
 * @return Signer's CN or null if there's a problem.
 */
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 cert " + t.getMessage());
        return null;
    }
}

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

@Test
public void testLookupPerson() throws NamingException {

    Name dn = new LdapName("cn=dummy_person,ou=personnes");

    when(dirContext.lookup(dn)).thenReturn(dirContext);

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

    assertNotNull(p);/*from w w  w  .  j a v a 2s . c  o m*/

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

    assertThat(p.getSn(), is("dummy"));
}

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

/**
 * @see com.alfaariss.oa.api.idmapper.IIDMapper#map(java.lang.String)
 *//*from w ww  . jav  a2  s.co m*/
public String map(String id) throws OAException {
    if (id == null)
        throw new IllegalArgumentException("Could not map: 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 (_sIDAttribute == null) {//must be null, otherwise you can't do the inverse
                Name nameLdap = new LdapName(id);
                if (_sMapperAttribute != null)
                    return getAttributes(oDirContext, _sMapperAttribute, nameLdap);

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

            sReturn = searchAttributes(oDirContext, _sIDAttribute, _sMapperAttribute, 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 map 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 mapping id: " + id, e);
            }
        }
    }
    return sReturn;
}

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 {// w  ww .  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:fr.mtlx.odm.TestSessionImpl.java

@Test
public void testLookupOrganizationalPerson() throws InvalidNameException, javax.naming.NameNotFoundException {
    Name dn = new LdapName("cn=dummy_op,ou=personnes");

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

    assertNotNull(entry);/*from  w w w  . java  2s  .c  o m*/

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

    assertThat(entry.getSn(), is("op"));

    assertThat(entry.getTelephoneNumber().size(), is(2));

    assertTrue(entry.getTelephoneNumber().containsAll(Lists.newArrayList("0491141300", "0491141312")));

    assertTrue(entry.getUserPassword().length > 0);
}

From source file:ca.tnt.ldaputils.impl.LdapGroup.java

public Map getMembers(final String keyAttribute, final int objectType) throws InvalidNameException {
    final Iterator memberIt;
    final Map members;

    final LdapManager manager = new LdapManager();
    members = new TreeMap();
    memberIt = sortedMembers.iterator();
    while (memberIt.hasNext()) {
        final String member;
        member = (String) memberIt.next();
        final ILdapEntry ldapEntry = (ILdapEntry) manager.find(LdapEntry.class, new LdapName(member));
        if (ldapEntry != null) {
            members.put(ldapEntry.getStringValue(keyAttribute), ldapEntry);
        }//  ww  w.jav a 2 s . c  o  m
    }

    return members;
}