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.Questo contenuto non è disponibile nella lingua selezionata.
59.6. Entity Reader Interceptor
Overview Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
This section explains how to implement and register an entity reader interceptor, which enables you to intercept the input stream when reading a message body either on the client side or on the server side. This is typically useful for generic transformations of the request body, such as encryption and decryption, or compressing and decompressing.
ReaderInterceptor interface Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
javax.ws.rs.ext.ReaderInterceptor
interface is defined as follows:
By implementing the
ReaderInterceptor
interface, you can intercept the message body (Entity
object) as it is being read either on the server side or the client side. You can use an entity reader interceptor in either of the following contexts:
- Server side—if bound as a server-side interceptor, the entity reader interceptor intercepts the request message body when it is accessed by the application code (in the matched resource). Depending on the semantics of the REST request, the message body might not be accessed by the matched resource, in which case the reader interceptor is not called.
- Client side—if bound as a client-side interceptor, the entity reader interceptor intercepts the response message body when it is accessed by the client code. If the client code does not explicitly access the response message (for example, by calling the
Response.getEntity
method), the reader interceptor is not called.
ReaderInterceptorContext interface Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
aroundReadFrom
method of ReaderInterceptor
receives one argument of type javax.ws.rs.ext.ReaderInterceptorContext
, which can be used to access both the message body (Entity
object) and message metadata.
The
ReaderInterceptorContext
interface is defined as follows:
InterceptorContext interface Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The
ReaderInterceptorContext
interface also supports the methods inherited from the base InterceptorContext
interface.
The
InterceptorContext
interface is defined as follows:
Sample implementation on the client side Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
To implement an entity reader interceptor for the client side, define a class that implements the
ReaderInterceptor
interface.
For example, the following code shows an example of an entity reader interceptor for the client side (with a priority of 10), which replaces all instances of
COMPANY_NAME
by Red Hat
in the message body of the incoming response:
Sample implementation on the server side Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
To implement an entity reader interceptor for the server side, define a class that implements the
ReaderInterceptor
interface and annotate it with the @Provider
annotation.
For example, the following code shows an example of an entity reader interceptor for the server side (with a priority of 10), which replaces all instances of
COMPANY_NAME
by Red Hat
in the message body of the incoming request:
Binding a reader interceptor on the client side Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
Using the JAX-RS 2.0 client API, you can register an entity reader interceptor directly on a
javax.ws.rs.client.Client
object or on a javax.ws.rs.client.WebTarget
object. Effectively, this means that the reader interceptor can optionally be applied to different scopes, so that only certain URI paths are affected by the interceptor.
For example, the following code shows how to register the
SampleClientReaderInterceptor
interceptor so that it applies to all invocations made using the client
object:
For more details about registering interceptors with a JAX-RS 2.0 client, see ???.
Binding a reader interceptor on the server side Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
To bind a reader interceptor on the server side (that is, to install it into the Apache CXF runtime), perform the following steps:
- Add the
@Provider
annotation to the reader interceptor class, as shown in the following code fragment:Copy to Clipboard Copied! Toggle word wrap Toggle overflow When the reader interceptor 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 reader interceptor 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 interceptor. But in practice, the standard approach is somewhat inflexible and can lead to clashing providers when many libraries are included in a large project.