diff --git a/java/google/registry/flows/ResourceFlowUtils.java b/java/google/registry/flows/ResourceFlowUtils.java index 99e97349d..5700da0fd 100644 --- a/java/google/registry/flows/ResourceFlowUtils.java +++ b/java/google/registry/flows/ResourceFlowUtils.java @@ -31,6 +31,7 @@ import com.googlecode.objectify.Key; import com.googlecode.objectify.Work; import google.registry.flows.EppException.AuthorizationErrorException; import google.registry.flows.EppException.InvalidAuthorizationInformationErrorException; +import google.registry.flows.exceptions.MissingTransferRequestAuthInfoException; import google.registry.flows.exceptions.ResourceAlreadyExistsException; import google.registry.flows.exceptions.ResourceStatusProhibitsOperationException; import google.registry.flows.exceptions.ResourceToDeleteIsReferencedException; @@ -314,6 +315,15 @@ public class ResourceFlowUtils { } } + /** Check that the given AuthInfo is present and valid for a resource being transferred. */ + public static void verifyRequiredAuthInfoForResourceTransfer( + Optional authInfo, ContactResource existingContact) throws EppException { + if (!authInfo.isPresent()) { + throw new MissingTransferRequestAuthInfoException(); + } + verifyOptionalAuthInfoForResource(authInfo, existingContact); + } + /** Check that the given AuthInfo is valid for the given resource. */ public static void verifyAuthInfoForResource(AuthInfo authInfo, EppResource resource) throws EppException { diff --git a/java/google/registry/flows/contact/ContactCreateFlow.java b/java/google/registry/flows/contact/ContactCreateFlow.java index f3f9db24e..f6d8ac553 100644 --- a/java/google/registry/flows/contact/ContactCreateFlow.java +++ b/java/google/registry/flows/contact/ContactCreateFlow.java @@ -14,10 +14,10 @@ package google.registry.flows.contact; +import static google.registry.flows.ResourceFlowUtils.verifyResourceDoesNotExist; import static google.registry.flows.contact.ContactFlowUtils.validateAsciiPostalInfo; import static google.registry.flows.contact.ContactFlowUtils.validateContactAgainstPolicy; import static google.registry.model.EppResourceUtils.createContactHostRoid; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -27,7 +27,6 @@ import google.registry.flows.FlowModule.ClientId; import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; -import google.registry.flows.exceptions.ResourceAlreadyExistsException; import google.registry.model.contact.ContactCommand.Create; import google.registry.model.contact.ContactResource; import google.registry.model.contact.ContactResource.Builder; @@ -64,28 +63,26 @@ public class ContactCreateFlow extends LoggedInFlow implements TransactionalFlow @Override protected final EppOutput run() throws EppException { Create command = (Create) resourceCommand; - if (loadByUniqueId(ContactResource.class, targetId, now) != null) { - throw new ResourceAlreadyExistsException(targetId); - } + verifyResourceDoesNotExist(ContactResource.class, targetId, now); Builder builder = new Builder(); command.applyTo(builder); - ContactResource newResource = builder + ContactResource newContact = builder .setCreationClientId(clientId) .setCurrentSponsorClientId(clientId) .setRepoId(createContactHostRoid(ObjectifyService.allocateId())) .build(); - validateAsciiPostalInfo(newResource.getInternationalizedPostalInfo()); - validateContactAgainstPolicy(newResource); + validateAsciiPostalInfo(newContact.getInternationalizedPostalInfo()); + validateContactAgainstPolicy(newContact); historyBuilder .setType(HistoryEntry.Type.CONTACT_CREATE) .setModificationTime(now) .setXmlBytes(null) // We don't want to store contact details in the history entry. - .setParent(Key.create(newResource)); + .setParent(Key.create(newContact)); ofy().save().entities( - newResource, + newContact, historyBuilder.build(), - ForeignKeyIndex.create(newResource, newResource.getDeletionTime()), - EppResourceIndex.create(Key.create(newResource))); - return createOutput(SUCCESS, ContactCreateData.create(newResource.getContactId(), now)); + ForeignKeyIndex.create(newContact, newContact.getDeletionTime()), + EppResourceIndex.create(Key.create(newContact))); + return createOutput(SUCCESS, ContactCreateData.create(newContact.getContactId(), now)); } } diff --git a/java/google/registry/flows/contact/ContactDeleteFlow.java b/java/google/registry/flows/contact/ContactDeleteFlow.java index 71a3bb134..d1e5003b7 100644 --- a/java/google/registry/flows/contact/ContactDeleteFlow.java +++ b/java/google/registry/flows/contact/ContactDeleteFlow.java @@ -15,10 +15,10 @@ package google.registry.flows.contact; import static google.registry.flows.ResourceFlowUtils.failfastForAsyncDelete; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyNoDisallowedStatuses; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS_WITH_ACTION_PENDING; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -33,7 +33,6 @@ import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.async.AsyncFlowEnqueuer; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.domain.DomainBase; import google.registry.model.domain.metadata.MetadataExtension; @@ -83,23 +82,20 @@ public class ContactDeleteFlow extends LoggedInFlow implements TransactionalFlow @Override public final EppOutput run() throws EppException { failfastForAsyncDelete(targetId, now, ContactResource.class, GET_REFERENCED_CONTACTS); - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - verifyNoDisallowedStatuses(existingResource, DISALLOWED_STATUSES); - verifyOptionalAuthInfoForResource(authInfo, existingResource); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyNoDisallowedStatuses(existingContact, DISALLOWED_STATUSES); + verifyOptionalAuthInfoForResource(authInfo, existingContact); if (!isSuperuser) { - verifyResourceOwnership(clientId, existingResource); + verifyResourceOwnership(clientId, existingContact); } - asyncFlowEnqueuer.enqueueAsyncDelete(existingResource, clientId, isSuperuser); - ContactResource newResource = - existingResource.asBuilder().addStatusValue(StatusValue.PENDING_DELETE).build(); + asyncFlowEnqueuer.enqueueAsyncDelete(existingContact, clientId, isSuperuser); + ContactResource newContact = + existingContact.asBuilder().addStatusValue(StatusValue.PENDING_DELETE).build(); historyBuilder .setType(HistoryEntry.Type.CONTACT_PENDING_DELETE) .setModificationTime(now) - .setParent(Key.create(existingResource)); - ofy().save().entities(newResource, historyBuilder.build()); + .setParent(Key.create(existingContact)); + ofy().save().entities(newContact, historyBuilder.build()); return createOutput(SUCCESS_WITH_ACTION_PENDING); } } diff --git a/java/google/registry/flows/contact/ContactInfoFlow.java b/java/google/registry/flows/contact/ContactInfoFlow.java index 5e52e3ef2..ab0365f34 100644 --- a/java/google/registry/flows/contact/ContactInfoFlow.java +++ b/java/google/registry/flows/contact/ContactInfoFlow.java @@ -14,16 +14,15 @@ package google.registry.flows.contact; +import static google.registry.flows.ResourceFlowUtils.loadResourceForQuery; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.model.EppResourceUtils.cloneResourceWithLinkedStatus; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import com.google.common.base.Optional; import google.registry.flows.EppException; import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; -import google.registry.flows.exceptions.ResourceToQueryDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.eppcommon.AuthInfo; import google.registry.model.eppoutput.EppOutput; @@ -42,11 +41,8 @@ public class ContactInfoFlow extends LoggedInFlow { @Override public final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToQueryDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); - return createOutput(SUCCESS, cloneResourceWithLinkedStatus(existingResource, now)); + ContactResource contact = loadResourceForQuery(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, contact); + return createOutput(SUCCESS, cloneResourceWithLinkedStatus(contact, now)); } } diff --git a/java/google/registry/flows/contact/ContactTransferApproveFlow.java b/java/google/registry/flows/contact/ContactTransferApproveFlow.java index 66aa588d5..6d5369155 100644 --- a/java/google/registry/flows/contact/ContactTransferApproveFlow.java +++ b/java/google/registry/flows/contact/ContactTransferApproveFlow.java @@ -15,11 +15,11 @@ package google.registry.flows.contact; import static google.registry.flows.ResourceFlowUtils.approvePendingTransfer; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership; import static google.registry.flows.contact.ContactFlowUtils.createGainingTransferPollMessage; import static google.registry.flows.contact.ContactFlowUtils.createTransferResponse; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -31,7 +31,6 @@ import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.exceptions.NotPendingTransferException; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.domain.metadata.MetadataExtension; import google.registry.model.eppcommon.AuthInfo; @@ -67,30 +66,27 @@ public class ContactTransferApproveFlow extends LoggedInFlow implements Transact @Override public final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); - TransferData transferData = existingResource.getTransferData(); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, existingContact); + TransferData transferData = existingContact.getTransferData(); if (transferData.getTransferStatus() != TransferStatus.PENDING) { throw new NotPendingTransferException(targetId); } - verifyResourceOwnership(clientId, existingResource); - ContactResource newResource = - approvePendingTransfer(existingResource, TransferStatus.CLIENT_APPROVED, now); + verifyResourceOwnership(clientId, existingContact); + ContactResource newContact = + approvePendingTransfer(existingContact, TransferStatus.CLIENT_APPROVED, now); HistoryEntry historyEntry = historyBuilder .setType(HistoryEntry.Type.CONTACT_TRANSFER_APPROVE) .setModificationTime(now) - .setParent(Key.create(existingResource)) + .setParent(Key.create(existingContact)) .build(); // Create a poll message for the gaining client. PollMessage gainingPollMessage = - createGainingTransferPollMessage(targetId, newResource.getTransferData(), historyEntry); - ofy().save().entities(newResource, historyEntry, gainingPollMessage); + createGainingTransferPollMessage(targetId, newContact.getTransferData(), historyEntry); + ofy().save().entities(newContact, historyEntry, gainingPollMessage); // Delete the billing event and poll messages that were written in case the transfer would have // been implicitly server approved. ofy().delete().keys(transferData.getServerApproveEntities()); - return createOutput(SUCCESS, createTransferResponse(targetId, newResource.getTransferData())); + return createOutput(SUCCESS, createTransferResponse(targetId, newContact.getTransferData())); } } diff --git a/java/google/registry/flows/contact/ContactTransferCancelFlow.java b/java/google/registry/flows/contact/ContactTransferCancelFlow.java index c66362e79..edb9b08d5 100644 --- a/java/google/registry/flows/contact/ContactTransferCancelFlow.java +++ b/java/google/registry/flows/contact/ContactTransferCancelFlow.java @@ -15,10 +15,10 @@ package google.registry.flows.contact; import static google.registry.flows.ResourceFlowUtils.denyPendingTransfer; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.contact.ContactFlowUtils.createLosingTransferPollMessage; import static google.registry.flows.contact.ContactFlowUtils.createTransferResponse; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -31,7 +31,6 @@ import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.exceptions.NotPendingTransferException; import google.registry.flows.exceptions.NotTransferInitiatorException; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.domain.metadata.MetadataExtension; import google.registry.model.eppcommon.AuthInfo; @@ -67,33 +66,29 @@ public class ContactTransferCancelFlow extends LoggedInFlow implements Transacti @Override protected final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - // Fail if the object doesn't exist or was deleted. - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); - TransferData transferData = existingResource.getTransferData(); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, existingContact); + TransferData transferData = existingContact.getTransferData(); if (transferData.getTransferStatus() != TransferStatus.PENDING) { throw new NotPendingTransferException(targetId); } if (!clientId.equals(transferData.getGainingClientId())) { throw new NotTransferInitiatorException(); } - ContactResource newResource = - denyPendingTransfer(existingResource, TransferStatus.CLIENT_CANCELLED, now); + ContactResource newContact = + denyPendingTransfer(existingContact, TransferStatus.CLIENT_CANCELLED, now); HistoryEntry historyEntry = historyBuilder .setType(HistoryEntry.Type.CONTACT_TRANSFER_CANCEL) .setModificationTime(now) - .setParent(Key.create(existingResource)) + .setParent(Key.create(existingContact)) .build(); // Create a poll message for the losing client. PollMessage losingPollMessage = - createLosingTransferPollMessage(targetId, newResource.getTransferData(), historyEntry); - ofy().save().entities(newResource, historyEntry, losingPollMessage); + createLosingTransferPollMessage(targetId, newContact.getTransferData(), historyEntry); + ofy().save().entities(newContact, historyEntry, losingPollMessage); // Delete the billing event and poll messages that were written in case the transfer would have // been implicitly server approved. ofy().delete().keys(transferData.getServerApproveEntities()); - return createOutput(SUCCESS, createTransferResponse(targetId, newResource.getTransferData())); + return createOutput(SUCCESS, createTransferResponse(targetId, newContact.getTransferData())); } } diff --git a/java/google/registry/flows/contact/ContactTransferQueryFlow.java b/java/google/registry/flows/contact/ContactTransferQueryFlow.java index f7cbe5c9a..d3b939b2c 100644 --- a/java/google/registry/flows/contact/ContactTransferQueryFlow.java +++ b/java/google/registry/flows/contact/ContactTransferQueryFlow.java @@ -14,9 +14,9 @@ package google.registry.flows.contact; +import static google.registry.flows.ResourceFlowUtils.loadResourceForQuery; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.contact.ContactFlowUtils.createTransferResponse; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import com.google.common.base.Optional; @@ -26,7 +26,6 @@ import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.exceptions.NoTransferHistoryToQueryException; import google.registry.flows.exceptions.NotAuthorizedToViewTransferException; -import google.registry.flows.exceptions.ResourceToQueryDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.eppcommon.AuthInfo; import google.registry.model.eppoutput.EppOutput; @@ -49,24 +48,20 @@ public class ContactTransferQueryFlow extends LoggedInFlow { @Override public final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToQueryDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); + ContactResource contact = loadResourceForQuery(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, contact); // Most of the fields on the transfer response are required, so there's no way to return valid // XML if the object has never been transferred (and hence the fields aren't populated). - if (existingResource.getTransferData().getTransferStatus() == null) { + if (contact.getTransferData().getTransferStatus() == null) { throw new NoTransferHistoryToQueryException(); } // Note that the authorization info on the command (if present) has already been verified. If // it's present, then the other checks are unnecessary. if (!authInfo.isPresent() - && !clientId.equals(existingResource.getTransferData().getGainingClientId()) - && !clientId.equals(existingResource.getTransferData().getLosingClientId())) { + && !clientId.equals(contact.getTransferData().getGainingClientId()) + && !clientId.equals(contact.getTransferData().getLosingClientId())) { throw new NotAuthorizedToViewTransferException(); } - return createOutput( - SUCCESS, createTransferResponse(targetId, existingResource.getTransferData())); + return createOutput(SUCCESS, createTransferResponse(targetId, contact.getTransferData())); } } diff --git a/java/google/registry/flows/contact/ContactTransferRejectFlow.java b/java/google/registry/flows/contact/ContactTransferRejectFlow.java index a62e28a82..1487c328c 100644 --- a/java/google/registry/flows/contact/ContactTransferRejectFlow.java +++ b/java/google/registry/flows/contact/ContactTransferRejectFlow.java @@ -15,11 +15,11 @@ package google.registry.flows.contact; import static google.registry.flows.ResourceFlowUtils.denyPendingTransfer; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership; import static google.registry.flows.contact.ContactFlowUtils.createGainingTransferPollMessage; import static google.registry.flows.contact.ContactFlowUtils.createTransferResponse; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -31,7 +31,6 @@ import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.exceptions.NotPendingTransferException; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.domain.metadata.MetadataExtension; import google.registry.model.eppcommon.AuthInfo; @@ -65,29 +64,26 @@ public class ContactTransferRejectFlow extends LoggedInFlow implements Transacti @Override protected final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); - TransferData transferData = existingResource.getTransferData(); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, existingContact); + TransferData transferData = existingContact.getTransferData(); if (transferData.getTransferStatus() != TransferStatus.PENDING) { throw new NotPendingTransferException(targetId); } - verifyResourceOwnership(clientId, existingResource); - ContactResource newResource = - denyPendingTransfer(existingResource, TransferStatus.CLIENT_REJECTED, now); + verifyResourceOwnership(clientId, existingContact); + ContactResource newContact = + denyPendingTransfer(existingContact, TransferStatus.CLIENT_REJECTED, now); HistoryEntry historyEntry = historyBuilder .setType(HistoryEntry.Type.CONTACT_TRANSFER_REJECT) .setModificationTime(now) - .setParent(Key.create(existingResource)) + .setParent(Key.create(existingContact)) .build(); PollMessage gainingPollMessage = - createGainingTransferPollMessage(targetId, newResource.getTransferData(), historyEntry); - ofy().save().entities(newResource, historyEntry, gainingPollMessage); + createGainingTransferPollMessage(targetId, newContact.getTransferData(), historyEntry); + ofy().save().entities(newContact, historyEntry, gainingPollMessage); // Delete the billing event and poll messages that were written in case the transfer would have // been implicitly server approved. ofy().delete().keys(transferData.getServerApproveEntities()); - return createOutput(SUCCESS, createTransferResponse(targetId, newResource.getTransferData())); + return createOutput(SUCCESS, createTransferResponse(targetId, newContact.getTransferData())); } } diff --git a/java/google/registry/flows/contact/ContactTransferRequestFlow.java b/java/google/registry/flows/contact/ContactTransferRequestFlow.java index 7f6cd4d26..3d8572bdc 100644 --- a/java/google/registry/flows/contact/ContactTransferRequestFlow.java +++ b/java/google/registry/flows/contact/ContactTransferRequestFlow.java @@ -14,12 +14,12 @@ package google.registry.flows.contact; -import static google.registry.flows.ResourceFlowUtils.verifyAuthInfoForResource; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyNoDisallowedStatuses; +import static google.registry.flows.ResourceFlowUtils.verifyRequiredAuthInfoForResourceTransfer; import static google.registry.flows.contact.ContactFlowUtils.createGainingTransferPollMessage; import static google.registry.flows.contact.ContactFlowUtils.createLosingTransferPollMessage; import static google.registry.flows.contact.ContactFlowUtils.createTransferResponse; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS_WITH_ACTION_PENDING; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -33,9 +33,7 @@ import google.registry.flows.FlowModule.TargetId; import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.exceptions.AlreadyPendingTransferException; -import google.registry.flows.exceptions.MissingTransferRequestAuthInfoException; import google.registry.flows.exceptions.ObjectAlreadySponsoredException; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.model.contact.ContactResource; import google.registry.model.domain.metadata.MetadataExtension; import google.registry.model.eppcommon.AuthInfo; @@ -79,28 +77,22 @@ public class ContactTransferRequestFlow extends LoggedInFlow implements Transact @Override protected final EppOutput run() throws EppException { - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - if (!authInfo.isPresent()) { - throw new MissingTransferRequestAuthInfoException(); - } - verifyAuthInfoForResource(authInfo.get(), existingResource); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyRequiredAuthInfoForResourceTransfer(authInfo, existingContact); // Verify that the resource does not already have a pending transfer. - if (TransferStatus.PENDING.equals(existingResource.getTransferData().getTransferStatus())) { + if (TransferStatus.PENDING.equals(existingContact.getTransferData().getTransferStatus())) { throw new AlreadyPendingTransferException(targetId); } - String losingClientId = existingResource.getCurrentSponsorClientId(); + String losingClientId = existingContact.getCurrentSponsorClientId(); // Verify that this client doesn't already sponsor this resource. if (gainingClientId.equals(losingClientId)) { throw new ObjectAlreadySponsoredException(); } - verifyNoDisallowedStatuses(existingResource, DISALLOWED_STATUSES); + verifyNoDisallowedStatuses(existingContact, DISALLOWED_STATUSES); HistoryEntry historyEntry = historyBuilder .setType(HistoryEntry.Type.CONTACT_TRANSFER_REQUEST) .setModificationTime(now) - .setParent(Key.create(existingResource)) + .setParent(Key.create(existingContact)) .build(); DateTime transferExpirationTime = now.plus(automaticTransferLength); TransferData serverApproveTransferData = new TransferData.Builder() @@ -129,19 +121,19 @@ public class ContactTransferRequestFlow extends LoggedInFlow implements Transact createLosingTransferPollMessage(targetId, pendingTransferData, historyEntry).asBuilder() .setEventTime(now) // Unlike the serverApprove messages, this applies immediately. .build(); - ContactResource newResource = existingResource.asBuilder() + ContactResource newContact = existingContact.asBuilder() .setTransferData(pendingTransferData) .addStatusValue(StatusValue.PENDING_TRANSFER) .build(); ofy().save().entities( - newResource, + newContact, historyEntry, requestPollMessage, serverApproveGainingPollMessage, serverApproveLosingPollMessage); return createOutput( SUCCESS_WITH_ACTION_PENDING, - createTransferResponse(targetId, newResource.getTransferData())); + createTransferResponse(targetId, newContact.getTransferData())); } } diff --git a/java/google/registry/flows/contact/ContactUpdateFlow.java b/java/google/registry/flows/contact/ContactUpdateFlow.java index 930c85ba5..cc769d047 100644 --- a/java/google/registry/flows/contact/ContactUpdateFlow.java +++ b/java/google/registry/flows/contact/ContactUpdateFlow.java @@ -14,12 +14,12 @@ package google.registry.flows.contact; +import static google.registry.flows.ResourceFlowUtils.loadResourceToMutate; import static google.registry.flows.ResourceFlowUtils.verifyNoDisallowedStatuses; import static google.registry.flows.ResourceFlowUtils.verifyOptionalAuthInfoForResource; import static google.registry.flows.ResourceFlowUtils.verifyResourceOwnership; import static google.registry.flows.contact.ContactFlowUtils.validateAsciiPostalInfo; import static google.registry.flows.contact.ContactFlowUtils.validateContactAgainstPolicy; -import static google.registry.model.EppResourceUtils.loadByUniqueId; import static google.registry.model.eppoutput.Result.Code.SUCCESS; import static google.registry.model.ofy.ObjectifyService.ofy; @@ -34,7 +34,6 @@ import google.registry.flows.LoggedInFlow; import google.registry.flows.TransactionalFlow; import google.registry.flows.exceptions.AddRemoveSameValueEppException; import google.registry.flows.exceptions.ResourceHasClientUpdateProhibitedException; -import google.registry.flows.exceptions.ResourceToMutateDoesNotExistException; import google.registry.flows.exceptions.StatusNotClientSettableException; import google.registry.model.contact.ContactCommand.Update; import google.registry.model.contact.ContactResource; @@ -86,13 +85,10 @@ public class ContactUpdateFlow extends LoggedInFlow implements TransactionalFlow @Override public final EppOutput run() throws EppException { Update command = (Update) resourceCommand; - ContactResource existingResource = loadByUniqueId(ContactResource.class, targetId, now); - if (existingResource == null) { - throw new ResourceToMutateDoesNotExistException(ContactResource.class, targetId); - } - verifyOptionalAuthInfoForResource(authInfo, existingResource); + ContactResource existingContact = loadResourceToMutate(ContactResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, existingContact); if (!isSuperuser) { - verifyResourceOwnership(clientId, existingResource); + verifyResourceOwnership(clientId, existingContact); } for (StatusValue statusValue : Sets.union( command.getInnerAdd().getStatusValues(), @@ -101,32 +97,32 @@ public class ContactUpdateFlow extends LoggedInFlow implements TransactionalFlow throw new StatusNotClientSettableException(statusValue.getXmlName()); } } - verifyNoDisallowedStatuses(existingResource, DISALLOWED_STATUSES); + verifyNoDisallowedStatuses(existingContact, DISALLOWED_STATUSES); historyBuilder .setType(HistoryEntry.Type.CONTACT_UPDATE) .setModificationTime(now) .setXmlBytes(null) // We don't want to store contact details in the history entry. - .setParent(Key.create(existingResource)); - Builder builder = existingResource.asBuilder(); + .setParent(Key.create(existingContact)); + Builder builder = existingContact.asBuilder(); try { command.applyTo(builder); } catch (AddRemoveSameValueException e) { throw new AddRemoveSameValueEppException(); } - ContactResource newResource = builder + ContactResource newContact = builder .setLastEppUpdateTime(now) .setLastEppUpdateClientId(clientId) .build(); // If the resource is marked with clientUpdateProhibited, and this update did not clear that // status, then the update must be disallowed (unless a superuser is requesting the change). if (!isSuperuser - && existingResource.getStatusValues().contains(StatusValue.CLIENT_UPDATE_PROHIBITED) - && newResource.getStatusValues().contains(StatusValue.CLIENT_UPDATE_PROHIBITED)) { + && existingContact.getStatusValues().contains(StatusValue.CLIENT_UPDATE_PROHIBITED) + && newContact.getStatusValues().contains(StatusValue.CLIENT_UPDATE_PROHIBITED)) { throw new ResourceHasClientUpdateProhibitedException(); } - validateAsciiPostalInfo(newResource.getInternationalizedPostalInfo()); - validateContactAgainstPolicy(newResource); - ofy().save().entities(newResource, historyBuilder.build()); + validateAsciiPostalInfo(newContact.getInternationalizedPostalInfo()); + validateContactAgainstPolicy(newContact); + ofy().save().entities(newContact, historyBuilder.build()); return createOutput(SUCCESS); } } diff --git a/java/google/registry/flows/host/HostCreateFlow.java b/java/google/registry/flows/host/HostCreateFlow.java index ee910f81b..eaf3946f5 100644 --- a/java/google/registry/flows/host/HostCreateFlow.java +++ b/java/google/registry/flows/host/HostCreateFlow.java @@ -96,7 +96,7 @@ public class HostCreateFlow extends LoggedInFlow implements TransactionalFlow { } Builder builder = new Builder(); command.applyTo(builder); - HostResource newResource = builder + HostResource newHost = builder .setCreationClientId(clientId) .setCurrentSponsorClientId(clientId) .setRepoId(createContactHostRoid(ObjectifyService.allocateId())) @@ -106,12 +106,12 @@ public class HostCreateFlow extends LoggedInFlow implements TransactionalFlow { historyBuilder .setType(HistoryEntry.Type.HOST_CREATE) .setModificationTime(now) - .setParent(Key.create(newResource)); + .setParent(Key.create(newHost)); ImmutableSet entitiesToSave = ImmutableSet.of( - newResource, + newHost, historyBuilder.build(), - ForeignKeyIndex.create(newResource, newResource.getDeletionTime()), - EppResourceIndex.create(Key.create(newResource))); + ForeignKeyIndex.create(newHost, newHost.getDeletionTime()), + EppResourceIndex.create(Key.create(newHost))); if (superordinateDomain.isPresent()) { entitiesToSave = union( entitiesToSave, diff --git a/java/google/registry/flows/host/HostDeleteFlow.java b/java/google/registry/flows/host/HostDeleteFlow.java index 2ec392463..b16093312 100644 --- a/java/google/registry/flows/host/HostDeleteFlow.java +++ b/java/google/registry/flows/host/HostDeleteFlow.java @@ -79,20 +79,20 @@ public class HostDeleteFlow extends LoggedInFlow implements TransactionalFlow { @Override public final EppOutput run() throws EppException { failfastForAsyncDelete(targetId, now, HostResource.class, GET_NAMESERVERS); - HostResource existingResource = loadResourceToMutate(HostResource.class, targetId, now); - verifyNoDisallowedStatuses(existingResource, DISALLOWED_STATUSES); - verifyOptionalAuthInfoForResource(authInfo, existingResource); + HostResource existingHost = loadResourceToMutate(HostResource.class, targetId, now); + verifyNoDisallowedStatuses(existingHost, DISALLOWED_STATUSES); + verifyOptionalAuthInfoForResource(authInfo, existingHost); if (!isSuperuser) { - verifyResourceOwnership(clientId, existingResource); + verifyResourceOwnership(clientId, existingHost); } - asyncFlowEnqueuer.enqueueAsyncDelete(existingResource, clientId, isSuperuser); - HostResource newResource = - existingResource.asBuilder().addStatusValue(StatusValue.PENDING_DELETE).build(); + asyncFlowEnqueuer.enqueueAsyncDelete(existingHost, clientId, isSuperuser); + HostResource newHost = + existingHost.asBuilder().addStatusValue(StatusValue.PENDING_DELETE).build(); historyBuilder .setType(HistoryEntry.Type.HOST_PENDING_DELETE) .setModificationTime(now) - .setParent(Key.create(existingResource)); - ofy().save().entities(newResource, historyBuilder.build()); + .setParent(Key.create(existingHost)); + ofy().save().entities(newHost, historyBuilder.build()); return createOutput(SUCCESS_WITH_ACTION_PENDING); } } diff --git a/java/google/registry/flows/host/HostInfoFlow.java b/java/google/registry/flows/host/HostInfoFlow.java index aaba90c94..e67691508 100644 --- a/java/google/registry/flows/host/HostInfoFlow.java +++ b/java/google/registry/flows/host/HostInfoFlow.java @@ -41,8 +41,8 @@ public class HostInfoFlow extends LoggedInFlow { @Override public EppOutput run() throws EppException { - HostResource existingResource = loadResourceForQuery(HostResource.class, targetId, now); - verifyOptionalAuthInfoForResource(authInfo, existingResource); - return createOutput(SUCCESS, cloneResourceWithLinkedStatus(existingResource, now)); + HostResource host = loadResourceForQuery(HostResource.class, targetId, now); + verifyOptionalAuthInfoForResource(authInfo, host); + return createOutput(SUCCESS, cloneResourceWithLinkedStatus(host, now)); } } diff --git a/java/google/registry/flows/host/HostUpdateFlow.java b/java/google/registry/flows/host/HostUpdateFlow.java index 286eb2bfb..dc7b6a567 100644 --- a/java/google/registry/flows/host/HostUpdateFlow.java +++ b/java/google/registry/flows/host/HostUpdateFlow.java @@ -106,17 +106,17 @@ public class HostUpdateFlow extends LoggedInFlow implements TransactionalFlow { public final EppOutput run() throws EppException { Update command = (Update) resourceCommand; String suppliedNewHostName = command.getInnerChange().getFullyQualifiedHostName(); - HostResource existingResource = loadResourceToMutate(HostResource.class, targetId, now); + HostResource existingHost = loadResourceToMutate(HostResource.class, targetId, now); boolean isHostRename = suppliedNewHostName != null; String oldHostName = targetId; String newHostName = firstNonNull(suppliedNewHostName, oldHostName); Optional superordinateDomain = Optional.fromNullable(lookupSuperordinateDomain(validateHostName(newHostName), now)); - verifyUpdateAllowed(command, existingResource, superordinateDomain.orNull()); + verifyUpdateAllowed(command, existingHost, superordinateDomain.orNull()); if (isHostRename && loadAndGetKey(HostResource.class, newHostName, now) != null) { throw new HostAlreadyExistsException(newHostName); } - Builder builder = existingResource.asBuilder(); + Builder builder = existingHost.asBuilder(); try { command.applyTo(builder); } catch (AddRemoveSameValueException e) { @@ -132,23 +132,23 @@ public class HostUpdateFlow extends LoggedInFlow implements TransactionalFlow { superordinateDomain.isPresent() ? Key.create(superordinateDomain.get()) : null) .setLastSuperordinateChange(superordinateDomain == null ? null : now); // Rely on the host's cloneProjectedAtTime() method to handle setting of transfer data. - HostResource newResource = builder.build().cloneProjectedAtTime(now); - verifyHasIpsIffIsExternal(command, existingResource, newResource); + HostResource newHost = builder.build().cloneProjectedAtTime(now); + verifyHasIpsIffIsExternal(command, existingHost, newHost); ImmutableSet.Builder entitiesToSave = new ImmutableSet.Builder<>(); - entitiesToSave.add(newResource); + entitiesToSave.add(newHost); // Keep the {@link ForeignKeyIndex} for this host up to date. if (isHostRename) { // Update the foreign key for the old host name and save one for the new host name. entitiesToSave.add( - ForeignKeyIndex.create(existingResource, now), - ForeignKeyIndex.create(newResource, newResource.getDeletionTime())); - updateSuperordinateDomains(existingResource, newResource); + ForeignKeyIndex.create(existingHost, now), + ForeignKeyIndex.create(newHost, newHost.getDeletionTime())); + updateSuperordinateDomains(existingHost, newHost); } - enqueueTasks(existingResource, newResource); + enqueueTasks(existingHost, newHost); entitiesToSave.add(historyBuilder .setType(HistoryEntry.Type.HOST_UPDATE) .setModificationTime(now) - .setParent(Key.create(existingResource)) + .setParent(Key.create(existingHost)) .build()); ofy().save().entities(entitiesToSave.build()); return createOutput(SUCCESS);