#1 Add host to domain transformation method

已合併
jonathan 4 年之前 將 1 次代碼提交從 kris/host_to_domain合併至 master
  1. +14
    -7
      src/main/java/org/cobbzilla/util/http/URIUtil.java

+ 14
- 7
src/main/java/org/cobbzilla/util/http/URIUtil.java 查看文件

@@ -1,5 +1,6 @@
package org.cobbzilla.util.http;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
@@ -48,17 +49,23 @@ public class URIUtil {
}

/**
* getRegisteredDomain("foo.bar.baz") == "bar.baz"
* @param uri A URI that includes a host part
* getRegisteredDomain("http://foo.bar.baz/...") == "bar.baz"
* @param url A URI that includes a host part
* @return the "registered" domain, which includes the TLD and one level up.
*/
@NonNull public static String getRegisteredDomain(@NonNull final String url) { return hostToDomain(getHost(url)); }

/**
* hostToDomain("foo.bar.baz") == "bar.baz"
* @param host A full host name
* @return the "registered" domain, which includes the TLD and one level up.
*/
public static String getRegisteredDomain(String uri) {
final String host = getHost(uri);
jonathan 評論 4 年之前
Review

The getHost call was used to ensure that the uri argument is a valid URI. Can we leave it in? If that breaks something, maybe use a different method?

The `getHost` call was used to ensure that the `uri` argument is a valid URI. Can we leave it in? If that breaks something, maybe use a different method?
kris 評論 4 年之前
Review

Nothing is removed. getRegisteredDomain still works as before - redefined in line 56 above, and does call getHost.

hostToDomain is a brand new method with the second part of the previous method required and used currently only in some test.

Nothing is removed. `getRegisteredDomain` still works as before - redefined in line 56 above, and does call `getHost`. `hostToDomain` is a brand new method with the second part of the previous method required and used currently only in some test.
jonathan 評論 4 年之前
Review

Sorry, I missed that. Thanks.

Sorry, I missed that. Thanks.
final String parts[] = host.split("\\.");
@NonNull public static String hostToDomain(@NonNull final String host) {
final var parts = host.split("\\.");
switch (parts.length) {
case 0: throw new IllegalArgumentException("Invalid host: "+host);
case 0: throw new IllegalArgumentException("Invalid host: " + host);
case 1: return host;
default: return parts[parts.length-2] + "." + parts[parts.length-1];
default: return parts[parts.length - 2] + "." + parts[parts.length - 1];
}
}



Loading…
取消
儲存