Java Reference
In-Depth Information
WebTarget Injection
A WebTarget can be injected into any JAX-RS managed resource by specifying the @Uri annotation and passing the
WebTarget URI. In following example, where a WebTarget resource is injected into a JAX-RS resource to demonstrate
this concept:
@Path("/orderservice")
public class OrderService {
@Uri("order/{id}")
WebTarget orderId;
//...
}
Filters and Interceptors
The concept of filters and interceptors is analogous to the post office processing your mail before it comes to your
address. Rather than a message being delivered directly from point A to point B, it is first routed to one or more postal
offices where it is further processed before reaching point B. Web resource filters and interceptors apply that same
concept to requests or responses that are being processed via a web service. If a filter or interceptor is bound to a web
resource, then it will be invoked at some point in the life cycle of a request or response to that web resource. The type
of filter or interceptor determines at what point in the life cycle it is applied. Interceptors (otherwise known as entity
interceptors ) wrap around a method invocation at a specified extension point. Filters, on the other hand, execute code
at a specified extension point, but they are not wrapped around methods. In the next few sections, you will take a
closer look at each and how they are used.
Filters
An extension point is an interface that includes a method, which is responsible for filtering or intercepting the request
or response. Filters have four such extension point interfaces: ClientRequestFilter , ClientResponseFilter ,
ContainerRequestFilter , and ContainerResponseFilter . The name of the extension point helps describe to
what a filter is applied and at what point. ClientRequestFilter and ClientResponseFilter are for use with
the JAX-RS Client API. A ClientRequestFilter is applied before an HTTP request is delivered to the network,
and a ClientResponseFilter is applied when a server response is received and before control is returned to the
application. ContainerRequestFilter and ContainerResponseFilter classes are for use with the JAX-RS Server API.
Similar to the client-side filters, a ContainerRequestFilter is applied upon receiving a request from a client, and
a ContainerResponseFilter is applied before the HTTP response is delivered.
Before going any further, let's take a look at an example of a client filter, as well as an example of a server filter.
The client filter will be used to print a message to the server log when a request is received or before a response is sent.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
 
Search WWH ::




Custom Search