In this session, let us learn about Zul filters. Already we have learned about soul APA gateway, this whole APA access of undos for all the requests that want to connect to the micro service ecosystem. We have seen an example of using Zul API gateway together with that the Zul has got filters. What is the purpose of a filter filters are used to filter the records that are coming in from the client. You can use filters for logging for authenticating and slide for providing static content are for dynamically routing the request I mean routing the in response based on the request that you have got. What are the filters that are available in routes are how to create a filter in Zul there are standard predefined filters and Zul if you want to create your own filters that are Also possible by creating a class which extends Zul filter, then it will be identified as a spring beam.
Of course, you know how to convert it into a string bass using the red bean annotation, what actually you are going to do in this filter, with the help of request context, you can get the HTTP servlet request and also HTTP servlet response. So once you've got the request and response object, you can perform whatever operation you want. If you want to log some information in the console that is possible if you want to authenticate the user based on the records that is coming in, or if you want to provide a static content if an error occurs. All these are possible with the help of the sisters. These are the four standard filters of Zul pre route posts and pre filters are executed before the request is routed. Route filters will be executed on the routing path for filters are executed.
After the request has been executed, I mean routers then Yara filters will be called when an exception occurs while connecting to the micro system a covert and now, let us talk about the method that has to be implemented, you are creating a class which extends Zul filter. So, it has got four methods which has to be implemented filter type. This specifies what type of filter you are using, whether it is a free route or force or era, you're just going to return a string which says that whether it is a route filter or era filter, then filter order this specifies in which order the filter will be executed related to other filters should filter of course the name itself is a special I mean specifies based on a request, whether you want to filter it or you want to continue with the flow, then this has the functionality of the filters.
Let me just show you that exactly these filters within in Zulu APA We'll show the pictorial representation. Here I've got assume I have a few services, service one service to service three. And this is only a PA, this is a client. The client can be a web client, R can be a micro service connecting to the backend using Zul AP, both are possible. In my case, I'm having it as a web client. So when a request comes in, it will be taken in by what is the first filter, pre filter.
Okay, and then next, it has to go through route filter. What is the purpose of this route? I am checking in for the request. If the request doesn't have the name, what I'm expecting, I don't want to proceed forward, the further to the back end micro service, I can return a static content to that particular application to this web client. Then Okay, When the error filter will be caused, if an exception occurs automatically the CRR filter will be called This is a pictorial representation. Now, let us try this example, already we have done a session on this zulay pH, I am going to work on the same application, we had actually Zul web client to a client connecting to Zul API gateway and the source connects to order service.
So I was having like kind order service and just change it order service and internally this order service was connecting to book service. If you see actually the order services directly connecting to book service if you want if you want to give a try you can also add this book service in Zulu APA and make our service Connect only to Zulu APA through Zulu APA, it can connect to a web service that has one micro service trying to connect to another micro service using the Zulu AP that is also possible. Okay, now, let us go back to a clip. This time, we are going to run Eureka Service order service book service, these two are micro services then Zul gateway and Zul web playing already running Eureka server order service and book service. So the micro service part is done in Eureka gateway for in Zul gateway, what actually we need we need to add the filters, I have already added the filters, let me show you the code it is very simple, you need to create a class which extends Zul filter, see if you just go here, this is my rule, a gateway application right and then I have got filters, I have created, pre filter post filter and route filters.
So what is the first step, create a class which extends all filters and then override the methods which are available there. Let us see the methods one by one first thing filter type, because this is a free pre filter, I have to return three within double quotes. Then want to start this is First filter which will be executed. Suppose if you're having multiple pre filters, you need to specify the order in which they have to be executed filter one filter to filter three means you need to specify the order then shouldn't filter of course, whatever request is coming in should be filtered by this pre filter, then I have got run method, this run method is going to give the functionality for this particular filter. So, I am trying to get the request context you can just have a look request context context is equal to request context dot get current context.
I am trying to get the context of this application with the help of which I am trying to retrieve the request. You can see this HTTP servlet request. One way about the request, I can ident I can call request dot getparameter request dot get URL then request dot get method. So, whatever information you want that you can just give it over here, so, I have got request dot get url.to string Okay, there is one more thing that records dot get URL that also is fine, then return null URI or returning null if you want to return an object that also is possible, this is part three filters. Similarly, I have done for route filter and post filter, no change here in case should filter is true filter on this one, and then filter a filter out which one means what in raw filter, this is the first filter which has to be executed, what is the filter type, this is route filter, so I have to give it a route.
And then now I can just have the same thing in whatever I have got in pre filter, I'm just going to copy it as such, you can write your own logic, this is just a simple demo. So I am just showing how actually the filters work. So here I am trying to retrieve the request and get the warrant. Then next post filter info filter when it will be called only after the request is routed means it is called on the return path. So this will have a response. So yeah, I have got HTTP servlet response trying to retrieve the response object and get the status so it is returned true okay here let me just remove and then should filter through filter order one and what type of filter it is it is post filter.
Now let me save this and then if you see here I am having been annotated i mean i mean that year symbol only in three filters I'm not having it enforced filter out filter why because I have not created them as being that is you have to go to the main class rule gateway APA and here you have to create a create this as a beam and add the red bean annotation are what they can do very simple. You can add add rate component classes. So, this is post filter. I've got route filter Also, if you need to create your filter also, let me scroll up and here I will import style just give it a start. Okay and I think have not got the error filter, I need to create it. So let me just show you now.
Conduct example demo dot filters new class. Class Name is era filter and then click Finish there itself if you want, you can extend it, extend the Zul filters and now you are to override all the methods keep the cursor and add unimplemented methods. Okay, what is the filter type, this is here and then filter order one true here on the top. If we add the status, the response should be response. I'll just copy paste it. Now see, actually I'm getting your symbol in all the three but not an error filter.
I have to add it in Zul API gateway application. I'll copy paste this and I will just change it. So this is the error syntax. See errors Okay all set. Now let us run this application. This is also micro service so select it and run it directly the application has started.
Now I will run this whole web client also select the application right click run as the non service service getting started. The application has started here and just copying it. Let me open the open it in the browser. You can see the application of scattering Eclipse, I will open the browser I am in Eureka registry. You can see here book service order service rule service, these are the three services we ran and they are running successfully. Now I will come to this URL here I'll just change it to Zul web client.
Let me press Enter I must get the first page here. Let me give the author name is Mohan or whatever. And click Search. In here let me just check it out. Exception has occurred anyhow it is fine. We wanted to check in for the CRR filter.
You can see the status we are given it as response dot get status. To say, so I have to be in Zul gateway. And let us check whether three filter and route filter is called. Let me scroll up to the top. What is the exception the exception is forwarding error, it is not able to forward you can see the URL which is given. So this is from the first one is from pre filter.
The second one is from route filters. Okay, what is the next one it has to call for filter if everything is fine, but in our case there was an exception so automatically this calling only. I mean this giving me an exception. Now I'm just returning it let me check. It has to work fine. Now it is working perfectly fine.
I'll scroll down to Zul gateway pH, this one's the server dual gateway application fee. This is pre filter, then this is route filter. And what is this 200 this is post filters. Okay, and now again, like if I just click order now Again Can you see this pre route and post filter. So, in this order only the filters will be called your I thought of showing it using the show hotels where we are not earning hotel service. So obviously this gives you it will be giving an exception so I thought of explaining the error filter using show hotels but instead of that because the request timed out automatically it was giving the exception now I have given show for hotels.
Okay and trying to run it from here that's why but still it has to throw the exception. Can you see 500 entities avoided if you see actually on the top you're trying to call APA slash hotel slash all hotels. This is pre filter. The second one is route filter and the last one is yes filter. So this is all about Julie APA gateway and the filters associated with food. Thank you