KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.presentation.filters;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.roller.model.RefererManager;
6 import org.roller.presentation.RollerContext;
7 import org.roller.presentation.RollerRequest;
8
9 import java.io.IOException JavaDoc;
10
11 import javax.servlet.Filter JavaDoc;
12 import javax.servlet.FilterChain JavaDoc;
13 import javax.servlet.FilterConfig JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.ServletRequest JavaDoc;
16 import javax.servlet.ServletResponse JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20
21
22 /**
23  * Keep track of referers.
24  *
25  * @web.filter name="RefererFilter"
26  * *web.filter-mapping url-pattern="/page/*"
27  *
28  * @author David M. Johnson
29  */

30 public class RefererFilter implements Filter JavaDoc
31 {
32     private FilterConfig JavaDoc mFilterConfig = null;
33     private static Log mLogger =
34         LogFactory.getFactory().getInstance(RefererFilter.class);
35    
36     /**
37      * destroy
38      */

39     public void destroy()
40     {
41     }
42
43     /**
44      * doFilter
45      */

46     public void doFilter(
47         ServletRequest JavaDoc req, ServletResponse JavaDoc res, FilterChain JavaDoc chain)
48         throws IOException JavaDoc, ServletException JavaDoc
49     {
50         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)req;
51         boolean isRefSpammer = false;
52         try
53         {
54             RollerRequest rreq = RollerRequest.getRollerRequest(request);
55             RollerContext rctx = RollerContext.getRollerContext(
56                 mFilterConfig.getServletContext());
57             
58             if ( rreq.getUser() != null )
59             {
60                 String JavaDoc userName = rreq.getUser().getUserName();
61                 
62                 // Base page URLs, with and without www.
63
String JavaDoc basePageUrlWWW =
64                     rctx.getAbsoluteContextUrl(request)+"/page/"+userName;
65                 String JavaDoc basePageUrl = basePageUrlWWW;
66                 if ( basePageUrlWWW.startsWith("http://www.") )
67                 {
68                     // chop off the http://www.
69
basePageUrl = "http://"+basePageUrlWWW.substring(11);
70                 }
71                                  
72                 // Base comment URLs, with and without www.
73
String JavaDoc baseCommentsUrlWWW =
74                     rctx.getAbsoluteContextUrl(request)+"/comments/"+userName;
75                 String JavaDoc baseCommentsUrl = baseCommentsUrlWWW;
76                 if ( baseCommentsUrlWWW.startsWith("http://www.") )
77                 {
78                     // chop off the http://www.
79
baseCommentsUrl= "http://"+baseCommentsUrlWWW.substring(11);
80                 }
81                 
82                 // Don't process hits from same user's blogs as referers by
83
// ignoring Don't process referer from pages that start with base URLs.
84
String JavaDoc referer = request.getHeader("Referer");
85                 if ( referer==null ||
86                       (
87                          !referer.startsWith( basePageUrl )
88                       && !referer.startsWith( basePageUrlWWW )
89                       && !referer.startsWith( baseCommentsUrl )
90                       && !referer.startsWith( baseCommentsUrlWWW )
91                       )
92                    )
93                 {
94                     RefererManager refMgr =
95                         rreq.getRoller().getRefererManager();
96                     isRefSpammer = refMgr.processRequest(rreq);
97                 }
98                 else
99                 {
100                     if (mLogger.isDebugEnabled())
101                     {
102                         mLogger.debug("Ignoring referer="+referer);
103                     }
104                 }
105             }
106         }
107         catch (Exception JavaDoc e)
108         {
109             mLogger.error("Processing referer",e);
110         }
111         
112         if (isRefSpammer)
113         {
114             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)res;
115             response.sendError(HttpServletResponse.SC_FORBIDDEN);
116         }
117         else
118         {
119             chain.doFilter(req, res);
120         }
121     }
122
123     /**
124      * init
125      */

126     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc
127     {
128         mFilterConfig = filterConfig;
129     }
130 }
131
Popular Tags