2788N/A/*
2788N/A * CDDL HEADER START
2788N/A *
2788N/A * The contents of this file are subject to the terms of the
2788N/A * Common Development and Distribution License, Version 1.0 only
2788N/A * (the "License"). You may not use this file except in compliance
2788N/A * with the License.
2788N/A *
2788N/A * You can obtain a copy of the license at
2788N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
2788N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
2788N/A * See the License for the specific language governing permissions
2788N/A * and limitations under the License.
2788N/A *
2788N/A * When distributing Covered Code, include this CDDL HEADER in each
2788N/A * file and include the License file at
2788N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
2788N/A * add the following below this CDDL HEADER, with the fields enclosed
2788N/A * by brackets "[]" replaced with your own identifying information:
2788N/A * Portions Copyright [yyyy] [name of copyright owner]
2788N/A *
2788N/A * CDDL HEADER END
2788N/A *
2788N/A *
5065N/A * Copyright 2008-2010 Sun Microsystems, Inc.
2788N/A */
2788N/A
2788N/Aimport java.util.Hashtable;
3600N/Aimport javax.naming.Context;
3600N/Aimport javax.naming.NamingException;
3600N/Aimport javax.naming.directory.Attributes;
3600N/Aimport javax.naming.ldap.LdapContext;
3600N/Aimport javax.naming.ldap.InitialLdapContext;
3600N/Aimport javax.naming.CompositeName;
3600N/Aimport javax.naming.directory.BasicAttribute;
3600N/Aimport javax.naming.directory.BasicAttributes;
3600N/Aimport javax.naming.CommunicationException;
3600N/Aimport java.util.HashSet;
2788N/Aimport java.util.Iterator;
2788N/A
2788N/A/**
3600N/A * Add a new entry.
3600N/A * Input :
3600N/A * -D : principal
3600N/A * -w : credential
3600N/A * -p : ldapport
3600N/A * -h : ldaphost
3600N/A * -d : dn to add
3600N/A * -v : attribut name:attribut value
3600N/A * : this option is multi valued
3600N/A */
2788N/Apublic class addAnEntry {
2788N/A
3600N/A /**
3600N/A * Main.
3600N/A *
3600N/A * @param args arguments
3600N/A */
3600N/A public static void main(String[] args) {
2788N/A
3600N/A String hostname=null;
3600N/A String ldapPort=null;
3600N/A String principal=null;
3600N/A String credential=null;
3600N/A String dnToAdd=null;
3600N/A String attributeToAdd=null;
3600N/A String errorCode=null;
3600N/A String errorMessage=null;
3600N/A
3600N/A int ind1;
3600N/A String attributeName;
3600N/A String attributeValue;
3600N/A
3600N/A Attributes attributes = new BasicAttributes();
3600N/A HashSet attributeSet = new HashSet();
2788N/A
3600N/A for (int k=0; k< args.length; k++) {
3600N/A String opt1 = args[k];
3600N/A String val1 = args[k+1];
2788N/A
3600N/A if (opt1.equals("-h")) {
3600N/A hostname = val1;
3600N/A }
3600N/A if (opt1.equals("-p")) {
3600N/A ldapPort = val1;
3600N/A }
3600N/A if (opt1.equals("-D")) {
3600N/A principal = val1;
3600N/A }
3600N/A if (opt1.equals("-w")) {
3600N/A credential = val1;
3600N/A }
3600N/A if (opt1.equals("-d")) {
3600N/A dnToAdd = val1;
3600N/A }
3600N/A if (opt1.equals("-v")) {
3600N/A attributeToAdd = val1;
3600N/A
3600N/A ind1= val1.indexOf(":");
3600N/A
3600N/A attributeName=val1.substring(0,ind1);
3600N/A attributeValue=val1.substring(ind1+1);
3600N/A
3600N/A BasicAttribute attrToComplete = null;
2788N/A
3600N/A Iterator it = attributeSet.iterator();
3600N/A while (attrToComplete == null && it.hasNext()) {
3600N/A BasicAttribute attr = (BasicAttribute) it.next();
3600N/A if ((attr.getID()).equalsIgnoreCase(attributeName)) {
3600N/A attrToComplete = attr;
3600N/A }
3600N/A }
3600N/A
3600N/A if (attrToComplete == null) {
3600N/A attrToComplete = new BasicAttribute(attributeName);
3600N/A attributeSet.add(attrToComplete);
3600N/A }
5065N/A if (attributeValue != null) {
5065N/A attributeValue = attributeValue.replaceAll("QUOT","\\\"");
5065N/A attrToComplete.add(attributeValue);
5065N/A }
3600N/A }
3600N/A k++;
3600N/A }
3600N/A
3600N/A Iterator it2 = attributeSet.iterator();
3600N/A while (it2.hasNext()) {
3600N/A BasicAttribute attr = (BasicAttribute)it2.next();
3600N/A attributes.put(attr);
3600N/A }
3600N/A
2788N/A String provider = "ldap://" + hostname + ":" + ldapPort + "/";
3600N/A
2788N/A Hashtable envLdap = new Hashtable();
2788N/A
3600N/A envLdap.put("java.naming.factory.initial",
3600N/A "com.sun.jndi.ldap.LdapCtxFactory");
3600N/A envLdap.put(Context.SECURITY_AUTHENTICATION, "simple");
3600N/A envLdap.put(Context.SECURITY_PRINCIPAL, principal);
3600N/A envLdap.put(Context.SECURITY_CREDENTIALS, credential);
3600N/A envLdap.put(Context.PROVIDER_URL, provider);
3600N/A
3600N/A LdapContext ctx;
2788N/A
3600N/A try {
3600N/A CompositeName entryDN = new CompositeName(dnToAdd);
3600N/A System.out.println("Add a new entry " + entryDN);
2788N/A
3600N/A // connect to server
3600N/A ctx = new InitialLdapContext(envLdap, null);
3600N/A
3600N/A ctx.createSubcontext(entryDN, attributes);
2788N/A
3600N/A ctx.close();
3600N/A } catch (CommunicationException e1) {
3600N/A errorMessage = e1.getMessage();
3600N/A } catch (NamingException e2) {
3600N/A errorMessage = e2.getMessage();
2788N/A } catch (Exception e3) {
3600N/A errorMessage= e3.getMessage();
3600N/A }
3600N/A
3600N/A // No error, the modify is success
2788N/A if ( errorMessage == null ) {
3600N/A errorCode="0";
3600N/A } else {
3600N/A System.out.println(errorMessage);
3600N/A int ind=errorMessage.indexOf("-");
3600N/A if ( ind > 0 ) {
3600N/A errorCode=errorMessage.substring(18, ind-1);
3600N/A } else errorCode="0";
2788N/A }
3600N/A
2788N/A int RC = Integer.parseInt(errorCode);
2788N/A System.exit(RC);
3600N/A }
3600N/A
2788N/A}