Advanced JAX-RS 21 - Implementing Filters

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial I'm going to talk about filters in jax-rs the best way to implement authentication in your jax-rs api's is using filters so we'll learn that feature of jax-rs first what is a filter filter is a way for you to take some of the cross-cutting concerns and crosscutting logic out from your individual resource classes into a common filter class the idea is if you have some logic to be applied to different API in different resources rather than make a copy of that logic in each of those resources you take that logic out and create one common class which contains that logic and then you apply it to different requests right so that's the concept of a filter it's a cross-cutting concern that you isolate out into its own separate entity so what you do is you create a filter which contains that cross-cutting logic and then you configure what are the API requests for which that filter needs to apply an example is logging let's say you want to log all the API requests and responses and the times at which they were made one way you could do it is make sure that logging statement is you know coded in each and every resource class which is a lot of duplication of work what you could do instead is create a logging filter and have that do the logging now when you create a jax-rs filter jax-rs is sure to call that filters logic when an API call is mean so that's how you isolate that logic out I'm going to demonstrate the filter logic by creating a couple of filters in this tutorial the first filter that I'm going to create is a powered by filter you must have seen some software solutions that provide a powered by tag to applications or websites you see you tags like powered by WordPress or powered by angularjs or something like that so I'm going to do something similar here when there's a REST API call that's made the response it goes out should ideally have a header value which is powered by and the value should be Java brains so every response has a powered by Java brains so I'm going to create this filter now instead of having this be a part of every resource I'm going to isolate this out as a filter so I'm going to go ahead and create this new class in the rest package I'm going to call this powered by response filter okay so for a class to be a filter it needs to implement an interface that comes with jax-rs now what interface you want to implement depends on what you want that filter to be so turns out there are two types of filters in jax-rs one is a request filter and one is a response filter a request filter is a filter that executes before a request is served and the response filter is a filter that's executed after the response is prepared and it's about to be sent to the client right so you're basically intercepting either the request or you're intercepting the response so depending on whether you want your filter to be a request filter or response filter you got to pick the right interface to implement in the case of this powered by filter I want the response to be changed right so in the response I want to add a header so what I need here is a response filter so what I'm going to do is I'm going to implement an interface called container response filter and the mean port this and I had the unimplemented methods now you see here I need to implement this filter method now the powered by response filter implements container response filter so this is a response filter the jacksons framework is going to call this filter method implementation when the response is about to be sent okay now here you have a chance to modify the response you have a chance to add a header value let's look at the parameters to this filter method the parameters depend on whether it's a request filter or a response filter in the case of a request filter the jax-rs framework sends an instance of a class called container request context it's basically a context object right it's an object which contains details about the request in the case of a response filter though you get two context objects one is the request context and one is the response context since the request filter happens before the request is processed all you have is the request context but the response filter is called after the response is prepared so you have access to both the request context and the response context so you can actually tweak and modify both in our case we're doing the powered by filter so I'm going to modify response using the response context so what I'm going to do is change these names I'm going to call them request context in response context and then I'm going to do a response context dot I need to get the header values so I'm going to say it get headers dot add need to add this powered by header so I'm going to say X powered by as the key the value is Java brains now the last thing I need to do is mark this as a provider as with all the other things in jax-rs marking this as a provider conveys that special functionality that this class has so I'm going to save and run this on Tomcat and I'm going to switch to postman make a REST API call to this endpoint examine the headers you see there is this extra header value X powered by Java brains so this is a filter in action so this is not something that we added in a resource class this is a separate filter so it's applicable to any REST API call that's made to an endpoint that this application serves what's also interesting is if I make any API call to send to an endpoint that doesn't exist for instance I change this to error I changed look at the header value here well it still says powered by Java brains so the filter is actually getting executed no matter what you whether it's a successful response are there is an error response the filter is guaranteed to execute which is pretty cool alright so I'm going to do one more implementation off a filter this is going to be a logging filter I want to log let's say the header values for the request and the header value for the response so I need both I need to log the header values of a request when the requesters main and before the response is sent back I want to log all the header values of the response so I'm going to go ahead and create this new class called a logging filter click finish and now since I need to do both before and after I can do this I can actually do an implements contain a request filter import this and at the unemployment admits this as we just saw is a filter method which takes in just the request context change this to request context and let's print the request header value here I'm going to say request context dot get headers and I'm just going to do us this out of this alright so with this I am going to be printing the header values for every request that lets me do this to this application you can see here the request headers are all being logged in the console but now what I want to do is also log the response headers so I could of course create a separate filter for it but since these are just interfaces what I'm going to do is I'm going to also implement that response filter interface so I'm going to say implements container response filter and now if I add the unimplemented methods I'm going to get the method that we just saw it's a filter method which takes in two arguments let's the container request context and the container response context now both these methods happen to be in the same class but since we have registered them as a provider and an implementation of these two interfaces jax-rs knows what method to call when right it's going to call the first method for request filter and we actually do us this out request filter here and I'm going to do us this out response filter here to make sure that this is printed in the response and I'm going to print out the response headers response context dot get headers now I make this call in postman if I switch to the console but here you can see there is a request filter and request headers and we have the response filter the response headers so it's pretty cool so this is how you implement filters and jax-rs you can either do a request order responds our like we've seen with the logging filter you can do one filter which does both just has two methods and the jax-rs framework knows which method to call when
Info
Channel: Java Brains
Views: 48,112
Rating: undefined out of 5
Keywords: java brains, java, brains, koushik kothagal, koushik, kothagal, kaushik, JAX-RS, REST, RESTful, Web services, Jersey, Java EE, Tomcat, Apache, Java API For RESTful Web Services
Id: nDW6DQSNrIY
Channel Id: undefined
Length: 10min 50sec (650 seconds)
Published: Tue Jan 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.