import java.util.Enumeration;
import java.util.Iterator;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPResponse;
import com.novell.ldap.LDAPSearchResult;
import com.novell.ldap.LDAPAddRequest;
import com.novell.ldap.LDAPDeleteRequest;
import com.novell.ldap.LDAPModifyDNRequest;
import com.novell.ldap.LDAPModifyRequest;
import com.novell.ldap.util.LDIFReader;
import com.novell.ldap.LDAPMessage;
import com.novell.ldap.LDAPMessageQueue;
public class Ldif2Ldap {
public static void main( String[] args ) {
if (args.length != 4) {
usage();
}
int version = 1;
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String fileName = args[0];
String ldapHost = args[1];
String loginDN = args[2];
String password = args[3];
LDIFReader reader = null;
LDAPEntry entry;
LDAPMessage msg, retMsg;
Ldif2Ldap readerTest = new Ldif2Ldap();
LDAPConnection lc = new LDAPConnection();
try {
FileInputStream fis = new FileInputStream( fileName);
reader = new LDIFReader(fis, version);
}
catch (Exception e) {
System.err.println("\nFailed to read LDIF file " + fileName +
", " + e.toString());
System.exit (1);
}
try {
lc.connect( ldapHost, ldapPort );
lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );
if (!reader.isRequest()) {
System.out.println("\nLDIF content file\n");
while ( (msg = reader.readMessage()) != null) {
entry = ((LDAPSearchResult)msg).getEntry();
System.out.println("\nEntry DN:" + entry.getDN());
readerTest.showAttributes(entry);
}
}
else {
System.out.println("\nLDIF change file\n");
while ( (msg = reader.readMessage()) != null) {
if (msg instanceof LDAPAddRequest) {
System.out.println("Adding entry...");
}
else if (msg instanceof LDAPDeleteRequest) {
System.out.println("Deleting entry...");
}
else if (msg instanceof LDAPModifyDNRequest) {
System.out.println("Modifying entry's RDN...");
}
else if (msg instanceof LDAPModifyRequest) {
System.out.println("Modifying entry's attribute(s)...");
}
LDAPMessageQueue queue = lc.sendRequest(msg, null, null);
if ((retMsg = queue.getResponse()) != null) {
LDAPResponse response = (LDAPResponse)retMsg;
int status = response.getResultCode();
if ( status == LDAPException.SUCCESS ) {
System.out.println("Directory information has been"
+ " modified.");
}
else if ( status == LDAPException.REFERRAL ) {
String urls[]=((LDAPResponse)retMsg).getReferrals();
System.out.println("Referrals:");
for ( int i = 0; i < urls.length; i++ )
System.out.println(" " + urls[i]);
}
else {
System.out.println( response.getErrorMessage());
}
}
System.out.println();
}
}
}
catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}
catch ( IOException ioe ) {
System.out.println("Error: " + ioe.toString() );
System.exit( 1 );
}
catch ( LDAPException le ) {
System.out.println("Error: " + le.toString() );
System.exit( 1 );
}
System.exit (0);
}
public static void usage() {
System.err.println("Usage: java Lidf2Ldap <in file name> "
+ "<host name> <login dn> <password>");
System.err.println("Example: java Lidf2Ldap inFile Acme.com "
+ " \"cn=admin,o=Acme\" secret");
System.exit(1);
}
public void showAttributes(LDAPEntry entry) {
String value;
LDAPAttributeSet as = null;
LDAPAttribute[] attrs = null;
LDAPAttribute attr = null;
Iterator allAttrs;
Enumeration allAttrValues;
as = entry.getAttributeSet();
allAttrs = as.iterator();
System.out.println(" Attributes:");
while(allAttrs.hasNext()) {
attr = (LDAPAttribute)allAttrs.next();
System.out.println(" " + attr.getName());
allAttrValues = attr.getStringValues();
if( allAttrValues != null) {
while(allAttrValues.hasMoreElements()) {
value = (String) allAttrValues.nextElement();
System.out.println(" " + value);
}
}
}
}
}