Skip to content

Commit

Permalink
DATALDAP-1 - Create namespace handler and XML schema.
Browse files Browse the repository at this point in the history
Spring Data LDAP now uses its own Namespace: http://www.springframework.org/schema/data/ldap. Using XML configuration for Spring LDAP and Spring Data LDAP requires both namespaces to be declared.

<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		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 http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd
		http://www.springframework.org/schema/data/ldap http://www.springframework.org/schema/data/ldap/spring-ldap.xsd">

	<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin"/>

	<ldap:ldap-template/>

	<data-ldap:repositories base-package="org.springframework.data.ldap.config"/>

</beans>
  • Loading branch information
mp911de committed Dec 6, 2016
1 parent 8647253 commit 8303194
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/main/asciidoc/reference/ldap-repositories.adoc
Expand Up @@ -4,9 +4,9 @@
[[ldap.repo-intro]]
== Introduction

This chapter will point out the specialties for repository support for MongoDB. This builds on the core repository support explained in <<repositories>>. So make sure you've got a sound understanding of the basic concepts explained there.
This chapter will point out the specialties for repository support for LDAP. This builds on the core repository support explained in <<repositories>>. So make sure you've got a sound understanding of the basic concepts explained there.

* Spring LDAP repositories can be enabled using an `<ldap:repositories>` tag in your XML configuration or using an `@EnableLdapRepositories` annotation on a configuration class.
* Spring LDAP repositories can be enabled using an `<data-ldap:repositories>` tag in your XML configuration or using an `@EnableLdapRepositories` annotation on a configuration class.
* To include support for `LdapQuery` parameters in automatically generated repositories, have your interface extend `LdapRepository` rather than `CrudRepository`.
* All Spring LDAP repositories must work with entities annotated with the ODM annotations, as described in http://docs.spring.io/spring-ldap/docs/{springLdapVersion}.RELEASE/reference/#odm[Object-Directory Mapping].
* Since all ODM managed classes must have a Distinguished Name as ID, all Spring LDAP repositories must have the ID type parameter set to `javax.naming.Name`.
Expand Down Expand Up @@ -67,6 +67,37 @@ public interface PersonRepository extends CrudRepository<Person, Long> {

Right now this interface simply serves typing purposes but we will add additional methods to it later. In your Spring configuration simply add

.General LDAP repository Spring configuration
====
[source,xml]
----
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd
http://www.springframework.org/schema/data/ldap
http://www.springframework.org/schema/data/ldap/spring-ldap.xsd">
<ldap:context-source url="ldap://127.0.0.1:389"
username="cn=Admin"
password="secret" />
<ldap:ldap-template />
<data-ldap:repositories base-package="com.acme.*.repositories" />
</beans>
----
====

This namespace element will cause the base packages to be scanned for interfaces extending `LdapRepository` and create Spring beans for each of them found. By default the repositories will get a `LdapTemplate` Spring bean wired that is called `ldapTemplate`, so you only need to configure `ldap-template-ref` explicitly if you deviate from this convention.

If you'd rather like to go with JavaConfig use the `@EnableLdapRepositories` annotation. The annotation carries the very same attributes like the namespace element. If no base package is configured the infrastructure will scan the package of the annotated configuration class.

.JavaConfig for repositories
====
Expand Down Expand Up @@ -120,7 +151,7 @@ The sample creates an application context with Spring's unit test support which
[[ldap.repositories.queries]]
== Query methods

Most of the data access operations you usually trigger on a repository result a query being executed against the MongoDB databases. Defining such a query is just a matter of declaring a method on the repository interface
Most of the data access operations you usually trigger on a repository result a query being executed against the LDAP directory. Defining such a query is just a matter of declaring a method on the repository interface

.PersonRepository with query methods
====
Expand All @@ -134,7 +165,7 @@ public interface PersonRepository extends PagingAndSortingRepository<Person, Str
}
----
<1> The method shows a query for all people with the given lastname. The query will be derived parsing the method name for constraints which can be concatenated with `And` and `Or`. Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname))`.
<1> The method shows a query for all people with the given lastname and firstname. The query will be derived parsing the method name.Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname)(firstname=firstname))`.
<2> The method shows a query for all people with the given lastname and firstname. The query will be derived parsing the method name.Thus the method name will result in a query expression of `(&(objectclass=person)(lastname=lastname)(firstname=firstname))`.
====

[cols="1,2,3", options="header"]
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright 2016 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.data.ldap.config;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.data.ldap.repository.config.LdapRepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;

/**
* {@link org.springframework.beans.factory.xml.NamespaceHandler} for LDAP configuration.
*
* @author Mark Paluch
*/
public class LdapNamespaceHandler extends NamespaceHandlerSupport {

/*
* (non-Javadoc)
* @see org.springframework.beans.factory.xml.NamespaceHandler#init()
*/
public void init() {

registerBeanDefinitionParser("repositories",
new RepositoryBeanDefinitionParser(new LdapRepositoryConfigurationExtension()));
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/spring.handlers
@@ -0,0 +1 @@
http\://www.springframework.org/schema/data/ldap=org.springframework.data.ldap.config.LdapNamespaceHandler
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/spring.schemas
@@ -0,0 +1,2 @@
http\://www.springframework.org/schema/data/ldap/spring-ldap-1.0.xsd=org/springframework/data/ldap/config/spring-ldap-1.0.xsd
http\://www.springframework.org/schema/data/ldap/spring-ldap.xsd=org/springframework/data/ldap/config/spring-ldap-1.0.xsd
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns="http://www.springframework.org/schema/data/ldap"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:repository="http://www.springframework.org/schema/data/repository"
targetNamespace="http://www.springframework.org/schema/data/ldap"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:import namespace="http://www.springframework.org/schema/data/repository"
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd"/>

<xsd:element name="repositories">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="repository:repositories">
<xsd:attributeGroup ref="repository:repository-attributes"/>
<xsd:attribute name="ldap-template-ref" type="ldapTemplateRef"
default="ldapTemplate"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>

<xsd:simpleType name="ldapTemplateRef">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to type="org.springframework.ldap.core.LdapTemplate"/>
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string"/>
</xsd:simpleType>

</xsd:schema>
@@ -0,0 +1,44 @@
/*
* Copyright 2016 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.data.ldap.config;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Unit test for {@link LdapNamespaceHandler}.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class LdapNamespaceHandlerTests {

@Autowired private ApplicationContext context;

@Test
public void shouldCreateRepository() {
assertThat(context.getBean(DummyLdapRepository.class), is(notNullValue()));
}
}
11 changes: 11 additions & 0 deletions src/test/resources/infrastructure.xml
@@ -0,0 +1,11 @@
<?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:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">

<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin"/>

<ldap:ldap-template/>
</beans>
@@ -0,0 +1,12 @@
<?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:data-ldap="http://www.springframework.org/schema/data/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/ldap http://www.springframework.org/schema/data/ldap/spring-ldap.xsd">

<import resource="classpath:infrastructure.xml" />

<data-ldap:repositories base-package="org.springframework.data.ldap.config"/>

</beans>

0 comments on commit 8303194

Please sign in to comment.