`

表示层模式:Intercepting Filter—拦截过滤器模式

阅读更多

Context
        The presentation-tier request handling mechanism receives many different types of
requests, which require varied types of processing. Some requests are simply forwarded to
the appropriate handler component, while other requests must be modified, audited, or
uncompressed before being further processed.
Problem
        Preprocessing and post-processing of a client Web request and response are required.
When a request enters a Web application, it often must pass several entrance tests
prior to the main processing stage. For example,
Has the client been authenticated?
Does the client have a valid session?
Is the client's IP address from a trusted network?
Does the request path violate any constraints?
What encoding does the client use to send the data?
Do we support the browser type of the client?
Some of these checks are tests, resulting in a yes or no answer that determines
whether processing will continue. Other checks manipulate the incoming data stream into a
form suitable for processing.
        The classic solution consists of a series of conditional checks, with any failed check
aborting the request. Nested if/else statements are a standard strategy, but this solution leads
to code fragility and a copy-and-paste style of programming, because the flow of the
filtering and the action of the filters is compiled into the application.
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.
Forces
        Common processing, such as checking the data-encoding scheme or logging
information about each request, completes per request.
Centralization of common logic is desired.
Services should be easy to add or remove unobtrusively without affecting existing
components, so that they can be used in a variety of combinations, such as
Logging and authentication
Debugging and transformation of output for a specific client
Uncompressing and converting encoding scheme of input
Solution
        Create pluggable filters to process common services in a standard manner without
requiring changes to core request processing code. The filters intercept incoming requests
and outgoing responses, allowing preprocessing and post-processing. We are able to add
and remove these filters unobtrusively, without requiring changes to our existing code.
We are able, in effect, to decorate our main processing with a variety of common
services, such as security, logging, debugging, and so forth. These filters are components
that are independent of the main application code, and they may be added or removed
declaratively. For example, a deployment configuration file may be modified to set up a
chain of filters. The same configuration file might include a mapping of specific URLs to
this filter chain. When a client requests a resource that matches this configured URL
mapping, the filters in the chain are each processed in order before the requested target
resource is invoked.

分享到:
评论

相关推荐

    33种JAVA设计模式DEMO

    创建型模式 这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而...拦截过滤器模式(Intercepting Filter Pattern) 服务定位器模式(Service Locator Pattern) 传输对象模式(Transfer Object Pattern)

    设计模式.docx

    #### 拦截过滤器模式(Intercepting Filter Pattern) 拦截过滤器模式允许你在一个请求到达目标对象之前,对其进行预处理或后处理。这种模式常用于实现统一的安全性检查、事务管理等功能。 #### 服务定位器模式...

    J2EE Patterns

    - **拦截过滤器**(Intercepting Filter):用于处理请求和响应的过滤机制。 - **前置控制器**(Front Controller):集中处理请求的入口点。 - **上下文对象**(Context Object):封装请求上下文的数据结构。 - **...

    Killtest 免费提供 310-084 最新资料下载

    1. **Intercepting Filter(拦截过滤器)**:此模式允许在请求到达目标资源前插入一系列过滤器,每个过滤器可以执行预处理逻辑(如日志记录)并选择是否继续传递请求到下一过滤器或目标资源。 2. **Front Controller...

    SCWCD认证 CX310-084

    过滤器(Intercepting Filter):通过实现`javax.servlet.Filter`接口,过滤器可以在请求到达目标之前或之后进行操作,包括记录请求信息。 3. **HttpServletRequestWrapper类** 关于`HttpServletRequestWrapper`...

Global site tag (gtag.js) - Google Analytics