markthegrea
markthegrea

Reputation: 3841

How can I hook up a filter in CXF Jaxrs?

I would like to intercept the request both before and after the client call.

Given:

Form formData = new Form();
    formData.param("grant_type", "client_credentials");

    List<Object> providers = new ArrayList();
    providers.add(new GsonMessageBodyProvider());
    providers.add(new RestLogger ()); <--doesn't work

    WebClient client = WebClient.create("https://blah.com", providers);

    // sets timeouts.
    HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
    conduit.getClient().setReceiveTimeout(1000);
    conduit.getClient().setConnectionTimeout(1000);

    client.path("token.oauth2");
    client.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept(MediaType.APPLICATION_JSON);

    WebClient wc = WebClient.fromClient(client);// thread safe
    BearerTokenResponse r = wc.post(formData, BearerTokenResponse.class);
    System.out.println(ToStringBuilder.reflectionToString(r));

I wrote a class like this that I thought would work:

    public class RestLogger implements ContainerRequestFilter, ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        System.out.println("request");
    }
    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        System.out.println("response");

    }
}

But it does not fire. Any help? I am executing this in a Storm cluster so I can't use Annotations. I need to hook this up programmatically. I am using 3.1.10 (newest)

    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-client -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.1.10</version>
    </dependency>

Upvotes: 0

Views: 1463

Answers (1)

cassiomolin
cassiomolin

Reputation: 130837

To intercept requests and responses on client side, use ClientRequestFilter and ClientResponseFilter.

To intercept requests and responses on server side, use ContainerRequestFilter and ContainerResponseFilter.

Upvotes: 2

Related Questions