KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > legacy > clickstream > BotChecker


1 package net.jforum.util.legacy.clickstream;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7
8 import net.jforum.util.legacy.clickstream.config.ClickstreamConfig;
9 import net.jforum.util.legacy.clickstream.config.ConfigLoader;
10
11 /**
12  * Determines if a request is actually a bot or spider.
13  *
14  * @author <a HREF="plightbo@hotmail.com">Patrick Lightbody</a>
15  * @author Rafael Steil (little hacks for JForum)
16  * @version $Id: BotChecker.java,v 1.6 2005/12/18 02:12:54 rafaelsteil Exp $
17  */

18 public class BotChecker
19 {
20     /**
21      * Checks if we have a bot
22      * @param request the request
23      * @return <code>null</code> if there is no bots in the current request,
24      * or the bot's name otherwise
25      */

26     public static String JavaDoc isBot(HttpServletRequest JavaDoc request)
27     {
28         if (request.getRequestURI().indexOf("robots.txt") != -1) {
29             // there is a specific request for the robots.txt file, so we assume
30
// it must be a robot (only robots request robots.txt)
31
return "Unknown (asked for robots.txt)";
32         }
33         
34         String JavaDoc userAgent = request.getHeader("User-Agent");
35         
36         ClickstreamConfig config = ConfigLoader.instance().getConfig();
37         
38         if (userAgent != null && config != null) {
39             List JavaDoc agents = config.getBotAgents();
40             
41             userAgent = userAgent.toLowerCase();
42             
43             for (Iterator JavaDoc iterator = agents.iterator(); iterator.hasNext(); ) {
44                 String JavaDoc agent = (String JavaDoc) iterator.next();
45                 
46                 if (agent == null) {
47                     continue;
48                 }
49                 
50                 if (userAgent.indexOf(agent) != -1) {
51                     return userAgent;
52                 }
53             }
54         }
55         
56         String JavaDoc remoteHost = request.getRemoteHost(); // requires a DNS lookup
57

58         if (remoteHost != null && remoteHost.length() > 0 && remoteHost.charAt(remoteHost.length() - 1) > 64) {
59             List JavaDoc hosts = config.getBotHosts();
60             
61             remoteHost = remoteHost.toLowerCase();
62             
63             for (Iterator JavaDoc iterator = hosts.iterator(); iterator.hasNext(); ) {
64                 String JavaDoc host = (String JavaDoc) iterator.next();
65                 
66                 if (host == null) {
67                     continue;
68                 }
69                 
70                 if (remoteHost.indexOf(host) != -1) {
71                     return remoteHost;
72                 }
73             }
74         }
75
76         return null;
77     }
78 }
Popular Tags