Skip to content

Commit 32179f2

Browse files
committed
Merge branch '4.1.x'
2 parents fae250d + 7576be1 commit 32179f2

3 files changed

Lines changed: 121 additions & 1 deletion

File tree

‎core/src/main/java/org/springframework/ldap/control/AbstractFallbackRequestAndResponseControlDirContextProcessor.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void postProcess(DirContext ctx) throws NamingException {
192192
// Go through response controls and get info, regardless of class
193193
for (Control responseControl : responseControls) {
194194
// check for match, try fallback otherwise
195-
if (responseControl.getClass().isAssignableFrom(this.responseControlClass)) {
195+
if (matchesResponseControlClass(responseControl.getClass())) {
196196
handleResponse(responseControl);
197197
return;
198198
}
@@ -201,6 +201,12 @@ public void postProcess(DirContext ctx) throws NamingException {
201201
this.log.info("No matching response control found - looking for '{}", this.responseControlClass);
202202
}
203203

204+
private boolean matchesResponseControlClass(Class<?> actual) {
205+
// A subclass of the expected class is a match. A superclass of the expected class
206+
// has always been accepted as well, so it remains one.
207+
return this.responseControlClass.isAssignableFrom(actual) || actual.isAssignableFrom(this.responseControlClass);
208+
}
209+
204210
/**
205211
* Set whether this control should be indicated as critical.
206212
* @param critical whether the control is critical.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2006-present 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.control;
18+
19+
import javax.naming.ldap.BasicControl;
20+
import javax.naming.ldap.Control;
21+
import javax.naming.ldap.LdapContext;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.BDDMockito.given;
27+
import static org.mockito.BDDMockito.mock;
28+
29+
/**
30+
* Unit tests for how {@link AbstractFallbackRequestAndResponseControlDirContextProcessor}
31+
* matches a response control to the expected response control class.
32+
*/
33+
class AbstractFallbackRequestAndResponseControlDirContextProcessorTests {
34+
35+
private final LdapContext ldapContext = mock(LdapContext.class);
36+
37+
@Test
38+
void postProcessWhenResponseIsExpectedClassThenHandles() throws Exception {
39+
RecordingProcessor processor = new RecordingProcessor(BaseControl.class);
40+
Control response = new BaseControl();
41+
given(this.ldapContext.getResponseControls()).willReturn(new Control[] { response });
42+
processor.postProcess(this.ldapContext);
43+
assertThat(processor.handled).isSameAs(response);
44+
}
45+
46+
@Test
47+
void postProcessWhenResponseIsSubclassOfExpectedClassThenHandles() throws Exception {
48+
RecordingProcessor processor = new RecordingProcessor(BaseControl.class);
49+
Control response = new SubControl();
50+
given(this.ldapContext.getResponseControls()).willReturn(new Control[] { response });
51+
processor.postProcess(this.ldapContext);
52+
assertThat(processor.handled).isSameAs(response);
53+
}
54+
55+
@Test
56+
void postProcessWhenResponseIsSuperclassOfExpectedClassThenHandles() throws Exception {
57+
RecordingProcessor processor = new RecordingProcessor(SubControl.class);
58+
Control response = new BaseControl();
59+
given(this.ldapContext.getResponseControls()).willReturn(new Control[] { response });
60+
processor.postProcess(this.ldapContext);
61+
assertThat(processor.handled).isSameAs(response);
62+
}
63+
64+
@Test
65+
void postProcessWhenResponseIsUnrelatedClassThenIgnores() throws Exception {
66+
RecordingProcessor processor = new RecordingProcessor(SubControl.class);
67+
given(this.ldapContext.getResponseControls()).willReturn(new Control[] { new OtherControl() });
68+
processor.postProcess(this.ldapContext);
69+
assertThat(processor.handled).isNull();
70+
}
71+
72+
private static final class RecordingProcessor extends AbstractFallbackRequestAndResponseControlDirContextProcessor {
73+
74+
private Object handled;
75+
76+
private RecordingProcessor(Class<?> responseControlClass) {
77+
setResponseControlClass(responseControlClass);
78+
}
79+
80+
@Override
81+
public Control createRequestControl() {
82+
return null;
83+
}
84+
85+
@Override
86+
protected void handleResponse(Object control) {
87+
this.handled = control;
88+
}
89+
90+
}
91+
92+
private static class BaseControl extends BasicControl {
93+
94+
BaseControl() {
95+
super("1.2.3.4");
96+
}
97+
98+
}
99+
100+
private static final class SubControl extends BaseControl {
101+
102+
}
103+
104+
private static final class OtherControl extends BasicControl {
105+
106+
OtherControl() {
107+
super("1.2.3.5");
108+
}
109+
110+
}
111+
112+
}

‎modules/ROOT/pages/processing-the-dircontext.adoc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,5 @@ public List<String> getAllPersonNames() {
154154

155155
NOTE: For a paged results cookie to continue being valid, you must use the same underlying connection for each paged results call. You can do so by using the `SingleContextSource`, as demonstrated in the preceding example or by using a `ContextSourceTransactionManager` and `TransactionAwareContextSourceProxy`.
156156

157+
NOTE: `hasMore()` returns `false` only after the server returns a `PagedResultsResponseControl` that has no cookie as per RFC 2696; otherwise a loop conditioned on `hasMore()` will continue. As with other LDAP queries, your application should take into account how many pages of results it expects when looping.
158+

0 commit comments

Comments
 (0)