mirror of
https://github.com/google/nomulus
synced 2026-01-06 21:47:31 +00:00
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:
@@ -14,12 +14,11 @@
|
||||
|
||||
package google.registry.ui.server.console.domains;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonElement;
|
||||
import google.registry.model.console.ConsolePermission;
|
||||
|
||||
/** 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 =
|
||||
"""
|
||||
@@ -42,16 +41,13 @@ public class ConsoleBulkDomainDeleteActionType implements ConsoleDomainActionTyp
|
||||
</command>
|
||||
</epp>""";
|
||||
|
||||
private final String reason;
|
||||
|
||||
public ConsoleBulkDomainDeleteActionType(JsonElement jsonElement) {
|
||||
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString();
|
||||
super(jsonElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getXmlContentsToRun(String domainName) {
|
||||
return ConsoleDomainActionType.fillSubstitutions(
|
||||
DOMAIN_DELETE_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
|
||||
protected String getXmlTemplate() {
|
||||
return DOMAIN_DELETE_XML;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,12 +14,11 @@
|
||||
|
||||
package google.registry.ui.server.console.domains;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonElement;
|
||||
import google.registry.model.console.ConsolePermission;
|
||||
|
||||
/** 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 =
|
||||
"""
|
||||
@@ -52,16 +51,13 @@ public class ConsoleBulkDomainSuspendActionType implements ConsoleDomainActionTy
|
||||
</command>
|
||||
</epp>""";
|
||||
|
||||
private final String reason;
|
||||
|
||||
public ConsoleBulkDomainSuspendActionType(JsonElement jsonElement) {
|
||||
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString();
|
||||
super(jsonElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getXmlContentsToRun(String domainName) {
|
||||
return ConsoleDomainActionType.fillSubstitutions(
|
||||
DOMAIN_SUSPEND_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
|
||||
protected String getXmlTemplate() {
|
||||
return DOMAIN_SUSPEND_XML;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,12 +14,11 @@
|
||||
|
||||
package google.registry.ui.server.console.domains;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonElement;
|
||||
import google.registry.model.console.ConsolePermission;
|
||||
|
||||
/** 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 =
|
||||
"""
|
||||
@@ -52,16 +51,13 @@ public class ConsoleBulkDomainUnsuspendActionType implements ConsoleDomainAction
|
||||
</command>
|
||||
</epp>""";
|
||||
|
||||
private final String reason;
|
||||
|
||||
public ConsoleBulkDomainUnsuspendActionType(JsonElement jsonElement) {
|
||||
this.reason = jsonElement.getAsJsonObject().get("reason").getAsString();
|
||||
super(jsonElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getXmlContentsToRun(String domainName) {
|
||||
return ConsoleDomainActionType.fillSubstitutions(
|
||||
DOMAIN_SUSPEND_XML, ImmutableMap.of("DOMAIN_NAME", domainName, "REASON", reason));
|
||||
protected String getXmlTemplate() {
|
||||
return DOMAIN_SUSPEND_XML;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,20 +14,18 @@
|
||||
|
||||
package google.registry.ui.server.console.domains;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.escape.Escaper;
|
||||
import com.google.common.xml.XmlEscapers;
|
||||
import com.google.gson.JsonElement;
|
||||
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}.
|
||||
*
|
||||
* <p>Each {@link BulkAction} defines the class that implements that action, including the EPP XML
|
||||
* that will be run and the permission required.
|
||||
* <p>Each {@link BulkAction} defines the class that extends that action, including the EPP XML that
|
||||
* will be run and the permission required.
|
||||
*/
|
||||
public interface ConsoleDomainActionType {
|
||||
public abstract class ConsoleDomainActionType {
|
||||
|
||||
enum BulkAction {
|
||||
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) {
|
||||
BulkAction bulkAction = BulkAction.valueOf(bulkDomainAction);
|
||||
try {
|
||||
@@ -67,4 +51,38 @@ public interface ConsoleDomainActionType {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user