Sunday, May 18, 2008

Spring LDAP

When we're writing the intranet web application we should implement some authentification mechanism. There are a lot of ways to do it, but a more conveniently just to use a Spring LDAP framework. Here are the steps to set up this structure.
1. Download Spring LDAP form the site: http://springframework.org/ldap
2. Create Java Project;
2.1 Create package com.ldap.examples;
3. Import to it following jars:
i. commons-lang.jar
ii. commons-logging.jar
iii. commons-pool.jar
iv. ldapbp.jar
v. spring-beans.jar
vi. spring-context.jar
vii. spring-core.jar
viii. spring-dao.jar
ix. spring-jdbc.jar
x. spring-ldap-1.2.1.jar
4. Create PersonDao interface:

package com.ldap.examples;
import java.util.List;
public interface PersonDao {
public List getAllContactNames();
public List getContactDetails(String commonName,String lastName);
public List getPersonMail(String mail);
}

5. Create Java's class that implemets PersonDao interface


package com.ldap.examples;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
public class PersonDaoImpl implements PersonDao {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
/**
* @param args
*/
public static void main(String[] args) {
Resource resource = new ClassPathResource("com/ldap/examples/springldap.xml");
BeanFactory factory = new XmlBeanFactory(resource);
LdapTemplate ldapContact = (LdapTemplate) factory.getBean("ldapTemplate");
PersonDaoImpl my = new PersonDaoImpl();
my.setLdapTemplate(ldapContact);
System.out.println(my.getPersonMail(desiredMail@company.com));
}
public List getAllContactNames() {
// TODO Auto-generated method stub
return null;
}
public List getPersonMail(String mail) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass","person"));
andFilter.and(new EqualsFilter("mail",mail));
System.out.println("LDAP Query " + andFilter.encode());
return ldapTemplate.search("", andFilter.encode(),new ContactAttributeMapper());
}
public List getContactDetails(String commonName, String lastName) {
// TODO Auto-generated method stub
return null;
}
}

6. Create springldap.xml
-??xml version="1.0" encoding="UTF-8"?>
-??!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" -"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
-??beans>
-?bean id="contextSource"
-?class="org.springframework.ldap.core.support.LdapContextSource">
-?property name="url" value="ldap://ldapServerIPorName:389" />
-?property name="base" value="o=company.com" />
-?/bean>
-?bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
-?constructor-arg ref="contextSource" />
-?/bean>
-?bean id="ldapContact"
-?class="com.ldap.examples.PersonDaoImpl">
-?property name="ldapTemplate" ref="ldapTemplate" />
-?/bean>
-?/beans>
Change ? to the < (Blogger bug???)
Put this file into package.
7. Create ContactDTO java's class

package com.ldap.examples;
public class ContactDTO {
String commonName;
String lastName;
String description;
String mail;
public String getCommonName() {
return commonName;
}
public void setCommonName(String commonName) {
this.commonName = commonName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLastName() {
System.out.println("Last name: " + lastName);
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
StringBuffer contactDTOStr = new StringBuffer("Person=[");
contactDTOStr.append(" Mail = " + mail);
contactDTOStr.append(", Common Name = " + commonName);
contactDTOStr.append(", Last Name = " + lastName);
contactDTOStr.append(", Description = " + description);
contactDTOStr.append(" ]");
return contactDTOStr.toString();
}
public String getMail() {
System.out.println("Mail: " + mail);
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}

8. Create ContactAttributeMapper


package com.ldap.examples;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;

public class ContactAttributeMapper implements AttributesMapper{
public Object mapFromAttributes(Attributes attributes) throws NamingException {
ContactDTO contactDTO = new ContactDTO();
String commonName = (String)attributes.get("cn").get();
if(commonName != null)
contactDTO.setCommonName(commonName);
String lastName = (String)attributes.get("sn").get();
if(lastName != null)
contactDTO.setLastName(lastName);
Attribute description = attributes.get("description");
if(description != null)
contactDTO.setDescription((String)description.get());
Attribute mail = attributes.get("mail");
if(mail != null){
contactDTO.setMail((String) mail.get());
}
return contactDTO;
}
}

That's all. Thanks to the Spring LDAP team!!!

Tuesday, March 25, 2008

How to integrate ICEfaces with WTP?

Ok. After almost a week of attempts to integrate ICEFaces v.1.6.2 with WTP (Eurora based on Eclipse 3.3) i still succeeded. The steps which have been described were not correct. The correct steps (at least for me) are:
Requirements:
1. As usual, Java 2 EE 5/6;
2. Eclipse IDE for Java EE Developers - Eurora 3.3.1
3. ICEFaces - ICEfaces-Eclipse-IDE-v3.0.0.zip and ICEfaces-1.6.2-libs-Eclipse.zip
4. Unzip all files from step 3.
5. In the Eclipse, go to Help -> Software Updates -> Find and Install
6. In the Features Updates check Search for new features to install
7. Click Next
8. In the Update sites to visit check Eurora site
9. Click Finish
10.Check all relevant updates and install
11. Repeat the step 5 - 7
12. Click New Local Site...
13. Select the directory where has been unzipped ICEFaces
14. Click OK
15. Select this location in the sites to include in search
16. Click Finish
17. Install the ICEfaces
So it's all.
Now you are able to use these faces into your project.