A Java implementation of the Public Suffix List algorithm. More information about the list can be found at https://www.publicsuffix.org
Requires Java 8 runtime.
// The Public Suffix List has two divisions, ICANN and PRIVATE suffixes
// It is possible to search for domains and suffixes,
// from ICANN, PRIVATE or ANY (for both divisions)
PublicSuffixList psl = PublicSuffixList.fromDefaultList();
// returns a domain that can be registered under a ICANN suffix,
// local or invalid domains are not returned
String icannDomain = psl.getRegisterableDomain("news.mail.example.com");
System.out.println(icannDomain); //should return 'example.com'
String icannOrPrivate = psl.getDomainRoot("a.b.c.uk.com");
// should return 'c.uk.com', 'uk.com' is a private suffix
System.out.println(icannOrPrivate);
String icannOrPrivate2 = psl.getDomainRoot("a.b.c.uk.com", SuffixType.ANY);
// should return 'c.uk.com', 'uk.com' is a private suffix
System.out.println(icannOrPrivate2);
String suffix = psl.getSuffix("news.mail.local.example");
// should return 'example'
System.out.println(suffix);
String suffix2 = psl.getSuffix("news.mail.example.net", SuffixType.ICANN);
// should return 'net'
System.out.println(suffix2);
String suffixIcann = psl.getSuffix("a.b.c.uk.com", SuffixType.PRIVATE);
// should return 'uk.com', example.com would return null
System.out.println(suffixIcann);
String suffix3 = psl.getSuffix("platform.sh", SuffixType.ANY);
// should return 'sh'
System.out.println(suffix3);
String name = psl.getNameRoot("a.x.s.s.a.example");
//should return 'a.example', the domain is invalid, but is syntactically valid
System.out.println(name);
String name2 = psl.getNameRoot("a.x.s.s.a.uk.com");
//should return 'a.uk.com', it is from private list
System.out.println(name2);
boolean maySetCookie = psl.maySetCookies("example.org");
//should return a boolean indicating if domain can set cookies
System.out.println(maySetCookie);
Public Suffix List for Java is licensed under Apache License 2.0.