08. Filters & Interceptors. CDI . Conclusions

As Java Brains says we should distinguish several components that can be used both from client and server in order intercept the request or response and modify them.

1 Interceptors, Filters and MessageBody

  • Interceptors manipulate entities (body) in input and output streams while filters only manipulate the request or response params like headers, URIs etc
  • There are 2 kinds of interceptors ReaderInterceptor and WriterInterceptor. To create an interceptor, you should implement one of the 2 interfaces. One of the possible use is to GZip the body for slow communications.There are 2 kinds too of filters Container RequestFilters and ContainerResponseFilters one for the request and the other for the response. the basic usages are Logging, security..
  • Filters and interceptors can work both on the client and server side.
  • On the client side, we have these filters: ClientRequestFilter and ClientResponseFilter.
  • We also have MessageBodyReader and MessageBodyWriter for the body manipulation.
Here is a schema of a Request and a Response and the order these elements interact



2. CDI Injection

JAX-RS Resources can be converted to Singleton and Stateless beans. This can be achieved by means of annotations. Here are some examples:

2.1 Stateless bean


1
2
3
@Stateless
@Path("stateless-bean")
public class StatlesResource() {}

2.2 Singleton bean


1
2
3
@Singleton
@Path("singleton-bean")
public class SingletonResource() {}

2.3 Request scoped bean

This is the default, so it is not necessary o annotate i 


1
2
3
@RequestScoped
@Path("/employee/{id}")
public class RequestScopedResource() {}

3. Conclusions

We have seen:

  • Create a JAX-RS application that extends Application and may have an annotation @ApplicationPath("somePath")
  • Created Resources with annotations like @Path(), @GET or @Produces
  • Resource lifecycle, Singleton etc.
  • Custom PartamConverters that implement ParamConverterProviders and provide a class conversion. The annotation used is @Provider and should implement ParamConverterProvider interface.
  • MessageBodyReaders and MessageBodyWriters to convert data types that should implement the correspònding interface and use the @Provider annotation.
  • Create our own Custom Media Types with the annotation @Produces in the MessageBodyWriter
  • Created a JAX-RS client with ClientBuilder and make a request and response.
  • Used WebTarget class to manipulate URLs and parameters as templates and assign parameters to produce client request.
  • Created POST request and have seen how the Response class has some methods to see cookies, headers and so on.
  • Can prepare request with the buildGet() method in the client (instead of get())
  • We can also use the GenericType<> wrapper to get the elements of a response in the client
  • Used Filters for Base Security implementation using header parameters Authentication Basic user:password 
  • We have seen about interceptors, Request and Response flows
  • CDI (@Singleton, @Stateless, and default @RequestScoped)




Comentarios

Entradas populares de este blog

04. MessageBodyReaders and MessageBodyWriters. Custom Media Types

03. Param annotations, and param converters

01. Let's go! Creating a Java Project