Java Code Examples for javax.naming.directory.DirContext#bind()

The following examples show how to use javax.naming.directory.DirContext#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LdapDao.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param ctx
 * @param ouBase If organizational units are given by the given obj then this parameter will be ignored, otherwise this is the ou where
 *          the new object will be inserted.
 * @param obj
 * @param args
 * @throws NamingException
 */
public void create(final DirContext ctx, final String ouBase, final T obj, final Object... args) throws NamingException
{
  final String dn = buildDn(ouBase, obj);
  log.info("Create " + getObjectClass() + ": " + dn + ": " + getLogInfo(obj));
  final Attributes attrs = new BasicAttributes();
  final List<ModificationItem> modificationItems = getModificationItems(new ArrayList<ModificationItem>(), obj);
  modificationItems.add(createModificationItem(DirContext.ADD_ATTRIBUTE, "objectClass", getObjectClass()));
  final String[] additionalObjectClasses = getAdditionalObjectClasses(obj);
  if (additionalObjectClasses != null) {
    for (final String objectClass : additionalObjectClasses) {
      modificationItems.add(createModificationItem(DirContext.ADD_ATTRIBUTE, "objectClass", objectClass));
    }
  }
  for (final ModificationItem modItem : modificationItems) {
    final Attribute attr = modItem.getAttribute();
    LdapUtils.putAttribute(attrs, attr.getID(), (String) attr.get());
  }
  LdapUtils.putAttribute(attrs, "cn", LdapUtils.escapeCommonName(obj.getCommonName()));
  onBeforeBind(dn, attrs, args);
  ctx.bind(dn, null, attrs);
}
 
Example 2
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
       try {
           LdapName baseDn = (LdapName)
                   context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

           LdifParser parser = new LdifParser(ldifFile);
           parser.open();
           while (parser.hasMoreRecords()) {
               LdapAttributes record = parser.getRecord();

               LdapName dn = record.getName();

               if(baseDn != null) {
                   dn = LdapUtils.removeFirst(dn, baseDn);
               }

               if(!rootNode.isEmpty()) {
                   dn = LdapUtils.prepend(dn, rootNode);
               }
               context.bind(dn, null, record);
           }
       } catch (Exception e) {
           throw new UncategorizedLdapException("Failed to populate LDIF", e);
       }
   }
 
Example 3
Source File: LdapTestUtils.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
    try {
        LdapName baseDn = (LdapName)
                context.getEnvironment().get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            LdapName dn = record.getName();

            if(baseDn != null) {
                dn = LdapUtils.removeFirst(dn, baseDn);
            }

            if(!rootNode.isEmpty()) {
                dn = LdapUtils.prepend(dn, rootNode);
            }
            context.bind(dn, null, record);
        }
    } catch (Exception e) {
        throw new UncategorizedLdapException("Failed to populate LDIF", e);
    }
}
 
Example 4
Source File: LdapUtil.java    From herd-mdl with Apache License 2.0 5 votes vote down vote up
private static void createOu(String ou) throws NamingException {
    DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectClass");
    objclass.add("top");
    objclass.add("organizationalUnit");
    attrs.put(objclass);
    attrs.put("ou", ou);
    ldapContext.bind(constructOuDn(ou), null, attrs);
}
 
Example 5
Source File: ldapConnection.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * adds the entry given when attributes were set, to the directory
 * with the set DN.
 */

public void add() throws NamingException{
  DirContext ctx = new InitialDirContext(env);
  ldapEntry e = new ldapEntry(processAttributes());
  ctx.bind(dn, e);
  ctx.close();
}
 
Example 6
Source File: ApacheLDAPServerTest.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private void addMyUser(DirContext ctx, String name)
    throws Exception {
    MyUser user = new MyUser("amilaj", "Jayasekara", "Amila");
    ctx.bind(name, user);

    // Lookup
    DirContext obj = (DirContext)ctx.lookup(name);
    assertNotNull(obj);
    LOG.info("User is bound to: " + obj.getNameInNamespace());

}