Friday, 30 August 2013

Intercepting Filter Design Pattern - Implementation

🚀 Level Up Your Java Architecture Skills!

Subscribe to Ram N Java for clean code walkthroughs, design pattern implementations, and step-by-step programming guides!

🔔 Subscribe to Ram N Java

Practical Implementation of Intercepting Filter

The Intercepting Filter Design Pattern is widely applied in modern web frameworks and enterprise backends to handle cross-cutting concerns cleanly. Instead of embedding security checks, request encoding, or auditing directly into your business controllers, this pattern lets you execute specialized filters in an ordered chain before reaching the final handler.

How the Components Work Together

  • Filter Interface: Defines a standard contract (such as an execute(String request) method) that every concrete filter implements.
  • Concrete Filters: Specific implementations that handle focused tasks (e.g., an AuthenticationFilter validating credentials or a DebugFilter logging request payloads).
  • Target: The ultimate business destination or resource (such as a core service or web handler) that processes the request once all filters approve.
  • FilterChain: Maintains an ordered list of filters, executes them sequentially, and finally triggers the target.
  • FilterManager: Coordinates the filter chain setup and manages which filters attach to incoming requests.
  • Client: The caller that sends requests through the FilterManager rather than directly to the target.

Execution Flow in Code

  1. The Client issues an incoming request.
  2. The FilterManager receives the request and initializes the FilterChain.
  3. The FilterChain invokes each registered Filter in succession (e.g., checking tokens, validating headers, logging data).
  4. Once every filter executes successfully, the chain passes control directly to the Target.

Key Benefits for Software Design

  • Single Responsibility: Each filter class has only one job, making your codebase modular and easy to debug.
  • Dynamic Extensibility: You can plug in new filters (like rate limiting or data compression) without modifying the target resource.
  • High Reusability: Common filters can be attached to multiple handlers across different services.

Recommended Next Videos

Deepen your understanding of filters and servlet architecture with these related tutorials from our channel:


Click here to watch in Youtube : https://www.youtube.com/watch?v=sWtchigtgXk

Click the below Image to Enlarge
Intercepting Filter Design Pattern - Implementation






















Filter.Java

public interface Filter
{
public void processRequest(String request);
}

AuthenticationFilter.java

public class AuthenticationFilter implements Filter
{

@Override
        public void processRequest( String request )
        {
System.out.println("Authenticating the request by AuthenticationFilter : " + request);
       
        }

}

LoggingFilter.java

public class LoggingFilter implements Filter
{
@Override
        public void processRequest( String request )
        {
System.out.println("Request Tracking is done by LoggingFilter : " + request);        
        }
}

Target.java

public class Target
{
public void processRequest( String request )
{
System.out.println("Process the Request by Target Class: " + request);
}
}

FilterChain.java

import java.util.ArrayList;
import java.util.List;

public class FilterChain
{
private List<Filter> filters = new ArrayList<Filter>();
private Target       target;

public void addFilter( Filter filter )
{
filters.add(filter);
}

public void execute( String request )
{
for( Filter filter : filters )
{
filter.processRequest(request);
}
target.processRequest(request);
}

public void setTarget( Target target )
{
this.target = target;
}
}

FilterManager.java

public class FilterManager
{
FilterChain filterChain;

public FilterManager( Target target )
{
filterChain = new FilterChain();
filterChain.setTarget(target);
}

public void setFilter( Filter filter )
{
filterChain.addFilter(filter);
}

public void filterRequest( String request )
{
filterChain.execute(request);
}
}

Client.java

public class Client
{
FilterManager filterManager;

public void setFilterManager( FilterManager filterManager )
{
this.filterManager = filterManager;
}

public void sendRequest( String request )
{
filterManager.filterRequest(request);
}

public static void main( String[] args )
{
FilterManager filterManager = new FilterManager(new Target());
filterManager.setFilter(new AuthenticationFilter());
filterManager.setFilter(new LoggingFilter());
Client client = new Client();
client.setFilterManager(filterManager);
client.sendRequest("HOME");

}

}

Output

Authenticating the request by AuthenticationFilter : HOME
Request Tracking is done by LoggingFilter : HOME


1 comment:

  1. Why don't you create a login/authentication app and show the real implementation of Filters?

    ReplyDelete

Tutorials