Skip to content

Commit 8aae347

Browse files
authored
docs: Finished Explanation for Intercepting Filter iluwatar#2236 (iluwatar#2709)
* Added documentation for Feature Toggle design pattern * Explanation for Feature Toggle Issue iluwatar#2230 finished, added intent, explanation, programmatic example, applicability and consequences * Explanation for Intercepting Filter iluwatar#2236 finished
1 parent b186ff5 commit 8aae347

File tree

1 file changed

+144
-5
lines changed

1 file changed

+144
-5
lines changed

intercepting-filter/README.md

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,157 @@ tag:
77
---
88

99
## 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.
12140

13141
## Class diagram
14142
![alt text](./etc/intercepting-filter.png "Intercepting Filter")
15143

16144
## Applicability
17145
Use the Intercepting Filter pattern when
18146

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
22161

23162
## Tutorials
24163

0 commit comments

Comments
 (0)