Make loadByForeignKey() and related methods return Optional

This is safer and addresses a common source of confusion in the codebase because it's always explicit that the resource returned may not be present, whether because it's soft-deleted when projected to the given time or because it never existed in the first place.

In production code, the presence of the returned value is always checked. In test code, its presence is assumed using .get() where that is expected and convenient, as it not being present will throw an NPE that will cause the test to fail anyway.

Note that the roughly equivalent reloadResourceByForeignKey(), which is widely used in test code, is not having this same treatment applied to it. That is out of the scope of this CL, and has much smaller returns anyway because it's only used in tests (where the unexpected absence of a given resource would just cause the test to fail).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=225424002
This commit is contained in:
mcilwain 2018-12-13 13:17:30 -08:00 committed by jianglai
parent b573ec4969
commit 4491b7b909
52 changed files with 374 additions and 290 deletions

View file

@ -272,6 +272,18 @@ public abstract class RdapActionBase implements Runnable {
|| registrarParam.get().equals(eppResource.getPersistedCurrentSponsorClientId()));
}
/**
* Returns true if the EPP resource should be visible.
*
* <p>This is true iff:
* 1. The passed in resource exists and is not deleted (deleted ones will have been projected
* forward in time to empty),
* 2. The request did not specify a registrar to filter on, or the registrar matches.
*/
boolean shouldBeVisible(Optional<? extends EppResource> eppResource, DateTime now) {
return eppResource.isPresent() && shouldBeVisible(eppResource.get(), now);
}
/**
* Returns true if the registrar should be visible.
*

View file

@ -29,6 +29,7 @@ import google.registry.request.Action;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.request.auth.Auth;
import java.util.Optional;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -74,14 +75,14 @@ public class RdapDomainAction extends RdapActionBase {
pathSearchString, getHumanReadableObjectTypeName(), e.getMessage()));
}
// The query string is not used; the RDAP syntax is /rdap/domain/mydomain.com.
DomainResource domainResource =
Optional<DomainResource> domainResource =
loadByForeignKey(
DomainResource.class, pathSearchString, shouldIncludeDeleted() ? START_OF_TIME : now);
if ((domainResource == null) || !shouldBeVisible(domainResource, now)) {
if (!shouldBeVisible(domainResource, now)) {
throw new NotFoundException(pathSearchString + " not found");
}
return rdapJsonFormatter.makeRdapJsonForDomain(
domainResource,
domainResource.get(),
true,
fullServletPath,
rdapWhoisServer,

View file

@ -216,13 +216,13 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
*/
private RdapSearchResults searchByDomainNameWithoutWildcard(
final RdapSearchPattern partialStringQuery, final DateTime now) {
DomainResource domainResource =
Optional<DomainResource> domainResource =
loadByForeignKey(DomainResource.class, partialStringQuery.getInitialString(), now);
ImmutableList<DomainResource> results =
((domainResource == null) || !shouldBeVisible(domainResource, now))
? ImmutableList.of()
: ImmutableList.of(domainResource);
return makeSearchResults(results, now);
return makeSearchResults(
shouldBeVisible(domainResource, now)
? ImmutableList.of(domainResource.get())
: ImmutableList.of(),
now);
}
/** Searches for domains by domain name with an initial string, wildcard and possible suffix. */
@ -343,15 +343,15 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
// the key.
Optional<String> desiredRegistrar = getDesiredRegistrar();
if (desiredRegistrar.isPresent()) {
HostResource host =
Optional<HostResource> host =
loadByForeignKey(
HostResource.class,
partialStringQuery.getInitialString(),
shouldIncludeDeleted() ? START_OF_TIME : now);
return ((host == null)
|| !desiredRegistrar.get().equals(host.getPersistedCurrentSponsorClientId()))
return (!host.isPresent()
|| !desiredRegistrar.get().equals(host.get().getPersistedCurrentSponsorClientId()))
? ImmutableList.of()
: ImmutableList.of(Key.create(host));
: ImmutableList.of(Key.create(host.get()));
} else {
Key<HostResource> hostKey =
loadAndGetKey(
@ -370,15 +370,14 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
// with no initial string.
DomainResource domainResource =
loadByForeignKey(
DomainResource.class,
partialStringQuery.getSuffix(),
shouldIncludeDeleted() ? START_OF_TIME : now);
if (domainResource == null) {
// Don't allow wildcards with suffixes which are not domains we manage. That would risk a
// table scan in some easily foreseeable cases.
throw new UnprocessableEntityException(
"A suffix in a lookup by nameserver name must be a domain defined in the system");
}
DomainResource.class,
partialStringQuery.getSuffix(),
shouldIncludeDeleted() ? START_OF_TIME : now)
.orElseThrow(
() ->
new UnprocessableEntityException(
"A suffix in a lookup by nameserver name "
+ "must be a domain defined in the system"));
Optional<String> desiredRegistrar = getDesiredRegistrar();
ImmutableList.Builder<Key<HostResource>> builder = new ImmutableList.Builder<>();
for (String fqhn : ImmutableSortedSet.copyOf(domainResource.getSubordinateHosts())) {
@ -386,12 +385,12 @@ public class RdapDomainSearchAction extends RdapSearchActionBase {
// then the query ns.exam*.example.com would match against nameserver ns.example.com.
if (partialStringQuery.matches(fqhn)) {
if (desiredRegistrar.isPresent()) {
HostResource host =
Optional<HostResource> host =
loadByForeignKey(
HostResource.class, fqhn, shouldIncludeDeleted() ? START_OF_TIME : now);
if ((host != null)
&& desiredRegistrar.get().equals(host.getPersistedCurrentSponsorClientId())) {
builder.add(Key.create(host));
if (host.isPresent()
&& desiredRegistrar.get().equals(host.get().getPersistedCurrentSponsorClientId())) {
builder.add(Key.create(host.get()));
}
} else {
Key<HostResource> hostKey =

View file

@ -29,6 +29,7 @@ import google.registry.request.Action;
import google.registry.request.HttpException.BadRequestException;
import google.registry.request.HttpException.NotFoundException;
import google.registry.request.auth.Auth;
import java.util.Optional;
import javax.inject.Inject;
import org.joda.time.DateTime;
@ -76,13 +77,13 @@ public class RdapNameserverAction extends RdapActionBase {
}
// If there are no undeleted nameservers with the given name, the foreign key should point to
// the most recently deleted one.
HostResource hostResource =
Optional<HostResource> hostResource =
loadByForeignKey(
HostResource.class, pathSearchString, shouldIncludeDeleted() ? START_OF_TIME : now);
if ((hostResource == null) || !shouldBeVisible(hostResource, now)) {
if (!shouldBeVisible(hostResource, now)) {
throw new NotFoundException(pathSearchString + " not found");
}
return rdapJsonFormatter.makeRdapJsonForHost(
hostResource, true, fullServletPath, rdapWhoisServer, now, OutputDataType.FULL);
hostResource.get(), true, fullServletPath, rdapWhoisServer, now, OutputDataType.FULL);
}
}

View file

@ -183,9 +183,9 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
*/
private RdapSearchResults searchByNameUsingForeignKey(
final RdapSearchPattern partialStringQuery, final DateTime now) {
HostResource hostResource =
Optional<HostResource> hostResource =
loadByForeignKey(HostResource.class, partialStringQuery.getInitialString(), now);
if ((hostResource == null) || !shouldBeVisible(hostResource, now)) {
if (!shouldBeVisible(hostResource, now)) {
metricInformationBuilder.setNumHostsRetrieved(0);
throw new NotFoundException("No nameservers found");
}
@ -193,15 +193,20 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
return RdapSearchResults.create(
ImmutableList.of(
rdapJsonFormatter.makeRdapJsonForHost(
hostResource, false, fullServletPath, rdapWhoisServer, now, OutputDataType.FULL)));
hostResource.get(),
false,
fullServletPath,
rdapWhoisServer,
now,
OutputDataType.FULL)));
}
/** Searches for nameservers by name using the superordinate domain as a suffix. */
private RdapSearchResults searchByNameUsingSuperordinateDomain(
final RdapSearchPattern partialStringQuery, final DateTime now) {
DomainResource domainResource =
Optional<DomainResource> domainResource =
loadByForeignKey(DomainResource.class, partialStringQuery.getSuffix(), now);
if (domainResource == null) {
if (!domainResource.isPresent()) {
// Don't allow wildcards with suffixes which are not domains we manage. That would risk a
// table scan in many easily foreseeable cases. The user might ask for ns*.zombo.com,
// forcing us to query for all hosts beginning with ns, then filter for those ending in
@ -211,16 +216,16 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
"A suffix after a wildcard in a nameserver lookup must be an in-bailiwick domain");
}
List<HostResource> hostList = new ArrayList<>();
for (String fqhn : ImmutableSortedSet.copyOf(domainResource.getSubordinateHosts())) {
for (String fqhn : ImmutableSortedSet.copyOf(domainResource.get().getSubordinateHosts())) {
if (cursorString.isPresent() && (fqhn.compareTo(cursorString.get()) <= 0)) {
continue;
}
// We can't just check that the host name starts with the initial query string, because
// then the query ns.exam*.example.com would match against nameserver ns.example.com.
if (partialStringQuery.matches(fqhn)) {
HostResource hostResource = loadByForeignKey(HostResource.class, fqhn, now);
if ((hostResource != null) && shouldBeVisible(hostResource, now)) {
hostList.add(hostResource);
Optional<HostResource> hostResource = loadByForeignKey(HostResource.class, fqhn, now);
if (shouldBeVisible(hostResource, now)) {
hostList.add(hostResource.get());
if (hostList.size() > rdapResultSetMaxSize) {
break;
}
@ -230,7 +235,7 @@ public class RdapNameserverSearchAction extends RdapSearchActionBase {
return makeSearchResults(
hostList,
IncompletenessWarningType.COMPLETE,
domainResource.getSubordinateHosts().size(),
domainResource.get().getSubordinateHosts().size(),
CursorType.NAME,
now);
}