Example usage for javax.naming.directory DirContext bind

List of usage examples for javax.naming.directory DirContext bind

Introduction

In this page you can find the example usage for javax.naming.directory DirContext bind.

Prototype

public void bind(Name name, Object obj) throws NamingException;

Source Link

Document

Binds a name to an object.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);

    env.put(Context.PROVIDER_URL, MY_HOST);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, MGR_DN);
    env.put(Context.SECURITY_CREDENTIALS, MGR_PW);

    DirContext ctx = new InitialDirContext(env);

    Person p = new Person();
    ctx.bind("uid=mewilcox, ou=People, o=airius.com", p);
}

From source file:de.sub.goobi.helper.ldap.Ldap.java

/**
 * create new user in LDAP-directory.//from  ww  w.  ja  v a  2 s  .c  o m
 *
 * @param inBenutzer
 *            User object
 * @param inPasswort
 *            String
 */
public void createNewUser(User inBenutzer, String inPasswort)
        throws NamingException, NoSuchAlgorithmException, IOException {

    if (!ConfigCore.getBooleanParameter("ldap_readonly", false)) {
        Hashtable<String, String> env = getLdapConnectionSettings();
        env.put(Context.SECURITY_PRINCIPAL, ConfigCore.getParameter("ldap_adminLogin"));
        env.put(Context.SECURITY_CREDENTIALS, ConfigCore.getParameter("ldap_adminPassword"));

        LdapUser dr = new LdapUser();
        dr.configure(inBenutzer, inPasswort, getNextUidNumber());
        DirContext ctx = new InitialDirContext(env);
        ctx.bind(getUserDN(inBenutzer), dr);
        ctx.close();
        setNextUidNumber();
        Helper.setMeldung(null, Helper.getTranslation("ldapWritten") + " "
                + serviceManager.getUserService().getFullName(inBenutzer), "");
        /*
         * check if HomeDir exists, else create it
         */
        logger.debug("HomeVerzeichnis pruefen");
        URI homePath = URI.create(getUserHomeDirectory(inBenutzer));
        if (!new File(homePath).exists()) {
            logger.debug("HomeVerzeichnis existiert noch nicht");
            serviceManager.getFileService().createDirectoryForUser(homePath, inBenutzer.getLogin());
            logger.debug("HomeVerzeichnis angelegt");
        } else {
            logger.debug("HomeVerzeichnis existiert schon");
        }
    } else {
        Helper.setMeldung(Helper.getTranslation("ldapIsReadOnly"));
    }
}

From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java

/**
 * Copy a collection.//from  www. ja v  a2  s  . c  o  m
 *
 * @param resources Resources implementation to be used
 * @param errorList Hashtable containing the list of errors which occurred
 *                  during the copy operation
 * @param source    Path of the resource to be copied
 * @param dest      Destination path
 * @return Description of the Return Value
 */
private boolean copyResource(DirContext resources, Hashtable errorList, String source, String dest) {

    if (debug > 1) {
        System.out.println("Copy: " + source + " To: " + dest);
    }

    Object object = null;
    try {
        object = resources.lookup(source);
    } catch (NamingException e) {
    }

    if (object instanceof DirContext) {

        try {
            resources.createSubcontext(dest);
        } catch (NamingException e) {
            errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT));
            return false;
        }

        try {
            NamingEnumeration enum1 = resources.list(source);
            while (enum1.hasMoreElements()) {
                NameClassPair ncPair = (NameClassPair) enum1.nextElement();
                String childDest = dest;
                if (!childDest.equals("/")) {
                    childDest += "/";
                }
                childDest += ncPair.getName();
                String childSrc = source;
                if (!childSrc.equals("/")) {
                    childSrc += "/";
                }
                childSrc += ncPair.getName();
                copyResource(resources, errorList, childSrc, childDest);
            }
        } catch (NamingException e) {
            errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }

    } else {

        if (object instanceof Resource) {
            try {
                resources.bind(dest, object);
            } catch (NamingException e) {
                errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                return false;
            }
        } else {
            errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }

    }

    return true;
}

From source file:org.kitodo.production.services.data.LdapServerService.java

/**
 * create new user in LDAP-directory./*from w ww .ja  va 2 s. c  om*/
 *
 * @param user
 *            User object
 * @param password
 *            String
 */
public void createNewUser(User user, String password)
        throws NamingException, NoSuchAlgorithmException, IOException {

    if (!user.getLdapGroup().getLdapServer().isReadOnly()) {
        Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(
                user.getLdapGroup().getLdapServer());

        LdapUser ldapUser = new LdapUser();
        ldapUser.configure(user, password, getNextUidNumber(user.getLdapGroup().getLdapServer()));
        DirContext ctx = new InitialDirContext(ldapEnvironment);
        ctx.bind(buildUserDN(user), ldapUser);
        ctx.close();
        setNextUidNumber(user.getLdapGroup().getLdapServer());
        Helper.setMessage(
                Helper.getTranslation("ldapWritten") + " " + ServiceManager.getUserService().getFullName(user));
        /*
         * check if HomeDir exists, else create it
         */
        logger.debug("HomeVerzeichnis pruefen");

        URI homePath = getUserHomeDirectory(user);

        if (!new File(homePath).exists()) {
            logger.debug("HomeVerzeichnis existiert noch nicht");
            ServiceManager.getFileService().createDirectoryForUser(homePath, user.getLogin());
            logger.debug("HomeVerzeichnis angelegt");
        } else {
            logger.debug("HomeVerzeichnis existiert schon");
        }
    } else {
        Helper.setMessage("ldapIsReadOnly");
    }
}

From source file:org.kitodo.services.data.LdapServerService.java

/**
 * create new user in LDAP-directory.//from  w  w w.  ja  v  a 2s .c  o  m
 *
 * @param user
 *            User object
 * @param password
 *            String
 */
public void createNewUser(User user, String password)
        throws NamingException, NoSuchAlgorithmException, IOException {

    if (!user.getLdapGroup().getLdapServer().isReadOnly()) {
        Hashtable<String, String> ldapEnvironment = initializeWithLdapConnectionSettings(
                user.getLdapGroup().getLdapServer());

        LdapUser ldapUser = new LdapUser();
        ldapUser.configure(user, password, getNextUidNumber(user.getLdapGroup().getLdapServer()));
        DirContext ctx = new InitialDirContext(ldapEnvironment);
        ctx.bind(buildUserDN(user), ldapUser);
        ctx.close();
        setNextUidNumber(user.getLdapGroup().getLdapServer());
        Helper.setMessage(
                Helper.getTranslation("ldapWritten") + " " + serviceManager.getUserService().getFullName(user));
        /*
         * check if HomeDir exists, else create it
         */
        logger.debug("HomeVerzeichnis pruefen");

        URI homePath = getUserHomeDirectory(user);

        if (!new File(homePath).exists()) {
            logger.debug("HomeVerzeichnis existiert noch nicht");
            serviceManager.getFileService().createDirectoryForUser(homePath, user.getLogin());
            logger.debug("HomeVerzeichnis angelegt");
        } else {
            logger.debug("HomeVerzeichnis existiert schon");
        }
    } else {
        Helper.setMessage("ldapIsReadOnly");
    }
}