All things Jythonic 12.7.2004

July 2004
MoTuWeThFrSaSu
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
262728293031 
Jun
2004
 Aug
2004

A blog tracking jython developments

XML-Image Syndication

XML-Image Comment Feed

Letterimage Contact me

2004-07-12

Scripting LDAP with Jython -- Store Java Objects in LDAP

This is the complement to the earlier example that read a java Hastable out of an ldap directory entry. Here is how you get the data in there to begin with.

# Jython LDAP - Store Java Hashtable into LDAP Example

from javax.naming import *
from java.util import *
from javax.naming.directory import *

# Credentials to access LDAP
user = "cn=master"
passwd = "password"

# Setup LDAP Context Options
settings = Hashtable()
settings.put(Context.INITIAL_CONTEXT_FACTORY, \ 
       "com.sun.jndi.ldap.LdapCtxFactory")
settings.put(Context.PROVIDER_URL, "ldap://localhost:389")
settings.put(Context.SECURITY_PRINCIPAL, user)
settings.put(Context.SECURITY_CREDENTIALS, passwd)

# Connect to LDAP Server
ctx = InitialDirContext(settings)

# build Hastable we want to store
userPrefs = Hashtable()
userPrefs.put("Server", "Someserver.com")
userPrefs.put("Color", "Red")
userPrefs.put("Wine", "Cabernet")
userPrefs.put("Year", "1994")

ctx.bind("cn=test_user,ou=Preferences,dc=Company,dc=com", userPrefs)

#done

If the entry already exists you need to use the context's rebind method like this:

ctx.rebind("cn=test_user,ou=Preferences,dc=Company,dc=com", \
    userPrefs)
posted at 01:11:28    #

Scripting LDAP with Jython -- Load Java Objects from LDAP

A common way to store user preference data for applications is to store them in LDAP as a serialized Java Hastable object. The application then reads that back from ldap on startup using the authenticated userid as the key. Its a convenient way to store application settings. But what if you need to adjust or migrate settings from one place to the other? Here is an approach to reading that data out using Jython. Look for a future post about how to save it back using the context's bind method ...

# Jython LDAP - Retrieve Stored Object from LDAP Example

from javax.naming import *
from java.util import *
from javax.naming.directory import *

# Credentials to access LDAP
user = "cn=master"
passwd = "password"

# Setup LDAP Context Options
settings = Hashtable()
settings.put(Context.INITIAL_CONTEXT_FACTORY, \ 
     "com.sun.jndi.ldap.LdapCtxFactory")
settings.put(Context.PROVIDER_URL, "ldap://localhost:389")
settings.put(Context.SECURITY_PRINCIPAL, user)
settings.put(Context.SECURITY_CREDENTIALS, passwd)

# Connect to LDAP Server
ctx = InitialDirContext(settings)

# load the java Hashtable out of the ldap server
prefs = ctx.lookup("cn=ccm_root,ou=Preferences,dc=Company,dc=com")

for pref in prefs.keys():
        print "PREF: %s\n VALUE: %s " % (pref, prefs[pref])
posted at 23:44:00    #

Scripting LDAP with Jython -- Queries

I've been playing with LDAP directories from Jython and thought I should share a couple of useful examples of what you can do. The first of these is a quick barebones query example. Enjoy.

# Jython LDAP Example

from javax.naming import *
from java.util import *
from javax.naming.directory import *

# Credentials to access LDAP
user = "cn=master"
passwd = "password"

# Query starting point and query target 
search_start = "ou=People,dc=Company,dc=com"
search_target = "uid=aUserID"

# Setup LDAP Context Options
settings = Hashtable()
settings.put(Context.INITIAL_CONTEXT_FACTORY, \ 
     "com.sun.jndi.ldap.LdapCtxFactory")
settings.put(Context.PROVIDER_URL, "ldap://localhost:389")
settings.put(Context.SECURITY_PRINCIPAL, user)
settings.put(Context.SECURITY_CREDENTIALS, passwd)

# Connect to LDAP Server
ctx = InitialDirContext(settings)

srch = SearchControls()
srch.setSearchScope(SearchControls.SUBTREE_SCOPE)

# Execute LDAP Search
results = ctx.search(search_start, search_target, srch )

#Display Search`
for result in results:

     attributes = result.getAttributes()
     names = []
     for atr in attributes.getIDs():
             names.append(str(atr))

     for name in names:
             print attributes.get(name)
posted at 23:29:04    #
Creative Commons License
This work is licensed under a Creative Commons License.