Skip to content

Commit 94b2168

Browse files
committed
Polishing.
Refactor ContextualValueExpressionEvaluator into Function, refactor BindingContext into nested class. Simplify tests. Rename tests to reflect what they are actually doing. Reformat code. See #453 Original pull request: #505
1 parent b88ce59 commit 94b2168

15 files changed

+393
-394
lines changed

src/main/antora/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
** xref:repositories/namespace-reference.adoc[]
1414
** xref:repositories/query-keywords-reference.adoc[]
1515
** xref:repositories/query-return-types-reference.adoc[]
16+
1617
* xref:ldap.adoc[]
1718
** xref:ldap/configuration.adoc[]
1819
** xref:ldap/usage.adoc[]
1920
** xref:ldap/query-methods.adoc[]
21+
** xref:ldap/value-expressions.adoc[]
2022
** xref:ldap/querydsl.adoc[]
2123
** xref:ldap/cdi-integration.adoc[]
2224

src/main/antora/modules/ROOT/pages/ldap/query-methods.adoc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,91 @@ The following table provides samples of the keywords that you can use with query
7676
| `(!(Firstname=name))`
7777

7878
|===
79+
80+
[[ldap.query-methods.at-query]]
81+
== Using `@Query`
82+
83+
If you need to use a custom query that can't be derived from the method name, you can use the `@Query` annotation to define the query.
84+
As queries are tied to the Java method that runs them, you can actually bind parameters to be passed to the query.
85+
86+
The following example shows a query created with the `@Query` annotation:
87+
88+
.Declare query at the query method using `@Query`
89+
====
90+
[source,java]
91+
----
92+
interface PersonRepository extends LdapRepository<Person, Long> {
93+
94+
@Query("(&(employmentType=*)(!(employmentType=Hired))(mail=:emailAddress))")
95+
Person findEmployeeByEmailAddress(String emailAddress);
96+
97+
}
98+
----
99+
====
100+
101+
NOTE: Spring Data supports named (parameter names prefixed with `:`) and positional parameter binding (in the form of zero-based `?0`).
102+
We recommend using named parameters for easier readability.
103+
Also, using positional parameters makes query methods a little error-prone when refactoring regarding the parameter position.
104+
105+
[[ldap.encoding]]
106+
== Parameter Encoding
107+
108+
Query parameters of String-based queries are encoded according to https://datatracker.ietf.org/doc/html/rfc2254[RFC2254].
109+
This can lead to undesired escaping of certain characters.
110+
You can specify your own encoder through the `@LdapEncode` annotation that defines which javadoc:org.springframework.data.ldap.repository.LdapEncoder[] to use.
111+
112+
`@LdapEncode` applies to individual parameters of a query method.
113+
It is not applies for derived queries or Value Expressions (SpEL, Property Placeholders).
114+
115+
.Declare a custom `LdapEncoder` for a query method
116+
====
117+
[source,java]
118+
----
119+
interface PersonRepository extends LdapRepository<Person, Long> {
120+
121+
@Query("(&(employmentType=*)(!(employmentType=Hired))(firstName=:firstName))")
122+
Person findEmployeeByFirstNameLike(@LdapEncode(MyLikeEncoder.class) String firstName);
123+
124+
}
125+
----
126+
====
127+
128+
[[ldap.query.spel-expressions]]
129+
== Using SpEL Expressions
130+
131+
Spring Data allows you to use SpEL expressions in your query methods.
132+
SpEL expressions are part of Spring Data's xref:ldap/value-expressions.adoc[Value Expressions] support.
133+
SpEL expressions can be used to manipulate query method arguments as well as to invoke bean methods.
134+
Method arguments can be accessed by name or index as demonstrated in the following example.
135+
136+
.Using SpEL expressions in Repository Query Methods
137+
====
138+
[source,java]
139+
----
140+
@Query("(&(firstName=?#{[0]})(mail=:?#{principal.emailAddress}))")
141+
List<Person> findByFirstnameAndCurrentUserWithCustomQuery(String firstname);
142+
----
143+
====
144+
145+
NOTE: Values provided by SpEL expressions are not escaped according to RFC2254.
146+
You have to ensure that the values are properly escaped if needed.
147+
Consider using Spring Ldap's `org.springframework.ldap.support.LdapEncoder` helper class.
148+
149+
[[ldap.query.property-placeholders]]
150+
== Using Property Placeholders
151+
152+
Property Placeholders (see xref:ldap/value-expressions.adoc[Value Expressions]) can help to easily customize your queries based on configuration properties from Spring's `Environment`.
153+
These are useful for queries that need to be customized based on the environment or configuration.
154+
155+
.Using Property Placeholders in Repository Query Methods
156+
====
157+
[source,java]
158+
----
159+
@Query("(&(firstName=?0)(stage=:?${myapp.stage:dev}))")
160+
List<Person> findByFirstnameAndStageWithCustomQuery(String firstname);
161+
----
162+
====
163+
164+
NOTE: Values provided by Property Placeholders are not escaped according to RFC2254.
165+
You have to ensure that the values are properly escaped if needed.
166+
Consider using Spring Ldap's `org.springframework.ldap.support.LdapEncoder` helper class.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include::{commons}@data-commons::page$value-expressions.adoc[]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
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;
3029
import org.springframework.ldap.core.LdapOperations;
3130
import org.springframework.ldap.query.LdapQuery;
3231
import org.springframework.util.Assert;

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
4040

4141
private final Query queryAnnotation;
42-
private final ValueExpressionDelegate valueExpressionDelegate;
43-
private final StringBasedQuery stringBasedQuery;
44-
private final StringBasedQuery stringBasedBase;
42+
private final StringBasedQuery query;
43+
private final StringBasedQuery base;
44+
private final ValueEvaluationContextProvider valueContextProvider;
4545

4646
/**
4747
* Construct a new instance.
@@ -81,34 +81,32 @@ public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entity
8181
Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present");
8282
Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified");
8383

84-
queryAnnotation = queryMethod.getRequiredQueryAnnotation();
85-
this.valueExpressionDelegate = valueExpressionDelegate;
86-
stringBasedQuery = new StringBasedQuery(queryAnnotation.value(), queryMethod.getParameters(), valueExpressionDelegate);
87-
stringBasedBase = new StringBasedQuery(queryAnnotation.base(), queryMethod.getParameters(), valueExpressionDelegate);
84+
this.queryAnnotation = queryMethod.getRequiredQueryAnnotation();
85+
this.query = new StringBasedQuery(queryAnnotation.value(), queryMethod.getParameters(), valueExpressionDelegate);
86+
this.base = new StringBasedQuery(queryAnnotation.base(), queryMethod.getParameters(), valueExpressionDelegate);
87+
this.valueContextProvider = valueExpressionDelegate.createValueContextProvider(getQueryMethod().getParameters());
8888
}
8989

9090
@Override
9191
protected LdapQuery createQuery(LdapParameterAccessor parameters) {
9292

93-
ValueEvaluationContextProvider valueContextProvider = valueExpressionDelegate
94-
.createValueContextProvider(getQueryMethod().getParameters());
93+
String query = bind(parameters, valueContextProvider, this.query);
94+
String base = bind(parameters, valueContextProvider, this.base);
9595

96-
String boundQuery = bind(parameters, valueContextProvider, stringBasedQuery);
97-
98-
String boundBase = bind(parameters, valueContextProvider, stringBasedBase);
99-
100-
return query().base(boundBase) //
96+
return query().base(base) //
10197
.searchScope(queryAnnotation.searchScope()) //
10298
.countLimit(queryAnnotation.countLimit()) //
10399
.timeLimit(queryAnnotation.timeLimit()) //
104-
.filter(boundQuery);
100+
.filter(query, parameters.getBindableParameterValues());
105101
}
106102

107103
private String bind(LdapParameterAccessor parameters, ValueEvaluationContextProvider valueContextProvider, StringBasedQuery query) {
104+
108105
ValueEvaluationContext evaluationContext = valueContextProvider
109106
.getEvaluationContext(parameters.getBindableParameterValues(), query.getExpressionDependencies());
107+
110108
return query.bindQuery(parameters,
111-
new ContextualValueExpressionEvaluator(valueExpressionDelegate, evaluationContext));
109+
expression -> expression.evaluate(evaluationContext));
112110
}
113111

114112
}

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

Lines changed: 0 additions & 177 deletions
This file was deleted.

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

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)