Last active
March 14, 2019 14:50
-
-
Save wpik/cf2333befc2c745d35ad4a2aedeb228b to your computer and use it in GitHub Desktop.
Get top domain from URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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