ldap sample to get optional attributes such as "createtimestamp"
import java.util.Hashtable;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import com.novell.service.ndssdk.jndi.ldap.ext.LDAPDSConstants;
class OperationalAttrs implements LDAPDSConstants {
public static void main(String args) {
int flags;
String attrName, attrValue;
if ( args.length != 4 ) {
usage();
}
String hostURL = args[0];
String loginDN = args[1];
String password = args[2];
String entryDN = args[3];
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, hostURL);
env.put(Context.SECURITY_PRINCIPAL, loginDN );
env.put(Context.SECURITY_CREDENTIALS, password );
// create the initial directory context
DirContext ctx = new InitialDirContext(env);
// specify the LDAP operational attributes to rerurn
String returnAttrs = { "createTimeStamp",
"creatorsName",
"entryFlags",
"federationBoundary",
"localEntryID",
"modifiersName",
"modifyTimeStamp",
"structuralObjectClass",
"subordinateCount",
"subschemaSubentry" };
// return ENTRYDN's LDAP operational attributes
Attributes attrs = ctx.getAttributes( entryDN, returnAttrs );
// get Enumeration of returned LDAP operational attributes
NamingEnumeration ae = attrs.getAll();
// print out LDAP operational attributes
while ( ae.hasMore() ) {
Attribute attr = (Attribute)ae.next();
attrName = attr.getID();
// get Enumeration of the attribute values
NamingEnumeration e = attr.getAll();
if ( e.hasMore() && (attrValue=(String)e.next()) != null) {
if ( attrName.equalsIgnoreCase( "modifyTimeStamp" )) {
System.out.println("\n modifyTimeStamp: "
+ attrValue + " (UTC)");
PrintTime( attrValue );
}
else if (attrName.equalsIgnoreCase("createTimeStamp")){
System.out.println("\n createTimeStamp: "
+ attrValue + " (UTC)");
PrintTime( attrValue );
}
else if ( attrName.equalsIgnoreCase( "entryFlags" )) {
if ( (flags = Integer.parseInt( attrValue )) != 0 )
EntryFlags( flags );
}
else
System.out.println("\n "+attrName+": "+attrValue);
}
}
// close the context
ctx.close();
}
catch (NamingException e) {
System.err.println("\n\tOperationalAttrs example failed.");
e.printStackTrace();
}
finally {
System.exit(0);
}
}
// PrintTime() prints utc in text format
public static void PrintTime( String utc )
{
Date date = null;
// setup x.208 generalized time formatter
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
try {
// parse utc into Date
date = formatter.parse( utc );
}
catch(ParseException pe) {
System.out.println( "Error: " + pe.toString() );
}
System.out.println(" " + date + " (UTC)");
}
// EntryFlags() decodes and displays ORed entry flags
public static void EntryFlags( int flags ) {
StringBuffer entryFlags = new StringBuffer();
if ( (flags & LDAP_DS_ALIAS_ENTRY) != 0 )
entryFlags.append("AliasEntry ");
if (( flags & LDAP_DS_PARTITION_ROOT) != 0 )
entryFlags.append("PartionRoot ");
if (( flags & LDAP_DS_CONTAINER_ENTRY) != 0 )
entryFlags.append("ContainerEntry ");
if (( flags & LDAP_DS_CONTAINER_ALIAS) != 0 )
entryFlags.append("ContainerAlias ");
if (( flags & LDAP_DS_MATCHES_LIST_FILTER) != 0 )
entryFlags.append("MatchesListFilter ");
if (( flags & LDAP_DS_REFERENCE_ENTRY) != 0 )
entryFlags.append("ReferenceEntry ");
if (( flags & LDAP_DS_40X_REFERENCE_ENTRY) != 0 )
entryFlags.append("40XReferenceEntry ");
if (( flags & LDAP_DS_BACKLINKED) != 0 )
entryFlags.append("Backlinked ");
if (( flags & LDAP_DS_NEW_ENTRY) != 0 )
entryFlags.append("NewEntry ");
if (( flags & LDAP_DS_AUDITED) != 0 )
entryFlags.append("TemporaryEntry ");
if (( flags & LDAP_DS_AUDITED) != 0 )
entryFlags.append("Audited ");
if (( flags & LDAP_DS_ENTRY_NOT_PRESENT) != 0 )
entryFlags.append("EntryNotPresent ");
if (( flags & LDAP_DS_ENTRY_DAMAGED) != 0 )
entryFlags.append("EntryVarifyCTS ");
if (( flags & LDAP_DS_ENTRY_DAMAGED) != 0 )
entryFlags.append("EntryDamaged");
System.out.println(" entryFlags: " + entryFlags);
}
public static void usage() {
System.err.println("\n Usage: java OperationalAttrs
+ "
System.err.println( " Example: java OperationalAttrs ldap://Acme.com:"
+ "389 \"cn=Admin,o=Acme\""
+"\n secret \"cn=James,o=Sales\"");
System.exit(1);
}
}