#1 Add host to domain transformation method

Fusionnée
jonathan a fusionné 1 révision(s) à partir de kris/host_to_domain vers master il y a 4 ans
  1. +14
    -7
      src/main/java/org/cobbzilla/util/http/URIUtil.java

+ 14
- 7
src/main/java/org/cobbzilla/util/http/URIUtil.java Voir le fichier

@@ -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 a commenté il y a 4 ans
Révision

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 a commenté il y a 4 ans
Révision

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 a commenté il y a 4 ans
Révision

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];
}
}



Chargement…
Annuler
Enregistrer