Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Este conteúdo não está disponível no idioma selecionado.
58.2. Container Request Filter
Overview Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
This section explains how to implement and register a container request filter, which is used to intercept an incoming request message on the server (container) side. Container request filters are often used to process headers on the server side and can be used for any kind of generic request processing (that is, processing that is independent of the particular resource method called).
Moreover, the container request filter is something of a special case, because it can be installed at two distinct extension points:
PreMatchContainerRequest
(before the resource matching step); and ContainerRequest
(after the resource matching step).
ContainerRequestFilter interface Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The
javax.ws.rs.container.ContainerRequestFilter
interface is defined as follows:
By implementing the
ContainerRequestFilter
interface, you can create a filter for either of the following extension points on the server side:
PreMatchContainerRequest
ContainerRequest
ContainerRequestContext interface Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
The
filter
method of ContainerRequestFilter
receives a single argument of type javax.ws.rs.container.ContainerRequestContext
, which can be used to access the incoming request message and its related metadata. The ContainerRequestContext
interface is defined as follows:
Sample implementation for PreMatchContainerRequest filter Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To implement a container request filter for the
PreMatchContainerRequest
extension point (that is, where the filter is executed prior to resource matching), define a class that implements the ContainerRequestFilter
interface, making sure to annotate the class with the @PreMatching
annotation (to select the PreMatchContainerRequest
extension point).
For example, the following code shows an example of a simple container request filter that gets installed in the
PreMatchContainerRequest
extension point, with a priority of 20:
Sample implementation for ContainerRequest filter Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To implement a container request filter for the
ContainerRequest
extension point (that is, where the filter is executed after resource matching), define a class that implements the ContainerRequestFilter
interface, without the @PreMatching
annotation.
For example, the following code shows an example of a simple container request filter that gets installed in the
ContainerRequest
extension point, with a priority of 30:
Injecting ResourceInfo Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
At the
ContainerRequest
extension point (that is, after resource matching has occurred), it is possible to access the matched resource class and resource method by injecting the ResourceInfo
class. For example, the following code shows how to inject the ResourceInfo
class as a field of the ContainerRequestFilter
class:
Aborting the invocation Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
It is possible to abort a server-side invocation by creating a suitable implementation of a container request filter. Typically, this is useful for implementing security features on the server side: for example, to implement an authentication feature or an authorization feature. If an incoming request fails to authenticate successfully, you could abort the invocation from within the container request filter.
For example, the following pre-matching feature attempts to extract a username and password from the URI's query parameters and calls an authenticate method to check the username and password credentials. If the authentication fails, the invocation is aborted by calling
abortWith
on the ContainerRequestContext
object, passing the error response that is to be returned to the client.
Binding the server request filter Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To bind a server request filter (that is, to install it into the Apache CXF runtime), perform the following steps:
- Add the
@Provider
annotation to the container request filter class, as shown in the following code fragment:Copy to Clipboard Copied! Toggle word wrap Toggle overflow When the container request filter implementation is loaded into the Apache CXF runtime, the REST implementation automatically scans the loaded classes to search for the classes marked with the@Provider
annotation (the scanning phase). - When defining a JAX-RS server endpoint in XML (for example, see Section 16.1, “Configuring JAX-RS Server Endpoints”), add the server request filter to the list of providers in the
jaxrs:providers
element.Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThis step is a non-standard requirement of Apache CXF. Strictly speaking, according to the JAX-RS standard, the@Provider
annotation should be all that is required to bind the filter. But in practice, the standard approach is somewhat inflexible and can lead to clashing providers when many libraries are included in a large project.