Client Response Filter

The Client Response Filter work in the phase next to the request filters phase and elaborate the response to give it to the client. In this example, we will see a collaborative work between the client request and response filters. Here's a sample of client request filter that set a header content-type to text/HTML:

@Provider
@Priority(ENTITY_CODER)
public class MyClientRequestFilter implements ClientRequestFilter {
...
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
...
requestContext.getHeaders().add(CONTENT_TYPE_STRING, TEXT_HTML);
}
}

This is a Client Response Filter that takes the created header and sets it to the response:

@Provider
@Priority(ENTITY_CODER + 2)
public class MyClientResponseFilter implements ClientResponseFilter {
...
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
...
responseContext.getHeaders().putSingle(CONTENT_TYPE_STRING, requestContext.getHeaderString(CONTENT_TYPE_STRING));
}
}

The client will be something like this:

Client client = newClient();
client.register(new MyClientResponseFilter());
client.register(new MyClientRequestFilter());
WebTarget target = client.target(url + "myjaxrs/simple/");
WebTarget resourceTarget = target.path("/valuesget");
resourceTarget = resourceTarget.queryParam("OrderID", "111").queryParam("UserName", "Luke");
Invocation invocation = resourceTarget.request().buildGet();
Response response = invocation.invoke();

As for the container components, the filters are executed in a chain of components in a mode very similar to the interceptors seen in Chapter 2, Working with Dependency Injection. If you want to break the chain, there is a very useful method in the ClientRequestContext interface:

@Provider
@Priority(ENTITY_CODER - 1)
public class BlockChainFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
ResponseBuilder responseBuilder = serverError();
Response response = responseBuilder.status(BAD_REQUEST).build();
requestContext.abortWith(response);
}
}

This filter creates an HTTP response through the javax.ws.rs.core tools and then executes the abortWith method. This method blocks the request chain, directly passing a response to give to the client. To test the chain, we can register this new filter to our client:

Client client = newClient();
client.register(new MyClientResponseFilter());
client.register(new MyClientRequestFilter());
client.register(new BlockChainFilter());

BlockChainFilter has a priority lower than MyClientRequestFilter, so it will be started for the first time, and it will block the chain, and the MyClientRequestFilter never will be executed.

During the execution, MyClientResponseFilter will not find the header created by the request, so the content-type header will not be created in the response.

We can register components either as classes or instances. The difference is that if we have the ready instance, the same instance will be used always. Using the class, the instance will be created and managed by the container.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.122.162