5046N/A/*
5046N/A * CDDL HEADER START
5046N/A *
5046N/A * The contents of this file are subject to the terms of the
5046N/A * Common Development and Distribution License, Version 1.0 only
5046N/A * (the "License"). You may not use this file except in compliance
5046N/A * with the License.
5046N/A *
6982N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6982N/A * or http://forgerock.org/license/CDDLv1.0.html.
5046N/A * See the License for the specific language governing permissions
5046N/A * and limitations under the License.
5046N/A *
5046N/A * When distributing Covered Code, include this CDDL HEADER in each
6982N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6982N/A * If applicable, add the following below this CDDL HEADER, with the
6982N/A * fields enclosed by brackets "[]" replaced with your own identifying
6982N/A * information:
5046N/A * Portions Copyright [yyyy] [name of copyright owner]
5046N/A *
5046N/A * CDDL HEADER END
5046N/A *
5046N/A *
5046N/A * Copyright 2010 Sun Microsystems, Inc.
5046N/A */
5046N/A
5046N/Aimport netscape.ldap.*;
5046N/Aimport netscape.ldap.util.*;
5046N/Aimport java.util.*;
5046N/Aimport java.io.*;
5046N/A
5046N/Apublic class ImprovedLDAPConnection extends LDAPConnection {
5046N/A
5046N/A public ImprovedLDAPConnection() {
5046N/A super();
5046N/A }
5046N/A
5046N/A public void apply (Change change) {
5046N/A
5046N/A for (String mychange: change.changes) {
5046N/A String mytype = null;
5046N/A // Parse LDIF content
5046N/A ByteArrayInputStream stream = new ByteArrayInputStream(mychange.getBytes());
5046N/A LDIF ldif = null;
5046N/A try {
5046N/A ldif = new LDIF(new DataInputStream(stream));
5046N/A LDIFContent content = ldif.nextRecord().getContent();
5046N/A
5046N/A //EclReadAndPlay.println ("DEBUG", "\n\nWriting the following update: \n" + content.toString() );
5046N/A switch (content.getType()) {
5046N/A case LDIFContent.ADD_CONTENT:
5046N/A mytype = "ADD";
5046N/A content = ( LDIFAddContent ) content;
5046N/A
5046N/A LDAPAttributeSet attrSet = new LDAPAttributeSet( ((LDIFAddContent)content).getAttributes());
5046N/A // remove non-user-modifiable attributes:
5046N/A // entryuuid, pwdchangedtime, creatorsname, createtimestamp
5046N/A LDAPAttribute entryuuidAttr = attrSet.getAttribute("entryuuid");
5046N/A if ( entryuuidAttr != null ) {
5046N/A attrSet.remove("entryuuid");
5046N/A }
5046N/A LDAPAttribute pwdchangedAttr = attrSet.getAttribute("pwdChangedTime");
5046N/A if ( entryuuidAttr != null ) {
5046N/A attrSet.remove("pwdchangedtime");
5046N/A }
5046N/A LDAPAttribute creatorAttr = attrSet.getAttribute("creatorsname");
5046N/A if ( creatorAttr != null ) {
5046N/A attrSet.remove("creatorsname");
5046N/A }
5046N/A LDAPAttribute createtimeAttr = attrSet.getAttribute("createtimestamp");
5046N/A if ( createtimeAttr != null ) {
5046N/A attrSet.remove("createtimestamp");
5046N/A }
5046N/A LDAPEntry addEntry = new LDAPEntry ( change.dn, attrSet );
5046N/A //EclReadAndPlay.println ("INFO", "********************* Entry: ************** \n" + addEntry + "\n******************\n" );
5046N/A try {
5046N/A this.add( addEntry );
5046N/A }
5046N/A catch( LDAPException e ) {
5046N/A EclReadAndPlay.println("ERROR", "Cannot add entry \"" + change.dn + "\" (csn="
5046N/A + change.csn + ")" );
5046N/A EclReadAndPlay.println("ERROR", e.toString() );
5046N/A e.printStackTrace();
5046N/A System.exit(1);
5046N/A }
5046N/A
5046N/A // replace the unique id
5046N/A// LDAPAttribute myAttr = new LDAPAttribute ("nsuniqueid", change.nsUniqueId);
5046N/A// LDAPAttribute myAttr = new LDAPAttribute ("entryuuid", change.nsUniqueId);
5046N/A// LDAPModification mod = new LDAPModification ( LDAPModification.REPLACE, myAttr );
5046N/A// try {
5046N/A// this.modify( change.dn, mod );
5046N/A// }
5046N/A// catch( LDAPException e ) {
5046N/A// EclReadAndPlay.println ("ERROR", "Cannot modify nsuniqueid of entry \""
5046N/A// + change.dn + "\" (csn=" + change.csn + ")" );
5046N/A// EclReadAndPlay.println ("ERROR", e.toString() );
5046N/A// System.exit(1);
5046N/A// }
5046N/A //System.out.EclReadAndPlay.println( addEntry);
5046N/A break;
5046N/A case LDIFContent.MODIFICATION_CONTENT:
5046N/A mytype="MOD";
5046N/A LDAPModification[] mods = ((LDIFModifyContent)content).getModifications();
5046N/A // remove modifiersname and modifytimestamp mods
5046N/A boolean[] deleteItem = new boolean[mods.length];
5046N/A int size = 0;
5046N/A for (int i = 0 ; i < mods.length ; i++) {
5046N/A LDAPAttribute modAttr = mods[i].getAttribute();
5046N/A if ( modAttr.getBaseName().equalsIgnoreCase("modifiersname") ||
5046N/A modAttr.getBaseName().equalsIgnoreCase("modifytimestamp") ) {
5046N/A // remove mods[i] from mods
5046N/A deleteItem[i] = true;
5046N/A } else {
5046N/A deleteItem[i] = false;
5046N/A size++;
5046N/A }
5046N/A }
5046N/A LDAPModification[] realMods = new LDAPModification[size];
5046N/A int index = 0;
5046N/A for (int i = 0 ; i < mods.length ; i++) {
5046N/A if ( !deleteItem[i] ) {
5046N/A realMods[index++] = mods[i];
5046N/A }
5046N/A }
5046N/A try {
5046N/A this.modify( change.dn, realMods );
5046N/A }
5046N/A catch( LDAPException e ) {
5046N/A EclReadAndPlay.println("ERROR", "Cannot modify entry \"" + change.dn
5046N/A + "\" (csn=" + change.csn + ")" );
5046N/A EclReadAndPlay.println("DEBUG", "mods\"" + mods + "\"" );
5046N/A EclReadAndPlay.println("ERROR", e.toString() );
5046N/A e.printStackTrace();
5046N/A System.exit(1);
5046N/A }
5046N/A break;
5046N/A case LDIFContent.MODDN_CONTENT:
5046N/A if ( change.newRDN == null ) { // => fixOP MODRDN
5046N/A change.newRDN=((LDIFModDNContent)content).getRDN();
5046N/A change.deleteOldRDN=((LDIFModDNContent)content).getDeleteOldRDN();
5046N/A change.newSuperior=((LDIFModDNContent)content).getNewParent();
5046N/A }
5046N/A
5046N/A
5046N/A try {
5046N/A if (change.newSuperior == null ) {
5046N/A mytype="MODRDN";
5046N/A this.rename( change.dn, change.newRDN, change.deleteOldRDN );
5046N/A }
5046N/A else {
5046N/A mytype="MODDN";
5046N/A this.rename( change.dn, change.newRDN, change.newSuperior, change.deleteOldRDN );
5046N/A }
5046N/A }
5046N/A catch( LDAPException e ) {
5046N/A EclReadAndPlay.println( "ERROR", "Cannot rename entry \"" + change.dn
5046N/A + "\" (csn=" + change.csn + ")" );
5046N/A EclReadAndPlay.println( "ERROR", "newRDN =\"" + change.newRDN
5046N/A + "\" (deleteOldRDN=" + change.deleteOldRDN + ")" );
5046N/A EclReadAndPlay.println( "ERROR", "change =\"" + mychange + ")" );
5046N/A EclReadAndPlay.println( "ERROR", e.toString());
5046N/A e.printStackTrace();
5046N/A System.exit(1);
5046N/A }
5046N/A break;
5046N/A case LDIFContent.DELETE_CONTENT:
5046N/A mytype="DEL";
5046N/A try {
5046N/A this.delete( change.dn );
5046N/A }
5046N/A catch( LDAPException e ) {
5046N/A EclReadAndPlay.println ("ERROR", "Cannot delete entry \"" + change.dn
5046N/A + "\" (csn=" + change.csn + ")" );
5046N/A EclReadAndPlay.println( "ERROR", e.toString() );
5046N/A e.printStackTrace();
5046N/A System.exit(1);
5046N/A }
5046N/A break;
5046N/A default:
5046N/A EclReadAndPlay.println("ERROR", "Cannot parse change (type=" + content.getType()
5046N/A + "):\n" + mychange + "_");
5046N/A mytype="Unknown";
5046N/A break;
5046N/A }
5046N/A
5046N/A } catch ( IOException e ) {
5046N/A EclReadAndPlay.println( "ERROR" , e.toString() );
5046N/A e.printStackTrace();
5046N/A EclReadAndPlay.println( "ERROR" , change.toString() );
5046N/A }
5046N/A EclReadAndPlay.accessOut.println(EclReadAndPlay.getDate() + "- INFO: " + mytype + " \"" + change.dn
5046N/A + "\" (" + change.csn +" / " + change.changeNumber + ")" );
5046N/A }
5046N/A }
5046N/A
5046N/A}
5046N/A
5046N/A