@Provider public class TracingContainerFilter extends Object implements javax.ws.rs.container.ContainerRequestFilter, javax.ws.rs.container.ContainerResponseFilter
ContainerRequestFilter, and finally, by creating a WriterInterceptor.
Interceptors share a common API for the server and the client side. Whereas filters are primarily intended to manipulate request and response parameters like HTTP headers, URIs and/or HTTP methods, interceptors are intended to manipulate entities, via manipulating entity input/output streams. In case of tracing, the state of the request body is never changed. Therefore, having a request filter makes more sense to intercept incoming request.
The request filter can either be added in the web.xml file, or via
Provider annotation. Here we are skipping the provider annotation, in
case user wants to use the TracingWebFilter by specifying the same in the web.xml.
Note that, Providers are a simply a way of extending and customizing the JAX-RS
runtime. You can think of them as plugins that (potentially) alter the behavior
of the runtime, in order to accomplish a set of (program defined) goals. If this
class is annotated with Provider, and the TracingWebFilter is also added
to the web.xml, then multiple span will be created for the same request. Hence, it
is recommended to add the TracingContainerFilter in the web.xml.
For example, in Jersey, you can specify the provider as below:
<web-app>
.....
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>io.opns.otl.jaxrs.filter.TracingContainerFilter</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
<param-value>io.opns.otl.jaxrs.filter.TracingContainerFilter</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.opns.otl.jaxrs.filter,package.name.for.other.rest.classes</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
.....
.....
</web-app>
| Constructor and Description |
|---|
TracingContainerFilter() |
| Modifier and Type | Method and Description |
|---|---|
void |
filter(javax.ws.rs.container.ContainerRequestContext requestCtx) |
void |
filter(javax.ws.rs.container.ContainerRequestContext requestCtx,
javax.ws.rs.container.ContainerResponseContext responseCtx) |
public void filter(javax.ws.rs.container.ContainerRequestContext requestCtx)
throws IOException
filter in interface javax.ws.rs.container.ContainerRequestFilterIOExceptionpublic void filter(javax.ws.rs.container.ContainerRequestContext requestCtx,
javax.ws.rs.container.ContainerResponseContext responseCtx)
throws IOException
filter in interface javax.ws.rs.container.ContainerResponseFilterIOExceptionCopyright © 2020. All rights reserved.