diff --git a/mvn-build/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/HtmlRowLdapTreeVisitor.java b/mvn-build/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/HtmlRowLdapTreeVisitor.java index 704ee425..30c9c3d0 100644 --- a/mvn-build/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/HtmlRowLdapTreeVisitor.java +++ b/mvn-build/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/HtmlRowLdapTreeVisitor.java @@ -15,11 +15,16 @@ public class HtmlRowLdapTreeVisitor implements LdapTreeVisitor { sb.append("    "); } - sb.append(node.getDn()).append("
\n"); + sb.append("").append(node.getDn()).append("") + .append("
\n"); rows.add(sb.toString()); } + protected String getLinkForNode(DirContextOperations node) { + return "#"; + } + public List getRows() { return rows; } diff --git a/mvn-build/samples/spring-ldap-article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/mvn-build/samples/spring-ldap-article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java index cf18d709..6c8ac431 100644 --- a/mvn-build/samples/spring-ldap-article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ b/mvn-build/samples/spring-ldap-article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java @@ -1,6 +1,11 @@ package org.springframework.ldap.samples.article.web; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.samples.article.dao.PersonDao; import org.springframework.ldap.samples.article.domain.Person; @@ -8,9 +13,15 @@ import org.springframework.ldap.samples.utils.HtmlRowLdapTreeVisitor; import org.springframework.ldap.samples.utils.LdapTree; import org.springframework.ldap.samples.utils.LdapTreeBuilder; import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; +/** + * Default controller. + * + * @author Mattias Hellborg Arthursson + */ @Controller public class DefaultController { @@ -24,10 +35,10 @@ public class DefaultController { public void welcomeHandler() { } - @RequestMapping("/doStuff.do") + @RequestMapping("/showTree.do") public ModelAndView showTree() { LdapTree ldapTree = ldapTreeBuilder.getLdapTree(DistinguishedName.EMPTY_PATH); - HtmlRowLdapTreeVisitor visitor = new HtmlRowLdapTreeVisitor(); + HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor(); ldapTree.traverse(visitor); return new ModelAndView("showTree", "rows", visitor.getRows()); } @@ -40,6 +51,15 @@ public class DefaultController { return showTree(); } + @RequestMapping("/updatePhoneNumber.do") + public ModelAndView updatePhoneNumber() { + Person person = personDao.findByPrimaryKey("Sweden", "company1", "John Doe"); + person.setPhone(StringUtils.join(new String[] { person.getPhone(), "0" })); + + personDao.update(person); + return showTree(); + } + @RequestMapping("/removePerson.do") public ModelAndView removePerson() { Person person = getPerson(); @@ -47,8 +67,13 @@ public class DefaultController { personDao.delete(person); return showTree(); } - - + + @RequestMapping("/showPerson.do") + public ModelMap showPerson(String country, String company, String fullName) { + Person person = personDao.findByPrimaryKey(country, company, fullName); + return new ModelMap("person", person); + } + private Person getPerson() { Person person = new Person(); person.setFullName("John Doe"); @@ -59,4 +84,46 @@ public class DefaultController { return person; } + /** + * Generates appropriate links for person leaves in the tree. + * + * @author Mattias Hellborg Arthursson + */ + private static final class PersonLinkHtmlRowLdapTreeVisitor extends HtmlRowLdapTreeVisitor { + @Override + protected String getLinkForNode(DirContextOperations node) { + String[] objectClassValues = node.getStringAttributes("objectClass"); + if (containsValue(objectClassValues, "person")) { + DistinguishedName distinguishedName = (DistinguishedName) node.getDn(); + String country = encodeValue(distinguishedName.getValue("c")); + String company = encodeValue(distinguishedName.getValue("ou")); + String fullName = encodeValue(distinguishedName.getValue("cn")); + + return "showPerson.do?country=" + country + "&company=" + company + "&fullName=" + fullName; + } + else { + return super.getLinkForNode(node); + } + } + + private String encodeValue(String value) { + try { + return URLEncoder.encode(value, "UTF8"); + } + catch (UnsupportedEncodingException e) { + // Not supposed to happen + throw new RuntimeException("Unexpected encoding exception", e); + } + } + + private boolean containsValue(String[] values, String value) { + for (String oneValue : values) { + if (StringUtils.equals(oneValue, value)) { + return true; + } + } + return false; + } + } + } diff --git a/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showPerson.jsp b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showPerson.jsp new file mode 100755 index 00000000..4d25b21b --- /dev/null +++ b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showPerson.jsp @@ -0,0 +1,20 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + +Back +

+ +Full name: ${person.fullName} +
+LastName: ${person.lastName} +
+Description: ${person.description} +
+Country: ${person.country} +
+Company: ${person.company} +
+Phone: ${person.phone} +
+

+ diff --git a/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showTree.jsp b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showTree.jsp index 58271cc2..80546b96 100644 --- a/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showTree.jsp +++ b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/jsp/showTree.jsp @@ -1,11 +1,17 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> - - -Add new test person -Remove test person - - - ${row} - - - +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + +

Operations

+

Clicking a link below performs the described operation which will be reflected in the LDAP tree below

+Add new test person 'John Doe' (only works once)
+Add a '0' to the phone number of test person (only works if the person has been created)
+Remove test person
+

+

Tree contents

+

Click a person row to see the attribute values (country and company rows do not have additional info)

+ + ${row} + +

+ + diff --git a/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/setup_data.ldif b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/setup_data.ldif index 885a8ca4..e6d7dce6 100644 --- a/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/setup_data.ldif +++ b/mvn-build/samples/spring-ldap-article/src/main/webapp/WEB-INF/setup_data.ldif @@ -1,54 +1,15 @@ -dn: ou=groups,dc=jayway,dc=se -objectclass: top -objectclass: organizationalUnit -ou: groups - -dn: cn=ROLE_USER,ou=groups,dc=jayway,dc=se -objectclass: top -objectclass: groupOfUniqueNames -cn: ROLE_USER -uniqueMember: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se -uniqueMember: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se -uniqueMember: cn=Some Person,ou=company1,c=Norway,dc=jayway,dc=se -uniqueMember: cn=Some Person,ou=company2,c=Sweden,dc=jayway,dc=se -uniqueMember: cn=Some Person3,ou=company1,c=Sweden,dc=jayway,dc=se - -dn: cn=ROLE_ADMIN,ou=groups,dc=jayway,dc=se -objectclass: top -objectclass: groupOfUniqueNames -cn: ROLE_ADMIN -uniqueMember: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se - dn: c=Sweden,dc=jayway,dc=se objectclass: top objectclass: country c: Sweden description: The country of Sweden -dn: c=Norway,dc=jayway,dc=se -objectclass: top -objectclass: country -c: Norway -description: The country of Norway - dn: ou=company1,c=Sweden,dc=jayway,dc=se objectclass: top objectclass: organizationalUnit ou: company1 description: First company in Sweden -dn: ou=company2,c=Sweden,dc=jayway,dc=se -objectclass: top -objectclass: organizationalUnit -ou: company2 -description: Second company in Sweden - -dn: ou=company1,c=Norway,dc=jayway,dc=se -objectclass: top -objectclass: organizationalUnit -ou: company1 -description: First company in Norway - dn: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se objectclass: top objectclass: person @@ -72,39 +33,3 @@ cn: Some Person2 sn: Person2 description: Sweden, Company1, Some Person2 telephoneNumber: +46 555-654321 - -dn: cn=Some Person3,ou=company1,c=Sweden,dc=jayway,dc=se -objectclass: top -objectclass: person -objectclass: organizationalPerson -objectclass: inetOrgPerson -uid: some.person3 -userPassword: password -cn: Some Person3 -sn: Person3 -description: Sweden, Company1, Some Person3 -telephoneNumber: +46 555-123654 - -dn: cn=Some Person,ou=company2,c=Sweden,dc=jayway,dc=se -objectclass: top -objectclass: person -objectclass: organizationalPerson -objectclass: inetOrgPerson -uid: some.person4 -userPassword: password -cn: Some Person -sn: Person -description: Sweden, Company2, Some Person -telephoneNumber: +46 555-456321 - -dn: cn=Some Person+sn=Person,ou=company1,c=Norway,dc=jayway,dc=se -objectclass: top -objectclass: person -objectclass: organizationalPerson -objectclass: inetOrgPerson -uid: some.norwegian -userPassword: password -cn: Some Person -sn: Person -description: Norway, Company1, Some Person+Person -telephoneNumber: +45 555-654123