Thank you Mira!
It turned out that I made a huge mistake. I bound the ResponseFilter to the
ClientResponseFilter class, instead of the MessageBodyFactory to the
MessageBodyWorkers class. I still don't know how did I make this mistake,
but now the worker gets injected.
And you were right, the @Inject annotation works.
Great help, than you again.
Cheers,
Gergely
2014-07-11 17:57 GMT+09:00 Miroslav Fuksa <miroslav.fuksa_at_oracle.com>:
> Hi,
>
> and is your filter called? I mean that ‘filter’ method is called for the
> response.
>
> you just need to register the filter into your client (or WebTarget).
> Using Binder is not necessary. For example:
>
> Client client = ClientBuilder.newClient().register(ResponseFilter.class);
>
> and then make requests based on the client instance.
> Response response = client.target(“http://localhost:8080/foo/bar”
> ).request().get();
>
> MessageBodyWorkers should be then injected. You can use @Context
> annotation to inject the MessageBodyWorkers (it is a JAX-RS type that
> should be injected by @Context but @Inject should work too).
>
> The SitemapBinder as it is implemented supports now only this injection:
> @Inject
> ResponseFilter responseFilter;
>
> You would need to bind it to ClientResponseFilter and not to
> ResponseFilter:
> bind(ResponseFilter.class).to(ClientResponseFilter.class)
>
> But as I wrote this is not needed. Just register the class and interfaces
> will be discovered automatically.
>
> Just a note. Do not register the filter as instance. Then injections would
> not work (instance can be shared by many clients so it is not injected for
> one specific client runtime).
>
>
> more information can be found here:
> https://jersey.java.net/documentation/latest/client.html
>
>
> I hope this helps
> Mira
>
>
>
>
> On Jul 10, 2014, at 11:07 AM, Gergely Nagy <fogetti_at_gmail.com> wrote:
>
> Can somebody confirm if the scenario below is possible or not?
>
> I want to use the following filter in my standalone application (no
> servlet or EE container or whatever).
>
> package somepackage.client.response;
>
> import java.io.IOException;
>
> import javax.inject.Inject;
> import javax.servlet.http.HttpServletResponse;
> import javax.ws.rs.client.ClientRequestContext;
> import javax.ws.rs.client.ClientResponseContext;
> import javax.ws.rs.client.ClientResponseFilter;
>
> import org.glassfish.jersey.message.MessageBodyWorkers;
> import org.jvnet.hk2.annotations.Service;
> import org.slf4j.Logger;
>
> @Service
> public class ResponseFilter implements ClientResponseFilter {
>
> @Inject
> private MessageBodyWorkers workers;
> private final Logger logger;
>
> public ResponseFilter(Logger logger) {
> this.logger = logger;
> }
>
> @Override
> public void filter(ClientRequestContext requestContext,
> ClientResponseContext responseContext)
> throws IOException {
> System.out.println(String.format("MessageBodyWorkers: [%s]",
> workers));
> if (responseValid(responseContext)) {
> return;
> }
> logger.error("Error", new Object(), new Object(), new Object());
> }
>
> private boolean responseValid(ClientResponseContext responseContext) {
> if (responseContext.getStatus() == HttpServletResponse.SC_OK) {
> return true;
> }
> return false;
> }
> }
>
> Then my idea was to register this HK2 AbstractBinder class in my client:
> package somepackage.client;
>
> import org.glassfish.hk2.utilities.binding.AbstractBinder;
>
> import somepackage.client.response.ResponseFilter;
>
> public class SitemapBinder extends AbstractBinder {
>
> @Override
> protected void configure() {
> bind(ResponseFilter.class).to(ResponseFilter.class);
> }
> }
>
> I wanted to do something like this (client is a JerseyClient instance):
> client.register(new SitemapBinder());
>
> But after performing the above nothing happens. I mean
> the MessageBodyWorkers reference is not getting injected however I try it.
>
> What am I missing here? Is it even possible? Or is there a workaround to
> make this work?
>
> Any help is appreciated.
>
> Kind regards,
> Gergely Nagy
>
>
>