Skip to content

Commit 392a422

Browse files
committed
resolving comments
1 parent f6a5bb0 commit 392a422

File tree

1 file changed

+54
-55
lines changed

1 file changed

+54
-55
lines changed

core/src/main/java/google/registry/tools/RdapQueryCommand.java

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,72 +29,71 @@
2929
import google.registry.request.Action.Service;
3030
import jakarta.inject.Inject;
3131
import java.io.IOException;
32+
import java.util.function.Function;
33+
import javax.annotation.Nullable;
3234

3335
/** Command to manually perform an authenticated RDAP query. */
3436
@Parameters(separators = " =", commandDescription = "Manually perform an authenticated RDAP query")
3537
public final class RdapQueryCommand implements CommandWithConnection {
3638

3739
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
38-
39-
/**
40-
* A simple data class to hold the path and query parameters for an RDAP request.
41-
*
42-
* <p>This is used as a return type for the {@code RdapQueryType.getRequestData} method to bundle
43-
* the two distinct return values into a single object.
44-
*/
45-
private static class RequestData {
46-
final String path;
47-
final ImmutableMap<String, String> queryParams;
48-
49-
private RequestData(String path, ImmutableMap<String, String> queryParams) {
50-
this.path = path;
51-
this.queryParams = queryParams;
52-
}
53-
54-
static RequestData create(String path, ImmutableMap<String, String> queryParams) {
55-
return new RequestData(path, queryParams);
56-
}
57-
}
40+
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
5841

5942
/** Defines the RDAP query types, encapsulating their path logic and parameter requirements. */
6043
enum RdapQueryType {
61-
DOMAIN(true, "/rdap/domain/%s"),
62-
DOMAIN_SEARCH(true, "/rdap/domains", "name"),
63-
NAMESERVER(true, "/rdap/nameserver/%s"),
64-
NAMESERVER_SEARCH(true, "/rdap/nameservers", "name"),
65-
ENTITY(true, "/rdap/entity/%s"),
66-
ENTITY_SEARCH(true, "/rdap/entities", "fn"),
67-
HELP(false, "/rdap/help");
44+
DOMAIN_LOOKUP("/rdap/domain/%s"),
45+
DOMAIN_SEARCH("/rdap/domains", queryTerm -> ImmutableMap.of("name", queryTerm)),
46+
NAMESERVER_LOOKUP("/rdap/nameserver/%s"),
47+
NAMESERVER_SEARCH("/rdap/nameservers", queryTerm -> ImmutableMap.of("name", queryTerm)),
48+
ENTITY_LOOKUP("/rdap/entity/%s"),
49+
ENTITY_SEARCH("/rdap/entities", queryTerm -> ImmutableMap.of("fn", queryTerm)),
50+
HELP("/rdap/help", false);
6851

69-
private final boolean requiresQueryTerm;
7052
private final String pathFormat;
71-
private final String searchParamKey;
53+
private final boolean requiresQueryTerm;
54+
private final Function<String, ImmutableMap<String, String>> queryParametersFunction;
7255

73-
RdapQueryType(boolean requiresQueryTerm, String pathFormat) {
74-
this(requiresQueryTerm, pathFormat, null);
56+
/** Constructor for lookup queries that require a query term. */
57+
RdapQueryType(String pathFormat) {
58+
this(pathFormat, true, queryTerm -> ImmutableMap.of());
7559
}
7660

77-
RdapQueryType(boolean requiresQueryTerm, String pathFormat, String searchParamKey) {
78-
this.requiresQueryTerm = requiresQueryTerm;
61+
/** Constructor for search queries that require a query term. */
62+
RdapQueryType(
63+
String pathFormat, Function<String, ImmutableMap<String, String>> queryParametersFunction) {
64+
this(pathFormat, true, queryParametersFunction);
65+
}
66+
67+
/** Constructor for queries that may not require a query term (e.g., HELP). */
68+
RdapQueryType(String pathFormat, boolean requiresQueryTerm) {
69+
this(pathFormat, requiresQueryTerm, queryTerm -> ImmutableMap.of());
70+
}
71+
72+
RdapQueryType(
73+
String pathFormat,
74+
boolean requiresQueryTerm,
75+
Function<String, ImmutableMap<String, String>> queryParametersFunction) {
7976
this.pathFormat = pathFormat;
80-
this.searchParamKey = searchParamKey;
77+
this.requiresQueryTerm = requiresQueryTerm;
78+
this.queryParametersFunction = queryParametersFunction;
79+
}
80+
81+
void validate(@Nullable String queryTerm) {
82+
checkArgument(
83+
requiresQueryTerm == (queryTerm != null),
84+
"A query term is %srequired for type %s",
85+
requiresQueryTerm ? "" : "not ",
86+
this.name());
87+
}
88+
89+
String getQueryPath(@Nullable String queryTerm) {
90+
return getQueryParameters(queryTerm).isEmpty()
91+
? String.format(pathFormat, queryTerm)
92+
: pathFormat;
8193
}
8294

83-
/** Returns a RequestData object containing the path and query parameters for the request. */
84-
public RequestData getRequestData(String queryTerm) {
85-
if (requiresQueryTerm) {
86-
checkArgument(queryTerm != null, "A query term is required for the %s query.", this);
87-
} else {
88-
checkArgument(queryTerm == null, "The %s query does not take a query term.", this);
89-
}
90-
91-
if (searchParamKey != null) {
92-
return RequestData.create(pathFormat, ImmutableMap.of(searchParamKey, queryTerm));
93-
} else if (requiresQueryTerm) {
94-
return RequestData.create(String.format(pathFormat, queryTerm), ImmutableMap.of());
95-
} else {
96-
return RequestData.create(pathFormat, ImmutableMap.of());
97-
}
95+
ImmutableMap<String, String> getQueryParameters(@Nullable String queryTerm) {
96+
return queryParametersFunction.apply(queryTerm);
9897
}
9998
}
10099

@@ -106,7 +105,7 @@ public RequestData getRequestData(String queryTerm) {
106105
required = false)
107106
private String queryTerm;
108107

109-
private ServiceConnection defaultConnection;
108+
@Inject ServiceConnection defaultConnection;
110109

111110
@Inject
112111
@Config("useCanary")
@@ -120,15 +119,15 @@ public void setConnection(ServiceConnection connection) {
120119
@Override
121120
public void run() throws IOException {
122121
checkState(defaultConnection != null, "ServiceConnection was not set by RegistryCli.");
122+
type.validate(queryTerm);
123123

124-
RequestData requestData = type.getRequestData(queryTerm);
124+
String path = type.getQueryPath(queryTerm);
125+
ImmutableMap<String, String> queryParams = type.getQueryParameters(queryTerm);
125126

126127
ServiceConnection pubapiConnection = defaultConnection.withService(Service.PUBAPI, useCanary);
127-
String rdapResponse =
128-
pubapiConnection.sendGetRequest(requestData.path, requestData.queryParams);
128+
String rdapResponse = pubapiConnection.sendGetRequest(path, queryParams);
129129

130130
JsonElement rdapJson = JsonParser.parseString(rdapResponse);
131-
Gson gson = new GsonBuilder().setPrettyPrinting().create();
132-
logger.atInfo().log(gson.toJson(rdapJson));
131+
logger.atInfo().log(GSON.toJson(rdapJson));
133132
}
134133
}

0 commit comments

Comments
 (0)