Tuesday, May 10, 2011

LDAP Acess by Java

public static void setLdapAttribute(String username,String password,MessageHeaderVO messageHeaderVO){

String attributeName[] ={"extensionattribute1","extensionattribute2","extensionattribute3"};
DirContext ctx=null;
try{

Hashtable env = new Hashtable();
//String password ="password";
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
String providerurl =CommonUtil.getResourceBundleMsg("ads_service_url", "com.ibm.ahb.idt.nl.AlHilalPortalPortletResourceMessages");
env.put(Context.PROVIDER_URL,providerurl);
//env.put(Context.PROVIDER_URL,PortletConstant.LDAP_PROVIDER_URL);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"CN="+username+",CN=Users,DC=testbank,DC=ae");
env.put(Context.SECURITY_CREDENTIALS,password);
ctx=new InitialDirContext(env);

Attributes attrs = ctx.getAttributes("CN="+username+",CN=Users,DC=testbank,DC=ae",attributeName);
NamingEnumeration answer = attrs.getAll();
while (answer.hasMoreElements()) {
Attribute attr = (Attribute) answer.nextElement();
NamingEnumeration attributevalues= attr.getAll();
while(attributevalues.hasMoreElements())
{
if(attr.getID().equalsIgnoreCase(attributeName[0])){
messageHeaderVO.setRequestorRole((String)attributevalues.nextElement());
}
if(attr.getID().equalsIgnoreCase(attributeName[1])){
messageHeaderVO.setIsManagerFlag((String)attributevalues.nextElement());
}
if(attr.getID().equalsIgnoreCase(attributeName[2])){
messageHeaderVO.setBranchCode((String)attributevalues.nextElement());
}
}

}

}catch (AuthenticationException authe) {
logger.error("Error while creating Ldapcontext " + authe.getMessage());
}catch (Exception e) {
logger.error("Error while creating Ldapcontext" + e.getMessage());
}finally{
try{
ctx.close();
}catch(Exception Ignore)
{
}
}
}

Access LDAP from IBM Portlet Code

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.jsr168.* ,com.ibm.portal.um.*,com.ibm.portal.um.portletservice.PumaHome,com.ibm.portal.portlet.service.PortletServiceHome"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>





<%
ArrayList GROUP_ATTRS = null;
ArrayList EDIT_USER_ATTRS = null;

ArrayList USER_ATTRS = null;
GROUP_ATTRS = new ArrayList();

USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS = new ArrayList();

USER_ATTRS.add("givenName");
USER_ATTRS.add("sn");
USER_ATTRS.add("uid");

GROUP_ATTRS = new ArrayList();
GROUP_ATTRS.add("cn");
GROUP_ATTRS.add("description");

EDIT_USER_ATTRS = new ArrayList();
EDIT_USER_ATTRS.add("uid");
// EDIT_USER_ATTRS.add("userPassword");
EDIT_USER_ATTRS.add("givenName");
EDIT_USER_ATTRS.add("sn");
// EDIT_USER_ATTRS.add("ibm-primaryEmail");
//EDIT_USER_ATTRS.add("preferredLanguage");
EDIT_USER_ATTRS.add("extensionattribute1");
EDIT_USER_ATTRS.add("extensionattribute2");
EDIT_USER_ATTRS.add("extensionattribute3");


com.ibm.jsr168.Jsr168PortletSessionBean sessionBean = (com.ibm.jsr168.Jsr168PortletSessionBean)renderRequest.getPortletSession().getAttribute(com.ibm.jsr168.Jsr168Portlet.SESSION_BEAN);



try{

PortletServiceHome psh;


javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");


com.ibm.portal.um.portletservice.PumaHome service = (com.ibm.portal.um.portletservice.PumaHome) psh.getPortletService(com.ibm.portal.um.portletservice.PumaHome.class);
PumaProfile pp = service.getProfile(renderRequest);
PumaLocator pl = service.getLocator(renderRequest);

List userList = pl.findUsersByAttribute("uid", "idtsso1");

System.out.println("Test1"+userList);

User editUser = (User)userList.get(0);
Map userMap = pp.getAttributes(editUser, EDIT_USER_ATTRS);

Object attrObj = userMap.get("givenName");
ArrayList attrArray = null;
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1givenName"+(String)attrArray.get(0));

} else {
System.out.println("Test1givenName"+(String) attrObj);

}

attrObj = userMap.get("sn");
if (attrObj instanceof java.util.List) {
attrArray = (ArrayList)attrObj;
System.out.println("Test1sn 1"+(String)attrArray.get(0));


} else {
System.out.println("Test1sn 1"+(String) attrObj);

}

attrObj = userMap.get("uid");
if (attrObj instanceof java.util.List) {

attrArray = (ArrayList)attrObj;
System.out.println("Harishuid"+(String)attrArray.get(0));

} else {
System.out.println("Test1uid"+(String) attrObj);

}

attrObj = userMap.get("extensionattribute1");
if (attrObj instanceof java.util.List) {

attrArray = (ArrayList)attrObj;
System.out.println("Test1extensionattribute1"+(String)attrArray.get(0));

} else {
System.out.println("Test1hextensionattribute1"+(String) attrObj);

}





}catch(Exception e){

System.out.println("Test1JSR168 "+e.getMessage());

}

%>

Access LDAP from Java Code

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.jsr168.* ,com.ibm.portal.um.*,com.ibm.portal.um.PumaHome,javax.naming.*,javax.naming.directory.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@taglib uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model" prefix="portlet-client-model" %>






<%

DirContext ctx=null;
Hashtable env = null;

String password ="123";

env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"Ldap://127.0.0.1:389");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
// env.put(Context.SECURITY_PROTOCOL, "ssl");
/*
         * sample website: company.com.tw
         *
         * DN: DC=company,DC=com,DC=tw
         */
env.put(Context.SECURITY_PRINCIPAL,"cn="+acct+",cn=users,DC=COMPANY DOMAIN");//,DC=com,DC=tw");


env.put(Context.SECURITY_CREDENTIALS,password);
try{
System.out.println("connecting...");
ctx=new InitialDirContext(env);

// Ask for all attributes of the object
Attributes attrs = ctx.getAttributes("CN="+user+",CN=Users,DC=ldapname,DC=ae");


NamingEnumeration answer = attrs.getAll();
while (answer.hasMoreElements()) {
//System.out.println("Inside while1....");
Attribute attr = (Attribute) answer.nextElement();
System.out.println(attr.getID() + "=");
NamingEnumeration answer1 = attr.getAll();
while(answer1.hasMoreElements())
{ System.out.println(attr.getID() + ":Value =" + (String)answer1.nextElement());

}


}
Attribute attr1 = attrs.get("departmentNumber");
Attribute attr2 =attrs.get("localityName");
Attribute attr3 =attrs.get("description");
if(attr1 != null)
{System.out.println("departmentNumber" + attr1.toString());
}
else
{System.out.println("departmentNumber is null");
}
if(attr2 != null)
{System.out.println("localityName" + attr2.toString());
}
else
{System.out.println("localityName is null");
}
if(attr3 != null)
{System.out.println("description" + attr3.toString());
}
else
{System.out.println("description is null");
}



System.out.println("success");
}catch(AuthenticationException authe){
System.out.println("fail");
authe.printStackTrace();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}finally{
try{
ctx.close();
}catch(Exception Ignore)
{
}
}

%>

Followers