• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to search with File Name in LDAP

 
Greenhorn
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
PropEnv.put(DirContext.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

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

// Get LDAP ip and port and set in environment
PropEnv.put(DirContext.PROVIDER_URL,"ldap://"+rsBundleSecu.getString("LDAPSERVERNAME").trim()+":"+rsBundleSecu.getString("LDAPPORTNUMBER").trim());

// 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
scCrl.setSearchScope(SearchControls.SUBTREE_SCOPE);

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

// 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

else
{
// 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);
fileWriteCrl.close();
}// End of else

}// End of while

dcLdap.close();

}// End of try

catch (NamingException nExc)
{
nExc.printStackTrace();
throw nExc;
}// End of NamingException catch

catch(Exception exc)
{
exc.printStackTrace();
throw exc;
}// End of Exception catch

}// End of fetchFile

}// End of CrlSearch
 
You have to be odd to be #1 - Seuss. An odd little ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic