From c5d6a1f6fb397dc7d40c22bf61377681354cc4e0 Mon Sep 17 00:00:00 2001 From: nickfelt Date: Fri, 21 Apr 2017 11:43:30 -0700 Subject: [PATCH] Add and adjust EppInput accessors for more useful properties This primarily adds accessors to EppInput that will be used for flow reporting logging in FlowReporter. Specifically, it adds: - Optional getResourceType() -> domain/host/contact - Optional getSingleTargetId() -> for SingleResourceCommands And in addition, it adjusts getCommandName() so that it's now named getCommandType() for better parallelism with the new getResourceType() (since getResourceName() would be misleading), and it changes the value returned to be lowercased, again for consistency. This isn't an issue because getCommandName() isn't actually used anywhere right now (it was formerly used for EPP whitebox metrics, but no longer due to recent changes there). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=153851957 --- .../registry/model/eppinput/EppInput.java | 53 +++++++++++++++++-- .../registry/flows/EppXmlTransformerTest.java | 2 +- .../registry/model/eppinput/EppInputTest.java | 18 ++++--- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/java/google/registry/model/eppinput/EppInput.java b/java/google/registry/model/eppinput/EppInput.java index 33604dfab..e86df37fc 100644 --- a/java/google/registry/model/eppinput/EppInput.java +++ b/java/google/registry/model/eppinput/EppInput.java @@ -17,6 +17,8 @@ package google.registry.model.eppinput; import static google.registry.util.CollectionUtils.nullSafeImmutableCopy; import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy; +import com.google.common.base.Ascii; +import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -63,6 +65,7 @@ import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlEnumValue; import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlSchema; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @@ -80,17 +83,57 @@ public class EppInput extends ImmutableObject { return commandWrapper; } - public String getCommandName() { - return (commandWrapper instanceof Hello) + /** + * Returns the EPP command name, defined as the name of the {@code InnerCommand} element within + * the {@code } element (e.g. "create" or "poll"), or "hello" for the hello command. + */ + public String getCommandType() { + return Ascii.toLowerCase((commandWrapper instanceof Hello) ? Hello.class.getSimpleName() - : commandWrapper.getCommand().getClass().getSimpleName(); + : commandWrapper.getCommand().getClass().getSimpleName()); } - public ImmutableList getTargetIds() { + /** + * Returns the EPP resource type ("domain", "contact", or "host") for commands that operate on + * EPP resources, otherwise absent. + */ + public Optional getResourceType() { + ResourceCommand resourceCommand = getResourceCommand(); + if (resourceCommand != null) { + XmlSchema xmlSchemaAnnotation = + resourceCommand.getClass().getPackage().getAnnotation(XmlSchema.class); + if (xmlSchemaAnnotation != null && xmlSchemaAnnotation.xmlns().length > 0) { + return Optional.of(xmlSchemaAnnotation.xmlns()[0].prefix()); + } + } + return Optional.absent(); + } + + @Nullable + private ResourceCommand getResourceCommand() { InnerCommand innerCommand = commandWrapper.getCommand(); - ResourceCommand resourceCommand = innerCommand instanceof ResourceCommandWrapper + return innerCommand instanceof ResourceCommandWrapper ? ((ResourceCommandWrapper) innerCommand).getResourceCommand() : null; + } + + /** + * Returns the target ID (name for domains and hosts, contact ID for contacts) if this command + * always acts on a single EPP resource, or absent otherwise (e.g. for "check" or "poll"). + */ + public Optional getSingleTargetId() { + ResourceCommand resourceCommand = getResourceCommand(); + return resourceCommand instanceof SingleResourceCommand + ? Optional.of(((SingleResourceCommand) resourceCommand).getTargetId()) + : Optional.absent(); + } + + /** + * Returns all the target IDs (name for domains and hosts, contact ID for contacts) that this + * command references if it acts on EPP resources, or the empty list otherwise (e.g. for "poll"). + */ + public ImmutableList getTargetIds() { + ResourceCommand resourceCommand = getResourceCommand(); if (resourceCommand instanceof SingleResourceCommand) { return ImmutableList.of(((SingleResourceCommand) resourceCommand).getTargetId()); } else if (resourceCommand instanceof ResourceCheck) { diff --git a/javatests/google/registry/flows/EppXmlTransformerTest.java b/javatests/google/registry/flows/EppXmlTransformerTest.java index f7034f6b1..b0ff472bd 100644 --- a/javatests/google/registry/flows/EppXmlTransformerTest.java +++ b/javatests/google/registry/flows/EppXmlTransformerTest.java @@ -39,7 +39,7 @@ public class EppXmlTransformerTest extends ShardableTestCase { public void testUnmarshalingEppInput() throws Exception { EppInput input = unmarshal( EppInput.class, readResourceBytes(getClass(), "testdata/contact_info.xml").read()); - assertThat(input.getCommandName()).isEqualTo("Info"); + assertThat(input.getCommandType()).isEqualTo("info"); } @Test diff --git a/javatests/google/registry/model/eppinput/EppInputTest.java b/javatests/google/registry/model/eppinput/EppInputTest.java index 87cd858f2..5a562caad 100644 --- a/javatests/google/registry/model/eppinput/EppInputTest.java +++ b/javatests/google/registry/model/eppinput/EppInputTest.java @@ -36,9 +36,11 @@ public class EppInputTest { unmarshal( EppInput.class, readResourceBytes(ContactResourceTest.class, "testdata/contact_info.xml").read()); - assertThat(input.getTargetIds()).containsExactly("sh8013"); assertThat(input.getCommandWrapper().getClTrid()).isEqualTo("ABC-12345"); - assertThat(input.getCommandName()).isEqualTo("Info"); + assertThat(input.getCommandType()).isEqualTo("info"); + assertThat(input.getResourceType()).hasValue("contact"); + assertThat(input.getSingleTargetId()).hasValue("sh8013"); + assertThat(input.getTargetIds()).containsExactly("sh8013"); } @Test @@ -47,18 +49,22 @@ public class EppInputTest { unmarshal( EppInput.class, readResourceBytes(DomainResourceTest.class, "testdata/domain_check.xml").read()); - assertThat(input.getTargetIds()).containsExactly("example.com", "example.net", "example.org"); assertThat(input.getCommandWrapper().getClTrid()).isEqualTo("ABC-12345"); - assertThat(input.getCommandName()).isEqualTo("Check"); + assertThat(input.getCommandType()).isEqualTo("check"); + assertThat(input.getResourceType()).hasValue("domain"); + assertThat(input.getSingleTargetId()).isAbsent(); + assertThat(input.getTargetIds()).containsExactly("example.com", "example.net", "example.org"); } @Test public void testUnmarshalling_login() throws Exception { EppInput input = unmarshal(EppInput.class, readResourceBytes(getClass(), "testdata/login_valid.xml").read()); - assertThat(input.getTargetIds()).isEmpty(); assertThat(input.getCommandWrapper().getClTrid()).isEqualTo("ABC-12345"); - assertThat(input.getCommandName()).isEqualTo("Login"); + assertThat(input.getCommandType()).isEqualTo("login"); + assertThat(input.getResourceType()).isAbsent(); + assertThat(input.getSingleTargetId()).isAbsent(); + assertThat(input.getTargetIds()).isEmpty(); InnerCommand command = input.getCommandWrapper().getCommand(); assertThat(command).isInstanceOf(Login.class); Login loginCommand = (Login) command;