4724N/A/*
4724N/A * CDDL HEADER START
4724N/A *
4724N/A * The contents of this file are subject to the terms of the
4724N/A * Common Development and Distribution License, Version 1.0 only
4724N/A * (the "License"). You may not use this file except in compliance
4724N/A * with the License.
4724N/A *
4724N/A * You can obtain a copy of the license at
4724N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
4724N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
4724N/A * See the License for the specific language governing permissions
4724N/A * and limitations under the License.
4724N/A *
4724N/A * When distributing Covered Code, include this CDDL HEADER in each
4724N/A * file and include the License file at
4724N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
4724N/A * add the following below this CDDL HEADER, with the fields enclosed
4724N/A * by brackets "[]" replaced with your own identifying information:
4724N/A * Portions Copyright [yyyy] [name of copyright owner]
4724N/A *
4724N/A * CDDL HEADER END
4724N/A *
4724N/A *
4724N/A * Copyright 2009 Sun Microsystems, Inc.
4724N/A */
4724N/A
4724N/Aimport java.util.Hashtable;
4724N/Aimport javax.naming.Context;
4724N/Aimport javax.naming.NamingException;
4724N/Aimport javax.naming.ldap.LdapContext;
4724N/Aimport javax.naming.ldap.LdapName;
4724N/Aimport javax.naming.ldap.InitialLdapContext;
4724N/Aimport javax.naming.CommunicationException;
4724N/Aimport java.util.HashSet;
4724N/Aimport java.util.Iterator;
4724N/A
4724N/A/**
4724N/A * Modify the DN of an entry.
4724N/A * The operation can be a modDN (tree move) or a modRDN.
4724N/A * The function returns the ldap error code
4724N/A */
4724N/Apublic class modifyDn {
4724N/A
4724N/A /**
4724N/A * Main.
4724N/A *
4724N/A * @param args arguments
4724N/A */
4724N/A public static void main(String[] args) {
4724N/A
4724N/A String hostname=null;
4724N/A String ldapPort=null;
4724N/A String principal=null;
4724N/A String credential=null;
4724N/A String dnToModify=null;
4724N/A String newRDN=null;
4724N/A String deleteOldRDN=null;
4724N/A String newSuperior=null;
4724N/A String errorCode=null;
4724N/A String errorMessage=null;
4724N/A
4724N/A Hashtable envLdap = new Hashtable();
4724N/A LdapContext ctx;
4724N/A LdapName nameToModify;
4724N/A LdapName targetName;
4724N/A String targetDn;
4724N/A
4724N/A
4724N/A for (int k=0; k< args.length; k++) {
4724N/A String opt1 = args[k];
4724N/A String val1 = args[k+1];
4724N/A
4724N/A if (opt1.equals("-h")) {
4724N/A hostname = val1;
4724N/A }
4724N/A if (opt1.equals("-p")) {
4724N/A ldapPort = val1;
4724N/A }
4724N/A if (opt1.equals("-D")) {
4724N/A principal = val1;
4724N/A }
4724N/A if (opt1.equals("-w")) {
4724N/A credential = val1;
4724N/A }
4724N/A if (opt1.equals("-d")) {
4724N/A dnToModify = val1;
4724N/A }
4724N/A if (opt1.equals("-e")) {
4724N/A newRDN = val1;
4724N/A }
4724N/A if (opt1.equals("-f")) {
4724N/A deleteOldRDN = val1;
4724N/A }
4724N/A if (opt1.equals("-g")) {
4724N/A newSuperior = val1;
4724N/A }
4724N/A k++;
4724N/A }
4724N/A
4724N/A
4724N/A String provider = "ldap://" + hostname + ":" + ldapPort + "/";
4724N/A
4724N/A envLdap.put("java.naming.factory.initial",
4724N/A "com.sun.jndi.ldap.LdapCtxFactory");
4724N/A envLdap.put(Context.SECURITY_AUTHENTICATION, "simple");
4724N/A envLdap.put(Context.SECURITY_PRINCIPAL, principal);
4724N/A envLdap.put(Context.SECURITY_CREDENTIALS, credential);
4724N/A envLdap.put(Context.PROVIDER_URL, provider);
4724N/A
4724N/A // Whether the old RDN attribute values are to be retained
4724N/A // as attributes of the entry, or deleted from the entry.
4724N/A // Default is 'true'
4724N/A if (deleteOldRDN != null) {
4724N/A envLdap.put("java.naming.ldap.deleteRDN", deleteOldRDN);
4724N/A }
4724N/A
4724N/A try {
4724N/A System.out.println("Modify the entry " + dnToModify);
4724N/A
4724N/A if ( newRDN == null ) {
4724N/A // newRDN = same as old rdn
4724N/A nameToModify = new LdapName(dnToModify);
4724N/A newRDN = (String) nameToModify.remove(nameToModify.size() - 1);
4724N/A }
4724N/A
4724N/A if ( newSuperior != null ) {
4724N/A // modDN operation => new parent = newSuperior
4724N/A targetName = new LdapName(newSuperior);
4724N/A } else {
4724N/A // modRDN operation => new parent = old parent
4724N/A targetName = new LdapName(dnToModify);
4724N/A targetName.remove(targetName.size() - 1);
4724N/A }
4724N/A targetName.add(newRDN);
4724N/A targetDn = targetName.toString();
4724N/A
4724N/A
4724N/A // connect to server
4724N/A ctx = new InitialLdapContext(envLdap, null);
4724N/A
4724N/A // rename the entry
4724N/A ctx.rename(dnToModify, targetDn);
4724N/A
4724N/A ctx.close();
4724N/A } catch (CommunicationException e1) {
4724N/A errorMessage = e1.getMessage();
4724N/A } catch (NamingException e2) {
4724N/A errorMessage = e2.getMessage();
4724N/A } catch (Exception e3) {
4724N/A errorMessage= e3.getMessage();
4724N/A }
4724N/A
4724N/A // No error, the modify is success
4724N/A if ( errorMessage == null ) {
4724N/A errorCode="0";
4724N/A } else {
4724N/A System.out.println(errorMessage);
4724N/A int ind=errorMessage.indexOf("-");
4724N/A if ( ind > 0 ) {
4724N/A errorCode=errorMessage.substring(18, ind-1);
4724N/A } else errorCode="0";
4724N/A }
4724N/A
4724N/A int RC = Integer.parseInt(errorCode);
4724N/A System.exit(RC);
4724N/A }
4724N/A
4724N/A}