Skip to content

Commit 5914686

Browse files
WIP
1 parent 7f44a30 commit 5914686

File tree

11 files changed

+634
-27
lines changed

11 files changed

+634
-27
lines changed

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<spring-ldap>3.2.6</spring-ldap>
2222
<springdata.commons>3.4.0-SNAPSHOT</springdata.commons>
2323
<java-module-name>spring.data.ldap</java-module-name>
24+
<unboundid-ldapsdk>7.0.1</unboundid-ldapsdk>
2425
</properties>
2526

2627
<developers>
@@ -109,6 +110,20 @@
109110
<scope>test</scope>
110111
</dependency>
111112

113+
<dependency>
114+
<groupId>org.springframework.ldap</groupId>
115+
<artifactId>spring-ldap-test</artifactId>
116+
<version>${spring-ldap}</version>
117+
<scope>test</scope>
118+
</dependency>
119+
120+
<dependency>
121+
<groupId>com.unboundid</groupId>
122+
<artifactId>unboundid-ldapsdk</artifactId>
123+
<version>${unboundid-ldapsdk}</version>
124+
<scope>test</scope>
125+
</dependency>
126+
112127
</dependencies>
113128

114129
<build>

src/main/java/org/springframework/data/ldap/repository/query/AbstractLdapRepositoryQuery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.data.repository.query.QueryMethod;
2727
import org.springframework.data.repository.query.RepositoryQuery;
2828
import org.springframework.data.repository.query.ResultProcessor;
29+
import org.springframework.data.repository.query.ValueExpressionDelegate;
2930
import org.springframework.ldap.core.LdapOperations;
3031
import org.springframework.ldap.query.LdapQuery;
3132
import org.springframework.util.Assert;

src/main/java/org/springframework/data/ldap/repository/query/AnnotatedLdapRepositoryQuery.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.data.mapping.PersistentProperty;
2323
import org.springframework.data.mapping.context.MappingContext;
2424
import org.springframework.data.mapping.model.EntityInstantiators;
25+
import org.springframework.data.repository.query.ValueExpressionDelegate;
26+
import org.springframework.data.repository.query.ValueExpressionQueryRewriter;
2527
import org.springframework.ldap.core.LdapOperations;
2628
import org.springframework.ldap.query.LdapQuery;
2729
import org.springframework.util.Assert;
@@ -35,35 +37,61 @@
3537
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
3638

3739
private final Query queryAnnotation;
40+
private final ValueExpressionDelegate valueExpressionDelegate;
3841

3942
/**
4043
* Construct a new instance.
4144
*
42-
* @param queryMethod the QueryMethod.
43-
* @param entityType the managed class.
44-
* @param ldapOperations the LdapOperations instance to use.
45-
* @param mappingContext must not be {@literal null}.
46-
* @param instantiators must not be {@literal null}.
45+
* @param queryMethod the QueryMethod.
46+
* @param entityType the managed class.
47+
* @param ldapOperations the LdapOperations instance to use.
48+
* @param mappingContext must not be {@literal null}.
49+
* @param instantiators must not be {@literal null}.
50+
* @deprecated use the constructor with {@link ValueExpressionDelegate}
4751
*/
52+
@Deprecated(since = "3.4")
4853
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
4954
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
5055
EntityInstantiators instantiators) {
5156

57+
this(queryMethod, entityType, ldapOperations, mappingContext, instantiators, ValueExpressionDelegate.create());
58+
}
59+
60+
/**
61+
* Construct a new instance.
62+
*
63+
* @param queryMethod the QueryMethod.
64+
* @param entityType the managed class.
65+
* @param ldapOperations the LdapOperations instance to use.
66+
* @param mappingContext must not be {@literal null}.
67+
* @param instantiators must not be {@literal null}.
68+
* @param valueExpressionDelegate must not be {@literal null}
69+
*/
70+
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
71+
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
72+
EntityInstantiators instantiators, ValueExpressionDelegate valueExpressionDelegate) {
73+
5274
super(queryMethod, entityType, ldapOperations, mappingContext, instantiators);
5375

5476
Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present");
5577
Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified");
5678

5779
queryAnnotation = queryMethod.getRequiredQueryAnnotation();
80+
this.valueExpressionDelegate = valueExpressionDelegate;
5881
}
5982

