import com.novell.ldap.*;
import com.novell.ldap.util.DSMLWriter;
import com.novell.ldap.util.LDAPWriter;
import com.novell.ldap.util.LDAPReader;
import com.novell.ldap.util.DSMLReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Dsml2Ldap
{
public static void main( String[] args )
{
if (args.length != 4 && args.length != 5) {
System.err.println("Usage: java Dsml2Ldap <host name>"
+ " <login dn> <password>"
+ " <dsmlFileIN> [<dsmlFileOUT>]");
System.err.println("Example: java Dsml2Ldap Acme.com"
+ " \"cn=admin,o=Acme\""
+ " secret search.dsml results.dsml");
System.exit(1);
}
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
LDAPConnection lc = new LDAPConnection();
String ldapHost = args[0];
String loginDN = args[1];
String password = args[2];
String inFile = args[3];
String outFile = null;
if (args.length == 5) {
outFile = args[4];
}
try {
lc.connect( ldapHost, ldapPort );
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8") );
DSMLWriter out;
FileOutputStream outFileStream = null;
if (outFile == null){
out = new DSMLWriter(System.out);
} else {
outFileStream = new FileOutputStream(outFile);
out = new DSMLWriter(outFileStream);
}
out.useIndent(true);
out.setIndent(4);
LDAPReader in = new DSMLReader(inFile);
LDAPMessage inMessage, outMessage;
inMessage = in.readMessage();
while( inMessage != null ){
LDAPMessageQueue queue =
lc.sendRequest( inMessage, null, null);
while (( outMessage = queue.getResponse()) != null ) {
out.writeMessage( outMessage );
}
inMessage = in.readMessage();
}
out.finish();
if (outFile != null) {
outFileStream.close();
}
lc.disconnect();
}catch( UnsupportedEncodingException e ) {
System.out.println( "Error: " + e.toString() );
}catch (FileNotFoundException e) {
System.out.println( "Error: could not find input DSML file:" + e );
} catch (IOException e) {
System.out.println( "Error reading or writing to file:" + e);
} catch (LDAPLocalException e) {
System.out.println(
"The following error occured handling a DSML file:" + e);
} catch (LDAPException e) {
System.out.println(
"The following error occured on the server:" + e);
}
System.exit(0);
}
}