Get domain name from given URL

This commit is contained in:
apeterlic 2022-05-19 06:50:58 +02:00
parent f8ce248236
commit ae6ed6ba95
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.networking.domain_name;
import com.google.common.net.InternetDomainName;
import java.net.URI;
import java.net.URISyntaxException;
public class DomainNameClient {
public String getHost(String url) throws URISyntaxException {
URI uri = new URI(url);
return uri.getHost();
}
public String getTopPrivateDomain(String url) {
InternetDomainName internetDomainName = InternetDomainName.from(url)
.topPrivateDomain();
return internetDomainName.toString();
}
public String getName(String url) {
InternetDomainName internetDomainName = InternetDomainName.from(url)
.topPrivateDomain();
String publicSuffix = internetDomainName.publicSuffix()
.toString();
String domainName = internetDomainName.toString();
return domainName.substring(0, domainName.lastIndexOf("." + publicSuffix));
}
public String getDomainName(String url) {
String regex = "http(s)?://|www\\.|/.*";
return url.replaceAll(regex, "");
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.networking.domain_name;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DomainNameClientUnitTest {
DomainNameClient domainNameClient = new DomainNameClient();
@Test
void givenUrl_whenGetHost_thenReturnSubdomainAndDomainName() {
Assertions.assertAll(() -> {
assertEquals("www.baeldung.com", domainNameClient.getHost("https://www.baeldung.com/domain"));
assertEquals("www.google.co.uk", domainNameClient.getHost("https://www.google.co.uk/domain"));
assertEquals("jira.baeldung.com", domainNameClient.getHost("https://jira.baeldung.com/secure"));
});
}
@Test
void givenUrl_whenGetTopPrivateDomain_thenReturnDomainName() {
assertEquals("baeldung.com", domainNameClient.getTopPrivateDomain("www.baeldung.com"));
}
@Test
void givenUrlWithPublicSuffix_whenGetTopPrivateDomain_thenReturnDomainName() {
assertEquals("google.co.uk", domainNameClient.getTopPrivateDomain("www.google.co.uk"));
assertEquals("baeldung.blogspot.com", domainNameClient.getTopPrivateDomain("www.baeldung.blogspot.com"));
}
@Test
void givenUrlWithPublicSuffix_whenGetName_thenReturnSecondLevelDomain() {
assertEquals("baeldung", domainNameClient.getName("jira.baeldung.com"));
}
@Test
void givenUrlWithPublicSuffix_whenGetName_thenReturnThirdLevelDomain() {
assertEquals("baeldung", domainNameClient.getName("www.baeldung.co.uk"));
assertEquals("google", domainNameClient.getName("www.google.co.uk"));
}
@Test
void givenUrl_whenGetDomainNameRegex_thenReturnDomainName() {
assertEquals("google.com", domainNameClient.getDomainName("www.google.com"));
assertEquals("google.co.uk", domainNameClient.getDomainName("www.google.co.uk"));
assertEquals("jira.baeldung.com", domainNameClient.getDomainName("jira.baeldung.com"));
assertEquals("baeldung.com", domainNameClient.getDomainName("www.baeldung.com/test"));
}
}