Adds a record to LDAP. - Java javax.naming.directory

Java examples for javax.naming.directory:Record

Description

Adds a record to LDAP.

Demo Code

/**//  w  ww  .j ava 2  s .  com
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: LDAPUtils.java,v 1.2 2009-08-24 11:37:44 hubertlvg Exp $
 *
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class Main{
    private static final Logger logger = Logger.getLogger(LDAPUtils.class
            .getName());
    /**
     * Adds a record.
     * 
     * @param name
     * @param objectClasses
     * @param attribs
     * @return true if added.
     */
    public static boolean addRecord(String name, ArrayList objectClasses,
            Map attribs, DirContext ctx) throws NamingException {

        boolean createStatus = false;
        BasicAttribute oc = new BasicAttribute("objectclass");

        if (objectClasses != null && !objectClasses.isEmpty()) {
            for (Iterator it = objectClasses.iterator(); it.hasNext();) {
                oc.add(it.next());
            }
        }
        BasicAttributes as = new BasicAttributes();
        as.put(oc);
        if (attribs != null && !attribs.isEmpty()) {
            for (Iterator it = attribs.keySet().iterator(); it.hasNext();) {
                String key = (String) it.next();
                String val = (String) attribs.get(key);

                if (key != null && val != null) {
                    as.put(key, val);
                }
            }
        }

        DirContext ctx1 = ctx.createSubcontext(name, as);
        createStatus = true;
        LDAPUtils.close(ctx1);

        return createStatus;
    }
    public static boolean addRecord(String name,
            ArrayList<String> objectClasses, Map<String, String> svAttribs,
            Map<String, ArrayList<String>> mvAttribs, DirContext ctx)
            throws NamingException {

        boolean createStatus = false;
        BasicAttribute oc = new BasicAttribute("objectclass");

        if (objectClasses != null && !objectClasses.isEmpty()) {
            for (Iterator<String> it = objectClasses.iterator(); it
                    .hasNext();) {
                oc.add(it.next());
            }
        }
        BasicAttributes as = new BasicAttributes();
        as.put(oc);
        if (svAttribs != null && !svAttribs.isEmpty()) {
            for (Iterator<String> it = svAttribs.keySet().iterator(); it
                    .hasNext();) {
                String key = (String) it.next();
                String val = (String) svAttribs.get(key);

                if (key != null && val != null) {
                    as.put(key, val);
                }
            }
        }

        if (mvAttribs != null && !mvAttribs.isEmpty()) {
            for (Iterator<String> it = mvAttribs.keySet().iterator(); it
                    .hasNext();) {
                String key = (String) it.next();
                ArrayList<String> vals = mvAttribs.get(key);

                if (key != null && vals != null && vals.size() > 0) {
                    Attribute attr = new BasicAttribute(key);
                    for (Iterator<String> v = vals.iterator(); v.hasNext();) {
                        attr.add(v.next());
                    }
                    as.put(attr);
                }
            }
        }

        DirContext ctx1 = ctx.createSubcontext(name, as);
        createStatus = true;
        LDAPUtils.close(ctx1);
        //LDAPUtils.close(ctx);

        return createStatus;
    }
    /**
     * Close context.
     * 
     * @param ctx
     */
    public static void close(DirContext ctx) {
        if (ctx == null) {
            return;
        }

        try {
            ctx.close();
        } catch (NamingException ignore) {
            logger.log(Level.SEVERE, ignore.getMessage(), ignore);
        }
    }
    /**
     * Close naming enumeration.
     * 
     * @param ne
     */
    public static void close(NamingEnumeration ne) {
        if (ne == null) {
            return;
        }

        try {
            ne.close();
        } catch (NamingException ignore) {
            logger.log(Level.WARNING, ignore.getMessage(), ignore);
        }
    }
}

Related Tutorials