fareed
fareed

Reputation: 3082

JAX-RS Client Filter to Modify Header Before Request is Dispatched to server

In JAX-RS (RestEasy), I want to implement a client filter that modifies the header before sending the request so I don't do this manually for every single call.

Currently I'm doing this in the receiving end to intercept requests before arriving to the resource.

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        // read header
    }

Now I know this (Correct me if I'm wrong):

In the receiving end, ContainerRequestFilter can be used before the request arrives to the resource and get the request.

But I want to implement this in the client side, to modify the header before the request is ever sent to the server. Can the same server filter be used or there is something similar to for the client?

Upvotes: 0

Views: 3205

Answers (1)

Luca Montagnoli
Luca Montagnoli

Reputation: 302

You must register a ClientRequestFilter into your Client

Client client = ClientBuilder.newClient().register(MyFilter.class);

@Provider
public class MyFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext ctx) throws IOException {
        // modify header before send: ctx.getHeaders() 
    }
}  

Upvotes: 4

Related Questions