[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Zimlets and LDAP...It Works!!



Hi all...

I finally figured this out.  It doesn't do anything much yet except return my LDAP attributes back to a Zimbra popup window....but at least it's working.  Here's the JSP that works for me.  I call it using the XML code I found in the "JSPSample" Zimlet....basically by dragging a message onto this Zimlet.  Eventually I'll make it clickable and we'll be using this for things like...

- Allow instructors to generate an email to all students in a course they teach
- Allow students to get more information about their courses
- Lookup more detailed info about an individual...for info people want that they can't find using the GAL
- Other stuff that I can't think of right now...  :)

Once I have something more exciting to share I'll put some details up at the Zimbra forums if anyone else is interested.

Oh and thought I'd mention this.....  I hate Java!  It's all about arcane error messages and writing code that doesn't read very cleanly....unlike Ruby which I am used to.

Have a nice Thanksgiving...

Matt Mencel
Western Illinois University




<%@ page language="java" import="com.sun.jndi.ldap.*, java.io.*, java.util.*, javax.naming.*, javax.naming.dir
ectory.*"%>
<%@ page import="com.zimbra.cs.account.Account" %>

<%
  String ldapServerName = "ldap.dom.edu";
  String user = "AuthUser";
  String pass = "AuthPass";
  String dn= "cn=" . concat(user) . concat(",ou=applications,dc=dom,dc=edu");

  PrintWriter output = response.getWriter();
  output.println("Hello BEGIN<br/><br/>");

  Properties env = new Properties();
  env.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory" );
  env.put("java.naming.ldap.version", "3");
  env.put( Context.PROVIDER_URL, "ldap://"+ ldapServerName + "/");
  env.put( Context.SECURITY_AUTHENTICATION, "simple");
  env.put( Context.SECURITY_PRINCIPAL, dn );
  env.put( Context.SECURITY_CREDENTIALS, pass);


  try {
        //Create the initial directory context
        DirContext ctx = new InitialDirContext( env );
        //Ask for all attributes of the object
        Attributes attrs = ctx.getAttributes("USERDN"); // e.g. "uid=username,ou=people,dc=dom,dc=edu"
        //Find the surname attribute ("sn") and print it
        //System.out.println(mySn);
        response.setContentType("text/html");  
        output.println("Hello MIDDLE");
        output.println("sn: "+ attrs.get("sn").get());

        // Enumerate through all attributes
        if ( attrs != null )
          {
                NamingEnumeration enumer = attrs.getAll();
                Attribute attr;
                while( enumer != null && enumer.hasMore())
                {
                        attr = ( Attribute )enumer.next();
                        output.print( "Attribute" );
                        output.print( attr.getID());
                        output.print( " has the value '" );
                        output.print( attr.get());
                        output.println( "'" );
                }
          }

        }
  catch (NamingException e){
        //Authentication Failed
        output.println("Caught Error: ");
        output.println(e);
        }
  

        output.println("Hello END");


%>