Filtering Requests with the Intercepting Filter Pattern

Some of these actions determine whether processing will continue, while others manipulate the incoming or outgoing data stream into a form suitable for further processing.
The key to solving this problem in a flexible and unobtrusive manner is to have a simple mechanism for adding and removing processing components, in which each component completes a specific filtering action.

Pre- and post-processing refer to actions taken around the core processing of a request. Certain actions dictate continuation of processing, while others modify the incoming or outgoing data stream for future processing.

The conventional approach involves a set of conditional verifications. If any of these verifications fail, the request is terminated. Although nested if/else statements are commonly used, they cause the code to become fragile and the programming style to become copy-and-paste, as the filtering flow and filter actions are hardcoded into the application. To address this issue in a versatile and non-intrusive way, a straightforward mechanism for adding and removing processing components is necessary, with each component carrying out a specific filtering action.

Illustration of Intercepting Filter Pattern using UML Diagram.

Component design

  • The filter processing is managed by
    filtermanager
    . It creates the
    filterchain
    in the accurate sequence with the required filters and triggers the process.
  • The FilterChain is a sequence of distinct filters that follow a specific order.
  • FilterOne and FilterTwo refer to unique filters that are associated with a particular destination. The FilterChain is responsible for managing the order in which they are applied.
  • The requested resource by the client is referred to as the target.

An instance of the Intercepting Filter Pattern will be demonstrated.

The following codes are listed:

// Java program to illustrate

through


, with no repetition.

The following codes are included:

for

through

{

.

Output:

Authenticating : Downloads
Log: Downloads
Executing : Downloads


Advantages :

  • Enhanced reusability is achieved by centralizing common code into pluggable components.
  • Enhanced adaptability:
    Declarative application and removal of generic shared components can enhance the system’s adaptability.


Disadvantages :

  • Sharing of information is ineffective in detecting patterns.

This post is authored by Saket Kumar. If you enjoy GeeksforGeeks and want to share your knowledge, you can submit an article through contribute.geeksforgeeks.org or send it to [email protected]. Your content could be featured on the main page of GeeksforGeeks, providing assistance to fellow tech enthusiasts.

If you come across any errors or have additional information related to the above-discussed topic, kindly drop a comment.

Frequently Asked Questions

Posted in Uncategorized