KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > filters > IPBanFilter


1
2 package org.roller.presentation.filters;
3
4 import java.io.IOException JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.List JavaDoc;
7
8 import javax.servlet.Filter JavaDoc;
9 import javax.servlet.FilterChain JavaDoc;
10 import javax.servlet.FilterConfig JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.ServletRequest JavaDoc;
13 import javax.servlet.ServletResponse JavaDoc;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.roller.util.StringUtils;
20
21 /**
22  * Braindead simple IPBanFilter. XDoclet tags disabled because I don't want this
23  * to be on by default. Users who want it can configure it themselves in web.xml.
24  * web.filter name="IPBanFilter"
25  * web.filter-init-param name="denyFrom" value="" description="Comma-separated list of banned IPs"
26  * @author David M Johnson
27  */

28 public class IPBanFilter implements Filter JavaDoc
29 {
30     private List JavaDoc denyFrom = null;
31     private static Log mLogger =
32         LogFactory.getFactory().getInstance(IPBanFilter.class);
33     
34     public IPBanFilter()
35     {
36         super();
37     }
38
39     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
40     {
41         /*
42          * This should be updated to the new config, however I don't want
43          * to do it myself since I'm not sure if/how it is being used.
44          *
45          * This looks like something that could be a long list, so maybe
46          * it should be in the DB or possibly its own file?
47          * -- Allen G
48          */

49         String JavaDoc denyFromParam = filterConfig.getInitParameter("denyFrom");
50         denyFrom = Arrays.asList(StringUtils.split(denyFromParam,","));
51     }
52
53     public void doFilter(
54         ServletRequest JavaDoc req,
55         ServletResponse JavaDoc res,
56         FilterChain JavaDoc chain)
57         throws IOException JavaDoc, ServletException JavaDoc
58     {
59         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) req;
60         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) res;
61         if (denyFrom.contains(request.getRemoteAddr()))
62         {
63             response.setStatus(HttpServletResponse.SC_FORBIDDEN);
64             return;
65         }
66         else
67         {
68             chain.doFilter(request, response);
69         }
70     }
71
72     public void destroy()
73     {
74     }
75 }
76
Popular Tags