Configuring the order of filters

In previous post under JAX-RS Client, I showed with example how to register filters for a particular client.

The filters are executed in the order they are registered.

We can change the order of execution by giving a priority to each filter. Filters with lower priority will be executed first.

To demo this, we will use two filters. The class structure of these filters are as shown below.

MyClientRequestFilter1


package defaultPackage;

import java.io.IOException;

import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientRequestFilter;

public class MyClientRequestFilter1 implements ClientRequestFilter {
    @Override
    public void filter(ClientRequestContext arg0) throws IOException {
        System.out.println(MyClientRequestFilter1.class.getName());
    }
}

MyClientRequestFilter2


package defaultPackage;

import java.io.IOException;

import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientRequestFilter;

public class MyClientRequestFilter2 implements ClientRequestFilter {
    @Override
    public void filter(ClientRequestContext arg0) throws IOException {
        System.out.println(MyClientRequestFilter2.class.getName());
    }
}

Now we will see the main class where these are used

Main Class


1  package defaultPackage;
2  
3  import jakarta.ws.rs.client.Client;
4  import jakarta.ws.rs.client.ClientBuilder;
5  import jakarta.ws.rs.client.Invocation;
6  import jakarta.ws.rs.client.WebTarget;
7  import jakarta.ws.rs.core.Response;
8  
9  public class Example18 {
10     public static void main(String[] args) {
11         System.out.println("Filters without user specified order");
12         Client client = ClientBuilder.newClient();
13         try {
14             client.register(MyClientRequestFilter1.class);
15             client.register(MyClientRequestFilter2.class);
16             WebTarget webTarget = client.target("https://jsonplaceholder.typicode.com/users/2");
17             Invocation.Builder builder = webTarget.request();
18             Invocation invocation = builder.buildGet();
19             Response response = invocation.invoke();
20             if(response.getStatus() == 200) {
21                 System.out.println("Successful");
22                 String result = response.readEntity(String.class);
23                 System.out.println(result);
24             } else {
25                 System.out.println("Failed");
26             }
27         } finally {
28             client.close();
29         }
30         System.out.println("----------------------------------------------------------------");
31         System.out.println("Filters with user specified order");
32         client = ClientBuilder.newClient();
33         try {
34             client.register(MyClientRequestFilter1.class, 2);
35             client.register(MyClientRequestFilter2.class, 1);
36             WebTarget webTarget = client.target("https://jsonplaceholder.typicode.com/users/2");
37             Invocation.Builder builder = webTarget.request();
38             Invocation invocation = builder.buildGet();
39             Response response = invocation.invoke();
40             if(response.getStatus() == 200) {
41                 System.out.println("Successful");
42                 String result = response.readEntity(String.class);
43                 System.out.println(result);
44             } else {
45                 System.out.println("Failed");
46             }
47         } finally {
48             client.close();
49         }
50     }
51 }

In the above code, from line 11 to 29 contains code in which filters are registered without specifying any priority. Refer to line 14 and 15.

In this case “MyClientRequestFilter1” filter will be executed first and then “MyClientRequestFilter2” filter.

In the above code, from line 31 to 49 contains code in which filters are registered and while registering the filters we are also mentioning the priority. Refer to line 34 and 35.

If you see the line 34 and 35, the “register” method takes a integer as a parameter in addition to filter class object. This integer value represents the priority.

In this case “MyClientRequestFilter2” filter will be executed first and then “MyClientRequestFilter1” filter.

In this way we change the order in which filters are executed.

In previous post I showed how to register filters
1) per client
2) per target
3) all clients

We can change the order of filters in all three cases by using “register” method which takes an integer as an argument. Below is the complete output

Output


Filters without user specified order
defaultPackage.MyClientRequestFilter1
defaultPackage.MyClientRequestFilter2
Successful
{
  "id": 2,
  "name": "Ervin Howell",
  "username": "Antonette",
  "email": "Shanna@melissa.tv",
  "address": {
    "street": "Victor Plains",
    "suite": "Suite 879",
    "city": "Wisokyburgh",
    "zipcode": "90566-7771",
    "geo": {
      "lat": "-43.9509",
      "lng": "-34.4618"
    }
  },
  "phone": "010-692-6593 x09125",
  "website": "anastasia.net",
  "company": {
    "name": "Deckow-Crist",
    "catchPhrase": "Proactive didactic contingency",
    "bs": "synergize scalable supply-chains"
  }
}
----------------------------------------------------------------
Filters with user specified order
defaultPackage.MyClientRequestFilter2
defaultPackage.MyClientRequestFilter1
Successful
{
  "id": 2,
  "name": "Ervin Howell",
  "username": "Antonette",
  "email": "Shanna@melissa.tv",
  "address": {
    "street": "Victor Plains",
    "suite": "Suite 879",
    "city": "Wisokyburgh",
    "zipcode": "90566-7771",
    "geo": {
      "lat": "-43.9509",
      "lng": "-34.4618"
    }
  },
  "phone": "010-692-6593 x09125",
  "website": "anastasia.net",
  "company": {
    "name": "Deckow-Crist",
    "catchPhrase": "Proactive didactic contingency",
    "bs": "synergize scalable supply-chains"
  }
}

Leave a Reply