aspose file tools*
The moose likes Java in General and the fly likes export ldap tree to ldif file, check performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "export ldap tree to ldif file, check performance" Watch "export ldap tree to ldif file, check performance" New topic
Author

export ldap tree to ldif file, check performance

shiv kumar
Greenhorn

Joined: Apr 27, 2006
Posts: 5
/*
shivkumar kore
Pune,India, 9890858987
*/


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import java.io.Writer;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.ReferralException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;


/*
This class responsible for writing "o=oblix" tree into the idif file.
*/
public class LdifExporter
{
// search scope specifications
private static final int BASE = 0;
private static final int ONELEVEL = 1;
private static final int SUBTREE = 2;

private DirContext context;
private Hashtable env;
private PrintWriter writer;

// coreid environment specific entries, we can replace it by
EnvironmentVO class instance
private String host = "";
private String port = "";
private String userID = "cn=Directory Manager";
private String password = "password";
private String baseDN = "dc=com";

public LdifExporter()
{
try
{
this.writer = new PrintWriter(new FileOutputStream
("c:\\ExportTestFile.ldif"));
}
catch(FileNotFoundException fnfe)
{
fnfe.printStackTrace();
}
}

// constructor, write code for creating context to directory here
public LdifExporter(String host, String port, String uid, String
password, String baseDN)
{
writer = null;

this.host = host;
this.port = port;
this.userID = uid; // most fo the time "cn=Directory Manager"
this.password = password;
this.baseDN = baseDN; // most fo the time "o=company,c=us"
}

public static void main(String [] args)
{
LdifExporter ex = new LdifExporter();// pass argument here
ex.export("ou=mycompany");// external handle for exporting to
file
}

public void connect()
{
env = new Hashtable(11);
env.put
(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://host ort/dc=com");
env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager");
env.put(Context.SECURITY_CREDENTIALS, "password");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
try
{
this.context = new InitialDirContext(env);
}
catch(NamingException e)
{
System.out.println("Directory server binding error");
e.printStackTrace();
// logging code goes here
}
}

// here dn is base in the directry, which will be exported
public boolean export(String dn)
{
connect();

if(this.context == null)
return false;

String entryDN = null;
String filter = "objectclass=*";

try
{
SearchResult si;
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SUBTREE);
for(NamingEnumeration results = context.search(dn, filter,
ctls); results.hasMore(); this.export(entryDN, si.getAttributes()))
{
si = (SearchResult)results.next();
entryDN = getFixedDN(si.getName(), dn + "," +
this.baseDN);
}
writer.flush();
writer.close();

}
catch(NamingException e)
{
System.out.println("Failure during exporting!" + e);
return false;
}
return true;
}

public void export(String entryDN, Attributes attribs)
{
try
{
writeLDIF(writer, entryDN, attribs);
writer.println();
}
catch(IOException e)
{
// log error
e.printStackTrace();
}
}

private boolean writeLDIF(PrintWriter w, String dn, Attributes set)
throws IOException
{
if(set == null)
return false;
try
{
this.writeString(w, "dn", dn);
for(NamingEnumeration ae = set.getAll();
ae.hasMoreElements()
{
Attribute attr = (Attribute)ae.next();
String attrName = attr.getID();
for(Enumeration vals = attr.getAll();
vals.hasMoreElements
(); writeLDIF(((Writer) (w)), attrName, vals.nextElement()));
}

}
catch(NamingException _ex)
{
return false;
}
return true;
}

public static void writeLDIF(Writer w, String attrName, Object
value)
throws IOException
{
String str = value.toString();
writeString(w, attrName, str);
}

public static void writeString(Writer w, String attrName, String
value)
throws IOException
{
writeFormatted(w, attrName + ": ", value.toCharArray());
}

public static void writeFormatted(Writer w, String firstline, char
p[])
throws IOException
{
int size = 76;
int len = (size + 1) - firstline.length();
w.write(firstline);

if(p.length + firstline.length() <= size)
{
w.write(p);
w.write(System.getProperty("line.separator"));
return;
}

w.write(p, 0, len);
w.write(System.getProperty("line.separator"));

for(int i = len; i < p.length; i += size)
{
w.write(" ");
if(i + size > p.length)
{
w.write(p, i, p.length - i);
}
else
{
w.write(p, i, size);
}
w.write(System.getProperty("line.separator"));
}
}

private String fixName(String name)
{
if(name.length() > 0 && name.charAt(0) == ',')
{
int size = name.length() - 1;
StringBuffer buf = new StringBuffer();
for(int i = 1; i < size; i++)
{
if(name.charAt(i) == '/')
buf.append("\\");
buf.append(name.charAt(i));
}

return buf.toString();
}
else
{
return name;
}
}

private String getFixedDN(String rdn, String base)
{
return getDN(fixName(rdn), base);
}

private String getDN(String rdn, String base)
{
if(rdn.length() == 0)
return base;
if(base.length() == 0)
return rdn;
else
return rdn + ", " + base;
}
}
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"smart pointer",
welcome to the Ranch!

We're a friendly group, but we take our display names seriously.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and fix your display name, since accounts with invalid display names get deleted.

thanks,
Dave.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Was there a question, or just a large chunk of code you wanted to show us?
shiv kumar
Greenhorn

Joined: Apr 27, 2006
Posts: 5
is there any other way to do it, like a single line call provided by JNDI.
Well i didn't found it, so i wrote junk code here.

I want to know, ny other better way to do it.
And now i am moving for , such code for Importing ldif in directory using jndi calls.

Do u know any single statements to do it? using java code....
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Just to confirm, did you read my notice about your display name? It was polite, but it is not optional.
shiv kumar
Greenhorn

Joined: Apr 27, 2006
Posts: 5
yes, this is the name by which i known popularly here.

actually my name is smart. Please dont argue on name again.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Sorry, I'm not convinced.

Please consider this your last warning to change your display name. If you have any questions please address them in the Javaranch forum or one of the sheriffs.

regards,
David O'Meara
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Thank you.

I do not believe that there is any mechanism for interacting with ldif files in JNDI. Usually any functionality is provided as a command line or web application tool by the LDAP server itself. I guess this means your options are to do it the way you are, or provide a wrapper on the tool provided.

Dave
shiv kumar
Greenhorn

Joined: Apr 27, 2006
Posts: 5
finally u made me change my name -
but it was nice what says?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I did warn you that we take our display names seriously
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456

...Shiv Kumar is perhaps the most poignant Punjabi poet of this century...


(taken from here).

well, at least it's real-sounding :-) what's your problem with this etiquette? just wondering.

:-)

Jan

ps: ... on my way renaming my account to William Shakespear. Or shall I take Johann Wolfgang Goethe?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: export ldap tree to ldif file, check performance
 
Similar Threads
Login into LDAP Fails...
HOW TO ADD LDAP ENTRIES USING JNDI?
JVM and Certificate
LDAP example in JAVA
How to get list of user groups of active directory?