Refactor console bulk domain action types (#2708)

This makes the action types a bit simpler -- this is possible because
we've reduced the scope of domain actions that we want to natively
support
This commit is contained in:
gbrodman
2025-03-07 18:12:32 +00:00
committed by GitHub
parent 6b0beeb477
commit 2ff4d97b0a
4 changed files with 49 additions and 43 deletions
@@ -14,12 +14,11 @@
package google.registry.ui.server.console.domains; package google.registry.ui.server.console.domains;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import google.registry.model.console.ConsolePermission; import google.registry.model.console.ConsolePermission;
/** An action that will run a delete EPP command on the given domain. */ /** An action that will run a delete EPP command on the given domain. */
public class ConsoleBulkDomainDeleteActionType implements ConsoleDomainActionType { public class ConsoleBulkDomainDeleteActionType extends ConsoleDomainActionType {
private static final String DOMAIN_DELETE_XML = private static final String DOMAIN_DELETE_XML =
""" """
@@ -42,16 +41,13 @@ public class ConsoleBulkDomainDeleteActionType implements ConsoleDomainActionTyp
</command> </command>
</epp>"""; </epp>""";
private final String reason;
public ConsoleBulkDomainDeleteActionType(JsonElement jsonElement) { public ConsoleBulkDomainDeleteActionType(JsonElement jsonElement) {
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString(); super(jsonElement);
} }
@Override @Override
public String getXmlContentsToRun(String domainName) { protected String getXmlTemplate() {
return ConsoleDomainActionType.fillSubstitutions( return DOMAIN_DELETE_XML;
DOMAIN_DELETE_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
} }
@Override @Override
@@ -14,12 +14,11 @@
package google.registry.ui.server.console.domains; package google.registry.ui.server.console.domains;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import google.registry.model.console.ConsolePermission; import google.registry.model.console.ConsolePermission;
/** An action that will suspend the given domain, assigning all 5 server*Prohibited statuses. */ /** An action that will suspend the given domain, assigning all 5 server*Prohibited statuses. */
public class ConsoleBulkDomainSuspendActionType implements ConsoleDomainActionType { public class ConsoleBulkDomainSuspendActionType extends ConsoleDomainActionType {
private static final String DOMAIN_SUSPEND_XML = private static final String DOMAIN_SUSPEND_XML =
""" """
@@ -52,16 +51,13 @@ public class ConsoleBulkDomainSuspendActionType implements ConsoleDomainActionTy
</command> </command>
</epp>"""; </epp>""";
private final String reason;
public ConsoleBulkDomainSuspendActionType(JsonElement jsonElement) { public ConsoleBulkDomainSuspendActionType(JsonElement jsonElement) {
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString(); super(jsonElement);
} }
@Override @Override
public String getXmlContentsToRun(String domainName) { protected String getXmlTemplate() {
return ConsoleDomainActionType.fillSubstitutions( return DOMAIN_SUSPEND_XML;
DOMAIN_SUSPEND_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
} }
@Override @Override
@@ -14,12 +14,11 @@
package google.registry.ui.server.console.domains; package google.registry.ui.server.console.domains;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import google.registry.model.console.ConsolePermission; import google.registry.model.console.ConsolePermission;
/** An action that will unsuspend the given domain, removing all 5 server*Prohibited statuses. */ /** An action that will unsuspend the given domain, removing all 5 server*Prohibited statuses. */
public class ConsoleBulkDomainUnsuspendActionType implements ConsoleDomainActionType { public class ConsoleBulkDomainUnsuspendActionType extends ConsoleDomainActionType {
private static final String DOMAIN_SUSPEND_XML = private static final String DOMAIN_SUSPEND_XML =
""" """
@@ -52,16 +51,13 @@ public class ConsoleBulkDomainUnsuspendActionType implements ConsoleDomainAction
</command> </command>
</epp>"""; </epp>""";
private final String reason;
public ConsoleBulkDomainUnsuspendActionType(JsonElement jsonElement) { public ConsoleBulkDomainUnsuspendActionType(JsonElement jsonElement) {
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString(); super(jsonElement);
} }
@Override @Override
public String getXmlContentsToRun(String domainName) { protected String getXmlTemplate() {
return ConsoleDomainActionType.fillSubstitutions( return DOMAIN_SUSPEND_XML;
DOMAIN_SUSPEND_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
} }
@Override @Override
@@ -14,20 +14,18 @@
package google.registry.ui.server.console.domains; package google.registry.ui.server.console.domains;
import com.google.common.collect.ImmutableMap;
import com.google.common.escape.Escaper; import com.google.common.escape.Escaper;
import com.google.common.xml.XmlEscapers; import com.google.common.xml.XmlEscapers;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import google.registry.model.console.ConsolePermission; import google.registry.model.console.ConsolePermission;
import java.util.Map;
/** /**
* A type of EPP action to perform on domain(s), run by the {@link ConsoleBulkDomainAction}. * A type of EPP action to perform on domain(s), run by the {@link ConsoleBulkDomainAction}.
* *
* <p>Each {@link BulkAction} defines the class that implements that action, including the EPP XML * <p>Each {@link BulkAction} defines the class that extends that action, including the EPP XML that
* that will be run and the permission required. * will be run and the permission required.
*/ */
public interface ConsoleDomainActionType { public abstract class ConsoleDomainActionType {
enum BulkAction { enum BulkAction {
DELETE(ConsoleBulkDomainDeleteActionType.class), DELETE(ConsoleBulkDomainDeleteActionType.class),
@@ -45,20 +43,6 @@ public interface ConsoleDomainActionType {
} }
} }
Escaper XML_ESCAPER = XmlEscapers.xmlContentEscaper();
static String fillSubstitutions(String xmlTemplate, ImmutableMap<String, String> replacements) {
String xml = xmlTemplate;
for (Map.Entry<String, String> entry : replacements.entrySet()) {
xml = xml.replaceAll("%" + entry.getKey() + "%", XML_ESCAPER.escape(entry.getValue()));
}
return xml;
}
String getXmlContentsToRun(String domainName);
ConsolePermission getNecessaryPermission();
static ConsoleDomainActionType parseActionType(String bulkDomainAction, JsonElement jsonElement) { static ConsoleDomainActionType parseActionType(String bulkDomainAction, JsonElement jsonElement) {
BulkAction bulkAction = BulkAction.valueOf(bulkDomainAction); BulkAction bulkAction = BulkAction.valueOf(bulkDomainAction);
try { try {
@@ -67,4 +51,38 @@ public interface ConsoleDomainActionType {
throw new RuntimeException(e); // shouldn't happen throw new RuntimeException(e); // shouldn't happen
} }
} }
private static final Escaper XML_ESCAPER = XmlEscapers.xmlContentEscaper();
private final String reason;
public ConsoleDomainActionType(JsonElement jsonElement) {
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString();
}
/** Returns the full XML representing this action, including all substitutions. */
public String getXmlContentsToRun(String domainName) {
return fillSubstitutions(getXmlTemplate(), domainName);
}
/** Returns the permission necessary to successfully perform this action. */
public abstract ConsolePermission getNecessaryPermission();
/** Returns the XML template contents for this action. */
protected abstract String getXmlTemplate();
/**
* Fills out the default set of substitutions in the provided XML template.
*
* <p>Override this method if non-default substitutions are required.
*/
protected String fillSubstitutions(String xmlTemplate, String domainName) {
String xml = xmlTemplate;
xml = replaceValue(xml, "DOMAIN_NAME", domainName);
return replaceValue(xml, "REASON", reason);
}
private String replaceValue(String xml, String key, String value) {
return xml.replaceAll("%" + key + "%", XML_ESCAPER.escape(value));
}
} }