Skip to content

Commit 9ba7cb2

Browse files
Added encoding of parameters + unit tests
1 parent e9bfa66 commit 9ba7cb2

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*
3535
* @author Mattias Hellborg Arthursson
3636
* @author Mark Paluch
37+
* @author Marcin Grzejszczak
3738
*/
3839
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
3940

@@ -92,9 +93,9 @@ protected LdapQuery createQuery(LdapParameterAccessor parameters) {
9293
ValueEvaluationContextProvider valueContextProvider = valueExpressionDelegate
9394
.createValueContextProvider(getQueryMethod().getParameters());
9495

95-
String boundQuery = boundQuery(parameters, valueContextProvider);
96+
String boundQuery = bind(parameters, valueContextProvider, stringBasedQuery);
9697

97-
String boundBase = boundBase(parameters, valueContextProvider);
98+
String boundBase = bind(parameters, valueContextProvider, stringBasedBase);
9899

99100
return query().base(boundBase) //
100101
.searchScope(queryAnnotation.searchScope()) //
@@ -103,17 +104,10 @@ protected LdapQuery createQuery(LdapParameterAccessor parameters) {
103104
.filter(boundQuery);
104105
}
105106

106-
private String boundQuery(LdapParameterAccessor parameters, ValueEvaluationContextProvider valueContextProvider) {
107+
private String bind(LdapParameterAccessor parameters, ValueEvaluationContextProvider valueContextProvider, StringBasedQuery query) {
107108
ValueEvaluationContext evaluationContext = valueContextProvider
108-
.getEvaluationContext(parameters.getBindableParameterValues(), stringBasedQuery.getExpressionDependencies());
109-
return stringBasedQuery.bindQuery(parameters,
110-
new ContextualValueExpressionEvaluator(valueExpressionDelegate, evaluationContext));
111-
}
112-
113-
private String boundBase(LdapParameterAccessor parameters, ValueEvaluationContextProvider valueContextProvider) {
114-
ValueEvaluationContext evaluationContext = valueContextProvider
115-
.getEvaluationContext(parameters.getBindableParameterValues(), stringBasedBase.getExpressionDependencies());
116-
return stringBasedBase.bindQuery(parameters,
109+
.getEvaluationContext(parameters.getBindableParameterValues(), query.getExpressionDependencies());
110+
return query.bindQuery(parameters,
117111
new ContextualValueExpressionEvaluator(valueExpressionDelegate, evaluationContext));
118112
}
119113

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.data.repository.query.ParameterAccessor;
2525
import org.springframework.data.repository.query.Parameters;
2626
import org.springframework.lang.Nullable;
27+
import org.springframework.ldap.support.LdapEncoder;
2728
import org.springframework.util.Assert;
2829

2930
/**
@@ -96,9 +97,10 @@ private Object getParameterValueForBinding(ParameterBinding binding) {
9697
return evaluator.evaluate(binding.getRequiredExpression());
9798
}
9899

99-
return binding.isNamed()
100-
? parameterAccessor.getBindableValue(getParameterIndex(parameters, binding.getRequiredParameterName()))
101-
: parameterAccessor.getBindableValue(binding.getParameterIndex());
100+
Object value = binding.isNamed() ?
101+
parameterAccessor.getBindableValue(getParameterIndex(parameters, binding.getRequiredParameterName())) :
102+
parameterAccessor.getBindableValue(binding.getParameterIndex());
103+
return value == null ? null : LdapEncoder.filterEncode(value.toString());
102104
}
103105

104106
private int getParameterIndex(Parameters<?, ?> parameters, String parameterName) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.ldap.repository.query;
17+
18+
import java.util.List;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mockito;
22+
23+
import org.springframework.data.ldap.core.mapping.LdapMappingContext;
24+
import org.springframework.data.ldap.repository.LdapRepository;
25+
import org.springframework.data.ldap.repository.Query;
26+
import org.springframework.data.mapping.model.EntityInstantiators;
27+
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
28+
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
29+
import org.springframework.data.repository.query.Param;
30+
import org.springframework.data.repository.query.ValueExpressionDelegate;
31+
import org.springframework.ldap.core.LdapOperations;
32+
import org.springframework.ldap.query.LdapQuery;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
class AnnotatedLdapRepositoryQueryTests {
37+
38+
LdapOperations ldapOperations = Mockito.mock();
39+
40+
ValueExpressionDelegate valueExpressionDelegate = ValueExpressionDelegate.create();
41+
42+
@Test
43+
void shouldEncodeQuery() throws NoSuchMethodException {
44+
LdapQueryMethod method = queryMethod("namedParameters");
45+
AnnotatedLdapRepositoryQuery query = repositoryQuery(method);
46+
47+
LdapQuery ldapQuery = query.createQuery(
48+
new LdapParametersParameterAccessor(method, new Object[] { "John)(cn=Doe)", "foo" }));
49+
50+
assertThat(ldapQuery.filter().encode()).isEqualTo("(cn=John\\29\\28cn=Doe\\29)");
51+
}
52+
53+
@Test
54+
void shouldEncodeBase() throws NoSuchMethodException {
55+
LdapQueryMethod method = queryMethod("baseNamedParameters");
56+
AnnotatedLdapRepositoryQuery query = repositoryQuery(method);
57+
58+
LdapQuery ldapQuery = query.createQuery(
59+
new LdapParametersParameterAccessor(method, new Object[] { "foo", "cn=John)" }));
60+
61+
assertThat(ldapQuery.base()).hasToString("cn=John\\29");
62+
}
63+
64+
private LdapQueryMethod queryMethod(String methodName) throws NoSuchMethodException {
65+
return new LdapQueryMethod(QueryRepository.class.getMethod(methodName, String.class, String.class),
66+
new DefaultRepositoryMetadata(QueryRepository.class), new SpelAwareProxyProjectionFactory());
67+
}
68+
69+
private AnnotatedLdapRepositoryQuery repositoryQuery(LdapQueryMethod method) {
70+
return new AnnotatedLdapRepositoryQuery(method, SchemaEntry.class, ldapOperations, new LdapMappingContext(),
71+
new EntityInstantiators(), valueExpressionDelegate);
72+
}
73+
74+
interface QueryRepository extends LdapRepository<SchemaEntry> {
75+
76+
@Query(value = "(cn=:fullName)")
77+
List<SchemaEntry> namedParameters(@Param("fullName") String fullName, @Param("lastName") String lastName);
78+
79+
@Query(base = ":dc", value = "(cn=:fullName)")
80+
List<SchemaEntry> baseNamedParameters(@Param("fullName") String fullName, @Param("dc") String dc);
81+
82+
}
83+
}

0 commit comments

Comments
 (0)