Skip to content
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

Be able to use invocation interceptor for multiple requests #4467

Merged
merged 1 commit into from
May 20, 2020
Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -104,11 +104,12 @@ static ChainableStage<ClientResponse> createResponseFilteringStage(InjectionMana
*/
private static <T> Iterable<T> prependFilter(T filter, Iterable<T> filters) {
return new Iterable<T>() {
boolean wasInterceptorFilterNext = false;
final Iterator<T> filterIterator = filters.iterator();
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
final Iterator<T> filterIterator = filters.iterator();
boolean wasInterceptorFilterNext = false;

@Override
public boolean hasNext() {
return !wasInterceptorFilterNext || filterIterator.hasNext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -92,12 +92,11 @@ static PostInvocationInterceptorStage createPostInvocationInterceptorStage(Injec
* The stage to execute all the {@link PreInvocationInterceptor PreInvocationInterceptors}.
*/
static class PreInvocationInterceptorStage {
private Iterator<PreInvocationInterceptor> preInvocationInterceptors;
private Iterable<PreInvocationInterceptor> preInvocationInterceptors;
private PreInvocationInterceptorStage(InjectionManager injectionManager) {
final RankedComparator<PreInvocationInterceptor> comparator =
new RankedComparator<>(RankedComparator.Order.DESCENDING);
preInvocationInterceptors = Providers.getAllProviders(injectionManager, PreInvocationInterceptor.class, comparator)
.iterator();
preInvocationInterceptors = Providers.getAllProviders(injectionManager, PreInvocationInterceptor.class, comparator);
}

/**
Expand All @@ -106,7 +105,7 @@ private PreInvocationInterceptorStage(InjectionManager injectionManager) {
* @return {@code true} if there is a {@link PreInvocationInterceptor} yet to be executed.
*/
boolean hasPreInvocationInterceptors() {
return preInvocationInterceptors.hasNext();
return preInvocationInterceptors.iterator().hasNext();
}

/**
Expand All @@ -117,9 +116,10 @@ boolean hasPreInvocationInterceptors() {
void beforeRequest(ClientRequest request) {
final LinkedList<Throwable> throwables = new LinkedList<>();
final ClientRequestContext requestContext = new InvocationInterceptorRequestContext(request);
while (preInvocationInterceptors.hasNext()) {
final Iterator<PreInvocationInterceptor> preInvocationInterceptorIterator = preInvocationInterceptors.iterator();
while (preInvocationInterceptorIterator.hasNext()) {
try {
preInvocationInterceptors.next().beforeRequest(requestContext);
preInvocationInterceptorIterator.next().beforeRequest(requestContext);
} catch (Throwable throwable) {
LOGGER.log(Level.FINE, LocalizationMessages.PREINVOCATION_INTERCEPTOR_EXCEPTION(), throwable);
throwables.add(throwable);
Expand Down Expand Up @@ -151,14 +151,13 @@ public void filter(ClientRequestContext requestContext) throws IOException {
* The stage to execute all the {@link PostInvocationInterceptor PostInvocationInterceptors}.
*/
static class PostInvocationInterceptorStage {
private final Iterator<PostInvocationInterceptor> postInvocationInterceptors;
private final Iterable<PostInvocationInterceptor> postInvocationInterceptors;

private PostInvocationInterceptorStage(InjectionManager injectionManager) {
final RankedComparator<PostInvocationInterceptor> comparator =
new RankedComparator<>(RankedComparator.Order.ASCENDING);
final Iterable<PostInvocationInterceptor> postInvocationInterceptors
postInvocationInterceptors
= Providers.getAllProviders(injectionManager, PostInvocationInterceptor.class, comparator);
this.postInvocationInterceptors = postInvocationInterceptors.iterator();
}

/**
Expand All @@ -167,7 +166,7 @@ private PostInvocationInterceptorStage(InjectionManager injectionManager) {
* @return {@code true} if there is a {@link PostInvocationInterceptor} yet to be executed.
*/
boolean hasPostInvocationInterceptor() {
return postInvocationInterceptors.hasNext();
return postInvocationInterceptors.iterator().hasNext();
}

private ClientResponse afterRequestWithoutException(Iterator<PostInvocationInterceptor> postInvocationInterceptors,
Expand Down Expand Up @@ -241,8 +240,8 @@ ClientResponse afterRequest(ClientRequest request, ClientResponse response, Thro
= new PostInvocationExceptionContext(response, previousException);
final InvocationInterceptorRequestContext requestContext = new InvocationInterceptorRequestContext(request);
return previousException != null
? afterRequestWithException(postInvocationInterceptors, requestContext, exceptionContext)
: afterRequestWithoutException(postInvocationInterceptors, requestContext, exceptionContext);
? afterRequestWithException(postInvocationInterceptors.iterator(), requestContext, exceptionContext)
: afterRequestWithoutException(postInvocationInterceptors.iterator(), requestContext, exceptionContext);
}

private static boolean resolveResponse(InvocationInterceptorRequestContext requestContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,19 @@ public void testPreThrowsPostResolves() {
}
}

@Test
public void testPostInvocationInterceptorIsHitforEachRequest() {
final Invocation.Builder builder = ClientBuilder.newBuilder()
.register(new CounterPostInvocationInterceptor((a, b) -> true, (a, b) -> false))
.register(new AbortRequestFilter()).build().target(URL).request();
for (int i = 1; i != 10; i++) {
try (Response response = builder.get()) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(i, counter.get());
}
}
}

private static class TestInvocationCallback implements InvocationCallback<Response> {
private final Predicate<Response> responsePredicate;
private final Predicate<Throwable> throwablePredicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ public void testMultiExceptionInPreInvocationInterceptor() {
}
}

@Test
public void testPreInvocationInterceptorIsHitforEachRequest() {
final Invocation.Builder builder = ClientBuilder.newBuilder()
.register(new CounterPreInvocationInterceptor(counter -> true))
.register(new AbortRequestFilter()).build().target(URL).request();
for (int i = 1; i != 10; i++) {
try (Response response = builder.get()) {
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(i, counter.get());
}
}
}

private static class AbortRequestFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
Expand Down