Merge pull request #12231 from apeterlic/get-domain-name
Get domain name
This commit is contained in:
commit
505d918fa5
|
@ -52,6 +52,12 @@
|
|||
<artifactId>java-ipv6</artifactId>
|
||||
<version>${googlecode.ipv6.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -84,6 +90,7 @@
|
|||
<jgonian.commons-ip-math.version>1.32</jgonian.commons-ip-math.version>
|
||||
<googlecode.ipv6.version>0.17</googlecode.ipv6.version>
|
||||
<javax.mail.version>1.6.2</javax.mail.version>
|
||||
<guava.version>31.0-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.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, "");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.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"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue