• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

How to search with File Name in LDAP

Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

How to search a file with file name in LDAP??

Here is my code

import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import java.util.Properties;
import java.util.ResourceBundle;

import java.io.FileWriter;

import sfmshub.coms.core.utils.SFMSConstants;

public class CrlSearch
* This metood gets the file from LDAP Server
* @Throws NamingException
* @Throws Exception
* @Return String

public static void fetchFile() throws NamingException, Exception
// The search base is the level in the hierarchy
// that our search will start at. Here was use ""
// which indicates the very root of the directory.
final String strBase = "c=IN";

// LDAP filters are sort of like a WHERE clause. It
// is constructed in a standard way based on LDAP
// standards. The search here is a simple one that
// says to return any entry with an objectclass value.
// Since all entries must contain an objectclass, all
// entries will be returned.
final String strFilter = "(objectclass=cRLDistributionPoint)";

// Here we set some connection properties for JNDI.
Properties PropEnv = new Properties();

// String Variable which holds the CRL File Data
String strCrlFile = "";

// ResourceBundle for fetching crl file path and ldap ip
ResourceBundle rsBundleSecu = null;

// String for CRL File Path
String strNewFilePath;

// String for new CRL File
String strCrlFilePath;

// Setting environment for search

rsBundleSecu = ResourceBundle.getBundle("coms.core.utils.SFMSSecurityConf");

// Get LDAP ip and port and set in environment

// Get CRL File path and set in path variables
strCrlFilePath = rsBundleSecu.getString("CRLFILENAME").trim();
strNewFilePath = strCrlFilePath.substring(0,strCrlFilePath.lastIndexOf("/"));

// InitialDirCOntext with the environment
DirContext dcLdap = new InitialDirContext(PropEnv);

// SearchControls for search
SearchControls scCrl = new SearchControls();

//LDAP Search Scope is sub tree

// should return attributes common name and mail
final String returnedAttributes[] = {"cn","certificateRevocationList;binary"};

// Performing search.
NamingEnumeration neSearch = dcLdap.search(strBase, strFilter, scCrl);

// Byte array which holds the CRL File data from LDAP
byte[] byteCrlFile = null;

String strCn = null;
Attribute attr = null;

// While search results is not null or has more elements perform the operation
while ((neSearch != null) && (neSearch.hasMore()))
SearchResult srLdapSearch = (SearchResult) neSearch.next();
Attributes attrs = srLdapSearch.getAttributes();

// If attributes are null skip the process
if (attrs == null)
System.out.println("No attributes");
}// End of if

// if attributes are not null get all attributes
for (NamingEnumeration neAttributes = attrs.getAll(); neAttributes.hasMore()
attr = (Attribute) neAttributes.next();
String strAttrID = attr.getID();

for (NamingEnumeration neAttribute = attr.getAll(); neAttribute.hasMore()
if (strAttrID.equals("cn"))
strCn = neAttribute.next().toString();
}// end of else if
else if (strAttrID.equals("certificateRevocationList;binary"))
// Get the file
byteCrlFile = (byte[]) neAttribute.next();

}// End of else if

}// End of inner for

}// End of outer for

// Converts binary data to String
strCrlFile = com.security.Base64.encodeBytes(byteCrlFile);

// Write into new file
FileWriter fileWriteCrl = new FileWriter(strNewFilePath+"/newcrl.crl");
fileWriteCrl.write(SFMSConstants.strCrlHeader + strCrlFile + SFMSConstants.strCrlFooter);
}// End of else

}// End of while


}// End of try

catch (NamingException nExc)
throw nExc;
}// End of NamingException catch

catch(Exception exc)
throw exc;
}// End of Exception catch

}// End of fetchFile

}// End of CrlSearch
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic