KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > Domains


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jun 2, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27
28 /**
29  * @author J. H. S.
30  */

31 public class Domains {
32     private static final Collection gTLDs;
33     
34     static {
35         gTLDs = new HashSet();
36         gTLDs.add(".com");
37         gTLDs.add(".edu");
38         gTLDs.add(".gov");
39         gTLDs.add(".int");
40         gTLDs.add(".mil");
41         gTLDs.add(".net");
42         gTLDs.add(".org");
43         gTLDs.add(".biz");
44         gTLDs.add(".info");
45         gTLDs.add(".name");
46         gTLDs.add(".pro");
47         gTLDs.add(".aero");
48         gTLDs.add(".coop");
49         gTLDs.add(".museum");
50         //TODO: New gTLDs?
51
}
52     
53     /**
54      *
55      */

56     private Domains() {
57         super();
58     }
59     
60     public static boolean isValidCookieDomain(String JavaDoc domain, String JavaDoc hostName) {
61         String JavaDoc plainDomain;
62         if(!domain.startsWith(".")) {
63             // Valid domains must start with a dot
64
// according to RFC 2109, but
65
// RFC 2965 specifies a dot is prepended
66
// in the Set-Cookie2 header.
67
plainDomain = domain;
68             domain = "." + domain;
69         }
70         else {
71             plainDomain = domain.substring(1);
72         }
73         String JavaDoc plainDomainTL = plainDomain.toLowerCase();
74         String JavaDoc hostNameTL = hostName.toLowerCase();
75         if(!hostNameTL.endsWith(plainDomainTL)) {
76             return false;
77         }
78         int lastDotIdx = domain.lastIndexOf('.');
79         if(lastDotIdx == -1) {
80             return false;
81         }
82         String JavaDoc suffix = domain.substring(lastDotIdx).toLowerCase();
83         if(gTLDs.contains(suffix)) {
84             return Strings.countChars(domain, '.') >= 2;
85         }
86         else {
87             return Strings.countChars(domain, '.') >= 3;
88         }
89     }
90
91     /**
92      *
93      * @param host A host name in lower case.
94      * @return
95      */

96     public static boolean endsWithGTLD(String JavaDoc host) {
97         Iterator i = gTLDs.iterator();
98         while(i.hasNext()) {
99             String JavaDoc ending = (String JavaDoc) i.next();
100             if(host.endsWith(ending)) {
101                 return true;
102             }
103         }
104         return false;
105     }
106     
107     public static boolean isLikelyHostName(String JavaDoc name) {
108         String JavaDoc nameTL = name.toLowerCase();
109         if(nameTL.startsWith("www.")) {
110             return true;
111         }
112         if(endsWithGTLD(name)) {
113             return true;
114         }
115         int lastDotIdx = nameTL.lastIndexOf('.');
116         if(lastDotIdx == -1) {
117             return false;
118         }
119         // Check for country code.
120
return lastDotIdx == nameTL.length() - 3;
121     }
122     
123     public static Collection getPossibleDomains(String JavaDoc hostName) {
124         Collection domains = new LinkedList();
125         domains.add(hostName);
126         int dotIdx = hostName.indexOf('.', 1);
127         if(dotIdx == -1) {
128             return domains;
129         }
130         String JavaDoc testDomain = hostName.substring(dotIdx);
131         if(!Domains.isValidCookieDomain(testDomain, hostName)) {
132             return domains;
133         }
134         domains.addAll(Domains.getPossibleDomains(testDomain.substring(1)));
135         return domains;
136     }
137 }
138
Popular Tags