Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-1806 Delegate processing of URL domains to crawler commons

Sebastian Nagel40881e8b755e
diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.java
index b439e5f40..0cfce1c65 100644
--- a/src/java/org/apache/nutch/util/URLUtil.java
+++ b/src/java/org/apache/nutch/util/URLUtil.java
@@ -149,7 +149,7 @@ public class URLUtil {
}
/**
- * Returns the top-level domain name of the URL. The top level domain name of
+ * Returns the top-level domain name of the URL. The top-level domain name of
* a URL is the substring of the URL's hostname, w/o subdomain names. As an
* example <br>
* <code>
@@ -162,11 +162,14 @@ public class URLUtil {
* returned.
*
* @param url
- * input {@link URL} to extract the top level domain name from
- * @return the top level domain name or the empty string if there is none
+ * input {@link URL} to extract the top-level domain name from
+ * @return the top-level domain name or null if there is none
*/
public static String getTopLevelDomainName(URL url) {
String suffix = getDomainSuffix(url);
+ if (suffix == null) {
+ return null;
+ }
int idx = suffix.lastIndexOf(".");
if (idx != -1) {
return suffix.substring(idx + 1);
@@ -176,7 +179,7 @@ public class URLUtil {
}
/**
- * Returns the top-level domain name of the URL. The top level domain name of
+ * Returns the top-level domain name of the URL. The top-level domain name of
* a URL is the substring of the URL's hostname, w/o subdomain names. As an
* example <br>
* <code>
@@ -189,8 +192,8 @@ public class URLUtil {
* returned.
*
* @param url
- * input URL string to extract the top level domain name from
- * @return the top level domain name or the empty string if there is none
+ * input URL string to extract the top-level domain name from
+ * @return the top-level domain name or null if there is none
* @throws MalformedURLException
* if the input URL is malformed
*/
@@ -247,7 +250,7 @@ public class URLUtil {
*
* @param url
* a {@link URL} to extract the domain suffix from
- * @return the domain suffix or the empty string if there is none
+ * @return the domain suffix or null if there is none
*/
public static String getDomainSuffix(URL url) {
String host = url.getHost();
@@ -262,7 +265,7 @@ public class URLUtil {
return suffix.getDomain();
}
- return "";
+ return null;
}
/**
@@ -275,7 +278,7 @@ public class URLUtil {
*
* @param url
* a {@link URL} to extract the domain suffix from
- * @return the domain suffix or the empty string if there is none
+ * @return the domain suffix or null if there is none
* @throws MalformedURLException
* if the input URL string is malformed
*/
diff --git a/src/test/org/apache/nutch/util/TestURLUtil.java b/src/test/org/apache/nutch/util/TestURLUtil.java
index 36724c32f..f8a0a8876 100644
--- a/src/test/org/apache/nutch/util/TestURLUtil.java
+++ b/src/test/org/apache/nutch/util/TestURLUtil.java
@@ -98,7 +98,7 @@ public class TestURLUtil {
Assert.assertEquals("org", URLUtil.getDomainSuffix(url));
url = new URL("http://140.211.11.130/foundation/contributing.html");
- Assert.assertEquals("", URLUtil.getDomainSuffix(url));
+ Assert.assertNull(URLUtil.getDomainSuffix(url));
url = new URL("http://www.example.co.uk:8080/index.html");
Assert.assertEquals("co.uk", URLUtil.getDomainSuffix(url));
@@ -111,10 +111,10 @@ public class TestURLUtil {
// "nn" is not a public suffix
url = new URL("http://example.com.nn");
- Assert.assertEquals("", URLUtil.getDomainSuffix(url));
+ Assert.assertNull(URLUtil.getDomainSuffix(url));
url = new URL("http://");
- Assert.assertEquals("", URLUtil.getDomainSuffix(url));
+ Assert.assertNull(URLUtil.getDomainSuffix(url));
/*
* "xyz" is an ICANN suffix since 2014, see
@@ -157,17 +157,17 @@ public class TestURLUtil {
Assert.assertEquals("org", URLUtil.getTopLevelDomainName(url));
url = new URL("http://140.211.11.130/foundation/contributing.html");
- Assert.assertEquals("", URLUtil.getTopLevelDomainName(url));
+ Assert.assertNull(URLUtil.getTopLevelDomainName(url));
url = new URL("http://www.example.co.uk:8080/index.html");
Assert.assertEquals("uk", URLUtil.getTopLevelDomainName(url));
// "nn" is not a public suffix
url = new URL("http://example.com.nn");
- Assert.assertEquals("", URLUtil.getTopLevelDomainName(url));
+ Assert.assertNull(URLUtil.getTopLevelDomainName(url));
url = new URL("http://");
- Assert.assertEquals("", URLUtil.getTopLevelDomainName(url));
+ Assert.assertNull(URLUtil.getTopLevelDomainName(url));
url = new URL("http://nic.삼성/");
Assert.assertEquals("xn--cg4bki", URLUtil.getTopLevelDomainName(url));