Add Spring LDAP Samples
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.domain;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
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;
|
||||
|
||||
import org.springframework.ldap.odm.annotations.Attribute;
|
||||
import org.springframework.ldap.odm.annotations.DnAttribute;
|
||||
import org.springframework.ldap.odm.annotations.Entry;
|
||||
import org.springframework.ldap.odm.annotations.Id;
|
||||
import org.springframework.ldap.odm.annotations.Transient;
|
||||
|
||||
/**
|
||||
* Simple class representing a single person.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
@Entry(objectClasses = { "inetOrgPerson", "organizationalPerson", "person", "top" })
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private Name dn;
|
||||
|
||||
@Attribute(name = "cn")
|
||||
@DnAttribute(value = "cn", index = 2)
|
||||
private String fullName;
|
||||
|
||||
@Attribute(name = "sn")
|
||||
private String lastName;
|
||||
|
||||
private String description;
|
||||
|
||||
@Transient
|
||||
@DnAttribute(value = "c", index = 0)
|
||||
private String country;
|
||||
|
||||
@Transient
|
||||
@DnAttribute(value = "ou", index = 1)
|
||||
private String company;
|
||||
|
||||
@Attribute(name = "telephoneNumber")
|
||||
private String phone;
|
||||
|
||||
public Person() {
|
||||
|
||||
}
|
||||
|
||||
public Person(String country, String company, String fullname) {
|
||||
this.country = country;
|
||||
this.company = company;
|
||||
this.fullName = fullname;
|
||||
}
|
||||
|
||||
public Name getDn() {
|
||||
return this.dn;
|
||||
}
|
||||
|
||||
public void setDn(Name dn) {
|
||||
this.dn = dn;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getFullName() {
|
||||
return this.fullName;
|
||||
}
|
||||
|
||||
public void setFullName(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return this.lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getCompany() {
|
||||
return this.company;
|
||||
}
|
||||
|
||||
public void setCompany(String company) {
|
||||
this.company = company;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return this.country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return this.phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return EqualsBuilder.reflectionEquals(this, obj);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return HashCodeBuilder.reflectionHashCode(this);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.ldap.repository.LdapRepository;
|
||||
import org.springframework.ldap.samples.odm.domain.Person;
|
||||
|
||||
/**
|
||||
* Data Access Object interface for the Person entity.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
public interface PersonRepository extends LdapRepository<Person>, PersonRepositoryExtension {
|
||||
|
||||
default List<String> getAllPersonNames() {
|
||||
return findAll().stream().map(Person::getFullName).toList();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.repository;
|
||||
|
||||
import org.springframework.ldap.samples.odm.domain.Person;
|
||||
|
||||
/**
|
||||
* An extension to {@link PersonRepository}, adding the ability to find a person by their
|
||||
* DN attributes.
|
||||
*
|
||||
* @author Josh Cummings
|
||||
*/
|
||||
public interface PersonRepositoryExtension {
|
||||
|
||||
Person findByPrimaryKey(String country, String company, String fullname);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.repository;
|
||||
|
||||
import org.springframework.LdapDataEntry;
|
||||
import org.springframework.ldap.core.ContextMapper;
|
||||
import org.springframework.ldap.core.LdapClient;
|
||||
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
|
||||
import org.springframework.ldap.samples.odm.domain.Person;
|
||||
|
||||
/**
|
||||
* An implementation of {@link PersonRepositoryExtension}.
|
||||
*
|
||||
* This extension uses ODM to calculate the DN so that it can be looked up.
|
||||
*
|
||||
* @author Josh Cummings
|
||||
*/
|
||||
public class PersonRepositoryExtensionImpl implements PersonRepositoryExtension {
|
||||
|
||||
private final LdapClient ldap;
|
||||
|
||||
private final ObjectDirectoryMapper odm;
|
||||
|
||||
public PersonRepositoryExtensionImpl(LdapClient ldap, ObjectDirectoryMapper odm) {
|
||||
this.ldap = ldap;
|
||||
this.odm = odm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person findByPrimaryKey(String country, String company, String fullname) {
|
||||
Person person = new Person(country, company, fullname);
|
||||
return this.ldap.search().name(this.odm.getCalculatedId(person)).toObject((ContextMapper<Person>) (ctx) -> {
|
||||
LdapDataEntry entry = (LdapDataEntry) ctx;
|
||||
return this.odm.mapFromLdapDataEntry(entry, Person.class);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.web;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.ldap.samples.odm.domain.Person;
|
||||
import org.springframework.ldap.samples.odm.repository.PersonRepository;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Default controller.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
@Controller
|
||||
public class DefaultController {
|
||||
|
||||
private final LdapTreeBuilder ldapTreeBuilder;
|
||||
|
||||
private final PersonRepository persons;
|
||||
|
||||
public DefaultController(LdapTreeBuilder ldapTreeBuilder, PersonRepository persons) {
|
||||
this.ldapTreeBuilder = ldapTreeBuilder;
|
||||
this.persons = persons;
|
||||
}
|
||||
|
||||
@RequestMapping("/welcome.do")
|
||||
public void welcomeHandler() {
|
||||
}
|
||||
|
||||
@RequestMapping("/showTree.do")
|
||||
public ModelAndView showTree() {
|
||||
LdapTree ldapTree = this.ldapTreeBuilder.getLdapTree(LdapUtils.emptyLdapName());
|
||||
HtmlRowLdapTreeVisitor visitor = new PersonLinkHtmlRowLdapTreeVisitor();
|
||||
ldapTree.traverse(visitor);
|
||||
return new ModelAndView("showTree", "rows", visitor.getRows());
|
||||
}
|
||||
|
||||
@RequestMapping("/addPerson.do")
|
||||
public String addPerson() {
|
||||
Person person = getPerson();
|
||||
|
||||
this.persons.save(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@RequestMapping("/updatePhoneNumber.do")
|
||||
public String updatePhoneNumber() {
|
||||
Person person = this.persons.findByPrimaryKey("Sweden", "company1", "John Doe");
|
||||
person.setPhone(StringUtils.join(new String[] { person.getPhone(), "0" }));
|
||||
|
||||
this.persons.save(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@RequestMapping("/removePerson.do")
|
||||
public String removePerson() {
|
||||
Person person = getPerson();
|
||||
|
||||
this.persons.delete(person);
|
||||
return "redirect:/showTree.do";
|
||||
}
|
||||
|
||||
@RequestMapping("/showPerson.do")
|
||||
public ModelMap showPerson(String country, String company, String fullName) {
|
||||
Person person = this.persons.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) {
|
||||
return URLEncoder.encode(value, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private boolean containsValue(String[] values, String value) {
|
||||
for (String oneValue : values) {
|
||||
if (StringUtils.equals(oneValue, value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.utils;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
|
||||
public class HtmlRowLdapTreeVisitor implements LdapTreeVisitor {
|
||||
|
||||
private final List<String> rows = new LinkedList<>();
|
||||
|
||||
public void visit(DirContextOperations node, int currentDepth) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < currentDepth; i++) {
|
||||
sb.append(" ");
|
||||
}
|
||||
|
||||
sb.append("<a href='")
|
||||
.append(getLinkForNode(node))
|
||||
.append("'>")
|
||||
.append(node.getDn())
|
||||
.append("</a>")
|
||||
.append("<br>\n");
|
||||
|
||||
this.rows.add(sb.toString());
|
||||
}
|
||||
|
||||
protected String getLinkForNode(DirContextOperations node) {
|
||||
return "#";
|
||||
}
|
||||
|
||||
public List<String> getRows() {
|
||||
return this.rows;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.utils;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
|
||||
public class LdapTree {
|
||||
|
||||
private final DirContextOperations node;
|
||||
|
||||
private List<LdapTree> subContexts = new LinkedList<>();
|
||||
|
||||
public LdapTree(DirContextOperations node) {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
public DirContextOperations getNode() {
|
||||
return this.node;
|
||||
}
|
||||
|
||||
public List<LdapTree> getSubContexts() {
|
||||
return this.subContexts;
|
||||
}
|
||||
|
||||
public void setSubContexts(List<LdapTree> subContexts) {
|
||||
this.subContexts = subContexts;
|
||||
}
|
||||
|
||||
public void addSubTree(LdapTree ldapTree) {
|
||||
this.subContexts.add(ldapTree);
|
||||
}
|
||||
|
||||
public void traverse(LdapTreeVisitor visitor) {
|
||||
traverse(visitor, 0);
|
||||
}
|
||||
|
||||
private void traverse(LdapTreeVisitor visitor, int currentDepth) {
|
||||
visitor.visit(this.node, currentDepth);
|
||||
for (LdapTree subContext : this.subContexts) {
|
||||
subContext.traverse(visitor, currentDepth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.utils;
|
||||
|
||||
import javax.naming.Name;
|
||||
|
||||
import org.springframework.ldap.core.ContextMapper;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.ldap.core.LdapClient;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
|
||||
public class LdapTreeBuilder {
|
||||
|
||||
private final LdapClient ldap;
|
||||
|
||||
public LdapTreeBuilder(LdapClient ldap) {
|
||||
this.ldap = ldap;
|
||||
}
|
||||
|
||||
public LdapTree getLdapTree(Name root) {
|
||||
DirContextOperations context = this.ldap.search().name(root).toEntry();
|
||||
return getLdapTree(context);
|
||||
}
|
||||
|
||||
private LdapTree getLdapTree(DirContextOperations rootContext) {
|
||||
LdapTree ldapTree = new LdapTree(rootContext);
|
||||
this.ldap.listBindings(rootContext.getDn()).toList((ContextMapper<Object>) (ctx) -> {
|
||||
Name dn = ((DirContextOperations) ctx).getDn();
|
||||
dn = LdapUtils.prepend(dn, rootContext.getDn());
|
||||
ldapTree.addSubTree(getLdapTree(dn));
|
||||
return null;
|
||||
});
|
||||
return ldapTree;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.utils;
|
||||
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
|
||||
public interface LdapTreeVisitor {
|
||||
|
||||
void visit(DirContextOperations node, int currentDepth);
|
||||
|
||||
}
|
||||
3
servlet/xml/java/odm/src/main/java/overview.html
Normal file
3
servlet/xml/java/odm/src/main/java/overview.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<body>
|
||||
Plain example of Spring LDAP usage.
|
||||
</body>
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:ldap="http://www.springframework.org/schema/ldap"
|
||||
xmlns:data-ldap="http://www.springframework.org/schema/data/ldap"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/ldap https://www.springframework.org/schema/ldap/spring-ldap.xsd
|
||||
http://www.springframework.org/schema/data/ldap https://www.springframework.org/schema/data/ldap/spring-ldap.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:/ldap.properties" />
|
||||
|
||||
<ldap:context-source id="contextSource"
|
||||
password="${sample.ldap.password}"
|
||||
url="${sample.ldap.url}"
|
||||
username="${sample.ldap.userDn}"
|
||||
base="${sample.ldap.base}" />
|
||||
|
||||
<ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource"/>
|
||||
|
||||
<bean id="ldap" class="org.springframework.ldap.core.LdapClient" factory-method="create">
|
||||
<constructor-arg ref="contextSource"/>
|
||||
</bean>
|
||||
|
||||
<bean id="odm" class="org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper"/>
|
||||
|
||||
<bean id="ldapTreeBuilder" class="org.springframework.ldap.samples.utils.LdapTreeBuilder">
|
||||
<constructor-arg ref="ldap" />
|
||||
</bean>
|
||||
|
||||
<data-ldap:repositories base-package="org.springframework.ldap.samples.odm.repository" />
|
||||
|
||||
<beans profile="default">
|
||||
<!-- Populates the LDAP server with initial data -->
|
||||
<bean class="org.springframework.ldap.test.unboundid.LdifPopulator" depends-on="embeddedLdapServer">
|
||||
<property name="contextSource" ref="contextSource" />
|
||||
<property name="resource" value="classpath:/setup_data.ldif" />
|
||||
<property name="base" value="${sample.ldap.base}" />
|
||||
<property name="clean" value="${sample.ldap.clean}" />
|
||||
<property name="defaultBase" value="dc=jayway,dc=se" />
|
||||
</bean>
|
||||
|
||||
<!--
|
||||
This is for test and demo purposes only - EmbeddedLdapServerFactoryBean launches an in-process
|
||||
LDAP server.
|
||||
-->
|
||||
<bean id="embeddedLdapServer" class="org.springframework.ldap.test.unboundid.EmbeddedLdapServerFactoryBean">
|
||||
<property name="partitionName" value="jayway"/>
|
||||
<property name="partitionSuffix" value="${sample.ldap.base}" />
|
||||
<property name="port" value="18880" />
|
||||
</bean>
|
||||
</beans>
|
||||
</beans>
|
||||
5
servlet/xml/java/odm/src/main/resources/ldap.properties
Normal file
5
servlet/xml/java/odm/src/main/resources/ldap.properties
Normal file
@@ -0,0 +1,5 @@
|
||||
sample.ldap.url=ldap://127.0.0.1:18880
|
||||
sample.ldap.userDn=uid=admin,ou=system
|
||||
sample.ldap.password=secret
|
||||
sample.ldap.base=dc=jayway,dc=se
|
||||
sample.ldap.clean=true
|
||||
14
servlet/xml/java/odm/src/main/resources/logback.xml
Normal file
14
servlet/xml/java/odm/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type
|
||||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
35
servlet/xml/java/odm/src/main/resources/setup_data.ldif
Normal file
35
servlet/xml/java/odm/src/main/resources/setup_data.ldif
Normal file
@@ -0,0 +1,35 @@
|
||||
dn: c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: country
|
||||
c: Sweden
|
||||
description: The country of Sweden
|
||||
|
||||
dn: ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: organizationalUnit
|
||||
ou: company1
|
||||
description: First company in Sweden
|
||||
|
||||
dn: cn=Some Person,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person
|
||||
userPassword: password
|
||||
cn: Some Person
|
||||
sn: Person
|
||||
description: Sweden, Company1, Some Person
|
||||
telephoneNumber: +46 555-123456
|
||||
|
||||
dn: cn=Some Person2,ou=company1,c=Sweden,dc=jayway,dc=se
|
||||
objectclass: top
|
||||
objectclass: person
|
||||
objectclass: organizationalPerson
|
||||
objectclass: inetOrgPerson
|
||||
uid: some.person2
|
||||
userPassword: password
|
||||
cn: Some Person2
|
||||
sn: Person2
|
||||
description: Sweden, Company1, Some Person2
|
||||
telephoneNumber: +46 555-654321
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan
|
||||
base-package="org.springframework.ldap.samples.odm.web" />
|
||||
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
|
||||
<property name="prefix" value="/WEB-INF/jsp/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
</beans>
|
||||
20
servlet/xml/java/odm/src/main/webapp/WEB-INF/jsp/showPerson.jsp
Executable file
20
servlet/xml/java/odm/src/main/webapp/WEB-INF/jsp/showPerson.jsp
Executable file
@@ -0,0 +1,20 @@
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<html>
|
||||
<body>
|
||||
<a href="showTree.do">Back</a>
|
||||
<p>
|
||||
|
||||
Full name: ${person.fullName}
|
||||
<br>
|
||||
LastName: ${person.lastName}
|
||||
<br>
|
||||
Description: ${person.description}
|
||||
<br>
|
||||
Country: ${person.country}
|
||||
<br>
|
||||
Company: ${person.company}
|
||||
<br>
|
||||
Phone: ${person.phone}
|
||||
<br>
|
||||
</p>
|
||||
</html>
|
||||
@@ -0,0 +1,17 @@
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<html>
|
||||
<body>
|
||||
<h2>Operations</h2>
|
||||
<h3>Clicking a link below performs the described operation which will be reflected in the LDAP tree below</h3>
|
||||
<a href="addPerson.do">Add new test person 'John Doe'</a> (only works once)<br>
|
||||
<a href="updatePhoneNumber.do">Add a '0' to the phone number of test person</a> (only works if the person has been created)<br>
|
||||
<a href="removePerson.do">Remove test person</a><br>
|
||||
<p>
|
||||
<h2>Tree contents</h2>
|
||||
<h3>Click a person row to see the attribute values (country and company rows do not have additional info)</h3>
|
||||
<c:forEach var="row" items="${rows}">
|
||||
${row}
|
||||
</c:forEach>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
39
servlet/xml/java/odm/src/main/webapp/WEB-INF/web.xml
Normal file
39
servlet/xml/java/odm/src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app id="Tiink-preview" xmlns="http://java.sun.com/xml/ns/j2ee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
|
||||
version="2.4">
|
||||
|
||||
<display-name>Spring LDAP Basic Example</display-name>
|
||||
<listener>
|
||||
<listener-class>
|
||||
org.springframework.web.context.ContextLoaderListener
|
||||
</listener-class>
|
||||
</listener>
|
||||
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>classpath:/applicationContext.xml</param-value>
|
||||
</context-param>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>basic</servlet-name>
|
||||
<servlet-class>
|
||||
org.springframework.web.servlet.DispatcherServlet
|
||||
</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/basic-servlet.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>basic</servlet-name>
|
||||
<url-pattern>*.do</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.htm</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
||||
5
servlet/xml/java/odm/src/main/webapp/index.htm
Normal file
5
servlet/xml/java/odm/src/main/webapp/index.htm
Normal file
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta HTTP-EQUIV="REFRESH" content="0; url=showTree.do">
|
||||
</head>
|
||||
</html>
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.odm.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.NameNotFoundException;
|
||||
import org.springframework.ldap.samples.odm.domain.Person;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Abstract base class for PersonDao integration tests.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
* @author Ulrik Sandberg
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration("/applicationContext.xml")
|
||||
public class PersonRepositorySampleIntegrationTests {
|
||||
|
||||
protected Person person;
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
|
||||
@BeforeEach
|
||||
void preparePerson() {
|
||||
this.person = new Person();
|
||||
this.person.setCountry("Sweden");
|
||||
this.person.setCompany("company1");
|
||||
this.person.setFullName("Some Person");
|
||||
this.person.setLastName("Person");
|
||||
this.person.setDescription("Sweden, Company1, Some Person");
|
||||
this.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
|
||||
void testCreateUpdateDelete() {
|
||||
this.person.setFullName("Another Person");
|
||||
this.personRepository.save(this.person);
|
||||
Person person = this.personRepository.findByPrimaryKey("Sweden", "company1", "Another Person");
|
||||
assertThat(person).isEqualTo(this.person);
|
||||
|
||||
this.person.setDescription("Another description");
|
||||
this.personRepository.save(this.person);
|
||||
person = this.personRepository.findByPrimaryKey("Sweden", "company1", "Another Person");
|
||||
assertThat(person.getDescription()).isEqualTo("Another description");
|
||||
|
||||
this.personRepository.delete(this.person);
|
||||
assertThatExceptionOfType(NameNotFoundException.class)
|
||||
.isThrownBy(() -> this.personRepository.findByPrimaryKey("Sweden", "company1", "Another Person"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllPersonNames() {
|
||||
List<String> result = this.personRepository.getAllPersonNames();
|
||||
assertThat(result).hasSize(2);
|
||||
String first = result.get(0);
|
||||
assertThat(first).isEqualTo("Some Person");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindAll() {
|
||||
List<Person> result = this.personRepository.findAll();
|
||||
assertThat(result).hasSize(2);
|
||||
Person first = result.get(0);
|
||||
assertThat(first.getFullName()).isEqualTo("Some Person");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFindByPrimaryKey() {
|
||||
Person result = this.personRepository.findByPrimaryKey("Sweden", "company1", "Some Person");
|
||||
|
||||
assertThat(result.getCountry()).isEqualTo("Sweden");
|
||||
assertThat(result.getCompany()).isEqualTo("company1");
|
||||
assertThat(result.getDescription()).isEqualTo("Sweden, Company1, Some Person");
|
||||
assertThat(result.getPhone()).isEqualTo("+46 555-123456");
|
||||
assertThat(result.getFullName()).isEqualTo("Some Person");
|
||||
assertThat(result.getLastName()).isEqualTo("Person");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2005-2024 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
|
||||
*
|
||||
* https://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.utils;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.ldap.LdapName;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.ldap.core.DirContextOperations;
|
||||
import org.springframework.ldap.support.LdapUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration("/applicationContext.xml")
|
||||
public class LdapTreeBuilderIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private LdapTreeBuilder tested;
|
||||
|
||||
@Test
|
||||
void testGetLdapTree() {
|
||||
LdapTree ldapTree = this.tested.getLdapTree(LdapUtils.newLdapName("c=Sweden"));
|
||||
ldapTree.traverse(new TestVisitor());
|
||||
}
|
||||
|
||||
private static final class TestVisitor implements LdapTreeVisitor {
|
||||
|
||||
private static final LdapName DN_1 = LdapUtils.newLdapName("c=Sweden");
|
||||
|
||||
private static final LdapName DN_2 = LdapUtils.newLdapName("ou=company1,c=Sweden");
|
||||
|
||||
private static final LdapName DN_3 = LdapUtils.newLdapName("cn=Some Person,ou=company1,c=Sweden");
|
||||
|
||||
private static final LdapName DN_4 = LdapUtils.newLdapName("cn=Some Person2,ou=company1,c=Sweden");
|
||||
|
||||
private final Map<LdapName, Integer> names = new LinkedHashMap<>();
|
||||
|
||||
private final Iterator<LdapName> keyIterator;
|
||||
|
||||
private TestVisitor() {
|
||||
this.names.put(DN_1, 0);
|
||||
this.names.put(DN_2, 1);
|
||||
this.names.put(DN_3, 2);
|
||||
this.names.put(DN_4, 2);
|
||||
|
||||
this.keyIterator = this.names.keySet().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DirContextOperations node, int currentDepth) {
|
||||
LdapName next = this.keyIterator.next();
|
||||
assertThat(node.getDn()).isEqualTo(next);
|
||||
assertThat(currentDepth).isEqualTo(this.names.get(next).intValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user