Skip to content

Commit 97d5f7f

Browse files
committed
Fix filtering in HttpServiceProxyRegistryFactoryBean
Closes gh-34867
1 parent 733e695 commit 97d5f7f

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

spring-web/src/main/java/org/springframework/web/service/registry/HttpServiceProxyRegistryFactoryBean.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private <CB> HttpExchangeAdapter initExchangeAdapter() {
221221

222222
@Override
223223
public String toString() {
224-
return getClass().getSimpleName() + "[id=" + name() + "]";
224+
return getClass().getSimpleName() + "[name=" + name() + "]";
225225
}
226226

227227
}
@@ -234,21 +234,24 @@ private static final class DefaultGroups<CB> implements HttpServiceGroupConfigur
234234

235235
private final Set<ProxyHttpServiceGroup> groups;
236236

237+
private final Predicate<HttpServiceGroup> defaultFilter;
238+
237239
private Predicate<HttpServiceGroup> filter;
238240

239241
DefaultGroups(Set<ProxyHttpServiceGroup> groups, HttpServiceGroup.ClientType clientType) {
240242
this.groups = groups;
241-
this.filter = group -> group.clientType().equals(clientType);
243+
this.defaultFilter = (group -> group.clientType().equals(clientType));
244+
this.filter = this.defaultFilter;
242245
}
243246

244247
@Override
245248
public HttpServiceGroupConfigurer.Groups<CB> filterByName(String... groupNames) {
246-
return filter(group -> Arrays.stream(groupNames).anyMatch(id -> id.equals(group.name())));
249+
return filter(group -> Arrays.stream(groupNames).anyMatch(name -> name.equals(group.name())));
247250
}
248251

249252
@Override
250253
public HttpServiceGroupConfigurer.Groups<CB> filter(Predicate<HttpServiceGroup> predicate) {
251-
this.filter = this.filter.or(predicate);
254+
this.filter = this.filter.and(predicate);
252255
return this;
253256
}
254257

@@ -274,8 +277,11 @@ public void configure(
274277
BiConsumer<HttpServiceGroup, CB> clientConfigurer,
275278
BiConsumer<HttpServiceGroup, HttpServiceProxyFactory.Builder> proxyFactoryConfigurer) {
276279

277-
this.groups.stream().filter(this.filter).forEach(group ->
278-
group.apply(clientConfigurer, proxyFactoryConfigurer));
280+
this.groups.stream().filter(this.filter)
281+
.forEach(group -> group.apply(clientConfigurer, proxyFactoryConfigurer));
282+
283+
// Reset filter
284+
this.filter = this.defaultFilter;
279285
}
280286
}
281287

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2002-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.web.service.registry;
18+
19+
import java.util.List;
20+
import java.util.function.Predicate;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
import org.springframework.util.LinkedMultiValueMap;
26+
import org.springframework.util.MultiValueMap;
27+
import org.springframework.web.client.RestClient;
28+
import org.springframework.web.client.support.RestClientHttpServiceGroupConfigurer;
29+
import org.springframework.web.service.registry.echo.EchoA;
30+
import org.springframework.web.service.registry.echo.EchoB;
31+
import org.springframework.web.service.registry.greeting.GreetingA;
32+
import org.springframework.web.service.registry.greeting.GreetingB;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Unit tests for {@link HttpServiceProxyRegistryFactoryBean}.
38+
* @author Rossen Stoyanchev
39+
*/
40+
public class HttpServiceProxyRegistryFactoryBeanTests {
41+
42+
@Test
43+
void twoGroups() {
44+
45+
GroupsMetadata groupsMetadata = new GroupsMetadata();
46+
47+
String echoName = "echo";
48+
String greetingName = "greeting";
49+
50+
groupsMetadata.getOrCreateGroup(echoName, HttpServiceGroup.ClientType.REST_CLIENT)
51+
.httpServiceTypeNames().addAll(List.of(EchoA.class.getName(), EchoB.class.getName()));
52+
53+
groupsMetadata.getOrCreateGroup(greetingName, HttpServiceGroup.ClientType.REST_CLIENT)
54+
.httpServiceTypeNames().addAll(List.of(GreetingA.class.getName(), GreetingB.class.getName()));
55+
56+
Predicate<HttpServiceGroup> echoFilter = group -> group.name().equals(echoName);
57+
Predicate<HttpServiceGroup> greetingFilter = group -> group.name().equals(greetingName);
58+
59+
TestConfigurer testConfigurer = new TestConfigurer(List.of(echoFilter, greetingFilter));
60+
61+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
62+
applicationContext.registerBean(TestConfigurer.class, () -> testConfigurer);
63+
applicationContext.refresh();
64+
65+
HttpServiceProxyRegistryFactoryBean factoryBean = new HttpServiceProxyRegistryFactoryBean(groupsMetadata);
66+
factoryBean.setApplicationContext(applicationContext);
67+
factoryBean.setBeanClassLoader(getClass().getClassLoader());
68+
factoryBean.afterPropertiesSet();
69+
70+
HttpServiceProxyRegistry registry = factoryBean.getObject();
71+
assertThat(registry.getGroupNames()).containsExactlyInAnyOrder(echoName, greetingName);
72+
assertThat(registry.getClientTypesInGroup(echoName)).containsExactlyInAnyOrder(EchoA.class, EchoB.class);
73+
assertThat(registry.getClientTypesInGroup(greetingName)).containsExactlyInAnyOrder(GreetingA.class, GreetingB.class);
74+
75+
assertThat(testConfigurer.invocations)
76+
.containsKeys(echoFilter, greetingFilter)
77+
.containsEntry(echoFilter, List.of(echoName))
78+
.containsEntry(greetingFilter, List.of(greetingName));
79+
}
80+
81+
82+
private static class TestConfigurer implements RestClientHttpServiceGroupConfigurer {
83+
84+
private final List<Predicate<HttpServiceGroup>> filters;
85+
86+
private final MultiValueMap<Predicate<HttpServiceGroup>, String> invocations = new LinkedMultiValueMap<>();
87+
88+
TestConfigurer(List<Predicate<HttpServiceGroup>> filters) {
89+
this.filters = filters;
90+
}
91+
92+
@Override
93+
public void configureGroups(Groups<RestClient.Builder> groups) {
94+
filters.forEach(filter -> groups.filter(filter)
95+
.configureClient((group, builder) -> invocations.add(filter, group.name())));
96+
}
97+
}
98+
99+
}

0 commit comments

Comments
 (0)