|
7 | 7 | ---
|
8 | 8 |
|
9 | 9 | ## Intent
|
10 |
| -Provide pluggable filters to conduct necessary pre-processing and |
11 |
| -post-processing to requests from a client to a target |
| 10 | +An intercepting filter is a useful Java Design Pattern used when you want to pre-process |
| 11 | +or post-process a request in an application. These filters are created and applied to the |
| 12 | +request before it is given to the target application. Such examples of uses include authentication, |
| 13 | +which is necessary to be processed before the request is given to the application. |
| 14 | + |
| 15 | +## Explanation |
| 16 | +Real-world Example |
| 17 | +> An example of using the Intercepting Filter design pattern is relevant when making an ecommerce |
| 18 | +> platform. It is important to implement various filters for authentication of account, authentication |
| 19 | +> of payment, logging and caching. Important types of filters in this example are authentication, logging, |
| 20 | +> security, and caching filters. |
| 21 | +
|
| 22 | +In plain words |
| 23 | +> An intercepting filter in Java is like a series of security checkpoints for requests and responses in a |
| 24 | +> software application. It checks and processes data as it comes in and goes out, helping with tasks like |
| 25 | +> authentication, logging, and security, while keeping the core application safe and clean. |
| 26 | +
|
| 27 | +Wikipedia says |
| 28 | +> Intercepting Filter is a Java pattern which creates pluggable filters to process common services in a |
| 29 | +> standard manner without requiring changes to core request processing code. |
| 30 | +
|
| 31 | +## Programmatic Example |
| 32 | +As an example, we can create a basic Filter class and define an Authentication Filter. The filter has missing logic, |
| 33 | +but |
| 34 | +```java |
| 35 | +// 1. Define a Filter interface |
| 36 | +interface Filter { |
| 37 | + void runFilter(String request); |
| 38 | +} |
| 39 | +// 2. Create a Authentication filter |
| 40 | +class AuthenticationFilter implements Filter { |
| 41 | + public void runFilter(String request) { |
| 42 | + // Authentication logic would be passed in here |
| 43 | + if (request.contains("authenticated=true")) { |
| 44 | + System.out.println("Authentication successful for request: " + request); |
| 45 | + } else { |
| 46 | + System.out.println("Authentication failed for request: " + request); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | +// 3. Create a Client to send requests and activate the filter |
| 51 | +class Client { |
| 52 | + // create an instance of the filter in the Client class |
| 53 | + private Filter filter; |
| 54 | + |
| 55 | + // create constructor |
| 56 | + public Client(Filter filter) { |
| 57 | + this.filter = filter; |
| 58 | + } |
| 59 | + |
| 60 | + // send the String request to the filter, the request does not have to be a string |
| 61 | + // it can be anything |
| 62 | + public void sendRequest(String request) { |
| 63 | + filter.runFilter(request); |
| 64 | + } |
| 65 | +} |
| 66 | +// 4. Demonstrate the Authentication Filter |
| 67 | +public class AuthenticationFilterExample { |
| 68 | + public static void main(String[] args) { |
| 69 | + Filter authenticationFilter = new AuthenticationFilter(); |
| 70 | + Client client = new Client(authenticationFilter); |
| 71 | + |
| 72 | + // Simulate requests for false |
| 73 | + client.sendRequest("GET /public-page"); |
| 74 | + // this request would come back as true as the link includes an argument |
| 75 | + // for successful authentication |
| 76 | + client.sendRequest("GET /private-page?authenticated=true"); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +This is a basic example of how to implement the skeleton of a filter. The authentication logic in AuthenticationFilterExample is missing, but can be filled into the gaps. |
| 81 | + |
| 82 | +Additionally, the client can be setup to run multiple filters on its request using a For loop populated with filters as can be seen below: |
| 83 | +```java |
| 84 | +// 1. Define a Filter interface |
| 85 | +interface Filter { |
| 86 | + void runFilter(String request); |
| 87 | +} |
| 88 | + |
| 89 | +// 2. Create an Authentication filter |
| 90 | +class AuthenticationFilter implements Filter { |
| 91 | + public void runFilter(String request) { |
| 92 | + // Authentication logic would be placed here |
| 93 | + if (request.contains("authenticated=true")) { |
| 94 | + System.out.println("Authentication successful for request: " + request); |
| 95 | + } else { |
| 96 | + System.out.println("Authentication failed for request: " + request); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// 3. Create a Client to send requests and activate multiple filters |
| 102 | +class Client { |
| 103 | + // create a list of filters in the Client class |
| 104 | + private List<Filter> filters = new ArrayList<>(); |
| 105 | + |
| 106 | + // add filters to the list |
| 107 | + public void addFilter(Filter filter) { |
| 108 | + filters.add(filter); |
| 109 | + } |
| 110 | + |
| 111 | + // send the request through all the filters |
| 112 | + public void sendRequest(String request) { |
| 113 | + for (Filter filter : filters) { |
| 114 | + filter.runFilter(request); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// 4. Demonstrate multiple filters |
| 120 | +public class MultipleFiltersExample { |
| 121 | + public static void main(String[] args) { |
| 122 | + // Create a client |
| 123 | + Client client = new Client(); |
| 124 | + |
| 125 | + // Add filters to the client |
| 126 | + Filter authenticationFilter = new AuthenticationFilter(); |
| 127 | + client.addFilter(authenticationFilter); |
| 128 | + |
| 129 | + // Add more filters as needed |
| 130 | + // Filter anotherFilter = new AnotherFilter(); |
| 131 | + // client.addFilter(anotherFilter); |
| 132 | + |
| 133 | + // Simulate requests |
| 134 | + client.sendRequest("GET /public-page"); |
| 135 | + client.sendRequest("GET /private-page?authenticated=true"); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | +This method allows quick and easy manipulation and checking of data before authenticating a login or finishing some other sort of action. |
12 | 140 |
|
13 | 141 | ## Class diagram
|
14 | 142 | 
|
15 | 143 |
|
16 | 144 | ## Applicability
|
17 | 145 | Use the Intercepting Filter pattern when
|
18 | 146 |
|
19 |
| -* a system uses pre-processing or post-processing requests |
20 |
| -* a system should do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers |
21 |
| -* you want a modular approach to configuring pre-processing and post-processing schemes |
| 147 | +* A program needs to pre-process or post-process data |
| 148 | +* A system needs authorisation/authentication services to access sensitive data |
| 149 | +* You want to log/audit requests or responses for debugging or storing purposes, such as timestamps and user actions |
| 150 | +* You want to transform data of a type to another type before it is given to the end process |
| 151 | +* You want to implement specific exception handling |
| 152 | + |
| 153 | +## Consequences |
| 154 | +Consequences that come with implementing Intercepting Filter |
| 155 | + |
| 156 | +* Increase in code complexity, diminishing ease of readability |
| 157 | +* Can have issues in the order that filters are applied if order is important |
| 158 | +* Applying multiple filters to a request can create a delay in response time |
| 159 | +* Testing the effects of multiple filters on a request can be hard |
| 160 | +* Compatibility and version management can be difficult if you have a lot of filters |
22 | 161 |
|
23 | 162 | ## Tutorials
|
24 | 163 |
|
|
0 commit comments