Skip to content

Instantly share code, notes, and snippets.

@wpik
Last active March 14, 2019 14:50
Show Gist options
  • Select an option

  • Save wpik/cf2333befc2c745d35ad4a2aedeb228b to your computer and use it in GitHub Desktop.

Select an option

Save wpik/cf2333befc2c745d35ad4a2aedeb228b to your computer and use it in GitHub Desktop.
Get top domain from URL
package example;
import com.google.common.net.InternetDomainName;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;
@Service
public class TopDomainExtractor {
public String getTopDomain(String requestUrl) {
try {
var host = UriComponentsBuilder.fromUriString(requestUrl).build().getHost();
return InternetDomainName.from(host).topPrivateDomain().toString();
} catch (IllegalStateException | NullPointerException e) {
throw new IllegalArgumentException("Cannot extract top domain from '" + requestUrl + "'", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment