• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: LDAP using cocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LDAP using cocoa


  • Subject: Re: LDAP using cocoa
  • From: "Alexander F. Hartner" <email@hidden>
  • Date: Mon, 16 Feb 2004 16:25:00 +0200

I went with my existing JAVA classes and used them from within the
COCOA Java framework. See attached code

[demime 0.98b removed an attachment of type application/octet-stream which had a name of AddressBook2LDAPController.m]
/* AddressBook2LDAPController */
#import <AddressBook/AddressBook.h>
#import <Cocoa/Cocoa.h>
#import <Foundation/NSEnumerator.h>
#import <JavaVM/JavaVM.h>
#import <Foundation/Foundation.h>


@interface AddressBook2LDAPController : NSObject
{
IBOutlet id abDataSource;
IBOutlet id abGroups;
IBOutlet id abMembers;
IBOutlet id abMemberSelections;
IBOutlet id abView;
IBOutlet id contextField;
IBOutlet id logonField;
IBOutlet id passwdField;
IBOutlet id progressBar;
IBOutlet id progressLabel;
IBOutlet id urlField;
}
- awakeFromNib;
- (IBAction)displayWindow:(id)sender;
- (IBAction)migrate:(id)sender;
- writePrefs;
- readPrefs;
@end
package com.j2anywhere.ldap;
import java.util.Hashtable;
import javax.naming.*;
import javax.naming.directory.*;
import com.apple.cocoa.foundation.*;

public class Connector
{
private String url;
private String principal;
private String passwd;
private String context;

public void configure(String url, String principal, String passwd, String context)
{
this.url=url;
this.principal=principal;
this.passwd=passwd;
this.context=context;
}

public void addPersonToLDAP(String firstname, String surname, String email, String phone) throws NamingException
{
BasicAttributes attributes = new BasicAttributes();

attributes.put(new BasicAttribute("objectClass","inetOrgPerson"));
if (firstname != null && !firstname.equals(""))attributes.put(new BasicAttribute("cn",firstname+" "+surname));
if (surname != null && !surname.equals(""))
{
attributes.put(new BasicAttribute("sn",surname));
}
else
{
attributes.put(new BasicAttribute("sn","Unknown"));
}
if (firstname != null && !firstname.equals(""))attributes.put(new BasicAttribute("givenName",firstname));
if (firstname != null && !firstname.equals(""))attributes.put(new BasicAttribute("initials", firstname.substring(0,1)));
attributes.put(new BasicAttribute("displayName",firstname+" "+surname));
if (email != null && !email.equals("")) attributes.put(new BasicAttribute("mail",email));
if (phone != null && !phone.equals(""))attributes.put(new BasicAttribute("telephoneNumber",phone));

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put("com.sun.jndi.ldap.connect.pool", "true");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL,principal);
env.put(Context.SECURITY_CREDENTIALS,passwd);
env.put(Context.SECURITY_AUTHENTICATION, "simple");

DirContext ctx = new InitialDirContext(env);

try
{
if (firstname != null && !firstname.equals(""))ctx.bind("cn="+firstname+" "+surname+","+context,null,attributes);
}
catch (javax.naming.NameAlreadyBoundException e)
{
//cn already in LDAP do nothing
System.err.println("CN already in LDAP");
}
}

public void addPersonToLDAP(String firstname, String surname, String displayname, NSMutableArray email, NSMutableArray phone) throws NamingException
{
BasicAttributes attributes = new BasicAttributes();

attributes.put(new BasicAttribute("objectClass","inetOrgPerson"));

if (firstname != null && !firstname.equals("")) attributes.put(new BasicAttribute("cn",displayname));
if (surname != null && !surname.equals(""))
{
attributes.put(new BasicAttribute("sn",surname));
}
else
{
attributes.put(new BasicAttribute("sn",displayname));
}
if (firstname != null && !firstname.equals("")) attributes.put(new BasicAttribute("givenName",firstname));
if (firstname != null && !firstname.equals("")) attributes.put(new BasicAttribute("initials", firstname.substring(0,1)));

attributes.put(new BasicAttribute("displayName", displayname));

BasicAttribute mail = new BasicAttribute("mail");
for (int index=0;index<email.count();index++)
{
mail.add((String)email.objectAtIndex(index));
}
if (email.count()>0)attributes.put(mail);

BasicAttribute telephoneNumber = new BasicAttribute("telephoneNumber");
for (int index=0;index<phone.count();index++)
{
telephoneNumber.add((String)phone.objectAtIndex(index));
}
if (phone.count()>0)attributes.put(telephoneNumber);

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put("com.sun.jndi.ldap.connect.pool", "true");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL,principal);
env.put(Context.SECURITY_CREDENTIALS,passwd);
env.put(Context.SECURITY_AUTHENTICATION, "simple");

DirContext ctx = new InitialDirContext(env);

try
{
ctx.bind("cn="+displayname+","+context,null,attributes);
}
catch (javax.naming.NameAlreadyBoundException e)
{
//cn already in LDAP do nothing
System.err.println("CN already in LDAP - Rebind");
ctx.rebind("cn="+displayname+","+context,null,attributes);
}
}
}
On 27 Jan 2004, at 0:41, Richard Wolf wrote:

>> does someone know of a code snippet to read/write access an LDAP
>> server?
>
> Stefan,
>
> I do not see that anyone has replied to you publicly ... so I will
> take a whack at it. :)
>
> It is simple to access LDAP from Cocoa. Just include the LDAP
> framework in your project and include the standard C LDAP headers in
> your code. There are a number of good books that explain the LDAP
> APIs (with code snippets) ... they mostly target Java/Perl ... but if
> you compare those books with what's in the C headers, you will see
> that there is nearly a one-to-one correspondence between the Java API,
> say, and the C API. Any code snippet would, pretty much, look like
> what's in the books.
>
> If you're looking for Objective-C model object wrappers for LDAP, I
> don't know that any exist. I have a very crude, homebrew set of model
> classes that I use myself ... but they would not be suitable for
> prime-time use. :)
> _______________________________________________
> cocoa-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: realize Audio CD Player
  • Next by Date: Re: Add NSString into an NSImage
  • Previous by thread: Re: realize Audio CD Player
  • Next by thread: how to save styled text
  • Index(es):
    • Date
    • Thread