首页 > 代码库 > OSChina 的全局过滤器源码

OSChina 的全局过滤器源码

源码如下,源码中的 RequestContext 代码请看这里。 很多人很好奇,但其实真的没有任何特殊之处,呵呵
标签: OSCHINA filter

[1].[代码] java代码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
packagemy.mvc;
 
importjava.io.IOException;
 
importjavax.servlet.Filter;
importjavax.servlet.FilterChain;
importjavax.servlet.FilterConfig;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletException;
importjavax.servlet.ServletRequest;
importjavax.servlet.ServletResponse;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
 
importmy.db.DBManager;
 
/**
 * 全局过滤器
 * @author Winter Lau
 * @date 2010-1-13 下午08:36:18
 */
publicclass ControllerFilter implementsFilter {
 
    privateServletContext context;
     
    /**
     * 过滤器初始化
     */
    publicvoid init(FilterConfig cfg) throwsServletException {
        this.context = cfg.getServletContext();
    }
 
    /**
     * 执行过滤操作
     */
    publicvoid doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throwsIOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;
        RequestContext rc = RequestContext.begin(this.context, request, response);
         
        if(rc.uri().indexOf("/.svn/")>=0){
            rc.forbidden();
            return;
        }
 
        try{
            chain.doFilter(rc.request(), rc.response());
        }finally{
            if(rc!=null) rc.end();
            DBManager.closeConnection();
        }
    }
 
    /**
     * 过滤器释放资源
     */
    publicvoid destroy() {
    }
 
}

OSChina 的全局过滤器源码