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

Changes to InterceptorInvocationContext related to the integration with Helidon #5311

Merged
merged 2 commits into from
Apr 27, 2023
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
5 changes: 5 additions & 0 deletions ext/microprofile/mp-rest-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
<artifactId>jersey-media-sse</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
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, 2023 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 @@ -31,7 +31,7 @@
* Invokes all interceptors bound to the target.
*
* This approach needs to be used due to CDI does not handle properly interceptor invocation
* on proxy instances.
* on proxy instances. This class is thread safe.
*
* @author David Kral
*/
Expand All @@ -42,8 +42,8 @@ class InterceptorInvocationContext implements InvocationContext {
private final Map<String, Object> contextData;
private final List<InvocationInterceptor> interceptors;
private final WebTarget classLevelWebTarget;
private Object[] args;
private int currentPosition;
private volatile Object[] args;
private final int currentPosition;

/**
* Creates new instance of InterceptorInvocationContext.
Expand All @@ -57,14 +57,23 @@ class InterceptorInvocationContext implements InvocationContext {
MethodModel methodModel,
Method method,
Object[] args) {
this.contextData = new HashMap<>();
this.currentPosition = 0;
this.contextData = new HashMap<>();
this.methodModel = methodModel;
this.method = method;
this.args = args;
this.classLevelWebTarget = classLevelWebTarget;
this.interceptors = methodModel.getInvocationInterceptors();
}

InterceptorInvocationContext(InterceptorInvocationContext other, int currentPosition) {
this.currentPosition = currentPosition;
this.contextData = other.contextData;
this.methodModel = other.methodModel;
this.method = other.method;
this.args = other.args;
this.classLevelWebTarget = other.classLevelWebTarget;
this.interceptors = other.interceptors;
}

@Override
Expand Down Expand Up @@ -102,10 +111,21 @@ public Map<String, Object> getContextData() {
return contextData;
}

/**
* This method shall create the next invocation context using {@code position + 1}
* and store it in the {@code contextData} map. This is currently used by Helidon's
* fault tolerance implementation to get around a problem with CDI's default invocation
* context {@code WeldInvocationContextImpl} not correctly supporting async calls.
*
* @return value returned by intercepted method.
*/
@Override
public Object proceed() {
InvocationContext nextContext = new InterceptorInvocationContext(this, currentPosition + 1);
contextData.put(getClass().getName(), nextContext); // accessible to FT interceptor

if (currentPosition < interceptors.size()) {
return interceptors.get(currentPosition++).intercept(this);
return interceptors.get(currentPosition).intercept(nextContext);
} else {
return methodModel.invokeMethod(classLevelWebTarget, method, args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.microprofile.restclient;

import javax.ws.rs.client.WebTarget;

import java.lang.reflect.Method;
import java.util.Collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.glassfish.jersey.microprofile.restclient.InterceptorInvocationContext.InvocationInterceptor;

class InterceptorInvocationContextTest {

@Test
void testContextDataAfterProceed() throws NoSuchMethodException {
WebTarget webTarget = Mockito.mock(WebTarget.class);
MethodModel methodModel = Mockito.mock(MethodModel.class);
InvocationInterceptor invocationInterceptor = Mockito.mock(InvocationInterceptor.class);
Mockito.when(methodModel.getInvocationInterceptors())
.thenReturn(Collections.singletonList(invocationInterceptor));

Method method = getClass().getMethod("method");
InterceptorInvocationContext c = new InterceptorInvocationContext(
webTarget, methodModel, method, new Object[]{});

Assertions.assertEquals(0, c.getContextData().size());
c.proceed();
Assertions.assertEquals(1, c.getContextData().size());
Assertions.assertTrue(c.getContextData().containsKey(InterceptorInvocationContext.class.getName()));
}

public static Object method() {
return null;
}
}