LDAP-262: Cleaned up and consolidated the samples.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.samples.plain.dao;
|
||||
|
||||
import org.springframework.ldap.samples.plain.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Data Access Object interface for the Person entity.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
public interface PersonDao {
|
||||
void create(Person person);
|
||||
|
||||
void update(Person person);
|
||||
|
||||
void delete(Person person);
|
||||
|
||||
List<String> getAllPersonNames();
|
||||
|
||||
List<Person> findAll();
|
||||
|
||||
Person findByPrimaryKey(String country, String company, String fullname);
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.samples.plain.dao;
|
||||
|
||||
import org.springframework.ldap.core.AttributesMapper;
|
||||
import org.springframework.ldap.core.ContextMapper;
|
||||
import org.springframework.ldap.core.DirContextAdapter;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.ldap.core.LdapTemplate;
|
||||
import org.springframework.ldap.core.support.AbstractContextMapper;
|
||||
import org.springframework.ldap.filter.EqualsFilter;
|
||||
import org.springframework.ldap.samples.plain.domain.Person;
|
||||
import org.springframework.ldap.support.LdapNameBuilder;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
|
||||
import javax.naming.Name;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.ldap.LdapName;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Default implementation of PersonDao. This implementation uses
|
||||
* DirContextAdapter for managing attribute values. We use a ContextMapper
|
||||
* to map from the found contexts to our domain objects. This is especially useful
|
||||
* since we in this case have properties in our domain objects that depend on parts of the DN.
|
||||
*
|
||||
* We could have worked with Attributes and an AttributesMapper implementation
|
||||
* instead, but working with Attributes is a bore and also, working with
|
||||
* AttributesMapper objects (or, indeed Attributes) does not give us access to
|
||||
* the distinguished name. However, we do use it in one method that only needs a
|
||||
* single attribute: {@link #getAllPersonNames()}.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
private LdapTemplate ldapTemplate;
|
||||
|
||||
@Override
|
||||
public void create(Person person) {
|
||||
Name dn = buildDn(person);
|
||||
DirContextAdapter context = new DirContextAdapter(dn);
|
||||
mapToContext(person, context);
|
||||
ldapTemplate.bind(dn, context, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Person person) {
|
||||
Name dn = buildDn(person);
|
||||
DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn);
|
||||
mapToContext(person, context);
|
||||
ldapTemplate.modifyAttributes(dn, context.getModificationItems());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Person person) {
|
||||
ldapTemplate.unbind(buildDn(person));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllPersonNames() {
|
||||
EqualsFilter filter = new EqualsFilter("objectclass", "person");
|
||||
return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper<String>() {
|
||||
public String mapFromAttributes(Attributes attrs) throws NamingException {
|
||||
return attrs.get("cn").get().toString();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> findAll() {
|
||||
EqualsFilter filter = new EqualsFilter("objectclass", "person");
|
||||
return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), PERSON_CONTEXT_MAPPER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person findByPrimaryKey(String country, String company, String fullname) {
|
||||
LdapName dn = buildDn(country, company, fullname);
|
||||
return ldapTemplate.lookup(dn, PERSON_CONTEXT_MAPPER);
|
||||
}
|
||||
|
||||
private LdapName buildDn(Person person) {
|
||||
return buildDn(person.getCountry(), person.getCompany(), person.getFullName());
|
||||
}
|
||||
|
||||
private LdapName buildDn(String country, String company, String fullname) {
|
||||
return LdapNameBuilder.newInstance()
|
||||
.add("c", country)
|
||||
.add("ou", company)
|
||||
.add("cn", fullname)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void mapToContext(Person person, DirContextAdapter context) {
|
||||
context.setAttributeValues("objectclass", new String[] { "top", "person" });
|
||||
context.setAttributeValue("cn", person.getFullName());
|
||||
context.setAttributeValue("sn", person.getLastName());
|
||||
context.setAttributeValue("description", person.getDescription());
|
||||
context.setAttributeValue("telephoneNumber", person.getPhone());
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps from DirContextAdapter to Person objects. A DN for a person will be
|
||||
* of the form <code>cn=[fullname],ou=[company],c=[country]</code>, so
|
||||
* the values of these attributes must be extracted from the DN. For this,
|
||||
* we use the LdapName along with utility methods in LdapUtils.
|
||||
*/
|
||||
private final static ContextMapper<Person> PERSON_CONTEXT_MAPPER = new AbstractContextMapper<Person>() {
|
||||
@Override
|
||||
public Person doMapFromContext(DirContextOperations context) {
|
||||
Person person = new Person();
|
||||
|
||||
LdapName dn = LdapUtils.newLdapName(context.getDn());
|
||||
person.setCountry(LdapUtils.getStringValue(dn, 0));
|
||||
person.setCompany(LdapUtils.getStringValue(dn, 1));
|
||||
person.setFullName(context.getStringAttribute("cn"));
|
||||
person.setLastName(context.getStringAttribute("sn"));
|
||||
person.setDescription(context.getStringAttribute("description"));
|
||||
person.setPhone(context.getStringAttribute("telephoneNumber"));
|
||||
|
||||
return person;
|
||||
}
|
||||
};
|
||||
|
||||
public void setLdapTemplate(LdapTemplate ldapTemplate) {
|
||||
this.ldapTemplate = ldapTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2005-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.samples.plain.domain;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
import org.apache.commons.lang.builder.HashCodeBuilder;
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
import org.apache.commons.lang.builder.ToStringStyle;
|
||||
|
||||
/**
|
||||
* Simple class representing a single person.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
public class Person {
|
||||
private String fullName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String description;
|
||||
|
||||
private String country;
|
||||
|
||||
private String company;
|
||||
|
||||
private String phone;
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
public void setFullName(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(
|
||||
this, obj);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder
|
||||
.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(
|
||||
this, ToStringStyle.MULTI_LINE_STYLE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package org.springframework.ldap.samples.plain.web;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.ldap.samples.plain.dao.PersonDao;
|
||||
import org.springframework.ldap.samples.plain.domain.Person;
|
||||
import org.springframework.ldap.samples.utils.HtmlRowLdapTreeVisitor;
|
||||
import org.springframework.ldap.samples.utils.LdapTree;
|
||||
import org.springframework.ldap.samples.utils.LdapTreeBuilder;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.naming.Name;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* Default controller.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@Controller
|
||||
public class DefaultController {
|
||||
|
||||
@Autowired
|
||||
private LdapTreeBuilder ldapTreeBuilder;
|
||||
|
||||
@Autowired
|
||||
private PersonDao personDao;
|
||||
|
||||
@RequestMapping("/welcome.do")
|
||||
public void welcomeHandler() {
|
||||
}
|
||||
|
||||
@RequestMapping("/showTree.do")
|
||||
public ModelAndView showTree() {
|
||||
LdapTree ldapTree = ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName());
|
||||
HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor();
|
||||
ldapTree.traverse(visitor);
|
||||
return new ModelAndView("showTree", "rows", visitor.getRows());
|
||||
}
|
||||
|
||||
@RequestMapping("/addPerson.do")
|
||||
public String addPerson() {
|
||||
Person person = getPerson();
|
||||
|
||||
personDao.create(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@RequestMapping("/updatePhoneNumber.do")
|
||||
public String updatePhoneNumber() {
|
||||
Person person = personDao.findByPrimaryKey("Sweden", "company1", "John Doe");
|
||||
person.setPhone(StringUtils.join(new String[] { person.getPhone(), "0" }));
|
||||
|
||||
personDao.update(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@RequestMapping("/removePerson.do")
|
||||
public String removePerson() {
|
||||
Person person = getPerson();
|
||||
|
||||
personDao.delete(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@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");
|
||||
person.setLastName("Doe");
|
||||
person.setCompany("company1");
|
||||
person.setCountry("Sweden");
|
||||
person.setDescription("Test user");
|
||||
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")) {
|
||||
Name dn = node.getDn();
|
||||
String country = encodeValue(LdapUtils.getStringValue(dn, "c"));
|
||||
String company = encodeValue(LdapUtils.getStringValue(dn, "ou"));
|
||||
String fullName = encodeValue(LdapUtils.getStringValue(dn, "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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
3
samples/plain/src/main/java/overview.html
Normal file
3
samples/plain/src/main/java/overview.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<body>
|
||||
Plain example of Spring LDAP usage.
|
||||
</body>
|
||||
52
samples/plain/src/main/resources/applicationContext.xml
Normal file
52
samples/plain/src/main/resources/applicationContext.xml
Normal file
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:/ldap.properties" />
|
||||
|
||||
<!--
|
||||
This is for test and demo purposes only - the TestContextSourceFactoryBean starts an in-process
|
||||
Apache Directory Server instance and populates it with data from the specified LDIF file.
|
||||
|
||||
A real-world application would use a DirContextSource instead.
|
||||
-->
|
||||
<bean id="contextSource"
|
||||
class="org.springframework.ldap.test.TestContextSourceFactoryBean">
|
||||
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
|
||||
<property name="defaultPartitionName" value="jayway" />
|
||||
<property name="principal" value="${userDn}" />
|
||||
<property name="password" value="${password}" />
|
||||
<property name="ldifFile" value="classpath:/setup_data.ldif" />
|
||||
<property name="port" value="18880" />
|
||||
</bean>
|
||||
|
||||
<!--
|
||||
Below is an example of a ContextSource definition as it would look in a real application, connecting
|
||||
against an external LDAP server.
|
||||
|
||||
<bean class="org.springframework.ldap.core.support.LdapContextSource" id="contextSource">
|
||||
<property name="url" value="ldap://ldap.example.com" />
|
||||
<property name="userDn" value="cn=admin,dc=261consulting,dc=com"/>
|
||||
<property name="password" value="secret"/>
|
||||
<property name="base" value="dc=261consulting,dc=com" />
|
||||
</bean>
|
||||
-->
|
||||
|
||||
<bean id="ldapTemplate"
|
||||
class="org.springframework.ldap.core.LdapTemplate">
|
||||
<constructor-arg ref="contextSource" />
|
||||
</bean>
|
||||
|
||||
<bean id="ldapTreeBuilder"
|
||||
class="org.springframework.ldap.samples.utils.LdapTreeBuilder">
|
||||
<constructor-arg ref="ldapTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="personDao"
|
||||
class="org.springframework.ldap.samples.plain.dao.PersonDaoImpl">
|
||||
<property name="ldapTemplate" ref="ldapTemplate" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
4
samples/plain/src/main/resources/ldap.properties
Normal file
4
samples/plain/src/main/resources/ldap.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
urls=ldap://127.0.0.1:18880
|
||||
userDn=uid=admin,ou=system
|
||||
password=secret
|
||||
base=dc=jayway,dc=se
|
||||
7
samples/plain/src/main/resources/log4j.properties
Normal file
7
samples/plain/src/main/resources/log4j.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
log4j.rootCategory=INFO, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
|
||||
log4j.logger.org.apache.directory=ERROR
|
||||
35
samples/plain/src/main/resources/setup_data.ldif
Normal file
35
samples/plain/src/main/resources/setup_data.ldif
Normal file
@@ -0,0 +1,35 @@
|
||||
dn: c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: country
|
||||
c: Sweden
|
||||
description: The country of Sweden
|
||||
|
||||
dn: ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: organizationalUnit
|
||||
ou: company1
|
||||
description: First company in Sweden
|
||||
|
||||
dn: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person
|
||||
userPassword: password
|
||||
cn: Some Person
|
||||
sn: Person
|
||||
description: Sweden, Company1, Some Person
|
||||
telephoneNumber: +46 555-123456
|
||||
|
||||
dn: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person2
|
||||
userPassword: password
|
||||
cn: Some Person2
|
||||
sn: Person2
|
||||
description: Sweden, Company1, Some Person2
|
||||
telephoneNumber: +46 555-654321
|
||||
17
samples/plain/src/main/webapp/WEB-INF/basic-servlet.xml
Normal file
17
samples/plain/src/main/webapp/WEB-INF/basic-servlet.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan
|
||||
base-package="org.springframework.ldap.samples.plain.web" />
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
|
||||
<property name="prefix" value="/WEB-INF/jsp/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
</beans>
|
||||
20
samples/plain/src/main/webapp/WEB-INF/jsp/showPerson.jsp
Executable file
20
samples/plain/src/main/webapp/WEB-INF/jsp/showPerson.jsp
Executable file
@@ -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>
|
||||
17
samples/plain/src/main/webapp/WEB-INF/jsp/showTree.jsp
Normal file
17
samples/plain/src/main/webapp/WEB-INF/jsp/showTree.jsp
Normal file
@@ -0,0 +1,17 @@
|
||||
<%@ 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>
|
||||
39
samples/plain/src/main/webapp/WEB-INF/web.xml
Normal file
39
samples/plain/src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app id="Tiink-preview" xmlns="http://java.sun.com/xml/ns/j2ee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
|
||||
version="2.4">
|
||||
|
||||
<display-name>Spring LDAP Basic Example</display-name>
|
||||
<listener>
|
||||
<listener-class>
|
||||
org.springframework.web.context.ContextLoaderListener
|
||||
</listener-class>
|
||||
</listener>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:/applicationContext.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>basic</servlet-name>
|
||||
<servlet-class>
|
||||
org.springframework.web.servlet.DispatcherServlet
|
||||
</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/basic-servlet.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>basic</servlet-name>
|
||||
<url-pattern>*.do</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.htm</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
||||
5
samples/plain/src/main/webapp/index.htm
Normal file
5
samples/plain/src/main/webapp/index.htm
Normal file
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta HTTP-EQUIV="REFRESH" content="0; url=showTree.do">
|
||||
</head>
|
||||
</html>
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright 2005-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.ldap.samples.plain.dao;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.NameNotFoundException;
|
||||
import org.springframework.ldap.samples.plain.domain.Person;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Abstract base class for PersonDao integration tests.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
@ContextConfiguration("/config/testContext.xml")
|
||||
public class PersonDaoSampleIntegrationTest extends
|
||||
AbstractJUnit4SpringContextTests {
|
||||
|
||||
protected Person person;
|
||||
|
||||
@Autowired
|
||||
private PersonDao personDao;
|
||||
|
||||
@Before
|
||||
public void preparePerson() throws Exception {
|
||||
person = new Person();
|
||||
person.setCountry("Sweden");
|
||||
person.setCompany("company1");
|
||||
person.setFullName("Some Person");
|
||||
person.setLastName("Person");
|
||||
person
|
||||
.setDescription("Sweden, Company1, Some Person");
|
||||
person.setPhone("+46 555-123456");
|
||||
}
|
||||
|
||||
/**
|
||||
* Having a single test method test create, update and delete is not exactly
|
||||
* the ideal way of testing, since they depend on each other. A better way
|
||||
* would be to separate the tests and load a test fixture before each
|
||||
* operation, in order to guarantee the expected state every time. See the
|
||||
* ldaptemplate-person sample for the correct way to do this.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateUpdateDelete() {
|
||||
try {
|
||||
person.setFullName("Another Person");
|
||||
personDao.create(person);
|
||||
personDao.findByPrimaryKey(
|
||||
"Sweden", "company1",
|
||||
"Another Person");
|
||||
// if we got here, create succeeded
|
||||
|
||||
person.setDescription("Another description");
|
||||
personDao.update(person);
|
||||
Person result = personDao
|
||||
.findByPrimaryKey(
|
||||
"Sweden", "company1",
|
||||
"Another Person");
|
||||
assertEquals(
|
||||
"Another description", result
|
||||
.getDescription());
|
||||
} finally {
|
||||
personDao.delete(person);
|
||||
try {
|
||||
personDao.findByPrimaryKey(
|
||||
"Sweden", "company1",
|
||||
"Another Person");
|
||||
fail("NameNotFoundException (when using Spring LDAP) or RuntimeException (when using traditional) expected");
|
||||
} catch (NameNotFoundException expected) {
|
||||
// expected
|
||||
} catch (RuntimeException expected) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllPersonNames() {
|
||||
List result = personDao.getAllPersonNames();
|
||||
assertEquals(2, result.size());
|
||||
String first = (String) result.get(0);
|
||||
assertEquals("Some Person", first);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAll() {
|
||||
List result = personDao.findAll();
|
||||
assertEquals(2, result.size());
|
||||
Person first = (Person) result.get(0);
|
||||
assertEquals("Some Person", first
|
||||
.getFullName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByPrimaryKey() {
|
||||
Person result = personDao.findByPrimaryKey(
|
||||
"Sweden", "company1", "Some Person");
|
||||
assertEquals(person, result);
|
||||
}
|
||||
}
|
||||
4
samples/plain/src/test/resources/config/ldap.properties
Normal file
4
samples/plain/src/test/resources/config/ldap.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
urls=ldap://127.0.0.1:18880
|
||||
userDn=uid=admin,ou=system
|
||||
password=secret
|
||||
base=dc=jayway,dc=se
|
||||
29
samples/plain/src/test/resources/config/testContext.xml
Normal file
29
samples/plain/src/test/resources/config/testContext.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
<bean id="placeholderConfig"
|
||||
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="classpath:/config/ldap.properties" />
|
||||
</bean>
|
||||
|
||||
<bean id="contextSource"
|
||||
class="org.springframework.ldap.test.TestContextSourceFactoryBean">
|
||||
<property name="defaultPartitionSuffix" value="dc=jayway,dc=se" />
|
||||
<property name="defaultPartitionName" value="jayway" />
|
||||
<property name="principal" value="${userDn}" />
|
||||
<property name="password" value="${password}" />
|
||||
<property name="ldifFile" value="/setup_data.ldif" />
|
||||
<property name="port" value="18880" />
|
||||
</bean>
|
||||
|
||||
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
|
||||
<constructor-arg ref="contextSource" />
|
||||
</bean>
|
||||
|
||||
<bean id="personDao"
|
||||
class="org.springframework.ldap.samples.plain.dao.PersonDaoImpl">
|
||||
<property name="ldapTemplate" ref="ldapTemplate" />
|
||||
</bean>
|
||||
</beans>
|
||||
35
samples/plain/src/test/resources/setup_data.ldif
Normal file
35
samples/plain/src/test/resources/setup_data.ldif
Normal file
@@ -0,0 +1,35 @@
|
||||
dn: c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: country
|
||||
c: Sweden
|
||||
description: The country of Sweden
|
||||
|
||||
dn: ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: organizationalUnit
|
||||
ou: company1
|
||||
description: First company in Sweden
|
||||
|
||||
dn: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person
|
||||
userPassword: password
|
||||
cn: Some Person
|
||||
sn: Person
|
||||
description: Sweden, Company1, Some Person
|
||||
telephoneNumber: +46 555-123456
|
||||
|
||||
dn: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person2
|
||||
userPassword: password
|
||||
cn: Some Person2
|
||||
sn: Person2
|
||||
description: Sweden, Company1, Some Person2
|
||||
telephoneNumber: +46 555-654321
|
||||
Reference in New Issue
Block a user