Skip to content

Add proximity filter ~= #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2005-2025 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
*
* https://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.ldap.filter;

import org.springframework.ldap.support.LdapEncoder;

/**
* A filter for 'proximity'. The following code:
*
* <pre>
* ProximityFilter filter = new ProximityFilter(&quot;cn&quot;, &quot;Some CN&quot;);
* System.out.println(filter.encode());
* </pre>
*
* would result in:
*
* <pre>
* (cn~=Some CN)
* </pre>
*
* @author Moritz Kobel
* @since 3.3
*/
public final class ProximityFilter extends CompareFilter {

private static final String PROXIMITY_SIGN = "~=";

public ProximityFilter(String attribute, String value) {
super(attribute, PROXIMITY_SIGN, value, LdapEncoder.filterEncode(value));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,17 @@ public interface ConditionCriteria {
*/
ConditionCriteria not();

/**
* Appends an {@link org.springframework.ldap.filter.ProximityFilter}.
* @param value the value to compare with.
* @return an ContainerCriteria instance that can be used to continue append more
* criteria or as the LdapQuery instance to be used as instance to e.g.
* {@link org.springframework.ldap.core.LdapOperations#search(LdapQuery, org.springframework.ldap.core.ContextMapper)}.
* @since 3.3
* @see org.springframework.ldap.filter.EqualsFilter
*/
default ContainerCriteria near(String value) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.ldap.filter.LikeFilter;
import org.springframework.ldap.filter.NotFilter;
import org.springframework.ldap.filter.PresentFilter;
import org.springframework.ldap.filter.ProximityFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;

/**
Expand Down Expand Up @@ -72,6 +73,11 @@ public ContainerCriteria isPresent() {
return appendToParent(new PresentFilter(this.attribute));
}

@Override
public ContainerCriteria near(String value) {
return appendToParent(new ProximityFilter(this.attribute, value));
}

private ContainerCriteria appendToParent(Filter filter) {
return this.parent.append(negateIfApplicable(filter));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2005-2025 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
*
* https://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.ldap.filter;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Moritz Kobel
*/
public class ProximityFilterTests {

@Test
public void testEncode() {

CompareFilter eqq = new ProximityFilter("foo", "*bar(fie)");

StringBuffer buff = new StringBuffer();
eqq.encode(buff);

assertThat(buff.toString()).isEqualTo("(foo~=\\2abar\\28fie\\29)");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ public void buildPresent() {
assertThat(result.filter().encode()).isEqualTo("(cn=*)");
}

@Test
public void buildProximity() {
LdapQuery result = LdapQueryBuilder.query().where("cn").near("John Doe");

assertThat(result.filter().encode()).isEqualTo("(cn~=John Doe)");
}

@Test
public void buildHardcodedFilter() {
LdapQuery result = LdapQueryBuilder.query().filter("(cn=Person*)");
Expand Down