KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > cds > WebBrowser


1 /*
2  * WebBrowser is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/cds/WebBrowser.java,v 1.3.2.1 2006/12/11 16:19:29 fxrobin Exp $
21  */

22
23 /*
24  * @(#)WebBrowser.java 1.8 2001/05/14
25  *
26  *
27  * Copyright (c) 1999-2001 KnightRidder.com
28  *
29  * This software is licensed under the Cofax Software License, Version 1.2.
30  * License.htm, provided by KnightRidder.com, documenting this license,
31  * must accompany this file in any distribution.
32  */

33 package org.cofax.cds;
34
35 import java.io.*;
36 import java.util.*;
37
38 import javax.servlet.http.*;
39
40 /**
41  *
42  * Gets Web Browser information for Cofax. To determine if the current browser
43  * is a search engine's web crawler, it uses a copy of the database obtained
44  * from http://info.webcrawler.com/mak/projects/robots/active/all.txt
45  *
46  * @author Rajiv Pant
47  * @author Hung Dao
48  * @author Karl Martino
49  * @version 1.5
50  *
51  */

52
53 public class WebBrowser {
54
55     static String JavaDoc dbLocation;
56
57     String JavaDoc userAgent;
58
59     String JavaDoc userAgentToCompare;
60
61     static HashSet searchRobots;
62
63     public WebBrowser(HttpServletRequest req) {
64
65         userAgent = req.getHeader("user-agent");
66
67         if (userAgent == null) {
68             userAgent = "";
69         }
70
71         userAgentToCompare = userAgent.toLowerCase().trim();
72
73         int positionOfSlash = userAgentToCompare.indexOf('/');
74         if (positionOfSlash > -1) {
75             userAgentToCompare = userAgentToCompare.substring(0, positionOfSlash);
76         }
77     }
78
79     public static void init(String JavaDoc db) {
80
81         dbLocation = db;
82
83         searchRobots = new HashSet();
84
85         try {
86             BufferedReader r = new BufferedReader(new FileReader(dbLocation));
87             String JavaDoc l;
88             while ((l = r.readLine()) != null) {
89
90                 if (l.length() > 0) {
91                     searchRobots.add(l);
92                 }
93             }
94         } catch (IOException e) {
95         }
96
97     }
98
99     public boolean isCrawler() {
100
101         if (searchRobots.contains(userAgentToCompare)) {
102             return true;
103         } else {
104             return false;
105         }
106     }
107
108 } // end class WebBrowser
109

110
Popular Tags