Skip to content

Commit b4b2b51

Browse files
committed
Implement proximity filter ~=
Signed-off-by: Moritz Kobel <[email protected]>
1 parent 47be984 commit b4b2b51

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2005-2025 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+
17+
package org.springframework.ldap.filter;
18+
19+
import org.springframework.ldap.support.LdapEncoder;
20+
21+
/**
22+
* A filter for 'proximity'. The following code:
23+
*
24+
* <pre>
25+
* ProximityFilter filter = new ProximityFilter(&quot;cn&quot;, &quot;Some CN&quot;);
26+
* System.out.println(filter.encode());
27+
* </pre>
28+
*
29+
* would result in:
30+
*
31+
* <pre>
32+
* (cn~=Some CN)
33+
* </pre>
34+
*
35+
* @author Moritz Kobel
36+
* @since 3.3
37+
*/
38+
public final class ProximityFilter extends CompareFilter {
39+
40+
private static final String PROXIMITY_SIGN = "~=";
41+
42+
public ProximityFilter(String attribute, String value) {
43+
super(attribute, PROXIMITY_SIGN, value, LdapEncoder.filterEncode(value));
44+
}
45+
46+
}

core/src/main/java/org/springframework/ldap/query/ConditionCriteria.java

+13
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,17 @@ public interface ConditionCriteria {
105105
*/
106106
ConditionCriteria not();
107107

108+
/**
109+
* Appends an {@link org.springframework.ldap.filter.ProximityFilter}.
110+
* @param value the value to compare with.
111+
* @return an ContainerCriteria instance that can be used to continue append more
112+
* criteria or as the LdapQuery instance to be used as instance to e.g.
113+
* {@link org.springframework.ldap.core.LdapOperations#search(LdapQuery, org.springframework.ldap.core.ContextMapper)}.
114+
* @since 3.3
115+
* @see org.springframework.ldap.filter.EqualsFilter
116+
*/
117+
default ContainerCriteria near(String value) {
118+
throw new UnsupportedOperationException();
119+
}
120+
108121
}

core/src/main/java/org/springframework/ldap/query/DefaultConditionCriteria.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.ldap.filter.LikeFilter;
2424
import org.springframework.ldap.filter.NotFilter;
2525
import org.springframework.ldap.filter.PresentFilter;
26+
import org.springframework.ldap.filter.ProximityFilter;
2627
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
2728

2829
/**
@@ -72,6 +73,11 @@ public ContainerCriteria isPresent() {
7273
return appendToParent(new PresentFilter(this.attribute));
7374
}
7475

76+
@Override
77+
public ContainerCriteria near(String value) {
78+
return appendToParent(new ProximityFilter(this.attribute, value));
79+
}
80+
7581
private ContainerCriteria appendToParent(Filter filter) {
7682
return this.parent.append(negateIfApplicable(filter));
7783
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2005-2025 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+
17+
package org.springframework.ldap.filter;
18+
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* @author Moritz Kobel
25+
*/
26+
public class ProximityFilterTests {
27+
28+
@Test
29+
public void testEncode() {
30+
31+
CompareFilter eqq = new ProximityFilter("foo", "*bar(fie)");
32+
33+
StringBuffer buff = new StringBuffer();
34+
eqq.encode(buff);
35+
36+
assertThat(buff.toString()).isEqualTo("(foo~=\\2abar\\28fie\\29)");
37+
38+
}
39+
40+
}

core/src/test/java/org/springframework/ldap/query/LdapQueryBuilderTests.java

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public void buildPresent() {
7373
assertThat(result.filter().encode()).isEqualTo("(cn=*)");
7474
}
7575

76+
@Test
77+
public void buildProximity() {
78+
LdapQuery result = LdapQueryBuilder.query().where("cn").near("John Doe");
79+
80+
assertThat(result.filter().encode()).isEqualTo("(cn~=John Doe)");
81+
}
82+
7683
@Test
7784
public void buildHardcodedFilter() {
7885
LdapQuery result = LdapQueryBuilder.query().filter("(cn=Person*)");

0 commit comments

Comments
 (0)