6083
@Override
6184
protected LdapQuery createQuery(LdapParameterAccessor parameters) {
6285

86+
String evaluatedFilterValue = (String) valueExpressionDelegate.parse(queryAnnotation.value()).evaluate(valueExpressionDelegate.createValueContextProvider(getQueryMethod().getParameters())
87+
.getEvaluationContext(parameters.getBindableParameterValues()));
88+
89+
valueExpressionDelegate.getValueExpressionParser();
90+
6391
return query().base(queryAnnotation.base()) //
6492
.searchScope(queryAnnotation.searchScope()) //
6593
.countLimit(queryAnnotation.countLimit()) //
6694
.timeLimit(queryAnnotation.timeLimit()) //
67-
.filter(queryAnnotation.value(), parameters.getBindableParameterValues());
95+
.filter(evaluatedFilterValue);
6896
}
6997
}

src/main/java/org/springframework/data/ldap/repository/support/LdapRepositoryFactory.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
4343
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
4444
import org.springframework.data.repository.query.RepositoryQuery;
45+
import org.springframework.data.repository.query.ValueExpressionDelegate;
4546
import org.springframework.lang.Nullable;
4647
import org.springframework.ldap.core.LdapOperations;
4748
import org.springframework.util.Assert;
@@ -56,7 +57,6 @@
5657
*/
5758
public class LdapRepositoryFactory extends RepositoryFactorySupport {
5859

59-
private final LdapQueryLookupStrategy queryLookupStrategy;
6060
private final LdapOperations ldapOperations;
6161
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext;
6262
private final EntityInstantiators instantiators = new EntityInstantiators();
@@ -72,7 +72,6 @@ public LdapRepositoryFactory(LdapOperations ldapOperations) {
7272

7373
this.ldapOperations = ldapOperations;
7474
this.mappingContext = new LdapMappingContext();
75-
this.queryLookupStrategy = new LdapQueryLookupStrategy(ldapOperations, instantiators, mappingContext);
7675
}
7776

7877
/**
@@ -87,7 +86,6 @@ public LdapRepositoryFactory(LdapOperations ldapOperations) {
8786
Assert.notNull(ldapOperations, "LdapOperations must not be null");
8887
Assert.notNull(mappingContext, "LdapMappingContext must not be null");
8988

90-
this.queryLookupStrategy = new LdapQueryLookupStrategy(ldapOperations, instantiators, mappingContext);
9189
this.ldapOperations = ldapOperations;
9290
this.mappingContext = mappingContext;
9391
}
@@ -153,10 +151,9 @@ protected Object getTargetRepository(RepositoryInformation information) {
153151
information.getDomainType());
154152
}
155153

156-
@Override
157-
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable Key key,
158-
QueryMethodEvaluationContextProvider evaluationContextProvider) {
159-
return Optional.of(queryLookupStrategy);
154+
@Override protected Optional<QueryLookupStrategy> getQueryLookupStrategy(Key key,
155+
ValueExpressionDelegate valueExpressionDelegate) {
156+
return Optional.of(new LdapQueryLookupStrategy(ldapOperations, instantiators, mappingContext, valueExpressionDelegate));
160157
}
161158

162159
/**
@@ -185,19 +182,9 @@ private static boolean acceptsMappingContext(RepositoryInformation information)
185182
return acceptsMappingContext;
186183
}
187184

188-
private static final class LdapQueryLookupStrategy implements QueryLookupStrategy {
189-
190-
private final LdapOperations ldapOperations;
191-
private final EntityInstantiators instantiators;
192-
private final MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext;
193-
194-
public LdapQueryLookupStrategy(LdapOperations ldapOperations, EntityInstantiators instantiators,
195-
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext) {
196-
197-
this.ldapOperations = ldapOperations;
198-
this.instantiators = instantiators;
199-
this.mappingContext = mappingContext;
200-
}
185+
private record LdapQueryLookupStrategy(LdapOperations ldapOperations,
186+
EntityInstantiators instantiators, MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
187+
ValueExpressionDelegate valueExpressionDelegate) implements QueryLookupStrategy {
201188

202189
@Override
203190
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
@@ -207,7 +194,7 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
207194
Class<?> domainType = metadata.getDomainType();
208195

209196
if (queryMethod.hasQueryAnnotation()) {
210-
return new AnnotatedLdapRepositoryQuery(queryMethod, domainType, ldapOperations, mappingContext, instantiators);
197+
return new AnnotatedLdapRepositoryQuery(queryMethod, domainType, ldapOperations, mappingContext, instantiators, valueExpressionDelegate);
211198
} else {
212199
return new PartTreeLdapRepositoryQuery(queryMethod, domainType, ldapOperations, mappingContext, instantiators);
213200
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package org.springframework.data.ldap.config;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.core.io.Resource;
7+
import org.springframework.util.StringUtils;
8+
9+
/**
10+
* Configuration properties for Embedded LDAP. Taken from Spring Boot.
11+
*
12+
* @author Eddú Meléndez
13+
* @author Mathieu Ouellet
14+
*/
15+
public class EmbeddedLdapProperties {
16+
17+
/**
18+
* Embedded LDAP port.
19+
*/
20+
private int port = 0;
21+
22+
/**
23+
* Embedded LDAP credentials.
24+
*/
25+
private Credential credential = new Credential();
26+
27+
/**
28+
* List of base DNs.
29+
*/
30+
private List<String> baseDn = new ArrayList<>();
31+
32+
/**
33+
* Schema (LDIF) script resource reference.
34+
*/
35+
private String ldif = "classpath:schema.ldif";
36+
37+
/**
38+
* Schema validation.
39+
*/
40+
private final Validation validation = new Validation();
41+
42+
public int getPort() {
43+
return this.port;
44+
}
45+
46+
public void setPort(int port) {
47+
this.port = port;
48+
}
49+
50+
public Credential getCredential() {
51+
return this.credential;
52+
}
53+
54+
public void setCredential(Credential credential) {
55+
this.credential = credential;
56+
}
57+
58+
public List<String> getBaseDn() {
59+
return this.baseDn;
60+
}
61+
62+
public void setBaseDn(List<String> baseDn) {
63+
this.baseDn = baseDn;
64+
}
65+
66+
public String getLdif() {
67+
return this.ldif;
68+
}
69+
70+
public void setLdif(String ldif) {
71+
this.ldif = ldif;
72+
}
73+
74+
public Validation getValidation() {
75+
return this.validation;
76+
}
77+
78+
public static class Credential {
79+
80+
/**
81+
* Embedded LDAP username.
82+
*/
83+
private String username;
84+
85+
/**
86+
* Embedded LDAP password.
87+
*/
88+
private String password;
89+
90+
public String getUsername() {
91+
return this.username;
92+
}
93+
94+
public void setUsername(String username) {
95+
this.username = username;
96+
}
97+
98+
public String getPassword() {
99+
return this.password;
100+
}
101+
102+
public void setPassword(String password) {
103+
this.password = password;
104+
}
105+
106+
boolean isAvailable() {
107+
return StringUtils.hasText(this.username) && StringUtils.hasText(this.password);
108+
}
109+
110+
}
111+
112+
public static class Validation {
113+
114+
/**
115+
* Whether to enable LDAP schema validation.
116+
*/
117+
private boolean enabled = false;
118+
119+
/**
120+
* Path to the custom schema.
121+
*/
122+
private Resource schema;
123+
124+
public boolean isEnabled() {
125+
return this.enabled;
126+
}
127+
128+
public void setEnabled(boolean enabled) {
129+
this.enabled = enabled;
130+
}
131+
132+
public Resource getSchema() {
133+
return this.schema;
134+
}
135+
136+
public void setSchema(Resource schema) {
137+
this.schema = schema;
138+
}
139+
140+
}
141+
142+
}

0 commit comments

Comments
 (0)