From 9458b80fa13c6369264dcbf02b040ad4d49a00f6 Mon Sep 17 00:00:00 2001 From: Mattias Hellborg Arthursson Date: Thu, 12 Sep 2013 07:34:11 +0200 Subject: [PATCH] LDAP-262: Cleaned up and consolidated the samples. --- .../ldap/support/LdapUtils.java | 4 +- samples/article-spring20/.gitignore | 4 - samples/article-spring20/.springBeans | 25 -- samples/article-spring20/build.gradle | 25 -- samples/article-spring20/readme.txt | 21 - .../samples/article/dao/PersonDaoImpl.java | 163 -------- .../article/dao/TraditionalPersonDaoImpl.java | 377 ------------------ .../ldap/samples/article/domain/Person.java | 104 ----- .../article/web/DefaultController.java | 132 ------ .../src/main/java/overview.html | 3 - .../webapp/WEB-INF/applicationContext.xml | 36 -- .../src/main/webapp/WEB-INF/basic-servlet.xml | 35 -- .../src/main/webapp/WEB-INF/ldap.properties | 4 - .../src/main/webapp/WEB-INF/web.xml | 34 -- .../dao/AbstractPersonDaoIntegrationTest.java | 122 ------ .../dao/PersonDaoImplIntegrationTest.java | 33 -- ...aditionalPersonDaoImplIntegrationTest.java | 31 -- .../src/test/resources/config/ldap.properties | 4 - .../src/test/resources/config/testContext.xml | 37 -- samples/article-spring30/.gitignore | 4 - samples/article-spring30/.springBeans | 13 - samples/article-spring30/build.gradle | 23 -- samples/article-spring30/readme.txt | 21 - .../ldap/samples/article/dao/PersonDao.java | 42 -- .../samples/article/dao/PersonDaoImpl.java | 163 -------- .../article/dao/TraditionalPersonDaoImpl.java | 377 ------------------ .../ldap/samples/article/domain/Person.java | 104 ----- .../article/web/DefaultController.java | 130 ------ .../src/main/java/overview.html | 3 - .../src/main/resources/log4j.properties | 7 - .../webapp/WEB-INF/applicationContext.xml | 36 -- .../main/webapp/WEB-INF/jsp/showPerson.jsp | 20 - .../src/main/webapp/WEB-INF/jsp/showTree.jsp | 17 - .../src/main/webapp/WEB-INF/ldap.properties | 4 - .../src/main/webapp/WEB-INF/setup_data.ldif | 35 -- .../src/main/webapp/WEB-INF/web.xml | 34 -- .../src/main/webapp/index.htm | 5 - .../dao/AbstractPersonDaoIntegrationTest.java | 122 ------ .../dao/PersonDaoImplIntegrationTest.java | 33 -- ...aditionalPersonDaoImplIntegrationTest.java | 31 -- .../src/test/resources/config/ldap.properties | 4 - .../src/test/resources/config/testContext.xml | 37 -- .../src/test/resources/setup_data.ldif | 35 -- samples/article/.gitignore | 4 - samples/article/.springBeans | 13 - samples/article/readme.txt | 20 - .../ldap/samples/article/dao/PersonDao.java | 42 -- .../article/dao/TraditionalPersonDaoImpl.java | 377 ------------------ .../ldap/samples/article/domain/Person.java | 104 ----- samples/article/src/main/java/overview.html | 3 - .../src/main/resources/log4j.properties | 7 - .../webapp/WEB-INF/applicationContext.xml | 36 -- .../src/main/webapp/WEB-INF/basic-servlet.xml | 17 - .../main/webapp/WEB-INF/jsp/showPerson.jsp | 20 - .../src/main/webapp/WEB-INF/jsp/showTree.jsp | 17 - .../src/main/webapp/WEB-INF/setup_data.ldif | 35 -- samples/article/src/main/webapp/index.htm | 5 - .../dao/AbstractPersonDaoIntegrationTest.java | 122 ------ .../dao/PersonDaoImplIntegrationTest.java | 33 -- ...aditionalPersonDaoImplIntegrationTest.java | 31 -- .../src/test/resources/setup_data.ldif | 35 -- samples/demos/demo-tiger/.gitignore | 4 - samples/demos/demo-tiger/.springBeans | 21 - samples/demos/demo-tiger/build.gradle | 12 - samples/demos/demo-tiger/readme.txt | 18 - .../ldap/demo/dao/PersonDao.java | 40 -- .../ldap/demo/dao/PersonDaoImpl.java | 344 ---------------- .../ldap/demo/domain/Person.java | 104 ----- .../ldap/demo/solution/PersonDaoImpl.java | 140 ------- .../solution/TraditionalPersonDaoImpl.java | 344 ---------------- .../demo-tiger/src/main/java/overview.html | 3 - .../demo/dao/PersonDaoIntegrationTest.java | 118 ------ .../src/test/resources/config/ldap.properties | 4 - .../src/test/resources/config/setup_data.ldif | 35 -- .../src/test/resources/config/testContext.xml | 28 -- .../src/test/resources/log4j.properties | 7 - samples/demos/demo/.gitignore | 4 - samples/demos/demo/.springBeans | 21 - samples/demos/demo/build.gradle | 12 - samples/demos/demo/readme.txt | 18 - .../ldap/demo/dao/PersonDao.java | 40 -- .../ldap/demo/dao/PersonDaoImpl.java | 343 ---------------- .../ldap/demo/solution/PersonDaoImpl.java | 138 ------- .../solution/TraditionalPersonDaoImpl.java | 344 ---------------- .../ldap/demo/solution/testContext.xml | 28 -- .../demos/demo/src/main/java/overview.html | 3 - .../demo/dao/PersonDaoIntegrationTest.java | 113 ------ .../src/test/resources/config/ldap.properties | 4 - .../src/test/resources/config/setup_data.ldif | 35 -- .../src/test/resources/config/testContext.xml | 28 -- .../demo/src/test/resources/log4j.properties | 7 - samples/demos/readme.txt | 1 - samples/{article => plain}/build.gradle | 0 samples/plain/readme.txt | 9 + .../ldap/samples/plain}/dao/PersonDao.java | 13 +- .../samples/plain}/dao/PersonDaoImpl.java | 73 ++-- .../ldap/samples/plain}/domain/Person.java | 2 +- .../samples/plain}/web/DefaultController.java | 18 +- samples/plain/src/main/java/overview.html | 3 + .../src/main/resources/applicationContext.xml | 52 +++ .../src/main/resources}/ldap.properties | 0 .../src/main/resources/log4j.properties | 0 .../src/main/resources}/setup_data.ldif | 0 .../src/main/webapp/WEB-INF/basic-servlet.xml | 8 +- .../main/webapp/WEB-INF/jsp/showPerson.jsp | 0 .../src/main/webapp/WEB-INF/jsp/showTree.jsp | 0 .../src/main/webapp/WEB-INF/web.xml | 7 +- .../src/main/webapp/index.htm | 0 .../dao/PersonDaoSampleIntegrationTest.java | 122 ++++++ .../src/test/resources/config/ldap.properties | 0 .../src/test/resources/config/testContext.xml | 10 +- .../src/test/resources/setup_data.ldif | 0 samples/readme.txt | 9 - .../ldap/samples/utils/LdapTreeBuilder.java | 2 +- settings.gradle | 6 +- 115 files changed, 244 insertions(+), 5866 deletions(-) delete mode 100644 samples/article-spring20/.gitignore delete mode 100644 samples/article-spring20/.springBeans delete mode 100644 samples/article-spring20/build.gradle delete mode 100644 samples/article-spring20/readme.txt delete mode 100644 samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java delete mode 100644 samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java delete mode 100644 samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/domain/Person.java delete mode 100644 samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java delete mode 100644 samples/article-spring20/src/main/java/overview.html delete mode 100644 samples/article-spring20/src/main/webapp/WEB-INF/applicationContext.xml delete mode 100644 samples/article-spring20/src/main/webapp/WEB-INF/basic-servlet.xml delete mode 100644 samples/article-spring20/src/main/webapp/WEB-INF/ldap.properties delete mode 100644 samples/article-spring20/src/main/webapp/WEB-INF/web.xml delete mode 100644 samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java delete mode 100644 samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java delete mode 100644 samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java delete mode 100644 samples/article-spring20/src/test/resources/config/ldap.properties delete mode 100644 samples/article-spring20/src/test/resources/config/testContext.xml delete mode 100644 samples/article-spring30/.gitignore delete mode 100644 samples/article-spring30/.springBeans delete mode 100644 samples/article-spring30/build.gradle delete mode 100644 samples/article-spring30/readme.txt delete mode 100644 samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java delete mode 100644 samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java delete mode 100644 samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java delete mode 100644 samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/domain/Person.java delete mode 100644 samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java delete mode 100644 samples/article-spring30/src/main/java/overview.html delete mode 100644 samples/article-spring30/src/main/resources/log4j.properties delete mode 100644 samples/article-spring30/src/main/webapp/WEB-INF/applicationContext.xml delete mode 100755 samples/article-spring30/src/main/webapp/WEB-INF/jsp/showPerson.jsp delete mode 100644 samples/article-spring30/src/main/webapp/WEB-INF/jsp/showTree.jsp delete mode 100644 samples/article-spring30/src/main/webapp/WEB-INF/ldap.properties delete mode 100644 samples/article-spring30/src/main/webapp/WEB-INF/setup_data.ldif delete mode 100644 samples/article-spring30/src/main/webapp/WEB-INF/web.xml delete mode 100644 samples/article-spring30/src/main/webapp/index.htm delete mode 100644 samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java delete mode 100644 samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java delete mode 100644 samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java delete mode 100644 samples/article-spring30/src/test/resources/config/ldap.properties delete mode 100644 samples/article-spring30/src/test/resources/config/testContext.xml delete mode 100644 samples/article-spring30/src/test/resources/setup_data.ldif delete mode 100644 samples/article/.gitignore delete mode 100644 samples/article/.springBeans delete mode 100644 samples/article/readme.txt delete mode 100644 samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java delete mode 100644 samples/article/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java delete mode 100644 samples/article/src/main/java/org/springframework/ldap/samples/article/domain/Person.java delete mode 100644 samples/article/src/main/java/overview.html delete mode 100644 samples/article/src/main/resources/log4j.properties delete mode 100644 samples/article/src/main/webapp/WEB-INF/applicationContext.xml delete mode 100644 samples/article/src/main/webapp/WEB-INF/basic-servlet.xml delete mode 100755 samples/article/src/main/webapp/WEB-INF/jsp/showPerson.jsp delete mode 100644 samples/article/src/main/webapp/WEB-INF/jsp/showTree.jsp delete mode 100644 samples/article/src/main/webapp/WEB-INF/setup_data.ldif delete mode 100644 samples/article/src/main/webapp/index.htm delete mode 100644 samples/article/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java delete mode 100644 samples/article/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java delete mode 100644 samples/article/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java delete mode 100644 samples/article/src/test/resources/setup_data.ldif delete mode 100644 samples/demos/demo-tiger/.gitignore delete mode 100644 samples/demos/demo-tiger/.springBeans delete mode 100644 samples/demos/demo-tiger/build.gradle delete mode 100644 samples/demos/demo-tiger/readme.txt delete mode 100644 samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java delete mode 100644 samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java delete mode 100644 samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/domain/Person.java delete mode 100644 samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java delete mode 100644 samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java delete mode 100644 samples/demos/demo-tiger/src/main/java/overview.html delete mode 100644 samples/demos/demo-tiger/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java delete mode 100644 samples/demos/demo-tiger/src/test/resources/config/ldap.properties delete mode 100644 samples/demos/demo-tiger/src/test/resources/config/setup_data.ldif delete mode 100644 samples/demos/demo-tiger/src/test/resources/config/testContext.xml delete mode 100644 samples/demos/demo-tiger/src/test/resources/log4j.properties delete mode 100644 samples/demos/demo/.gitignore delete mode 100644 samples/demos/demo/.springBeans delete mode 100644 samples/demos/demo/build.gradle delete mode 100644 samples/demos/demo/readme.txt delete mode 100644 samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java delete mode 100644 samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java delete mode 100644 samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java delete mode 100644 samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java delete mode 100644 samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/testContext.xml delete mode 100644 samples/demos/demo/src/main/java/overview.html delete mode 100644 samples/demos/demo/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java delete mode 100644 samples/demos/demo/src/test/resources/config/ldap.properties delete mode 100644 samples/demos/demo/src/test/resources/config/setup_data.ldif delete mode 100644 samples/demos/demo/src/test/resources/config/testContext.xml delete mode 100644 samples/demos/demo/src/test/resources/log4j.properties delete mode 100644 samples/demos/readme.txt rename samples/{article => plain}/build.gradle (100%) create mode 100644 samples/plain/readme.txt rename samples/{article-spring20/src/main/java/org/springframework/ldap/samples/article => plain/src/main/java/org/springframework/ldap/samples/plain}/dao/PersonDao.java (75%) rename samples/{article/src/main/java/org/springframework/ldap/samples/article => plain/src/main/java/org/springframework/ldap/samples/plain}/dao/PersonDaoImpl.java (71%) rename samples/{demos/demo/src/main/java/org/springframework/ldap/demo => plain/src/main/java/org/springframework/ldap/samples/plain}/domain/Person.java (93%) rename samples/{article/src/main/java/org/springframework/ldap/samples/article => plain/src/main/java/org/springframework/ldap/samples/plain}/web/DefaultController.java (90%) create mode 100644 samples/plain/src/main/java/overview.html create mode 100644 samples/plain/src/main/resources/applicationContext.xml rename samples/{article/src/main/webapp/WEB-INF => plain/src/main/resources}/ldap.properties (100%) rename samples/{article-spring20 => plain}/src/main/resources/log4j.properties (100%) rename samples/{article-spring20/src/main/webapp/WEB-INF => plain/src/main/resources}/setup_data.ldif (100%) rename samples/{article-spring30 => plain}/src/main/webapp/WEB-INF/basic-servlet.xml (90%) rename samples/{article-spring20 => plain}/src/main/webapp/WEB-INF/jsp/showPerson.jsp (100%) rename samples/{article-spring20 => plain}/src/main/webapp/WEB-INF/jsp/showTree.jsp (100%) rename samples/{article => plain}/src/main/webapp/WEB-INF/web.xml (84%) rename samples/{article-spring20 => plain}/src/main/webapp/index.htm (100%) create mode 100644 samples/plain/src/test/java/org/springframework/ldap/samples/plain/dao/PersonDaoSampleIntegrationTest.java rename samples/{article => plain}/src/test/resources/config/ldap.properties (100%) rename samples/{article => plain}/src/test/resources/config/testContext.xml (72%) rename samples/{article-spring20 => plain}/src/test/resources/setup_data.ldif (100%) delete mode 100644 samples/readme.txt diff --git a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java index 753166ed..e3d877ad 100644 --- a/core/src/main/java/org/springframework/ldap/support/LdapUtils.java +++ b/core/src/main/java/org/springframework/ldap/support/LdapUtils.java @@ -540,13 +540,14 @@ public final class LdapUtils { * @param index The 0-based index of the rdn value to retrieve. Must be in the range [0,size()). * @return the value of the rdn at the requested index. * @throws IndexOutOfBoundsException if index is outside the specified range. + * @since 2.0 */ public static Object getValue(Name name, int index) { Assert.notNull(name, "name must not be null"); LdapName ldapName = returnOrConstructLdapNameFromName(name); Rdn rdn = ldapName.getRdn(index); - if(rdn.size() > 0) { + if(rdn.size() > 1) { logger.warn("Rdn at position " + index + " of dn '" + name + "' is multi-value - returned value is not to be trusted. " + "Consider using name-based getValue method instead"); @@ -562,6 +563,7 @@ public final class LdapUtils { * @return the value of the rdn at the requested index as a String. * @throws IndexOutOfBoundsException if index is outside the specified range. * @throws ClassCastException if the value of the requested component is not a String. + * @since 2.0 */ public static String getStringValue(Name name, int index) { return (String) getValue(name, index); diff --git a/samples/article-spring20/.gitignore b/samples/article-spring20/.gitignore deleted file mode 100644 index 73df60d8..00000000 --- a/samples/article-spring20/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -.classpath -.project -.settings diff --git a/samples/article-spring20/.springBeans b/samples/article-spring20/.springBeans deleted file mode 100644 index fcb9ecc0..00000000 --- a/samples/article-spring20/.springBeans +++ /dev/null @@ -1,25 +0,0 @@ - - - 1 - - - - - - - src/main/webapp/WEB-INF/applicationContext.xml - src/main/webapp/WEB-INF/basic-servlet.xml - src/test/resources/config/testContext.xml - - - - - true - false - - src/main/webapp/WEB-INF/applicationContext.xml - src/main/webapp/WEB-INF/basic-servlet.xml - - - - diff --git a/samples/article-spring20/build.gradle b/samples/article-spring20/build.gradle deleted file mode 100644 index ec0b0f80..00000000 --- a/samples/article-spring20/build.gradle +++ /dev/null @@ -1,25 +0,0 @@ -apply from: JAVA_SCRIPT -apply plugin: 'war' -apply plugin: 'jetty' - -ext.springVersion = "2.0.8" - -dependencies { - compile project(':spring-ldap-test'), - project(':spring-ldap-samples-utils'), - 'log4j:log4j:1.2.9', - 'javax.servlet:jstl:1.2', - "org.springframework:spring-beans:$springVersion", - "org.springframework:spring-core:$springVersion", - "org.springframework:spring-dao:$springVersion", - "org.springframework:spring-jdbc:$springVersion", - "org.springframework:spring-context:$springVersion", - "org.springframework:spring-web:$springVersion", - "org.springframework:spring-webmvc:$springVersion", - "org.springframework:spring-support:$springVersion" - - provided "javax.servlet:servlet-api:2.5" - - testCompile "org.springframework:spring-mock:$springVersion", - "junit:junit:$junitVersion" -} \ No newline at end of file diff --git a/samples/article-spring20/readme.txt b/samples/article-spring20/readme.txt deleted file mode 100644 index e603ea31..00000000 --- a/samples/article-spring20/readme.txt +++ /dev/null @@ -1,21 +0,0 @@ -Uses Spring 2.0. -Sample application demonstrating how to do the most basic stuff in Spring LDAP. -A very simple dao implementation is provided in -org.springframework.ldap.samples.article.dao.PersonDaoImpl -It demonstrates some basic operations using Spring LDAP. For reference purposes, -a corresponding implementation using ordinary Java LDAP/JNDI implementation is -available in TraditionalPersonDaoImpl. - -How to use: ------------ -The project is in a Maven build structure. Make sure you have installed the samples-utils artifact, as this will be -needed for this project to work. - -'mvn jetty:run' will start up a web server demonstrating the capabilities. The web application will be available -under http://localhost:8080/spring-ldap-person-article-spring20/ - -'mvn eclipse:eclipse' will construct an Eclipse project for you to use. Import that project into Eclipse using -File/Import/Existing Project, and select this directory. - -'mvn test' will run some integration tests that require the LDAP server to be running. It's recommended to run -'mvn jetty:run' from another terminal window before 'mvn test'. diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java deleted file mode 100644 index 46b6429b..00000000 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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.article.dao; - -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.filter.EqualsFilter; -import org.springframework.ldap.samples.article.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 java.util.List; - -/** - * Default implementation of PersonDao. This implementation uses - * DirContextAdapter for managing attribute values. It has been specified in the - * Spring Context that the DirObjectFactory should be used when creating objects - * from contexts, which defaults to creating DirContextAdapter objects. This - * means that we can 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; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - Name dn = buildDn(person); - DirContextAdapter context = new DirContextAdapter(dn); - mapToContext(person, context); - ldapTemplate.bind(dn, context, null); - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - Name dn = buildDn(person); - DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn); - mapToContext(person, context); - ldapTemplate.modifyAttributes(dn, context.getModificationItems()); - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - ldapTemplate.unbind(buildDn(person)); - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { - public Object mapFromAttributes(Attributes attrs) throws NamingException { - return attrs.get("cn").get(); - } - }); - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, String fullname) { - Name dn = buildDn(country, company, fullname); - return (Person) ldapTemplate.lookup(dn, getContextMapper()); - } - - private ContextMapper getContextMapper() { - return new PersonContextMapper(); - } - - private Name buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person.getFullName()); - } - - private Name 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 cn=[fullname],ou=[company],c=[country], so - * the values of these attributes must be extracted from the DN. For this, - * we use LdapName and helper methods in LdapUtils. - * - *Mattias Hellborg Arthurssonellborg Arthursson - * @author Ulrik Sandberg - */ - private static class PersonContextMapper implements ContextMapper { - - public Object mapFromContext(Object ctx) { - DirContextAdapter context = (DirContextAdapter) ctx; - Name dn = context.getDn(); - Person person = new Person(); - 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; - } -} diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java deleted file mode 100644 index e45403f9..00000000 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * 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.article.dao; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.samples.article.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to - * contrast this implementation with that of {@link PersonDaoImpl}. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImpl implements - PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx - .rebind( - dn, null, - getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - Attributes attributes = searchResult - .getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult - .getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, - String company, String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn( - country, company, fullname); - try { - Attributes attributes = ctx - .getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException( - "Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person - .getCompany(), person.getFullName()); - } - - private String buildDn(String country, String company, - String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put( - Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable env = new Hashtable(); - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put( - Context.SECURITY_AUTHENTICATION, - "simple"); - env.put( - Context.SECURITY_PRINCIPAL, userName); - env.put( - Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind( - Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute( - "objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person - .getDescription()); - attrs.put("telephoneNumber", person - .getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, - Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get( - "cn").get()); - person.setLastName((String) attributes.get( - "sn").get()); - person.setDescription((String) attributes - .get("description").get()); - person.setPhone((String) attributes.get( - "telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn - .replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn - .lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn - .lastIndexOf(companyMarker); - - String country = cleanedDn - .substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring( - companyIndex + companyMarker.length(), - countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/domain/Person.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/domain/Person.java deleted file mode 100644 index d0dc998c..00000000 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/domain/Person.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.article.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); - } -} diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java deleted file mode 100644 index c276f721..00000000 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.springframework.ldap.samples.article.web; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.samples.article.dao.PersonDao; -import org.springframework.ldap.samples.article.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.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.multiaction.MultiActionController; - -import javax.naming.Name; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; - -/** - * Default controller. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class DefaultController extends MultiActionController { - - private LdapTreeBuilder ldapTreeBuilder; - - public void setLdapTreeBuilder(LdapTreeBuilder ldapTreeBuilder) { - this.ldapTreeBuilder = ldapTreeBuilder; - } - - public void setPersonDao(PersonDao personDao) { - this.personDao = personDao; - } - - private PersonDao personDao; - - public ModelAndView welcomeHandler(HttpServletRequest request, HttpServletResponse response) { - return new ModelAndView("welcome"); - } - - public ModelAndView showTree(HttpServletRequest request, HttpServletResponse response) { - LdapTree ldapTree = ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName()); - HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor(); - ldapTree.traverse(visitor); - return new ModelAndView("showTree", "rows", visitor.getRows()); - } - - public ModelAndView addPerson(HttpServletRequest request, HttpServletResponse response) { - Person person = getPerson(); - - personDao.create(person); - return showTree(request, response); - } - - public ModelAndView updatePhoneNumber(HttpServletRequest request, HttpServletResponse response) { - Person person = personDao.findByPrimaryKey("Sweden", "company1", "John Doe"); - person.setPhone(StringUtils.join(new String[] { person.getPhone(), "0" })); - - personDao.update(person); - return showTree(request, response); - } - - public ModelAndView removePerson(HttpServletRequest request, HttpServletResponse response) { - Person person = getPerson(); - - personDao.delete(person); - return showTree(request, response); - } - - public ModelAndView showPerson(HttpServletRequest request, HttpServletResponse response, Person query) { - String country = query.getCountry(); - String company = query.getCompany(); - String fullName = query.getFullName(); - Person person = personDao.findByPrimaryKey(country, company, fullName); - return new ModelAndView("showPerson", "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; - } - } -} diff --git a/samples/article-spring20/src/main/java/overview.html b/samples/article-spring20/src/main/java/overview.html deleted file mode 100644 index 309641cf..00000000 --- a/samples/article-spring20/src/main/java/overview.html +++ /dev/null @@ -1,3 +0,0 @@ - -This document is the API specification for the Spring LDAP Article sample. - \ No newline at end of file diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/applicationContext.xml b/samples/article-spring20/src/main/webapp/WEB-INF/applicationContext.xml deleted file mode 100644 index 56e6abad..00000000 --- a/samples/article-spring20/src/main/webapp/WEB-INF/applicationContext.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/basic-servlet.xml b/samples/article-spring20/src/main/webapp/WEB-INF/basic-servlet.xml deleted file mode 100644 index 8917ac4b..00000000 --- a/samples/article-spring20/src/main/webapp/WEB-INF/basic-servlet.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - -/welcome.do=welcome -/showTree.do=showTree -/addPerson.do=addPerson -/updatePhoneNumber.do=updatePhoneNumber -/removePerson.do=removePerson -/showPerson.do=showPerson - - - - - - - /*.do=defaultController - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/ldap.properties b/samples/article-spring20/src/main/webapp/WEB-INF/ldap.properties deleted file mode 100644 index b635ac60..00000000 --- a/samples/article-spring20/src/main/webapp/WEB-INF/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18881 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/web.xml b/samples/article-spring20/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 3482f7a9..00000000 --- a/samples/article-spring20/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - Spring LDAP Basic Example - - - org.springframework.web.context.ContextLoaderListener - - - - - basic - - org.springframework.web.servlet.DispatcherServlet - - - contextConfigLocation - /WEB-INF/basic-servlet.xml - - 1 - - - - basic - *.do - - - - index.htm - - diff --git a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java b/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java deleted file mode 100644 index 9d2d4c22..00000000 --- a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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.article.dao; - -import java.util.List; - -import org.springframework.ldap.NameNotFoundException; -import org.springframework.ldap.samples.article.dao.PersonDao; -import org.springframework.ldap.samples.article.domain.Person; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -/** - * Abstract base class for PersonDao integration tests. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public abstract class AbstractPersonDaoIntegrationTest - extends - AbstractDependencyInjectionSpringContextTests { - - protected Person person; - - protected PersonDao personDao; - - protected String[] getConfigLocations() { - return new String[] { "config/testContext.xml" }; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - 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"); - } - - protected void onTearDown() throws Exception { - super.onTearDown(); - person = null; - personDao = null; - } - - /** - * 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. - */ - 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 - } - } - } - - public void testGetAllPersonNames() { - List result = personDao.getAllPersonNames(); - assertEquals(2, result.size()); - String first = (String) result.get(0); - assertEquals("Some Person", first); - } - - public void testFindAll() { - List result = personDao.findAll(); - assertEquals(2, result.size()); - Person first = (Person) result.get(0); - assertEquals("Some Person", first - .getFullName()); - } - - public void testFindByPrimaryKey() { - Person result = personDao.findByPrimaryKey( - "Sweden", "company1", "Some Person"); - assertEquals(person, result); - } -} diff --git a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java b/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java deleted file mode 100644 index 31362c4f..00000000 --- a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.article.dao; - -import org.springframework.ldap.samples.article.dao.PersonDaoImpl; - -/** - * Integration tests for the PersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImplIntegrationTest extends - AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - PersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java b/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java deleted file mode 100644 index 856f2272..00000000 --- a/samples/article-spring20/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.article.dao; - -/** - * Integration tests for the TraditionalPersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImplIntegrationTest - extends AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - TraditionalPersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article-spring20/src/test/resources/config/ldap.properties b/samples/article-spring20/src/test/resources/config/ldap.properties deleted file mode 100644 index b635ac60..00000000 --- a/samples/article-spring20/src/test/resources/config/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18881 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/article-spring20/src/test/resources/config/testContext.xml b/samples/article-spring20/src/test/resources/config/testContext.xml deleted file mode 100644 index 2a095c85..00000000 --- a/samples/article-spring20/src/test/resources/config/testContext.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/article-spring30/.gitignore b/samples/article-spring30/.gitignore deleted file mode 100644 index 73df60d8..00000000 --- a/samples/article-spring30/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -.classpath -.project -.settings diff --git a/samples/article-spring30/.springBeans b/samples/article-spring30/.springBeans deleted file mode 100644 index 4a47426b..00000000 --- a/samples/article-spring30/.springBeans +++ /dev/null @@ -1,13 +0,0 @@ - - - 1 - - - - - - - - - - diff --git a/samples/article-spring30/build.gradle b/samples/article-spring30/build.gradle deleted file mode 100644 index c8f8cc73..00000000 --- a/samples/article-spring30/build.gradle +++ /dev/null @@ -1,23 +0,0 @@ -apply from: JAVA_SCRIPT -apply plugin: 'war' -apply plugin: 'jetty' - -dependencies { - compile project(':spring-ldap-test'), - project(':spring-ldap-samples-utils'), - 'log4j:log4j:1.2.9', - 'javax.servlet:jstl:1.2', - "org.springframework:spring-beans:$springVersion", - "org.springframework:spring-core:$springVersion", - "org.springframework:spring-tx:$springVersion", - "org.springframework:spring-jdbc:$springVersion", - "org.springframework:spring-context:$springVersion", - "org.springframework:spring-web:$springVersion", - "org.springframework:spring-webmvc:$springVersion", - "org.springframework:spring-aop:$springVersion" - - provided "javax.servlet:servlet-api:2.5" - - testCompile "org.springframework:spring-test:$springVersion", - "junit:junit:$junitVersion" -} \ No newline at end of file diff --git a/samples/article-spring30/readme.txt b/samples/article-spring30/readme.txt deleted file mode 100644 index 0a2ccee1..00000000 --- a/samples/article-spring30/readme.txt +++ /dev/null @@ -1,21 +0,0 @@ -Uses Spring 3.0. -Sample application demonstrating how to do the most basic stuff in Spring LDAP. -A very simple dao implementation is provided in -org.springframework.ldap.samples.article.dao.PersonDaoImpl -It demonstrates some basic operations using Spring LDAP. For reference purposes, -a corresponding implementation using ordinary Java LDAP/JNDI implementation is -available in TraditionalPersonDaoImpl. - -How to use: ------------ -The project is in a Maven build structure. Make sure you have installed the samples-utils artifact, as this will be -needed for this project to work. - -'mvn jetty:run' will start up a web server demonstrating the capabilities. The web application will be available -under http://localhost:8080/spring-ldap-person-article-spring30/ - -'mvn eclipse:eclipse' will construct an Eclipse project for you to use. Import that project into Eclipse using -File/Import/Existing Project, and select this directory. - -'mvn test' will run some integration tests that require the LDAP server to be running. It's recommended to run -'mvn jetty:run' from another terminal window before 'mvn test'. diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java deleted file mode 100644 index a8fd6f84..00000000 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.article.dao; - -import java.util.List; - -import org.springframework.ldap.samples.article.domain.Person; - - -/** - * 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 getAllPersonNames(); - - List findAll(); - - Person findByPrimaryKey(String country, - String company, String fullname); -} diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java deleted file mode 100644 index 771c64b8..00000000 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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.article.dao; - -import org.springframework.ldap.core.AttributesMapper; -import org.springframework.ldap.core.ContextMapper; -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.filter.EqualsFilter; -import org.springframework.ldap.samples.article.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 java.util.List; - -/** - * Default implementation of PersonDao. This implementation uses - * DirContextAdapter for managing attribute values. It has been specified in the - * Spring Context that the DirObjectFactory should be used when creating objects - * from contexts, which defaults to creating DirContextAdapter objects. This - * means that we can 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; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - Name dn = buildDn(person); - DirContextAdapter context = new DirContextAdapter(dn); - mapToContext(person, context); - ldapTemplate.bind(dn, context, null); - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - Name dn = buildDn(person); - DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn); - mapToContext(person, context); - ldapTemplate.modifyAttributes(dn, context.getModificationItems()); - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - ldapTemplate.unbind(buildDn(person)); - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { - public Object mapFromAttributes(Attributes attrs) throws NamingException { - return attrs.get("cn").get(); - } - }); - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, String fullname) { - Name dn = buildDn(country, company, fullname); - return (Person) ldapTemplate.lookup(dn, getContextMapper()); - } - - private ContextMapper getContextMapper() { - return new PersonContextMapper(); - } - - private Name buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person.getFullName()); - } - - private Name 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 cn=[fullname],ou=[company],c=[country], so - * the values of these attributes must be extracted from the DN. For this, - * we use LdapName and utility methods in LdapUtils. - * - *Mattias Hellborg Arthurssonellborg Arthursson - * @author Ulrik Sandberg - */ - private static class PersonContextMapper implements ContextMapper { - - public Object mapFromContext(Object ctx) { - DirContextAdapter context = (DirContextAdapter) ctx; - Name dn = context.getDn(); - Person person = new Person(); - 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; - } -} diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java deleted file mode 100644 index e45403f9..00000000 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * 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.article.dao; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.samples.article.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to - * contrast this implementation with that of {@link PersonDaoImpl}. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImpl implements - PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx - .rebind( - dn, null, - getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - Attributes attributes = searchResult - .getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult - .getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, - String company, String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn( - country, company, fullname); - try { - Attributes attributes = ctx - .getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException( - "Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person - .getCompany(), person.getFullName()); - } - - private String buildDn(String country, String company, - String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put( - Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable env = new Hashtable(); - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put( - Context.SECURITY_AUTHENTICATION, - "simple"); - env.put( - Context.SECURITY_PRINCIPAL, userName); - env.put( - Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind( - Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute( - "objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person - .getDescription()); - attrs.put("telephoneNumber", person - .getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, - Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get( - "cn").get()); - person.setLastName((String) attributes.get( - "sn").get()); - person.setDescription((String) attributes - .get("description").get()); - person.setPhone((String) attributes.get( - "telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn - .replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn - .lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn - .lastIndexOf(companyMarker); - - String country = cleanedDn - .substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring( - companyIndex + companyMarker.length(), - countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/domain/Person.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/domain/Person.java deleted file mode 100644 index d0dc998c..00000000 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/domain/Person.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.article.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); - } -} diff --git a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java deleted file mode 100644 index f45a3d75..00000000 --- a/samples/article-spring30/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ /dev/null @@ -1,130 +0,0 @@ -package org.springframework.ldap.samples.article.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.article.dao.PersonDao; -import org.springframework.ldap.samples.article.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 ModelAndView addPerson() { - Person person = getPerson(); - - personDao.create(person); - 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(); - - 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"); - 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; - } - } - -} diff --git a/samples/article-spring30/src/main/java/overview.html b/samples/article-spring30/src/main/java/overview.html deleted file mode 100644 index 309641cf..00000000 --- a/samples/article-spring30/src/main/java/overview.html +++ /dev/null @@ -1,3 +0,0 @@ - -This document is the API specification for the Spring LDAP Article sample. - \ No newline at end of file diff --git a/samples/article-spring30/src/main/resources/log4j.properties b/samples/article-spring30/src/main/resources/log4j.properties deleted file mode 100644 index 5ce7a3ee..00000000 --- a/samples/article-spring30/src/main/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -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 \ No newline at end of file diff --git a/samples/article-spring30/src/main/webapp/WEB-INF/applicationContext.xml b/samples/article-spring30/src/main/webapp/WEB-INF/applicationContext.xml deleted file mode 100644 index 99bd0f62..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/applicationContext.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - -z - - - - - - - - - - - - - diff --git a/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showPerson.jsp b/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showPerson.jsp deleted file mode 100755 index 4d25b21b..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showPerson.jsp +++ /dev/null @@ -1,20 +0,0 @@ -<%@ 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/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showTree.jsp b/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showTree.jsp deleted file mode 100644 index 80546b96..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/jsp/showTree.jsp +++ /dev/null @@ -1,17 +0,0 @@ -<%@ 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/samples/article-spring30/src/main/webapp/WEB-INF/ldap.properties b/samples/article-spring30/src/main/webapp/WEB-INF/ldap.properties deleted file mode 100644 index 0b55d000..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18882 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/article-spring30/src/main/webapp/WEB-INF/setup_data.ldif b/samples/article-spring30/src/main/webapp/WEB-INF/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/article-spring30/src/main/webapp/WEB-INF/web.xml b/samples/article-spring30/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 3482f7a9..00000000 --- a/samples/article-spring30/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - Spring LDAP Basic Example - - - org.springframework.web.context.ContextLoaderListener - - - - - basic - - org.springframework.web.servlet.DispatcherServlet - - - contextConfigLocation - /WEB-INF/basic-servlet.xml - - 1 - - - - basic - *.do - - - - index.htm - - diff --git a/samples/article-spring30/src/main/webapp/index.htm b/samples/article-spring30/src/main/webapp/index.htm deleted file mode 100644 index c4a3f6f3..00000000 --- a/samples/article-spring30/src/main/webapp/index.htm +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java b/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java deleted file mode 100644 index 9d2d4c22..00000000 --- a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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.article.dao; - -import java.util.List; - -import org.springframework.ldap.NameNotFoundException; -import org.springframework.ldap.samples.article.dao.PersonDao; -import org.springframework.ldap.samples.article.domain.Person; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -/** - * Abstract base class for PersonDao integration tests. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public abstract class AbstractPersonDaoIntegrationTest - extends - AbstractDependencyInjectionSpringContextTests { - - protected Person person; - - protected PersonDao personDao; - - protected String[] getConfigLocations() { - return new String[] { "config/testContext.xml" }; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - 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"); - } - - protected void onTearDown() throws Exception { - super.onTearDown(); - person = null; - personDao = null; - } - - /** - * 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. - */ - 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 - } - } - } - - public void testGetAllPersonNames() { - List result = personDao.getAllPersonNames(); - assertEquals(2, result.size()); - String first = (String) result.get(0); - assertEquals("Some Person", first); - } - - public void testFindAll() { - List result = personDao.findAll(); - assertEquals(2, result.size()); - Person first = (Person) result.get(0); - assertEquals("Some Person", first - .getFullName()); - } - - public void testFindByPrimaryKey() { - Person result = personDao.findByPrimaryKey( - "Sweden", "company1", "Some Person"); - assertEquals(person, result); - } -} diff --git a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java b/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java deleted file mode 100644 index 31362c4f..00000000 --- a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.article.dao; - -import org.springframework.ldap.samples.article.dao.PersonDaoImpl; - -/** - * Integration tests for the PersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImplIntegrationTest extends - AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - PersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java b/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java deleted file mode 100644 index 856f2272..00000000 --- a/samples/article-spring30/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.article.dao; - -/** - * Integration tests for the TraditionalPersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImplIntegrationTest - extends AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - TraditionalPersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article-spring30/src/test/resources/config/ldap.properties b/samples/article-spring30/src/test/resources/config/ldap.properties deleted file mode 100644 index 0b55d000..00000000 --- a/samples/article-spring30/src/test/resources/config/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18882 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/article-spring30/src/test/resources/config/testContext.xml b/samples/article-spring30/src/test/resources/config/testContext.xml deleted file mode 100644 index 5be8becf..00000000 --- a/samples/article-spring30/src/test/resources/config/testContext.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/article-spring30/src/test/resources/setup_data.ldif b/samples/article-spring30/src/test/resources/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/article-spring30/src/test/resources/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/article/.gitignore b/samples/article/.gitignore deleted file mode 100644 index 73df60d8..00000000 --- a/samples/article/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -.classpath -.project -.settings diff --git a/samples/article/.springBeans b/samples/article/.springBeans deleted file mode 100644 index 4a47426b..00000000 --- a/samples/article/.springBeans +++ /dev/null @@ -1,13 +0,0 @@ - - - 1 - - - - - - - - - - diff --git a/samples/article/readme.txt b/samples/article/readme.txt deleted file mode 100644 index ce205f2f..00000000 --- a/samples/article/readme.txt +++ /dev/null @@ -1,20 +0,0 @@ -Sample application demonstrating how to do the most basic stuff in Spring LDAP. -A very simple dao implementation is provided in -org.springframework.ldap.samples.article.dao.PersonDaoImpl -It demonstrates some basic operations using Spring LDAP. For reference purposes, -a corresponding implementation using ordinary Java LDAP/JNDI implementation is -available in TraditionalPersonDaoImpl. - -How to use: ------------ -The project is in a Maven build structure. Make sure you have installed the samples-utils artifact, as this will be -needed for this project to work. - -'mvn jetty:run' will start up a web server demonstrating the capabilities. The web application will be available -under http://localhost:8080/spring-ldap-person-article/ - -'mvn eclipse:eclipse' will construct an Eclipse project for you to use. Import that project into Eclipse using -File/Import/Existing Project, and select this directory. - -'mvn test' will run some integration tests that require the LDAP server to be running. It's recommended to run -'mvn jetty:run' from another terminal window before 'mvn test'. \ No newline at end of file diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java b/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java deleted file mode 100644 index a8fd6f84..00000000 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.article.dao; - -import java.util.List; - -import org.springframework.ldap.samples.article.domain.Person; - - -/** - * 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 getAllPersonNames(); - - List findAll(); - - Person findByPrimaryKey(String country, - String company, String fullname); -} diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java b/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java deleted file mode 100644 index e45403f9..00000000 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImpl.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * 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.article.dao; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.samples.article.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to - * contrast this implementation with that of {@link PersonDaoImpl}. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImpl implements - PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx - .rebind( - dn, null, - getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - Attributes attributes = searchResult - .getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls - .setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search( - "", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results - .next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult - .getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, - String company, String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn( - country, company, fullname); - try { - Attributes attributes = ctx - .getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException( - "Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person - .getCompany(), person.getFullName()); - } - - private String buildDn(String country, String company, - String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put( - Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable env = new Hashtable(); - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put( - Context.SECURITY_AUTHENTICATION, - "simple"); - env.put( - Context.SECURITY_PRINCIPAL, userName); - env.put( - Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind( - Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute( - "objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person - .getDescription()); - attrs.put("telephoneNumber", person - .getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, - Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get( - "cn").get()); - person.setLastName((String) attributes.get( - "sn").get()); - person.setDescription((String) attributes - .get("description").get()); - person.setPhone((String) attributes.get( - "telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn - .replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn - .lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn - .lastIndexOf(companyMarker); - - String country = cleanedDn - .substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring( - companyIndex + companyMarker.length(), - countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/domain/Person.java b/samples/article/src/main/java/org/springframework/ldap/samples/article/domain/Person.java deleted file mode 100644 index d0dc998c..00000000 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/domain/Person.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.article.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); - } -} diff --git a/samples/article/src/main/java/overview.html b/samples/article/src/main/java/overview.html deleted file mode 100644 index 309641cf..00000000 --- a/samples/article/src/main/java/overview.html +++ /dev/null @@ -1,3 +0,0 @@ - -This document is the API specification for the Spring LDAP Article sample. - \ No newline at end of file diff --git a/samples/article/src/main/resources/log4j.properties b/samples/article/src/main/resources/log4j.properties deleted file mode 100644 index 5ce7a3ee..00000000 --- a/samples/article/src/main/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -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 \ No newline at end of file diff --git a/samples/article/src/main/webapp/WEB-INF/applicationContext.xml b/samples/article/src/main/webapp/WEB-INF/applicationContext.xml deleted file mode 100644 index 1de82441..00000000 --- a/samples/article/src/main/webapp/WEB-INF/applicationContext.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/article/src/main/webapp/WEB-INF/basic-servlet.xml b/samples/article/src/main/webapp/WEB-INF/basic-servlet.xml deleted file mode 100644 index e3b71874..00000000 --- a/samples/article/src/main/webapp/WEB-INF/basic-servlet.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - diff --git a/samples/article/src/main/webapp/WEB-INF/jsp/showPerson.jsp b/samples/article/src/main/webapp/WEB-INF/jsp/showPerson.jsp deleted file mode 100755 index 4d25b21b..00000000 --- a/samples/article/src/main/webapp/WEB-INF/jsp/showPerson.jsp +++ /dev/null @@ -1,20 +0,0 @@ -<%@ 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/samples/article/src/main/webapp/WEB-INF/jsp/showTree.jsp b/samples/article/src/main/webapp/WEB-INF/jsp/showTree.jsp deleted file mode 100644 index 80546b96..00000000 --- a/samples/article/src/main/webapp/WEB-INF/jsp/showTree.jsp +++ /dev/null @@ -1,17 +0,0 @@ -<%@ 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/samples/article/src/main/webapp/WEB-INF/setup_data.ldif b/samples/article/src/main/webapp/WEB-INF/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/article/src/main/webapp/WEB-INF/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/article/src/main/webapp/index.htm b/samples/article/src/main/webapp/index.htm deleted file mode 100644 index c4a3f6f3..00000000 --- a/samples/article/src/main/webapp/index.htm +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java b/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java deleted file mode 100644 index 9d2d4c22..00000000 --- a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/AbstractPersonDaoIntegrationTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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.article.dao; - -import java.util.List; - -import org.springframework.ldap.NameNotFoundException; -import org.springframework.ldap.samples.article.dao.PersonDao; -import org.springframework.ldap.samples.article.domain.Person; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -/** - * Abstract base class for PersonDao integration tests. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public abstract class AbstractPersonDaoIntegrationTest - extends - AbstractDependencyInjectionSpringContextTests { - - protected Person person; - - protected PersonDao personDao; - - protected String[] getConfigLocations() { - return new String[] { "config/testContext.xml" }; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - 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"); - } - - protected void onTearDown() throws Exception { - super.onTearDown(); - person = null; - personDao = null; - } - - /** - * 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. - */ - 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 - } - } - } - - public void testGetAllPersonNames() { - List result = personDao.getAllPersonNames(); - assertEquals(2, result.size()); - String first = (String) result.get(0); - assertEquals("Some Person", first); - } - - public void testFindAll() { - List result = personDao.findAll(); - assertEquals(2, result.size()); - Person first = (Person) result.get(0); - assertEquals("Some Person", first - .getFullName()); - } - - public void testFindByPrimaryKey() { - Person result = personDao.findByPrimaryKey( - "Sweden", "company1", "Some Person"); - assertEquals(person, result); - } -} diff --git a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java b/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java deleted file mode 100644 index 31362c4f..00000000 --- a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/PersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.article.dao; - -import org.springframework.ldap.samples.article.dao.PersonDaoImpl; - -/** - * Integration tests for the PersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImplIntegrationTest extends - AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - PersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java b/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java deleted file mode 100644 index 856f2272..00000000 --- a/samples/article/src/test/java/org/springframework/ldap/samples/article/dao/TraditionalPersonDaoImplIntegrationTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.article.dao; - -/** - * Integration tests for the TraditionalPersonDaoImpl class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImplIntegrationTest - extends AbstractPersonDaoIntegrationTest { - - public void setPersonDao( - TraditionalPersonDaoImpl personDao) { - this.personDao = personDao; - } -} diff --git a/samples/article/src/test/resources/setup_data.ldif b/samples/article/src/test/resources/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/article/src/test/resources/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/demos/demo-tiger/.gitignore b/samples/demos/demo-tiger/.gitignore deleted file mode 100644 index 73df60d8..00000000 --- a/samples/demos/demo-tiger/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -.classpath -.project -.settings diff --git a/samples/demos/demo-tiger/.springBeans b/samples/demos/demo-tiger/.springBeans deleted file mode 100644 index b5e50207..00000000 --- a/samples/demos/demo-tiger/.springBeans +++ /dev/null @@ -1,21 +0,0 @@ - - - 1 - - - - - - - src/test/resources/config/testContext.xml - - - - - true - false - - - - - diff --git a/samples/demos/demo-tiger/build.gradle b/samples/demos/demo-tiger/build.gradle deleted file mode 100644 index 9c304490..00000000 --- a/samples/demos/demo-tiger/build.gradle +++ /dev/null @@ -1,12 +0,0 @@ -apply from: JAVA_SCRIPT - -description = "Example code for demonstrating the process of refactoring from plain JNDI to Spring LDAP (Java 5)." - -dependencies { - compile project(':spring-ldap-test'), - project(':spring-ldap-core-tiger'), - "org.springframework:spring-context:$springVersion" - - testCompile "org.springframework:spring-test:$springVersion", - "junit:junit:$junitVersion" -} \ No newline at end of file diff --git a/samples/demos/demo-tiger/readme.txt b/samples/demos/demo-tiger/readme.txt deleted file mode 100644 index 01edc5a6..00000000 --- a/samples/demos/demo-tiger/readme.txt +++ /dev/null @@ -1,18 +0,0 @@ -Demo application to be used for demonstrating how to convert a legacy JNDI-based -dao implementation written in Java 1.4 to instead use Spring LDAP. For reference -purposes, a corresponding implementation using ordinary Java LDAP/JNDI -implementation is available in TraditionalPersonDaoImpl. - -How to use: ------------ -'mvn test' will start up an LDAP server before running the integration tests that -verify the dao implementation. - -'mvn eclipse:eclipse' will construct an Eclipse project for you to use. Import -that project into Eclipse using File/Import/Existing Project, and select this -directory. - -You can start converting the org.springframework.ldap.demo.dao.PersonDaoImpl class. -The original traditional implementation, as well as a "solution", is available in -the org.springframework.ldap.demo.solution package. Run the tests after you have -converted a method. diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java deleted file mode 100644 index d2a7a60b..00000000 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.demo.dao; - -import java.util.List; - -import org.springframework.ldap.demo.domain.Person; - -/** - * 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 getAllPersonNames(); - - List findAll(); - - Person findByPrimaryKey(String country, String company, String fullname); -} diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java deleted file mode 100644 index ab0049f4..00000000 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java +++ /dev/null @@ -1,344 +0,0 @@ -/* - * 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.demo.dao; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.demo.dao.PersonDao; -import org.springframework.ldap.demo.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to contrast - * this implementation with an implementation based on Spring LDAP. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImpl implements PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.rebind(dn, null, getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - Attributes attributes = searchResult.getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult.getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn(country, company, fullname); - try { - Attributes attributes = ctx.getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException("Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private String buildDn(String country, String company, String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put(Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable hashtable = new Hashtable(); - Hashtable env = hashtable; - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); - env.put(Context.SECURITY_PRINCIPAL, userName); - env.put(Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind(Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person.getDescription()); - attrs.put("telephoneNumber", person.getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get("cn").get()); - person.setLastName((String) attributes.get("sn").get()); - person.setDescription((String) attributes.get("description").get()); - person.setPhone((String) attributes.get("telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn.replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn.lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn.lastIndexOf(companyMarker); - - String country = cleanedDn.substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring(companyIndex - + companyMarker.length(), countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/domain/Person.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/domain/Person.java deleted file mode 100644 index 50e6776c..00000000 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/domain/Person.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.demo.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); - } -} diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java deleted file mode 100644 index b64ed163..00000000 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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.demo.solution; - -import org.springframework.ldap.core.DirContextAdapter; -import org.springframework.ldap.core.DirContextOperations; -import org.springframework.ldap.core.simple.AbstractParameterizedContextMapper; -import org.springframework.ldap.core.simple.SimpleLdapTemplate; -import org.springframework.ldap.demo.dao.PersonDao; -import org.springframework.ldap.demo.domain.Person; -import org.springframework.ldap.support.LdapNameBuilder; -import org.springframework.ldap.support.LdapUtils; - -import javax.naming.Name; -import java.util.List; - -/** - * Spring LDAP implementation of PersonDao. This implementation uses many Spring - * LDAP features, such as the {@link DirContextAdapter}, - * {@link AbstractParameterizedContextMapper}, and {@link SimpleLdapTemplate}. The purpose is to - * contrast this implementation with that of {@link TraditionalPersonDaoImpl}. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImpl implements PersonDao { - - private static final class PersonContextMapper extends - AbstractParameterizedContextMapper { - @Override - protected Person doMapFromContext(DirContextOperations ctx) { - Person person = new Person(); - person.setFullName(ctx.getStringAttribute("cn")); - person.setLastName(ctx.getStringAttribute("sn")); - person.setDescription(ctx.getStringAttribute("description")); - person.setPhone(ctx.getStringAttribute("telephoneNumber")); - - Name dn = ctx.getDn(); - person.setCountry(LdapUtils.getStringValue(dn, "c")); - person.setCompany(LdapUtils.getStringValue(dn, "ou")); - return person; - } - } - - private SimpleLdapTemplate ldapTemplate; - - public void setLdapTemplate(SimpleLdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContextOperations ctx = new DirContextAdapter(buildDn(person)); - mapToContext(person, ctx); - ldapTemplate.bind(ctx); - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContextOperations ctx = ldapTemplate.lookupContext(buildDn(person)); - mapToContext(person, ctx); - ldapTemplate.modifyAttributes(ctx); - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - ldapTemplate.unbind(buildDn(person)); - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - return ldapTemplate.search("", "(objectclass=person)", - new AbstractParameterizedContextMapper() { - @Override - protected String doMapFromContext(DirContextOperations ctx) { - return ctx.getStringAttribute("cn"); - } - }); - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - return ldapTemplate.search("", "(objectclass=person)", - new PersonContextMapper()); - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - Name dn = buildDn(country, company, fullname); - return (Person) ldapTemplate.lookup(dn, new PersonContextMapper()); - } - - private Name buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private Name 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, DirContextOperations ctx) { - ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); - ctx.setAttributeValue("cn", person.getFullName()); - ctx.setAttributeValue("sn", person.getLastName()); - ctx.setAttributeValue("description", person.getDescription()); - ctx.setAttributeValue("telephoneNumber", person.getPhone()); - } -} diff --git a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java b/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java deleted file mode 100644 index 8905bc84..00000000 --- a/samples/demos/demo-tiger/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java +++ /dev/null @@ -1,344 +0,0 @@ -/* - * 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.demo.solution; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.demo.dao.PersonDao; -import org.springframework.ldap.demo.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to contrast - * this implementation with an implementation based on Spring LDAP. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImpl implements PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.rebind(dn, null, getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - Attributes attributes = searchResult.getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult.getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn(country, company, fullname); - try { - Attributes attributes = ctx.getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException("Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private String buildDn(String country, String company, String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put(Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable hashtable = new Hashtable(); - Hashtable env = hashtable; - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); - env.put(Context.SECURITY_PRINCIPAL, userName); - env.put(Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind(Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person.getDescription()); - attrs.put("telephoneNumber", person.getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get("cn").get()); - person.setLastName((String) attributes.get("sn").get()); - person.setDescription((String) attributes.get("description").get()); - person.setPhone((String) attributes.get("telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn.replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn.lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn.lastIndexOf(companyMarker); - - String country = cleanedDn.substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring(companyIndex - + companyMarker.length(), countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/demos/demo-tiger/src/main/java/overview.html b/samples/demos/demo-tiger/src/main/java/overview.html deleted file mode 100644 index 309641cf..00000000 --- a/samples/demos/demo-tiger/src/main/java/overview.html +++ /dev/null @@ -1,3 +0,0 @@ - -This document is the API specification for the Spring LDAP Article sample. - \ No newline at end of file diff --git a/samples/demos/demo-tiger/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java b/samples/demos/demo-tiger/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java deleted file mode 100644 index 4449d197..00000000 --- a/samples/demos/demo-tiger/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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.demo.dao; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.ldap.NameNotFoundException; -import org.springframework.ldap.demo.domain.Person; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; - -/** - * Integration tests for the PersonDao class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -@ContextConfiguration(locations = { "/config/testContext.xml" }) -public class PersonDaoIntegrationTest extends AbstractJUnit4SpringContextTests { - - private Person person; - - @Autowired - private PersonDao personDao; - - @Before - public void setUp() 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"); - } - - @After - public void tearDown() throws Exception { - person = null; - personDao = null; - } - - /** - * 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. - */ - @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); - } -} diff --git a/samples/demos/demo-tiger/src/test/resources/config/ldap.properties b/samples/demos/demo-tiger/src/test/resources/config/ldap.properties deleted file mode 100644 index a8475efd..00000000 --- a/samples/demos/demo-tiger/src/test/resources/config/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18884 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/demos/demo-tiger/src/test/resources/config/setup_data.ldif b/samples/demos/demo-tiger/src/test/resources/config/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/demos/demo-tiger/src/test/resources/config/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/demos/demo-tiger/src/test/resources/config/testContext.xml b/samples/demos/demo-tiger/src/test/resources/config/testContext.xml deleted file mode 100644 index f68de5ee..00000000 --- a/samples/demos/demo-tiger/src/test/resources/config/testContext.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/demos/demo-tiger/src/test/resources/log4j.properties b/samples/demos/demo-tiger/src/test/resources/log4j.properties deleted file mode 100644 index 5ce7a3ee..00000000 --- a/samples/demos/demo-tiger/src/test/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -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 \ No newline at end of file diff --git a/samples/demos/demo/.gitignore b/samples/demos/demo/.gitignore deleted file mode 100644 index 73df60d8..00000000 --- a/samples/demos/demo/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -.classpath -.project -.settings diff --git a/samples/demos/demo/.springBeans b/samples/demos/demo/.springBeans deleted file mode 100644 index b5e50207..00000000 --- a/samples/demos/demo/.springBeans +++ /dev/null @@ -1,21 +0,0 @@ - - - 1 - - - - - - - src/test/resources/config/testContext.xml - - - - - true - false - - - - - diff --git a/samples/demos/demo/build.gradle b/samples/demos/demo/build.gradle deleted file mode 100644 index 3019d9e9..00000000 --- a/samples/demos/demo/build.gradle +++ /dev/null @@ -1,12 +0,0 @@ -apply from: JAVA_SCRIPT - -description = "Example code for demonstrating the process of refactoring from plain JNDI to Spring LDAP (Java 1.4)." - -dependencies { - compile project(':spring-ldap-test'), - project(':spring-ldap-core'), - "org.springframework:spring-context:$springVersion" - - testCompile "org.springframework:spring-test:$springVersion", - "junit:junit:$junitVersion" -} \ No newline at end of file diff --git a/samples/demos/demo/readme.txt b/samples/demos/demo/readme.txt deleted file mode 100644 index f76dee9f..00000000 --- a/samples/demos/demo/readme.txt +++ /dev/null @@ -1,18 +0,0 @@ -Demo application to be used for demonstrating how to convert a legacy JNDI-based -dao implementation written in Java 1.4 to use Spring LDAP, focussing on the Java5 -support in Spring LDAP. For reference purposes, a corresponding implementation -using ordinary Java LDAP/JNDI implementation is available in TraditionalPersonDaoImpl. - -How to use: ------------ -'mvn test' will start up an LDAP server before running the integration tests that -verify the dao implementation. - -'mvn eclipse:eclipse' will construct an Eclipse project for you to use. Import -that project into Eclipse using File/Import/Existing Project, and select this -directory. - -You can start converting the org.springframework.ldap.demo.dao.PersonDaoImpl class. -The original traditional implementation, as well as a "solution", is available in -the org.springframework.ldap.demo.solution package. Run the tests after you have -converted a method. diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java deleted file mode 100644 index 73f36479..00000000 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDao.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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.demo.dao; - -import java.util.List; - -import org.springframework.ldap.demo.domain.Person; - -/** - * 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 getAllPersonNames(); - - List findAll(); - - Person findByPrimaryKey(String country, String company, String fullname); -} diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java deleted file mode 100644 index c5f7e7a3..00000000 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/dao/PersonDaoImpl.java +++ /dev/null @@ -1,343 +0,0 @@ -/* - * 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.demo.dao; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.demo.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to contrast - * this implementation with an implementation based on Spring LDAP. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImpl implements PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.rebind(dn, null, getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - Attributes attributes = searchResult.getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult.getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn(country, company, fullname); - try { - Attributes attributes = ctx.getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException("Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private String buildDn(String country, String company, String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put(Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable hashtable = new Hashtable(); - Hashtable env = hashtable; - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); - env.put(Context.SECURITY_PRINCIPAL, userName); - env.put(Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind(Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person.getDescription()); - attrs.put("telephoneNumber", person.getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get("cn").get()); - person.setLastName((String) attributes.get("sn").get()); - person.setDescription((String) attributes.get("description").get()); - person.setPhone((String) attributes.get("telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn.replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn.lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn.lastIndexOf(companyMarker); - - String country = cleanedDn.substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring(companyIndex - + companyMarker.length(), countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java deleted file mode 100644 index c46e229a..00000000 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/PersonDaoImpl.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * 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.demo.solution; - -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.demo.dao.PersonDao; -import org.springframework.ldap.demo.domain.Person; -import org.springframework.ldap.support.LdapNameBuilder; -import org.springframework.ldap.support.LdapUtils; - -import javax.naming.Name; -import java.util.List; - -/** - * Spring LDAP implementation of PersonDao. This implementation uses many Spring - * LDAP features, such as the {@link DirContextAdapter}, - * {@link AbstractContextMapper}, and {@link LdapTemplate}. The purpose is to - * contrast this implementation with that of {@link TraditionalPersonDaoImpl}. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoImpl implements PersonDao { - - private static final class PersonContextMapper extends - AbstractContextMapper { - protected Object doMapFromContext(DirContextOperations ctx) { - Person person = new Person(); - person.setFullName(ctx.getStringAttribute("cn")); - person.setLastName(ctx.getStringAttribute("sn")); - person.setDescription(ctx.getStringAttribute("description")); - person.setPhone(ctx.getStringAttribute("telephoneNumber")); - - Name dn = ctx.getDn(); - person.setCountry(LdapUtils.getStringValue(dn, "c")); - person.setCompany(LdapUtils.getStringValue(dn, "ou")); - return person; - } - } - - private LdapTemplate ldapTemplate; - - public void setLdapTemplate(LdapTemplate ldapTemplate) { - this.ldapTemplate = ldapTemplate; - } - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContextOperations ctx = new DirContextAdapter(buildDn(person)); - mapToContext(person, ctx); - ldapTemplate.bind(ctx); - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContextOperations ctx = ldapTemplate.lookupContext(buildDn(person)); - mapToContext(person, ctx); - ldapTemplate.modifyAttributes(ctx); - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - ldapTemplate.unbind(buildDn(person)); - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - return ldapTemplate.search("", "(objectclass=person)", - new AbstractContextMapper() { - protected Object doMapFromContext(DirContextOperations ctx) { - return ctx.getStringAttribute("cn"); - } - }); - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - return ldapTemplate.search("", "(objectclass=person)", - new PersonContextMapper()); - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - Name dn = buildDn(country, company, fullname); - return (Person) ldapTemplate.lookup(dn, new PersonContextMapper()); - } - - private Name buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private Name 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, DirContextOperations ctx) { - ctx.setAttributeValues("objectclass", new String[] { "top", "person" }); - ctx.setAttributeValue("cn", person.getFullName()); - ctx.setAttributeValue("sn", person.getLastName()); - ctx.setAttributeValue("description", person.getDescription()); - ctx.setAttributeValue("telephoneNumber", person.getPhone()); - } -} diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java deleted file mode 100644 index a25c62f8..00000000 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/TraditionalPersonDaoImpl.java +++ /dev/null @@ -1,344 +0,0 @@ -/* - * 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.demo.solution; - -import java.util.Hashtable; -import java.util.LinkedList; -import java.util.List; - -import javax.naming.Context; -import javax.naming.NameNotFoundException; -import javax.naming.NamingEnumeration; -import javax.naming.NamingException; -import javax.naming.directory.Attribute; -import javax.naming.directory.Attributes; -import javax.naming.directory.BasicAttribute; -import javax.naming.directory.BasicAttributes; -import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; -import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; - -import org.apache.commons.lang.StringUtils; -import org.springframework.ldap.demo.dao.PersonDao; -import org.springframework.ldap.demo.domain.Person; - -/** - * Traditional implementation of PersonDao. This implementation uses the basic - * JNDI interfaces and classes {@link DirContext}, {@link Attributes}, - * {@link Attribute}, and {@link NamingEnumeration}. The purpose is to contrast - * this implementation with an implementation based on Spring LDAP. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class TraditionalPersonDaoImpl implements PersonDao { - - private String userName; - - private String password; - - private String url; - - private String base; - - /* - * @see PersonDao#create(Person) - */ - public void create(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - Attributes attrs = getAttributesToBind(person); - ctx.bind(dn, null, attrs); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#update(Person) - */ - public void update(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.rebind(dn, null, getAttributesToBind(person)); - } catch (NamingException e) { - - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#delete(Person) - */ - public void delete(Person person) { - DirContext ctx = createAuthenticatedContext(); - String dn = buildDn(person); - try { - ctx.unbind(dn); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - Attributes attributes = searchResult.getAttributes(); - Attribute attr = attributes.get("cn"); - String cn = (String) attr.get(); - list.add(cn); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findAll() - */ - public List findAll() { - DirContext ctx = createAnonymousContext(); - - LinkedList list = new LinkedList(); - NamingEnumeration results = null; - try { - SearchControls controls = new SearchControls(); - controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); - - while (results.hasMore()) { - SearchResult searchResult = (SearchResult) results.next(); - String dn = searchResult.getName(); - Attributes attributes = searchResult.getAttributes(); - list.add(mapToPerson(dn, attributes)); - } - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (results != null) { - try { - results.close(); - } catch (Exception e) { - // Never mind this. - } - } - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - return list; - } - - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ - public Person findByPrimaryKey(String country, String company, - String fullname) { - - DirContext ctx = createAnonymousContext(); - String dn = buildDn(country, company, fullname); - try { - Attributes attributes = ctx.getAttributes(dn); - return mapToPerson(dn, attributes); - } catch (NameNotFoundException e) { - throw new RuntimeException("Did not find entry with primary key '" - + dn + "'", e); - } catch (NamingException e) { - throw new RuntimeException(e); - } finally { - if (ctx != null) { - try { - ctx.close(); - } catch (Exception e) { - // Never mind this. - } - } - } - } - - private String buildDn(Person person) { - return buildDn(person.getCountry(), person.getCompany(), person - .getFullName()); - } - - private String buildDn(String country, String company, String fullname) { - StringBuffer sb = new StringBuffer(); - sb.append("cn="); - sb.append(fullname); - sb.append(", "); - sb.append("ou="); - sb.append(company); - sb.append(", "); - sb.append("c="); - sb.append(country); - String dn = sb.toString(); - return dn; - } - - private DirContext createContext(Hashtable env) { - env.put(Context.INITIAL_CONTEXT_FACTORY, - "com.sun.jndi.ldap.LdapCtxFactory"); - String tempUrl = createUrl(); - env.put(Context.PROVIDER_URL, tempUrl); - DirContext ctx; - try { - ctx = new InitialDirContext(env); - } catch (NamingException e) { - throw new RuntimeException(e); - } - return ctx; - } - - private DirContext createAnonymousContext() { - Hashtable hashtable = new Hashtable(); - Hashtable env = hashtable; - return createContext(env); - } - - private DirContext createAuthenticatedContext() { - Hashtable env = new Hashtable(); - env.put(Context.SECURITY_AUTHENTICATION, "simple"); - env.put(Context.SECURITY_PRINCIPAL, userName); - env.put(Context.SECURITY_CREDENTIALS, password); - return createContext(env); - } - - private Attributes getAttributesToBind(Person person) { - Attributes attrs = new BasicAttributes(); - BasicAttribute ocattr = new BasicAttribute("objectclass"); - ocattr.add("top"); - ocattr.add("person"); - attrs.put(ocattr); - attrs.put("cn", person.getFullName()); - attrs.put("sn", person.getLastName()); - attrs.put("description", person.getDescription()); - attrs.put("telephoneNumber", person.getPhone()); - return attrs; - } - - private Person mapToPerson(String dn, Attributes attributes) - throws NamingException { - Person person = new Person(); - person.setFullName((String) attributes.get("cn").get()); - person.setLastName((String) attributes.get("sn").get()); - person.setDescription((String) attributes.get("description").get()); - person.setPhone((String) attributes.get("telephoneNumber").get()); - - // Remove any trailing spaces after comma - String cleanedDn = dn.replaceAll(", *", ","); - - String countryMarker = ",c="; - int countryIndex = cleanedDn.lastIndexOf(countryMarker); - - String companyMarker = ",ou="; - int companyIndex = cleanedDn.lastIndexOf(companyMarker); - - String country = cleanedDn.substring(countryIndex - + countryMarker.length()); - person.setCountry(country); - String company = cleanedDn.substring(companyIndex - + companyMarker.length(), countryIndex); - person.setCompany(company); - return person; - } - - private String createUrl() { - String tempUrl = url; - if (!tempUrl.endsWith("/")) { - tempUrl += "/"; - } - if (StringUtils.isNotEmpty(base)) { - tempUrl += base; - } - return tempUrl; - } - - public void setUrl(String url) { - this.url = url; - } - - public void setBase(String base) { - this.base = base; - } - - public void setPassword(String credentials) { - this.password = credentials; - } - - public void setUserDn(String principal) { - this.userName = principal; - } -} diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/testContext.xml b/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/testContext.xml deleted file mode 100644 index 2fd6b3ff..00000000 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/solution/testContext.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/demos/demo/src/main/java/overview.html b/samples/demos/demo/src/main/java/overview.html deleted file mode 100644 index 309641cf..00000000 --- a/samples/demos/demo/src/main/java/overview.html +++ /dev/null @@ -1,3 +0,0 @@ - -This document is the API specification for the Spring LDAP Article sample. - \ No newline at end of file diff --git a/samples/demos/demo/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java b/samples/demos/demo/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java deleted file mode 100644 index 23e542bd..00000000 --- a/samples/demos/demo/src/test/java/org/springframework/ldap/demo/dao/PersonDaoIntegrationTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.demo.dao; - -import java.util.List; - -import org.springframework.ldap.NameNotFoundException; -import org.springframework.ldap.demo.domain.Person; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -/** - * Integration tests for the PersonDao class. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg - */ -public class PersonDaoIntegrationTest extends - AbstractDependencyInjectionSpringContextTests { - - private Person person; - - private PersonDao personDao; - - public void setPersonDao(PersonDao personDao) { - this.personDao = personDao; - } - - protected String[] getConfigLocations() { - return new String[] { "/config/testContext.xml" }; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - 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"); - } - - protected void onTearDown() throws Exception { - super.onTearDown(); - person = null; - personDao = null; - } - - /** - * 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. - */ - 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 - } - } - } - - public void testGetAllPersonNames() { - List result = personDao.getAllPersonNames(); - assertEquals(2, result.size()); - String first = (String) result.get(0); - assertEquals("Some Person", first); - } - - public void testFindAll() { - List result = personDao.findAll(); - assertEquals(2, result.size()); - Person first = (Person) result.get(0); - assertEquals("Some Person", first.getFullName()); - } - - public void testFindByPrimaryKey() { - Person result = personDao.findByPrimaryKey("Sweden", "company1", - "Some Person"); - assertEquals(person, result); - } -} diff --git a/samples/demos/demo/src/test/resources/config/ldap.properties b/samples/demos/demo/src/test/resources/config/ldap.properties deleted file mode 100644 index aa373cb1..00000000 --- a/samples/demos/demo/src/test/resources/config/ldap.properties +++ /dev/null @@ -1,4 +0,0 @@ -urls=ldap://127.0.0.1:18883 -userDn=uid=admin,ou=system -password=secret -base=dc=jayway,dc=se diff --git a/samples/demos/demo/src/test/resources/config/setup_data.ldif b/samples/demos/demo/src/test/resources/config/setup_data.ldif deleted file mode 100644 index e6d7dce6..00000000 --- a/samples/demos/demo/src/test/resources/config/setup_data.ldif +++ /dev/null @@ -1,35 +0,0 @@ -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 diff --git a/samples/demos/demo/src/test/resources/config/testContext.xml b/samples/demos/demo/src/test/resources/config/testContext.xml deleted file mode 100644 index 23a74dab..00000000 --- a/samples/demos/demo/src/test/resources/config/testContext.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/samples/demos/demo/src/test/resources/log4j.properties b/samples/demos/demo/src/test/resources/log4j.properties deleted file mode 100644 index 5ce7a3ee..00000000 --- a/samples/demos/demo/src/test/resources/log4j.properties +++ /dev/null @@ -1,7 +0,0 @@ -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 \ No newline at end of file diff --git a/samples/demos/readme.txt b/samples/demos/readme.txt deleted file mode 100644 index d0cb962c..00000000 --- a/samples/demos/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Demo applications for demonstrating conversion of legacy JNDI to Spring LDAP. diff --git a/samples/article/build.gradle b/samples/plain/build.gradle similarity index 100% rename from samples/article/build.gradle rename to samples/plain/build.gradle diff --git a/samples/plain/readme.txt b/samples/plain/readme.txt new file mode 100644 index 00000000..00f58319 --- /dev/null +++ b/samples/plain/readme.txt @@ -0,0 +1,9 @@ +Sample application demonstrating how to do the most basic stuff in Spring LDAP. +A very simple dao implementation is provided in org.springframework.ldap.samples.plain.dao.PersonDaoImpl +It demonstrates some basic operations using Spring LDAP. + +The core Spring application context of the sample is defined in resources/applicationContext.xml. +This ApplicationContext will start an in-process Apache Directory Server instance, automatically populated +with some test data. The data will be reset every time the application is restarted. + +To run the example, do 'gradle jettyRun', and then navigate to http://localhost:8080/spring-ldap-plain-sample diff --git a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDao.java similarity index 75% rename from samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java rename to samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDao.java index a8fd6f84..c828b876 100644 --- a/samples/article-spring20/src/main/java/org/springframework/ldap/samples/article/dao/PersonDao.java +++ b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDao.java @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.samples.article.dao; +package org.springframework.ldap.samples.plain.dao; + +import org.springframework.ldap.samples.plain.domain.Person; import java.util.List; -import org.springframework.ldap.samples.article.domain.Person; - /** * Data Access Object interface for the Person entity. @@ -33,10 +33,9 @@ public interface PersonDao { void delete(Person person); - List getAllPersonNames(); + List getAllPersonNames(); - List findAll(); + List findAll(); - Person findByPrimaryKey(String country, - String company, String fullname); + Person findByPrimaryKey(String country, String company, String fullname); } diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDaoImpl.java similarity index 71% rename from samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java rename to samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDaoImpl.java index 07937525..6703665d 100644 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/dao/PersonDaoImpl.java +++ b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/dao/PersonDaoImpl.java @@ -13,14 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.samples.article.dao; +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.article.domain.Person; +import org.springframework.ldap.samples.plain.domain.Person; import org.springframework.ldap.support.LdapNameBuilder; import org.springframework.ldap.support.LdapUtils; @@ -32,12 +34,9 @@ import java.util.List; /** * Default implementation of PersonDao. This implementation uses - * DirContextAdapter for managing attribute values. It has been specified in the - * Spring Context that the DirObjectFactory should be used when creating objects - * from contexts, which defaults to creating DirContextAdapter objects. This - * means that we can 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. + * 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 @@ -52,9 +51,7 @@ public class PersonDaoImpl implements PersonDao { private LdapTemplate ldapTemplate; - /* - * @see PersonDao#create(Person) - */ + @Override public void create(Person person) { Name dn = buildDn(person); DirContextAdapter context = new DirContextAdapter(dn); @@ -62,9 +59,7 @@ public class PersonDaoImpl implements PersonDao { ldapTemplate.bind(dn, context, null); } - /* - * @see PersonDao#update(Person) - */ + @Override public void update(Person person) { Name dn = buildDn(person); DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn); @@ -72,44 +67,31 @@ public class PersonDaoImpl implements PersonDao { ldapTemplate.modifyAttributes(dn, context.getModificationItems()); } - /* - * @see PersonDao#delete(Person) - */ + @Override public void delete(Person person) { ldapTemplate.unbind(buildDn(person)); } - /* - * @see PersonDao#getAllPersonNames() - */ - public List getAllPersonNames() { + @Override + public List getAllPersonNames() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { - public Object mapFromAttributes(Attributes attrs) throws NamingException { - return attrs.get("cn").get(); + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), new AttributesMapper() { + public String mapFromAttributes(Attributes attrs) throws NamingException { + return attrs.get("cn").get().toString(); } }); } - /* - * @see PersonDao#findAll() - */ - public List findAll() { + @Override + public List findAll() { EqualsFilter filter = new EqualsFilter("objectclass", "person"); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getContextMapper()); + return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), PERSON_CONTEXT_MAPPER); } - /* - * @see PersonDao#findByPrimaryKey(java.lang.String, java.lang.String, - * java.lang.String) - */ + @Override public Person findByPrimaryKey(String country, String company, String fullname) { LdapName dn = buildDn(country, company, fullname); - return (Person) ldapTemplate.lookup(dn, getContextMapper()); - } - - private ContextMapper getContextMapper() { - return new PersonContextMapper(); + return ldapTemplate.lookup(dn, PERSON_CONTEXT_MAPPER); } private LdapName buildDn(Person person) { @@ -137,16 +119,13 @@ public class PersonDaoImpl implements PersonDao { * of the form cn=[fullname],ou=[company],c=[country], so * the values of these attributes must be extracted from the DN. For this, * we use the LdapName along with utility methods in LdapUtils. - * - * @author Mattias Hellborg Arthursson - * @author Ulrik Sandberg */ - private static class PersonContextMapper implements ContextMapper { - - public Object mapFromContext(Object ctx) { - DirContextAdapter context = (DirContextAdapter) ctx; - LdapName dn = LdapUtils.newLdapName(context.getDn()); + private final static ContextMapper PERSON_CONTEXT_MAPPER = new AbstractContextMapper() { + @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")); @@ -156,7 +135,7 @@ public class PersonDaoImpl implements PersonDao { return person; } - } + }; public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; diff --git a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/domain/Person.java b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/domain/Person.java similarity index 93% rename from samples/demos/demo/src/main/java/org/springframework/ldap/demo/domain/Person.java rename to samples/plain/src/main/java/org/springframework/ldap/samples/plain/domain/Person.java index 50e6776c..60d3e93e 100644 --- a/samples/demos/demo/src/main/java/org/springframework/ldap/demo/domain/Person.java +++ b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/domain/Person.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.ldap.demo.domain; +package org.springframework.ldap.samples.plain.domain; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; diff --git a/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/web/DefaultController.java similarity index 90% rename from samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java rename to samples/plain/src/main/java/org/springframework/ldap/samples/plain/web/DefaultController.java index afe4b4e8..8aef5fbf 100644 --- a/samples/article/src/main/java/org/springframework/ldap/samples/article/web/DefaultController.java +++ b/samples/plain/src/main/java/org/springframework/ldap/samples/plain/web/DefaultController.java @@ -1,10 +1,10 @@ -package org.springframework.ldap.samples.article.web; +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.article.dao.PersonDao; -import org.springframework.ldap.samples.article.domain.Person; +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; @@ -45,28 +45,28 @@ public class DefaultController { } @RequestMapping("/addPerson.do") - public ModelAndView addPerson() { + public String addPerson() { Person person = getPerson(); personDao.create(person); - return showTree(); + return "redirect:/showTree.do"; } @RequestMapping("/updatePhoneNumber.do") - public ModelAndView updatePhoneNumber() { + public String updatePhoneNumber() { Person person = personDao.findByPrimaryKey("Sweden", "company1", "John Doe"); person.setPhone(StringUtils.join(new String[] { person.getPhone(), "0" })); personDao.update(person); - return showTree(); + return "redirect:/showTree.do"; } @RequestMapping("/removePerson.do") - public ModelAndView removePerson() { + public String removePerson() { Person person = getPerson(); personDao.delete(person); - return showTree(); + return "redirect:/showTree.do"; } @RequestMapping("/showPerson.do") diff --git a/samples/plain/src/main/java/overview.html b/samples/plain/src/main/java/overview.html new file mode 100644 index 00000000..d0a7abb8 --- /dev/null +++ b/samples/plain/src/main/java/overview.html @@ -0,0 +1,3 @@ + +Plain example of Spring LDAP usage. + \ No newline at end of file diff --git a/samples/plain/src/main/resources/applicationContext.xml b/samples/plain/src/main/resources/applicationContext.xml new file mode 100644 index 00000000..55561d29 --- /dev/null +++ b/samples/plain/src/main/resources/applicationContext.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/article/src/main/webapp/WEB-INF/ldap.properties b/samples/plain/src/main/resources/ldap.properties similarity index 100% rename from samples/article/src/main/webapp/WEB-INF/ldap.properties rename to samples/plain/src/main/resources/ldap.properties diff --git a/samples/article-spring20/src/main/resources/log4j.properties b/samples/plain/src/main/resources/log4j.properties similarity index 100% rename from samples/article-spring20/src/main/resources/log4j.properties rename to samples/plain/src/main/resources/log4j.properties diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/setup_data.ldif b/samples/plain/src/main/resources/setup_data.ldif similarity index 100% rename from samples/article-spring20/src/main/webapp/WEB-INF/setup_data.ldif rename to samples/plain/src/main/resources/setup_data.ldif diff --git a/samples/article-spring30/src/main/webapp/WEB-INF/basic-servlet.xml b/samples/plain/src/main/webapp/WEB-INF/basic-servlet.xml similarity index 90% rename from samples/article-spring30/src/main/webapp/WEB-INF/basic-servlet.xml rename to samples/plain/src/main/webapp/WEB-INF/basic-servlet.xml index e3b71874..31857dd7 100644 --- a/samples/article-spring30/src/main/webapp/WEB-INF/basic-servlet.xml +++ b/samples/plain/src/main/webapp/WEB-INF/basic-servlet.xml @@ -2,11 +2,11 @@ - + 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"> + + base-package="org.springframework.ldap.samples.plain.web" /> diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/jsp/showPerson.jsp b/samples/plain/src/main/webapp/WEB-INF/jsp/showPerson.jsp similarity index 100% rename from samples/article-spring20/src/main/webapp/WEB-INF/jsp/showPerson.jsp rename to samples/plain/src/main/webapp/WEB-INF/jsp/showPerson.jsp diff --git a/samples/article-spring20/src/main/webapp/WEB-INF/jsp/showTree.jsp b/samples/plain/src/main/webapp/WEB-INF/jsp/showTree.jsp similarity index 100% rename from samples/article-spring20/src/main/webapp/WEB-INF/jsp/showTree.jsp rename to samples/plain/src/main/webapp/WEB-INF/jsp/showTree.jsp diff --git a/samples/article/src/main/webapp/WEB-INF/web.xml b/samples/plain/src/main/webapp/WEB-INF/web.xml similarity index 84% rename from samples/article/src/main/webapp/WEB-INF/web.xml rename to samples/plain/src/main/webapp/WEB-INF/web.xml index 3482f7a9..1c7d6a7d 100644 --- a/samples/article/src/main/webapp/WEB-INF/web.xml +++ b/samples/plain/src/main/webapp/WEB-INF/web.xml @@ -11,7 +11,12 @@ - + + contextConfigLocation + classpath:/applicationContext.xml + + + basic org.springframework.web.servlet.DispatcherServlet diff --git a/samples/article-spring20/src/main/webapp/index.htm b/samples/plain/src/main/webapp/index.htm similarity index 100% rename from samples/article-spring20/src/main/webapp/index.htm rename to samples/plain/src/main/webapp/index.htm diff --git a/samples/plain/src/test/java/org/springframework/ldap/samples/plain/dao/PersonDaoSampleIntegrationTest.java b/samples/plain/src/test/java/org/springframework/ldap/samples/plain/dao/PersonDaoSampleIntegrationTest.java new file mode 100644 index 00000000..a8ef025e --- /dev/null +++ b/samples/plain/src/test/java/org/springframework/ldap/samples/plain/dao/PersonDaoSampleIntegrationTest.java @@ -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); + } +} diff --git a/samples/article/src/test/resources/config/ldap.properties b/samples/plain/src/test/resources/config/ldap.properties similarity index 100% rename from samples/article/src/test/resources/config/ldap.properties rename to samples/plain/src/test/resources/config/ldap.properties diff --git a/samples/article/src/test/resources/config/testContext.xml b/samples/plain/src/test/resources/config/testContext.xml similarity index 72% rename from samples/article/src/test/resources/config/testContext.xml rename to samples/plain/src/test/resources/config/testContext.xml index 0ee3cee6..568d9f9d 100644 --- a/samples/article/src/test/resources/config/testContext.xml +++ b/samples/plain/src/test/resources/config/testContext.xml @@ -23,15 +23,7 @@ + class="org.springframework.ldap.samples.plain.dao.PersonDaoImpl"> - - - - - - - \ No newline at end of file diff --git a/samples/article-spring20/src/test/resources/setup_data.ldif b/samples/plain/src/test/resources/setup_data.ldif similarity index 100% rename from samples/article-spring20/src/test/resources/setup_data.ldif rename to samples/plain/src/test/resources/setup_data.ldif diff --git a/samples/readme.txt b/samples/readme.txt deleted file mode 100644 index fdeeace2..00000000 --- a/samples/readme.txt +++ /dev/null @@ -1,9 +0,0 @@ -Sample application demonstrating the very basics in Spring LDAP -A basic data access class for manipulating a person entry is located at: -org.springframework.ldap.samples.article.dao.PersonDaoImpl -For comparison, a corresponding implementation using traditional JNDI programming is provided in: -org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl - -To verify the functionality, start the sample webapp using -mvn jetty:run -and navigate to http://localhost:8080/spring-ldap-person-article/showTree.do diff --git a/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java b/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java index 2ef112ce..87b1a80f 100644 --- a/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java +++ b/samples/samples-utils/src/main/java/org/springframework/ldap/samples/utils/LdapTreeBuilder.java @@ -23,7 +23,7 @@ public class LdapTreeBuilder { private LdapTree getLdapTree(final DirContextOperations rootContext) { final LdapTree ldapTree = new LdapTree(rootContext); ldapTemplate.listBindings(rootContext.getDn(), - new AbstractContextMapper() { + new AbstractContextMapper() { @Override protected Object doMapFromContext(DirContextOperations ctx) { Name dn = ctx.getDn(); diff --git a/settings.gradle b/settings.gradle index f6546645..544260b7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -14,11 +14,7 @@ include 'test/integration-tests-spring30' include 'test/integration-tests-openldap' include 'test/integration-tests-sunone' include 'test/integration-tests-ad' -include 'samples/article' -include 'samples/article-spring20' -include 'samples/article-spring30' -include 'samples/demos/demo' -include 'samples/demos/demo-tiger' +include 'samples/plain' include 'samples/samples-utils' include 'samples/simple-odm'