From f8b4c9eddb6d8b00ca1ff200b3396ef1c7139a3c Mon Sep 17 00:00:00 2001 From: mcilwain Date: Wed, 30 Nov 2016 14:55:54 -0800 Subject: [PATCH] Add extensible custom logic to domain application create flow ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=140656367 --- .../flows/custom/CustomLogicFactory.java | 5 + .../flows/custom/CustomLogicModule.java | 6 + ...omainApplicationCreateFlowCustomLogic.java | 220 ++++++++++++++++++ .../domain/DomainApplicationCreateFlow.java | 41 +++- 4 files changed, 268 insertions(+), 4 deletions(-) create mode 100644 java/google/registry/flows/custom/DomainApplicationCreateFlowCustomLogic.java diff --git a/java/google/registry/flows/custom/CustomLogicFactory.java b/java/google/registry/flows/custom/CustomLogicFactory.java index 5610c9f9e..234919a06 100644 --- a/java/google/registry/flows/custom/CustomLogicFactory.java +++ b/java/google/registry/flows/custom/CustomLogicFactory.java @@ -31,6 +31,11 @@ import google.registry.model.eppinput.EppInput; */ public class CustomLogicFactory { + public DomainApplicationCreateFlowCustomLogic forDomainApplicationCreateFlow( + EppInput eppInput, SessionMetadata sessionMetadata) { + return new DomainApplicationCreateFlowCustomLogic(eppInput, sessionMetadata); + } + public DomainCreateFlowCustomLogic forDomainCreateFlow( EppInput eppInput, SessionMetadata sessionMetadata) { return new DomainCreateFlowCustomLogic(eppInput, sessionMetadata); diff --git a/java/google/registry/flows/custom/CustomLogicModule.java b/java/google/registry/flows/custom/CustomLogicModule.java index 27b5b614f..003439e2b 100644 --- a/java/google/registry/flows/custom/CustomLogicModule.java +++ b/java/google/registry/flows/custom/CustomLogicModule.java @@ -23,6 +23,12 @@ import google.registry.model.eppinput.EppInput; @Module public class CustomLogicModule { + @Provides + static DomainApplicationCreateFlowCustomLogic provideDomainApplicationCreateFlowCustomLogic( + CustomLogicFactory factory, EppInput eppInput, SessionMetadata sessionMetadata) { + return factory.forDomainApplicationCreateFlow(eppInput, sessionMetadata); + } + @Provides static DomainCreateFlowCustomLogic provideDomainCreateFlowCustomLogic( CustomLogicFactory factory, EppInput eppInput, SessionMetadata sessionMetadata) { diff --git a/java/google/registry/flows/custom/DomainApplicationCreateFlowCustomLogic.java b/java/google/registry/flows/custom/DomainApplicationCreateFlowCustomLogic.java new file mode 100644 index 000000000..652d1669b --- /dev/null +++ b/java/google/registry/flows/custom/DomainApplicationCreateFlowCustomLogic.java @@ -0,0 +1,220 @@ +// Copyright 2016 The Nomulus Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package google.registry.flows.custom; + +import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; +import com.google.common.net.InternetDomainName; +import google.registry.flows.EppException; +import google.registry.flows.SessionMetadata; +import google.registry.flows.domain.DomainApplicationCreateFlow; +import google.registry.model.ImmutableObject; +import google.registry.model.domain.DomainApplication; +import google.registry.model.eppinput.EppInput; +import google.registry.model.eppoutput.EppResponse.ResponseData; +import google.registry.model.eppoutput.EppResponse.ResponseExtension; +import google.registry.model.reporting.HistoryEntry; + +/** + * A no-op base class for {@link DomainApplicationCreateFlow} custom logic. + * + *

Extend this class and override the hook(s) to perform custom logic. + */ +public class DomainApplicationCreateFlowCustomLogic extends BaseFlowCustomLogic { + + protected DomainApplicationCreateFlowCustomLogic( + EppInput eppInput, SessionMetadata sessionMetadata) { + super(eppInput, sessionMetadata); + } + + /** A hook that runs before any validation. This is useful to e.g. add allowable extensions. */ + @SuppressWarnings("unused") + public void beforeValidation() throws EppException { + // Do nothing. + } + + /** A hook that runs at the end of the validation step to perform additional validation. */ + @SuppressWarnings("unused") + public void afterValidation(AfterValidationParameters parameters) throws EppException { + // Do nothing. + } + + /** + * A hook that runs before new entities are persisted, allowing them to be changed. + * + *

It returns the actual entity changes that should be persisted to Datastore. It is important + * to be careful when changing the flow behavior for existing entities, because the core logic + * across many different flows expects the existence of these entities and many of the fields on + * them. + */ + @SuppressWarnings("unused") + public EntityChanges beforeSave(BeforeSaveParameters parameters) throws EppException { + return parameters.entityChanges(); + } + + /** + * A hook that runs before the response is returned. + * + *

This takes the {@link ResponseData} and {@link ResponseExtension}s as input and returns + * them, potentially with modifications. + */ + @SuppressWarnings("unused") + public BeforeResponseReturnData beforeResponse(BeforeResponseParameters parameters) + throws EppException { + return BeforeResponseReturnData.newBuilder() + .setResData(parameters.resData()) + .setResponseExtensions(parameters.responseExtensions()) + .build(); + } + + /** A class to encapsulate parameters for a call to {@link #afterValidation}. */ + @AutoValue + public abstract static class AfterValidationParameters extends ImmutableObject { + + /** The parsed domain name of the domain that is being applied for. */ + public abstract InternetDomainName domainName(); + + /** + * The number of years that the application is requesting registration for. + * + *

On standard TLDs, this is usually 1. + */ + public abstract int years(); + + public static Builder newBuilder() { + return new AutoValue_DomainApplicationCreateFlowCustomLogic_AfterValidationParameters + .Builder(); + } + + /** Builder for {@link AfterValidationParameters}. */ + @AutoValue.Builder + public abstract static class Builder { + + public abstract Builder setDomainName(InternetDomainName domainName); + + public abstract Builder setYears(int years); + + public abstract AfterValidationParameters build(); + } + } + + /** A class to encapsulate parameters for a call to {@link #beforeSave}. */ + @AutoValue + public abstract static class BeforeSaveParameters extends ImmutableObject { + + /** + * The new {@link DomainApplication} entity that is going to be persisted at the end of the + * transaction. + */ + public abstract DomainApplication newApplication(); + + /** + * The new {@link HistoryEntry} entity for the application's creation that is going to be + * persisted at the end of the transaction. + */ + public abstract HistoryEntry historyEntry(); + + /** + * The collection of {@link EntityChanges} (including new entities and those to delete) that + * will be persisted at the end of the transaction. + * + *

Note that the new application and history entry are also included as saves in this + * collection, and are separated out above solely for convenience, as they are most likely to + * need to be changed. Removing them from the collection will cause them not to be saved, which + * is most likely not what you intended. + */ + public abstract EntityChanges entityChanges(); + + /** + * The number of years that the application is requesting registration for. + * + *

On standard TLDs, this is usually 1. + */ + public abstract int years(); + + public static Builder newBuilder() { + return new AutoValue_DomainApplicationCreateFlowCustomLogic_BeforeSaveParameters.Builder(); + } + + /** Builder for {@link BeforeSaveParameters}. */ + @AutoValue.Builder + public abstract static class Builder { + + public abstract Builder setNewApplication(DomainApplication newApplication); + + public abstract Builder setHistoryEntry(HistoryEntry historyEntry); + + public abstract Builder setEntityChanges(EntityChanges entityChanges); + + public abstract Builder setYears(int years); + + public abstract BeforeSaveParameters build(); + } + } + + /** A class to encapsulate parameters for a call to {@link #beforeResponse}. */ + @AutoValue + public abstract static class BeforeResponseParameters extends ImmutableObject { + + public abstract ResponseData resData(); + + public abstract ImmutableList responseExtensions(); + + public static BeforeResponseParameters.Builder newBuilder() { + return new AutoValue_DomainApplicationCreateFlowCustomLogic_BeforeResponseParameters + .Builder(); + } + + /** Builder for {@link DomainApplicationCreateFlowCustomLogic.BeforeResponseParameters}. */ + @AutoValue.Builder + public abstract static class Builder { + + public abstract BeforeResponseParameters.Builder setResData(ResponseData resData); + + public abstract BeforeResponseParameters.Builder setResponseExtensions( + ImmutableList responseExtensions); + + public abstract BeforeResponseParameters build(); + } + } + + /** + * A class to encapsulate parameters for the return values from a call to {@link #beforeResponse}. + */ + @AutoValue + public abstract static class BeforeResponseReturnData extends ImmutableObject { + + public abstract ResponseData resData(); + + public abstract ImmutableList responseExtensions(); + + public static BeforeResponseReturnData.Builder newBuilder() { + return new AutoValue_DomainApplicationCreateFlowCustomLogic_BeforeResponseReturnData + .Builder(); + } + + /** Builder for {@link DomainApplicationCreateFlowCustomLogic.BeforeResponseReturnData}. */ + @AutoValue.Builder + public abstract static class Builder { + + public abstract BeforeResponseReturnData.Builder setResData(ResponseData resData); + + public abstract BeforeResponseReturnData.Builder setResponseExtensions( + ImmutableList responseExtensions); + + public abstract BeforeResponseReturnData build(); + } + } +} diff --git a/java/google/registry/flows/domain/DomainApplicationCreateFlow.java b/java/google/registry/flows/domain/DomainApplicationCreateFlow.java index 6977c10b0..69064317f 100644 --- a/java/google/registry/flows/domain/DomainApplicationCreateFlow.java +++ b/java/google/registry/flows/domain/DomainApplicationCreateFlow.java @@ -15,6 +15,7 @@ package google.registry.flows.domain; import static com.google.common.collect.Iterables.getOnlyElement; +import static google.registry.flows.FlowUtils.persistEntityChanges; import static google.registry.flows.FlowUtils.validateClientIsLoggedIn; import static google.registry.flows.ResourceFlowUtils.verifyResourceDoesNotExist; import static google.registry.flows.domain.DomainFlowUtils.checkAllowedAccessToTld; @@ -58,6 +59,11 @@ import google.registry.flows.FlowModule.ClientId; import google.registry.flows.FlowModule.Superuser; import google.registry.flows.FlowModule.TargetId; import google.registry.flows.TransactionalFlow; +import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic; +import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.AfterValidationParameters; +import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.BeforeResponseParameters; +import google.registry.flows.custom.DomainApplicationCreateFlowCustomLogic.BeforeResponseReturnData; +import google.registry.flows.custom.EntityChanges; import google.registry.flows.domain.TldSpecificLogicProxy.EppCommandOperations; import google.registry.model.ImmutableObject; import google.registry.model.domain.DomainApplication; @@ -167,6 +173,7 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow { @Inject HistoryEntry.Builder historyBuilder; @Inject Trid trid; @Inject EppResponse.Builder responseBuilder; + @Inject DomainApplicationCreateFlowCustomLogic customLogic; @Inject DomainApplicationCreateFlow() {} @Override @@ -177,6 +184,7 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow { FlagsCreateCommandExtension.class, MetadataExtension.class, LaunchCreateExtension.class); + customLogic.beforeValidation(); extensionManager.validate(); validateClientIsLoggedIn(clientId); DateTime now = ofy().getTransactionTime(); @@ -197,6 +205,7 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow { // Superusers can create reserved domains, force creations on domains that require a claims // notice without specifying a claims key, and override blocks on registering premium domains. verifyUnitIsYears(command.getPeriod()); + int years = command.getPeriod().getValue(); validateCreateCommandContactsAndNameservers(command, tld); LaunchCreateExtension launchCreate = eppInput.getSingleExtension(LaunchCreateExtension.class); if (launchCreate != null) { @@ -217,6 +226,11 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow { validateFeeChallenge(targetId, tld, now, feeCreate, commandOperations.getTotalCost()); SecDnsCreateExtension secDnsCreate = validateSecDnsExtension(eppInput.getSingleExtension(SecDnsCreateExtension.class)); + customLogic.afterValidation( + AfterValidationParameters.newBuilder() + .setDomainName(domainName) + .setYears(years) + .build()); DomainApplication newApplication = new DomainApplication.Builder() .setCreationTrid(trid) .setCreationClientId(clientId) @@ -258,11 +272,30 @@ public final class DomainApplicationCreateFlow implements TransactionalFlow { entitiesToSave.add( prepareMarkedLrpTokenEntity(authInfo.getPw().getValue(), domainName, historyEntry)); } - ofy().save().entities(entitiesToSave.build()); + EntityChanges entityChanges = + customLogic.beforeSave( + DomainApplicationCreateFlowCustomLogic.BeforeSaveParameters.newBuilder() + .setNewApplication(newApplication) + .setHistoryEntry(historyEntry) + .setEntityChanges( + EntityChanges.newBuilder().setSaves(entitiesToSave.build()).build()) + .setYears(years) + .build()); + persistEntityChanges(entityChanges); + BeforeResponseReturnData responseData = + customLogic.beforeResponse( + BeforeResponseParameters.newBuilder() + .setResData(DomainCreateData.create(targetId, now, null)) + .setResponseExtensions( + createResponseExtensions( + newApplication.getForeignKey(), + launchCreate.getPhase(), + feeCreate, + commandOperations)) + .build()); return responseBuilder - .setResData(DomainCreateData.create(targetId, now, null)) - .setExtensions(createResponseExtensions( - newApplication.getForeignKey(), launchCreate.getPhase(), feeCreate, commandOperations)) + .setResData(responseData.resData()) + .setExtensions(responseData.responseExtensions()) .build(); }