Change to FlowModule to support the new flat flows

This factors out a huge chunk of boilerplate that
would otherwise be in every single flow.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133014837
This commit is contained in:
cgoldfeder 2016-09-13 10:18:33 -07:00 committed by Ben McIlwain
parent 04fd14995e
commit efd3424849

View file

@ -18,9 +18,14 @@ import static com.google.common.base.Preconditions.checkState;
import dagger.Module;
import dagger.Provides;
import google.registry.flows.exceptions.OnlyToolCanPassMetadataException;
import google.registry.flows.picker.FlowPicker;
import google.registry.model.domain.metadata.MetadataExtension;
import google.registry.model.eppcommon.Trid;
import google.registry.model.eppinput.EppInput;
import google.registry.model.eppinput.EppInput.ResourceCommandWrapper;
import google.registry.model.eppinput.ResourceCommand;
import google.registry.model.reporting.HistoryEntry;
import java.lang.annotation.Documented;
import javax.annotation.Nullable;
import javax.inject.Qualifier;
@ -164,6 +169,44 @@ public class FlowModule {
}
}
@Provides
@FlowScope
static ResourceCommand provideResourceCommand(EppInput eppInput) {
return ((ResourceCommandWrapper) eppInput.getCommandWrapper().getCommand())
.getResourceCommand();
}
/**
* Provides a partially filled in {@link HistoryEntry} builder.
*
* <p>This is not marked with {@link FlowScope} so that each retry gets a fresh one. Otherwise,
* the fact that the builder is one-use would cause NPEs.
*/
@Provides
static HistoryEntry.Builder provideHistoryEntryBuilder(
Trid trid,
@InputXml byte[] inputXmlBytes,
@Superuser boolean isSuperuser,
@ClientId @Nullable String clientId,
EppRequestSource eppRequestSource,
EppInput eppInput) {
HistoryEntry.Builder historyBuilder = new HistoryEntry.Builder()
.setTrid(trid)
.setXmlBytes(inputXmlBytes)
.setBySuperuser(isSuperuser)
.setClientId(clientId);
MetadataExtension metadataExtension = eppInput.getSingleExtension(MetadataExtension.class);
if (metadataExtension != null) {
if (!eppRequestSource.equals(EppRequestSource.TOOL)) {
throw new EppExceptionInProviderException(new OnlyToolCanPassMetadataException());
}
historyBuilder
.setReason(metadataExtension.getReason())
.setRequestedByRegistrar(metadataExtension.getRequestedByRegistrar());
}
return historyBuilder;
}
/** Wrapper class to carry an {@link EppException} to the calling code. */
static class EppExceptionInProviderException extends RuntimeException {
EppExceptionInProviderException(EppException exception) {