diff --git a/java/google/registry/model/registrar/Registrar.java b/java/google/registry/model/registrar/Registrar.java index 46aee314b..94fbd816a 100644 --- a/java/google/registry/model/registrar/Registrar.java +++ b/java/google/registry/model/registrar/Registrar.java @@ -29,7 +29,7 @@ import static google.registry.config.RegistryConfig.getDefaultRegistrarWhoisServ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.Ofy.RECOMMENDED_MEMCACHE_EXPIRATION; -import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.model.registry.Registries.assertTldsExist; import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy; import static google.registry.util.CollectionUtils.nullToEmptyImmutableSortedCopy; import static google.registry.util.X509Utils.getCertificateHash; @@ -670,10 +670,7 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable } public Builder setAllowedTlds(Set allowedTlds) { - for (String tld : allowedTlds) { - assertTldExists(tld); - } - getInstance().allowedTlds = ImmutableSortedSet.copyOf(allowedTlds); + getInstance().allowedTlds = ImmutableSortedSet.copyOf(assertTldsExist(allowedTlds)); return this; } diff --git a/java/google/registry/model/registry/Registries.java b/java/google/registry/model/registry/Registries.java index 3d654bf7b..20beb3ab7 100644 --- a/java/google/registry/model/registry/Registries.java +++ b/java/google/registry/model/registry/Registries.java @@ -15,8 +15,9 @@ package google.registry.model.registry; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Predicates.equalTo; +import static com.google.common.base.Predicates.in; +import static com.google.common.base.Predicates.not; import static com.google.common.base.Strings.emptyToNull; import static com.google.common.collect.Maps.filterValues; import static google.registry.model.CacheUtils.memoizeWithShortExpiration; @@ -24,8 +25,10 @@ import static google.registry.model.common.EntityGroupRoot.getCrossTldKey; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; +import com.google.common.base.Joiner; import com.google.common.base.Optional; import com.google.common.base.Supplier; +import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.net.InternetDomainName; @@ -76,15 +79,25 @@ public final class Registries { return ImmutableSet.copyOf(filterValues(cache.get(), equalTo(type)).keySet()); } - /** Shortcut to check whether a tld exists or else throw. If it exists, it is returned back. */ + /** Pass-through check that the specified TLD exists, otherwise throw an IAE. */ public static String assertTldExists(String tld) { checkArgument( - getTlds().contains(checkNotNull(emptyToNull(tld), "Null or empty TLD specified")), + getTlds().contains(checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified")), "TLD %s does not exist", tld); return tld; } + /** Pass-through check that every TLD in the given iterable exists, otherwise throw an IAE. */ + public static Iterable assertTldsExist(Iterable tlds) { + for (String tld : tlds) { + checkArgumentNotNull(emptyToNull(tld), "Null or empty TLD specified"); + } + ImmutableSet badTlds = FluentIterable.from(tlds).filter(not(in(getTlds()))).toSet(); + checkArgument(badTlds.isEmpty(), "TLDs do not exist: %s", Joiner.on(", ").join(badTlds)); + return tlds; + } + /** * Returns TLD which the domain name or hostname falls under, no matter how many levels of * sublabels there are. diff --git a/java/google/registry/tools/CreateLrpTokensCommand.java b/java/google/registry/tools/CreateLrpTokensCommand.java index 1c2b1fa4a..0e0c5d466 100644 --- a/java/google/registry/tools/CreateLrpTokensCommand.java +++ b/java/google/registry/tools/CreateLrpTokensCommand.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.collect.Sets.difference; import static google.registry.model.ofy.ObjectifyService.ofy; -import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.model.registry.Registries.assertTldsExist; import static google.registry.util.TokenUtils.TokenType.LRP; import static java.nio.charset.StandardCharsets.UTF_8; @@ -49,7 +49,7 @@ import google.registry.util.TokenUtils; import java.io.StringReader; import java.nio.file.Path; import java.util.Collection; -import java.util.Set; +import java.util.List; import java.util.concurrent.Callable; import javax.inject.Inject; @@ -74,7 +74,7 @@ public class CreateLrpTokensCommand implements RemoteApiCommand { names = {"-t", "--tlds"}, description = "Comma-delimited list of TLDs that the tokens to create will be valid on", required = true) - private String tlds; + private List tlds; @Parameter( names = {"-i", "--input"}, @@ -121,10 +121,7 @@ public class CreateLrpTokensCommand implements RemoteApiCommand { checkArgument( (assignee == null) || (metadataColumns == null), "Metadata columns cannot be specified along with an assignee."); - final Set validTlds = ImmutableSet.copyOf(Splitter.on(',').split(tlds)); - for (String tld : validTlds) { - assertTldExists(tld); - } + ImmutableSet validTlds = ImmutableSet.copyOf(assertTldsExist(tlds)); LineReader reader = new LineReader( (assigneesFile != null) diff --git a/java/google/registry/tools/GenerateZoneFilesCommand.java b/java/google/registry/tools/GenerateZoneFilesCommand.java index e9333f906..2a111976b 100644 --- a/java/google/registry/tools/GenerateZoneFilesCommand.java +++ b/java/google/registry/tools/GenerateZoneFilesCommand.java @@ -14,7 +14,7 @@ package google.registry.tools; -import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.model.registry.Registries.assertTldsExist; import static org.joda.time.DateTimeZone.UTC; import static org.joda.time.Duration.standardMinutes; @@ -54,9 +54,7 @@ final class GenerateZoneFilesCommand implements ServerSideCommand { @Override public void run() throws IOException { - for (String tld : mainParameters) { - assertTldExists(tld); - } + assertTldsExist(mainParameters); ImmutableMap params = ImmutableMap.of( "tlds", mainParameters, "exportTime", exportDate.toString()); diff --git a/java/google/registry/tools/GetTldCommand.java b/java/google/registry/tools/GetTldCommand.java index 438df5b98..e8c7ac64a 100644 --- a/java/google/registry/tools/GetTldCommand.java +++ b/java/google/registry/tools/GetTldCommand.java @@ -14,7 +14,7 @@ package google.registry.tools; -import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.model.registry.Registries.assertTldsExist; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; @@ -33,8 +33,8 @@ final class GetTldCommand implements RemoteApiCommand { @Override public void run() { - for (String tld : mainParameters) { - System.out.println(Registry.get(assertTldExists(tld))); + for (String tld : assertTldsExist(mainParameters)) { + System.out.println(Registry.get(tld)); } } } diff --git a/java/google/registry/tools/server/ListDomainsAction.java b/java/google/registry/tools/server/ListDomainsAction.java index 8393ab717..b2564d688 100644 --- a/java/google/registry/tools/server/ListDomainsAction.java +++ b/java/google/registry/tools/server/ListDomainsAction.java @@ -16,7 +16,7 @@ package google.registry.tools.server; import static com.google.common.base.Preconditions.checkArgument; import static google.registry.model.EppResourceUtils.queryNotDeleted; -import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.model.registry.Registries.assertTldsExist; import static google.registry.request.Action.Method.GET; import static google.registry.request.Action.Method.POST; @@ -68,9 +68,7 @@ public final class ListDomainsAction extends ListObjectsAction { @Override public ImmutableSet loadObjects() { checkArgument(!tlds.isEmpty(), "Must specify TLDs to query"); - for (String tld : tlds) { - assertTldExists(tld); - } + assertTldsExist(tlds); ImmutableSortedSet.Builder builder = new ImmutableSortedSet.Builder(COMPARATOR); for (List batch : Lists.partition(tlds.asList(), MAX_NUM_SUBQUERIES)) { diff --git a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java index 82758a0c4..a89f3f6ba 100644 --- a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java +++ b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java @@ -258,7 +258,7 @@ public class CreateLrpTokensCommandTest extends CommandTestCase