JAX-RS server filters
Use a ContainerResponseFilter
from the JAX-RS API 2.0 to intercept responses or server side:
@Provider
public class CustomResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
// Intercept the HTTP response and get the status code
int status = responseContext.getStatus()
}
}
The JAX-RS 2.0 API also provide the ContainerRequestFilter
to intercept requests:
@Provider
public class CustomRequestFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// Intercept the HTTP request
}
}
To bind the filters to resource classes and/or methods, use name binding annotations.
Registering server filters in Apache CXF
According to the Apache CXF documentation, the filters must be registered in the cxf.xml
configuration file. This file must be in the classpath of your application.
Here's an example extracted from the documentation of what your CXF configuration file can be like when registering a filter:
<beans>
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<bean class="org.CustomerService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="authorizationFilter" />
</jaxrs:providers>
<bean id="authorizationFilter" class="com.bar.providers.AuthorizationFilter">
<!-- authorization bean properties -->
</bean>
</jaxrs:server>
</beans>
For more details, check the CXF documentation about configuration.