mirror of
https://github.com/google/nomulus.git
synced 2025-06-27 06:44:51 +02:00
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:
parent
b573ec4969
commit
4491b7b909
52 changed files with 374 additions and 290 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue