views:

160

answers:

3

I find myself with a need for a java utility for taking a fully-qualified hostname, and producing the domain name from that.

In the simple case, that means turning host.company.com into company.com, but this gets rapidly more complicated with host.subdomain.company.com, for example, or host.company.co.uk, where the meaning of "domain name" gets a bit fuzzy. Throw in complications with the definition of SLD and ccSLD, and it gets messy.

So my question is whether there's a 3rd-party library out there that understands these things and can give me sensible interpreations.

+3  A: 

I don't think such a thing exists, since it's an adminstrative rather than technical issue, and a very multi-lateral one, at that.

If you end up rolling your own, this page on the Mozilla wiki looks like a good starting point, with lots of references. Looks like a major headache though. Just look at the rules for Japan. Ouch.

Michael Borgwardt
For cookies it becomes a technical issue. (Looking at that table, I thought there were 10 exceptional second level domains, not 8.)
Tom Hawtin - tackline
+3  A: 

Mozilla regularly maintains the rules that it uses in its browser for cookie security in a format that can be parsed and used by others:

http://publicsuffix.org/

Searching Google, there are probably Java libraries that can parse the list, but I don't know the quality of any of them.

bkail
I think that's the closest I'm going to get. Thanks for that.
skaffman
Do note that this list is unofficial, incomplete (eu.org is missing, among others) and not up to date (since rules always change).
bortzmeyer
+1  A: 

Not sure if it's for the same purpose, I do something similar in my code. When I set cookies, I want to set the domain as close to top as possible so I need to find the domain one-level lower than a public suffix. For example, the highest domain you can set cookie for host.div.example.com is .example.com. For host.div.example.co.jp is .example.co.jp.

Unfortunately, the code is not in the public domain. It's very easy to do. I basically use following 2 classes from Apache HttpClient 4,

org.apache.http.impl.cookie.PublicSuffixFilter
org.apache.http.impl.cookie.PublicSuffixListParser

I forgot the exact reason but we had to make some very minor tweaks. You just walk the domain from top to bottom, first valid cookie domain is what you need.

You need to download the public suffix list from here and include it in your JAR,

http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective%5Ftld%5Fnames.dat?raw=1

ZZ Coder