ntercepting Filter类似于职责链模式
有两种实现
其中一种是Filter之间没有联系,全部Filter都存放在FilterChain中,由FilterChain来有序或无序地把把所有Filter调用一遍。没有用到链表这种数据结构。示例如下:
Java代码 收藏代码
package com.ljn.filter.custom;
import java.util.ArrayList;
import java.util.List;
/**
* 由FilterManager组织FilterChain,在FilterChain里面的filter顺序执行,
* 各Filter之间没有交互
*/
public class InterceptingFilterManager {
interface Filter {
void execute(String request, String response);
}
static class AuthenticationFilter implements Filter {
@Override
public void execute(String request, String response) {
System.out.println("AuthenticationFilter");
}
}
static class DebugFilter implements Filter {
@Override
public void execute(String request, String response) {
System.out.println("DebugFilter");
}
}
static class Target {
public void execute(String request, String response) {
System.out.println("Target");
}
}
static class FilterChain {
private List<Filter> filters = new ArrayList<Filter>();
private Target target;
public void addFilter(Filter filter) {
filters.add(filter);
}
//顺序执行,各filter之间没有直接联系
public void execute(String request, String response) {
for (Filter filter : filters) {
filter.execute(request, response);
}
target.execute(request, response);
}
public void setTarget(Target target) {
this.target = target;
}
}
public static void main(String[] args) throws Exception {
Target target = new Target();
Filter debugFilter = new DebugFilter();
Filter authenticationFilter = new AuthenticationFilter();
FilterChain chain = new FilterChain();
chain.addFilter(authenticationFilter);
chain.addFilter(debugFilter);
chain.setTarget(target);
String req = null;
String res = null;
chain.execute(req, res);
}
}
另一种是一个Filter“持有”下一个Filter,下一个Filter的调用由上一个Filter决定。这其中就用到了链表这种数据结构。
Java代码 收藏代码
package com.ljn.filter.custom;
/**
* 结合装饰模式,每个filter完成自己的处理后,显式调用下一个filter
*/
public class InterceptingFilterDecorator {
interface Filter {
void execute(String request, String response);
}
static class AuthenticationFilter implements Filter {
private Filter next;
public AuthenticationFilter(Filter next) {
this.next = next;
}
@Override
public void execute(String request, String response) {
System.out.println("AuthenticationFilter");
next.execute(request, response);
}
}
static class DebugFilter implements Filter {
private Filter next;
public DebugFilter(Filter next) {
this.next = next;
}
@Override
public void execute(String request, String response) {
System.out.println("DebugFilter");
next.execute(request, response);
}
}
static class Target implements Filter {
@SuppressWarnings("unused")
private Filter next;
public Target() {
this(null);
}
public Target(Filter next) {
this.next = next;
}
@Override
public void execute(String request, String response) {
System.out.println("Target");
// next.execute(request, response); //last one, no need to call next
}
}
public static void main(String[] args) throws Exception {
Filter processor = new AuthenticationFilter(
new DebugFilter(
new Target()));
String req = null;
String res = null;
processor.execute(req, res);
}
}
分享到:
相关推荐
藏经阁-Intercepting-iCloud-Keychain
用Java实现23种设计模式 ... 拦截过滤器模式(Intercepting Filter Pattern) 服务定位器模式(Service Locator Pattern) 传输对象模式(Transfer Object Pattern) 生产者消费者模式(Producer Consumer Pattern)
创建型模式 这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而...拦截过滤器模式(Intercepting Filter Pattern) 服务定位器模式(Service Locator Pattern) 传输对象模式(Transfer Object Pattern)
拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理。定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上。过滤器可以做认证/授权/记录日志,或者跟踪请求,...
sw不拦截获取事件的情况继续出现: ://stackoverflow.com/questions/48381003/serviceworker-not-intercepting-urls 资料和指南 服务人员注意事项 由于我想使用WebPush以及其他可能的sw功能,因此必须使用“ ...
本文对应于Web表示模式集群,文章的前半部分重笔墨的描述了MVC模式的架构、设计及其ASP.NET实现,而在更加复杂的...简要介绍了Web表示模式集群的另外两个模式:Intercepting Filter(筛选器)和Page Cache(页面缓存)模式
intercepting mobile app network traffic 拦截移动应用程序网络流量.pdf
Physical layer security may be an effective way to improve the capability of anti-intercepting and anti-detecting of the satellite communication system. Necessary applications of physical layer ...
mitmproxy is an interactive, SSL/TLS-capable intercepting proxy with a console interface for HTTP/1, HTTP/2, and WebSockets. mitmdump is the command-line version of mitmproxy. Think tcpdump for HTTP....
电动汽车,主动配电网,电力系统规划对论文《Intercepting Rogue Robots: An Algorithm for Capturing Multiple Evaders With Multiple Pursuers》的matlab复现,基于voronoi图维诺图最小化围捕算法 完整注释
An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers. mitmproxy 该存储库包含 mitmproxy 和 pathod 项目。 mitmproxy 是一个交互式的、支持 SSL 的拦截代理,带...
OpenSSH: A Survival Guide for Secure ... OpenSSH provides an encrypted channel between computers and remote servers to ensure against a malicious attacker intercepting and reading information in transit.
英文书籍 Building custom components • Accepting user input from forms • Rendering lists of objects into views • Intercepting user clicks and acting on them • Deploying our app to a server
Missile intercepting simulator, Matlab, vevr good
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
4.4 Intercepting Packets for a Mobile Node 54 4.5 Returning Home 54 5 Route Optimization 57 5.1 Return Routability 58 5.2 Sending Initial Messages 58 5.3 Responding to Initial Messages 59 5.4 ...
是一个围绕对象方法基于责任链(拦截器)模式设计的开源轻量级Java容器。专注于方法的映射、调用、拦截和结果处理,采用基于配置和注解的方式来抽取和收集程序中对象的方法(method)以用于路由映射, HTTP控制器,...
the points of propagating microwaves andpoints of intercepting microwaves. Great care must betaken at the receive ends especially if it involvesastronomical applications where the microwaves to ...
API hooking consists of intercepting a function call in a program and redirecting it to another function. By doing this, the parameters can be modified, the original program can be tricked if you ...