From 458528a09f617f6a71cf028a24668f1749f02f84 Mon Sep 17 00:00:00 2001 From: Jan Supol Date: Thu, 14 May 2020 16:38:14 +0200 Subject: [PATCH] Be able to use invocation interceptor for multiple requests within a singe ClientRuntime Signed-off-by: Jan Supol --- .../jersey/client/ClientFilteringStages.java | 7 +++--- .../client/InvocationInterceptorStages.java | 25 +++++++++---------- .../spi/PostInvocationInterceptorTest.java | 13 ++++++++++ .../spi/PreInvocationInterceptorTest.java | 13 ++++++++++ 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/core-client/src/main/java/org/glassfish/jersey/client/ClientFilteringStages.java b/core-client/src/main/java/org/glassfish/jersey/client/ClientFilteringStages.java index c53f871c02..6555e92683 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/ClientFilteringStages.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/ClientFilteringStages.java @@ -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 @@ -104,11 +104,12 @@ static ChainableStage createResponseFilteringStage(InjectionMana */ private static Iterable prependFilter(T filter, Iterable filters) { return new Iterable() { - boolean wasInterceptorFilterNext = false; - final Iterator filterIterator = filters.iterator(); @Override public Iterator iterator() { return new Iterator() { + final Iterator filterIterator = filters.iterator(); + boolean wasInterceptorFilterNext = false; + @Override public boolean hasNext() { return !wasInterceptorFilterNext || filterIterator.hasNext(); diff --git a/core-client/src/main/java/org/glassfish/jersey/client/InvocationInterceptorStages.java b/core-client/src/main/java/org/glassfish/jersey/client/InvocationInterceptorStages.java index 670d9e0680..8616728ac4 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/InvocationInterceptorStages.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/InvocationInterceptorStages.java @@ -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 @@ -92,12 +92,11 @@ static PostInvocationInterceptorStage createPostInvocationInterceptorStage(Injec * The stage to execute all the {@link PreInvocationInterceptor PreInvocationInterceptors}. */ static class PreInvocationInterceptorStage { - private Iterator preInvocationInterceptors; + private Iterable preInvocationInterceptors; private PreInvocationInterceptorStage(InjectionManager injectionManager) { final RankedComparator comparator = new RankedComparator<>(RankedComparator.Order.DESCENDING); - preInvocationInterceptors = Providers.getAllProviders(injectionManager, PreInvocationInterceptor.class, comparator) - .iterator(); + preInvocationInterceptors = Providers.getAllProviders(injectionManager, PreInvocationInterceptor.class, comparator); } /** @@ -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(); } /** @@ -117,9 +116,10 @@ boolean hasPreInvocationInterceptors() { void beforeRequest(ClientRequest request) { final LinkedList throwables = new LinkedList<>(); final ClientRequestContext requestContext = new InvocationInterceptorRequestContext(request); - while (preInvocationInterceptors.hasNext()) { + final Iterator 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); @@ -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 postInvocationInterceptors; + private final Iterable postInvocationInterceptors; private PostInvocationInterceptorStage(InjectionManager injectionManager) { final RankedComparator comparator = new RankedComparator<>(RankedComparator.Order.ASCENDING); - final Iterable postInvocationInterceptors + postInvocationInterceptors = Providers.getAllProviders(injectionManager, PostInvocationInterceptor.class, comparator); - this.postInvocationInterceptors = postInvocationInterceptors.iterator(); } /** @@ -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 postInvocationInterceptors, @@ -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, diff --git a/core-client/src/test/java/org/glassfish/jersey/client/spi/PostInvocationInterceptorTest.java b/core-client/src/test/java/org/glassfish/jersey/client/spi/PostInvocationInterceptorTest.java index 1f5ceda7a8..b5f362402e 100644 --- a/core-client/src/test/java/org/glassfish/jersey/client/spi/PostInvocationInterceptorTest.java +++ b/core-client/src/test/java/org/glassfish/jersey/client/spi/PostInvocationInterceptorTest.java @@ -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 { private final Predicate responsePredicate; private final Predicate throwablePredicate; diff --git a/core-client/src/test/java/org/glassfish/jersey/client/spi/PreInvocationInterceptorTest.java b/core-client/src/test/java/org/glassfish/jersey/client/spi/PreInvocationInterceptorTest.java index b04a4f7b5c..905cb57ddf 100644 --- a/core-client/src/test/java/org/glassfish/jersey/client/spi/PreInvocationInterceptorTest.java +++ b/core-client/src/test/java/org/glassfish/jersey/client/spi/PreInvocationInterceptorTest.java @@ -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 {