Added possibility to inspect attribute values in samples LDAP tree.
This commit is contained in:
@@ -15,11 +15,16 @@ public class HtmlRowLdapTreeVisitor implements LdapTreeVisitor {
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
sb.append(node.getDn()).append("<br>\n");
|
||||
sb.append("<a href='").append(getLinkForNode(node)).append("'>").append(node.getDn()).append("</a>")
|
||||
.append("<br>\n");
|
||||
|
||||
rows.add(sb.toString());
|
||||
}
|
||||
|
||||
protected String getLinkForNode(DirContextOperations node) {
|
||||
return "#";
|
||||
}
|
||||
|
||||
public List<String> getRows() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<html>
|
||||
<body>
|
||||
<a href="showTree.do">Back</a>
|
||||
<p>
|
||||
|
||||
Full name: ${person.fullName}
|
||||
<br>
|
||||
LastName: ${person.lastName}
|
||||
<br>
|
||||
Description: ${person.description}
|
||||
<br>
|
||||
Country: ${person.country}
|
||||
<br>
|
||||
Company: ${person.company}
|
||||
<br>
|
||||
Phone: ${person.phone}
|
||||
<br>
|
||||
</p>
|
||||
</html>
|
||||
@@ -1,11 +1,17 @@
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<html>
|
||||
<body>
|
||||
<a href="addPerson.do">Add new test person</a>
|
||||
<a href="removePerson.do">Remove test person</a>
|
||||
|
||||
<c:forEach var="row" items="${rows}">
|
||||
${row}
|
||||
</c:forEach>
|
||||
</body>
|
||||
</html>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<html>
|
||||
<body>
|
||||
<h2>Operations</h2>
|
||||
<h3>Clicking a link below performs the described operation which will be reflected in the LDAP tree below</h3>
|
||||
<a href="addPerson.do">Add new test person 'John Doe'</a> (only works once)<br>
|
||||
<a href="updatePhoneNumber.do">Add a '0' to the phone number of test person</a> (only works if the person has been created)<br>
|
||||
<a href="removePerson.do">Remove test person</a><br>
|
||||
<p>
|
||||
<h2>Tree contents</h2>
|
||||
<h3>Click a person row to see the attribute values (country and company rows do not have additional info)</h3>
|
||||
<c:forEach var="row" items="${rows}">
|
||||
${row}
|
||||
</c:forEach>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user