Skip to content

Commit 92dafb9

Browse files
jansupolsenivam
authored andcommitted
Fix to invoke InvocationBuilderListener for each request.
Signed-off-by: jansupol <[email protected]>
1 parent ddd262b commit 92dafb9

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

core-client/src/main/java/org/glassfish/jersey/client/InvocationBuilderListenerStage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -40,18 +40,18 @@
4040
* would be created, this class is utilized.
4141
*/
4242
/* package */ class InvocationBuilderListenerStage {
43-
final Iterator<InvocationBuilderListener> invocationBuilderListenerIterator;
43+
private final Iterable<InvocationBuilderListener> invocationBuilderListenerIterable;
4444

4545
/* package */ InvocationBuilderListenerStage(InjectionManager injectionManager) {
4646
final RankedComparator<InvocationBuilderListener> comparator =
4747
new RankedComparator<>(RankedComparator.Order.ASCENDING);
48-
invocationBuilderListenerIterator = Providers
49-
.getAllProviders(injectionManager, InvocationBuilderListener.class, comparator).iterator();
48+
invocationBuilderListenerIterable = Providers
49+
.getAllProviders(injectionManager, InvocationBuilderListener.class, comparator);
5050
}
5151

5252
/* package */ void invokeListener(JerseyInvocation.Builder builder) {
53-
while (invocationBuilderListenerIterator.hasNext()) {
54-
invocationBuilderListenerIterator.next().onNewBuilder(new InvocationBuilderContextImpl(builder));
53+
for (InvocationBuilderListener invocationBuilderListener : invocationBuilderListenerIterable) {
54+
invocationBuilderListener.onNewBuilder(new InvocationBuilderContextImpl(builder));
5555
}
5656
}
5757

core-client/src/test/java/org/glassfish/jersey/client/spi/InvocationBuilderListenerTest.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -16,22 +16,17 @@
1616

1717
package org.glassfish.jersey.client.spi;
1818

19-
import org.glassfish.jersey.internal.PropertiesDelegate;
2019
import org.hamcrest.Matchers;
2120
import org.hamcrest.MatcherAssert;
2221
import org.junit.jupiter.api.Assertions;
2322
import org.junit.jupiter.api.BeforeEach;
2423
import org.junit.jupiter.api.Test;
2524

26-
import javax.annotation.Priority;
2725
import javax.ws.rs.client.ClientBuilder;
2826
import javax.ws.rs.client.ClientRequestContext;
2927
import javax.ws.rs.client.ClientRequestFilter;
30-
import javax.ws.rs.client.Entity;
3128
import javax.ws.rs.client.WebTarget;
3229
import javax.ws.rs.core.CacheControl;
33-
import javax.ws.rs.core.Configuration;
34-
import javax.ws.rs.core.Context;
3530
import javax.ws.rs.core.HttpHeaders;
3631
import javax.ws.rs.core.MediaType;
3732
import javax.ws.rs.core.Response;
@@ -95,6 +90,20 @@ public void testGetters() {
9590
}
9691
}
9792

93+
@Test
94+
public void testCounter() {
95+
CountingInvocationBuilderListener listener = new CountingInvocationBuilderListener();
96+
target = target.register(listener);
97+
try (Response r = target.request().get()) {
98+
assertDefault(r);
99+
Assertions.assertEquals(1, listener.getCount());
100+
}
101+
try (Response r = target.request().get()) {
102+
assertDefault(r);
103+
Assertions.assertEquals(2, listener.getCount());
104+
}
105+
}
106+
98107
private void assertDefault(Response response) {
99108
Assertions.assertEquals(key(ONE) + "=" + ONE, response.readEntity(String.class));
100109
}
@@ -193,4 +202,17 @@ public void onNewBuilder(InvocationBuilderContext context) {
193202
MatcherAssert.assertThat(context.getCookies().get("Cookie"), Matchers.notNullValue());
194203
}
195204
}
205+
206+
private static class CountingInvocationBuilderListener implements InvocationBuilderListener {
207+
private int counter = 0;
208+
209+
@Override
210+
public void onNewBuilder(InvocationBuilderContext context) {
211+
counter++;
212+
}
213+
214+
public int getCount() {
215+
return counter;
216+
}
217+
}
196218
}

0 commit comments

Comments
 (0)