Skip to content

Conversation

@andymc12
Copy link
Contributor

@andymc12 andymc12 commented Jan 4, 2020

Implements CXF-8188 - injecting @Context and @Inject into custom ClientHeadersFactory implementations.

This tests JAX-RS injection. The MP Rest Client 2.0 TCK will test CDI injection.

@andymc12 andymc12 self-assigned this Jan 4, 2020
public MultivaluedMap<String, String> update(MultivaluedMap<String, String> incomingHeaders,
MultivaluedMap<String, String> clientOutgoingHeaders) {

System.out.println("InjectClientHeadersFactory update");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug leftovers? ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - I'll remove that. Thanks!

@reta
Copy link
Member

reta commented Jan 5, 2020

@andymc12 please correct me if I am wrong, if we consider CDI-based deployments, the instances of the MicroProfileClientProxyImpl (and others) should be created as CDI beans, right?

@andymc12
Copy link
Contributor Author

andymc12 commented Jan 6, 2020

if we consider CDI-based deployments, the instances of the MicroProfileClientProxyImpl (and others) should be created as CDI beans, right?

@reta yes, it is done indirectly by creating the client proxy instance using the CxfTypeSafeClientBuilder from the RestClientBean / RestClientExtension.

@reta
Copy link
Member

reta commented Jan 6, 2020

Excellent, thanks @andymc12 , so here is my follow up question / concern: why won't we make CxfTypeSafeClientBuilder CDI-aware (using extension or alike) so the proxies are going to be managed by BeanManager and as such, could use @Inject annotation directly? In this case, the integration should work in all scenarios (managed, standalone, ...). Does it make sense?

@andymc12
Copy link
Contributor Author

andymc12 commented Jan 6, 2020

@reta I think CDI is already managing the proxy instances. The extension manages the creation of the proxy (it has to provide additional config via the MP annotations, like @RegisterProvider, @RegisterClientHeaders, etc.), but once it is created, then it turns the proxy over to CDI to manage.

The problem with the ClientHeadersFactory instance is that it is associated with the client interface proxy by annotation, not injected. The other problem is that we want the ClientHeadersFactory to be injected with any JAX-RS contextual info too.

Does that makes sense? Thanks again for your review!

@andymc12 andymc12 force-pushed the 8188-injectIntoClientHeadersFactory branch from 04aaa80 to 0a62f12 Compare January 7, 2020 23:10
@reta
Copy link
Member

reta commented Jan 12, 2020

hey @andymc12, sorry for the delay, finally found the time to finish the review:

  • CDIUtils and CDIFacade: we already use reflection-base BeanManager detection in CDIInterceptorWrapper::createWrapper, it think we should stick to one way to do it
  • may be MicroProfileClientFactoryBean is a better place for dealing with CDI & ClientHeadersFactory, which could be passed to MicroProfileClientProxyImpl in a similar way as CDIInterceptorWrapper. The BeanManager detection logic (whichever we pick), could be reused for both of them.

What do you think?

@andymc12 andymc12 force-pushed the 8188-injectIntoClientHeadersFactory branch from 0a62f12 to 62566ce Compare January 13, 2020 22:59
@andymc12
Copy link
Contributor Author

@reta

CDIUtils and CDIFacade: we already use reflection-base BeanManager detection in CDIInterceptorWrapper::createWrapper, it think we should stick to one way to do it

Agreed. I updated the CDIInterceptorWrapper to use CDIFacade/CDIUtils.

may be MicroProfileClientFactoryBean is a better place for dealing with CDI & ClientHeadersFactory, which could be passed to MicroProfileClientProxyImpl in a similar way as CDIInterceptorWrapper. The BeanManager detection logic (whichever we pick), could be reused for both of them.

My latest changes reuse the BeanManager detection logic. I'm not sure if moving the ClientHeadersFactory logic into MicroProfileClientFactoryBean will work since that is when the client proxy is being created. It's possible the the rest client instance is created in one JAX-RS context (or outside of a JAX-RS request entirely) and then used in a different JAX-RS context. So then the injection would need to occur on each request, right?

Thanks again!

@reta
Copy link
Member

reta commented Jan 14, 2020

Thanks @andymc12 , regarding

My latest changes reuse the BeanManager detection logic. I'm not sure if moving the ClientHeadersFactory logic into MicroProfileClientFactoryBean will work since that is when the client proxy is being created. It's possible the the rest client instance is created in one JAX-RS context (or outside of a JAX-RS request entirely) and then used in a different JAX-RS context. So then the injection would need to occur on each request, right?

you are right, it might happen, but probably the same would apply to CDIInterceptorWrapper: it will pick up the interceptors from the different context. I don't know what is the best option to be fair, just an opinion if we have a single place to make the decisions (the factory bean), it would be much easier to reason about the outcome (proxy) and maintain it. The uncertainty with contexts may cause issues but I would leave the decision to you, I think you have a better view of the picture.

Signed-off-by: Andy McCright <j.andrew.mccright@gmail.com>
- Caches BeanManager on the bus
- Centralizes CDI-accessing mechanism in CDIUtils/CDIFacade

Signed-off-by: Andy McCright <j.andrew.mccright@gmail.com>
@andymc12 andymc12 force-pushed the 8188-injectIntoClientHeadersFactory branch 2 times, most recently from e72f483 to b0bd49f Compare January 15, 2020 22:05
Code review comment - releasing contexts when the client
proxy is closed.

Signed-off-by: Andy McCright <j.andrew.mccright@gmail.com>
@andymc12 andymc12 force-pushed the 8188-injectIntoClientHeadersFactory branch from b0bd49f to 5134b3e Compare January 16, 2020 01:26
@andymc12 andymc12 merged commit 700f95c into apache:master Jan 16, 2020
ppalaga pushed a commit to ppalaga/cxf that referenced this pull request Nov 12, 2024
…ache.maven.plugins-maven-dependency-plugin-3.8.0

Bump org.apache.maven.plugins:maven-dependency-plugin from 3.7.1 to 3.8.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants