Refactor WhoisReader.readCommand() method to take a now param

This obviates the need for the use of @AutoFactory.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149570411
This commit is contained in:
mcilwain 2017-03-08 12:40:14 -08:00 committed by Ben McIlwain
parent 9eddbe2b6e
commit 815dae2749
8 changed files with 23 additions and 49 deletions

View file

@ -16,7 +16,6 @@ java_library(
"//java/google/registry/xml", "//java/google/registry/xml",
"//third_party/java/objectify:objectify-v4_1", "//third_party/java/objectify:objectify-v4_1",
"@com_google_appengine_api_1_0_sdk", "@com_google_appengine_api_1_0_sdk",
"@com_google_auto_factory",
"@com_google_auto_value", "@com_google_auto_value",
"@com_google_code_findbugs_jsr305", "@com_google_code_findbugs_jsr305",
"@com_google_dagger", "@com_google_dagger",

View file

@ -26,25 +26,21 @@ public final class Whois {
private final Clock clock; private final Clock clock;
private final String disclaimer; private final String disclaimer;
private final WhoisReaderFactory whoisReaderFactory; private final WhoisReader whoisReader;
@Inject @Inject
public Whois( public Whois(Clock clock, @Config("whoisDisclaimer") String disclaimer, WhoisReader whoisReader) {
Clock clock,
@Config("whoisDisclaimer") String disclaimer,
WhoisReaderFactory whoisReader) {
this.clock = clock; this.clock = clock;
this.disclaimer = disclaimer; this.disclaimer = disclaimer;
this.whoisReaderFactory = whoisReader; this.whoisReader = whoisReader;
} }
/** Performs a WHOIS lookup on a plaintext query string. */ /** Performs a WHOIS lookup on a plaintext query string. */
public String lookup(String query, boolean preferUnicode) { public String lookup(String query, boolean preferUnicode) {
DateTime now = clock.nowUtc(); DateTime now = clock.nowUtc();
try { try {
return whoisReaderFactory return whoisReader
.create(now) .readCommand(new StringReader(query), now)
.readCommand(new StringReader(query))
.executeQuery(now) .executeQuery(now)
.getResponse(preferUnicode, disclaimer) .getResponse(preferUnicode, disclaimer)
.plainTextOutput(); .plainTextOutput();

View file

@ -134,7 +134,7 @@ public final class WhoisHttpServer implements Runnable {
@Inject Response response; @Inject Response response;
@Inject @Config("whoisDisclaimer") String disclaimer; @Inject @Config("whoisDisclaimer") String disclaimer;
@Inject @Config("whoisHttpExpires") Duration expires; @Inject @Config("whoisHttpExpires") Duration expires;
@Inject WhoisReaderFactory whoisReaderFactory; @Inject WhoisReader whoisReader;
@Inject @RequestPath String requestPath; @Inject @RequestPath String requestPath;
@Inject WhoisMetric.Builder metricBuilder; @Inject WhoisMetric.Builder metricBuilder;
@Inject WhoisMetrics whoisMetrics; @Inject WhoisMetrics whoisMetrics;
@ -149,8 +149,7 @@ public final class WhoisHttpServer implements Runnable {
String commandText = String commandText =
decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n"; decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n";
DateTime now = clock.nowUtc(); DateTime now = clock.nowUtc();
WhoisCommand command = WhoisCommand command = whoisReader.readCommand(new StringReader(commandText), now);
whoisReaderFactory.create(now).readCommand(new StringReader(commandText));
metricBuilder.setCommand(command); metricBuilder.setCommand(command);
sendResponse(SC_OK, command.executeQuery(now)); sendResponse(SC_OK, command.executeQuery(now));
} catch (WhoisException e) { } catch (WhoisException e) {

View file

@ -20,8 +20,6 @@ import static google.registry.model.registry.Registries.findTldForName;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName; import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
@ -62,7 +60,6 @@ import org.joda.time.DateTime;
* @see <a href="http://tools.ietf.org/html/rfc3912">RFC 3912</a> * @see <a href="http://tools.ietf.org/html/rfc3912">RFC 3912</a>
* @see <a href="http://www.iana.org/assignments/registrar-ids">IANA Registrar IDs</a> * @see <a href="http://www.iana.org/assignments/registrar-ids">IANA Registrar IDs</a>
*/ */
@AutoFactory(allowSubclasses = true)
class WhoisReader { class WhoisReader {
/** /**
@ -73,15 +70,12 @@ class WhoisReader {
static final String NAMESERVER_LOOKUP_COMMAND = "nameserver"; static final String NAMESERVER_LOOKUP_COMMAND = "nameserver";
static final String REGISTRAR_LOOKUP_COMMAND = "registrar"; static final String REGISTRAR_LOOKUP_COMMAND = "registrar";
private final DateTime now;
private final WhoisCommandFactory commandFactory; private final WhoisCommandFactory commandFactory;
/** Creates a new WhoisReader that extracts its command from the specified Reader. */ /** Creates a new WhoisReader that extracts its command from the specified Reader. */
@Inject @Inject
WhoisReader( WhoisReader(@Config("whoisCommandFactory") WhoisCommandFactory commandFactory) {
@Provided @Config("whoisCommandFactory") WhoisCommandFactory commandFactory, DateTime now) {
this.commandFactory = commandFactory; this.commandFactory = commandFactory;
this.now = now;
} }
/** /**
@ -91,15 +85,15 @@ class WhoisReader {
* @throws IOException If the command could not be read from the reader. * @throws IOException If the command could not be read from the reader.
* @throws WhoisException If the command could not be parsed as a WhoisCommand. * @throws WhoisException If the command could not be parsed as a WhoisCommand.
*/ */
WhoisCommand readCommand(Reader reader) throws IOException, WhoisException { WhoisCommand readCommand(Reader reader, DateTime now) throws IOException, WhoisException {
return parseCommand(CharStreams.toString(checkNotNull(reader, "reader"))); return parseCommand(CharStreams.toString(checkNotNull(reader, "reader")), now);
} }
/** /**
* Given a WHOIS command string, parse it into its command type and target string. See class level * Given a WHOIS command string, parse it into its command type and target string. See class level
* comments for a full description of the command syntax accepted. * comments for a full description of the command syntax accepted.
*/ */
private WhoisCommand parseCommand(String command) throws WhoisException { private WhoisCommand parseCommand(String command, DateTime now) throws WhoisException {
// Split the string into tokens based on whitespace. // Split the string into tokens based on whitespace.
List<String> tokens = filterEmptyStrings(command.split("\\s")); List<String> tokens = filterEmptyStrings(command.split("\\s"));
if (tokens.isEmpty()) { if (tokens.isEmpty()) {

View file

@ -61,7 +61,7 @@ public class WhoisServer implements Runnable {
@Inject Clock clock; @Inject Clock clock;
@Inject Reader input; @Inject Reader input;
@Inject Response response; @Inject Response response;
@Inject WhoisReaderFactory whoisReaderFactory; @Inject WhoisReader whoisReader;
@Inject @Config("whoisDisclaimer") String disclaimer; @Inject @Config("whoisDisclaimer") String disclaimer;
@Inject WhoisMetric.Builder metricBuilder; @Inject WhoisMetric.Builder metricBuilder;
@Inject WhoisMetrics whoisMetrics; @Inject WhoisMetrics whoisMetrics;
@ -72,7 +72,7 @@ public class WhoisServer implements Runnable {
String responseText; String responseText;
DateTime now = clock.nowUtc(); DateTime now = clock.nowUtc();
try { try {
WhoisCommand command = whoisReaderFactory.create(now).readCommand(input); WhoisCommand command = whoisReader.readCommand(input, now);
metricBuilder.setCommand(command); metricBuilder.setCommand(command);
WhoisResponseResults results = WhoisResponseResults results =
command.executeQuery(now).getResponse(PREFER_UNICODE, disclaimer); command.executeQuery(now).getResponse(PREFER_UNICODE, disclaimer);

View file

@ -43,7 +43,6 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule; import google.registry.testing.InjectRule;
import google.registry.testing.Providers;
import google.registry.whois.WhoisMetrics.WhoisMetric; import google.registry.whois.WhoisMetrics.WhoisMetric;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -78,8 +77,7 @@ public class WhoisHttpServerTest {
whoisServer.expires = Duration.standardHours(1); whoisServer.expires = Duration.standardHours(1);
whoisServer.requestPath = WhoisHttpServer.PATH + pathInfo; whoisServer.requestPath = WhoisHttpServer.PATH + pathInfo;
whoisServer.response = response; whoisServer.response = response;
whoisServer.whoisReaderFactory = whoisServer.whoisReader = new WhoisReader(new WhoisCommandFactory());
new WhoisReaderFactory(Providers.of(new WhoisCommandFactory()));
whoisServer.whoisMetrics = new WhoisMetrics(); whoisServer.whoisMetrics = new WhoisMetrics();
whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock); whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock);
whoisServer.disclaimer = "Doodle Disclaimer"; whoisServer.disclaimer = "Doodle Disclaimer";
@ -375,14 +373,9 @@ public class WhoisHttpServerTest {
public void testRun_metricsLoggedForInternalServerError() throws Exception { public void testRun_metricsLoggedForInternalServerError() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisHttpServer server = newWhoisHttpServer("ns1.cat.lol"); WhoisHttpServer server = newWhoisHttpServer("ns1.cat.lol");
final WhoisReader reader = mock(WhoisReader.class); server.whoisReader = mock(WhoisReader.class);
when(reader.readCommand(any(Reader.class))).thenThrow(new IOException("missing cat interface")); when(server.whoisReader.readCommand(any(Reader.class), any(DateTime.class)))
.thenThrow(new IOException("missing cat interface"));
server.whoisReaderFactory = new WhoisReaderFactory(Providers.of(new WhoisCommandFactory())) {
@Override
WhoisReader create(DateTime now) {
return reader;
}};
server.whoisMetrics = mock(WhoisMetrics.class); server.whoisMetrics = mock(WhoisMetrics.class);
try { try {

View file

@ -45,8 +45,8 @@ public class WhoisReaderTest {
@SuppressWarnings("unchecked") // XXX: Generic abuse ftw. @SuppressWarnings("unchecked") // XXX: Generic abuse ftw.
<T> T readCommand(String commandStr) throws Exception { <T> T readCommand(String commandStr) throws Exception {
return (T) return (T)
new WhoisReader(new WhoisCommandFactory(), clock.nowUtc()) new WhoisReader(new WhoisCommandFactory())
.readCommand(new StringReader(commandStr)); .readCommand(new StringReader(commandStr), clock.nowUtc());
} }
void assertLoadsExampleTld(String commandString) throws Exception { void assertLoadsExampleTld(String commandString) throws Exception {

View file

@ -44,7 +44,6 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule; import google.registry.testing.InjectRule;
import google.registry.testing.Providers;
import google.registry.whois.WhoisMetrics.WhoisMetric; import google.registry.whois.WhoisMetrics.WhoisMetric;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -72,8 +71,7 @@ public class WhoisServerTest {
whoisServer.clock = clock; whoisServer.clock = clock;
whoisServer.input = new StringReader(input); whoisServer.input = new StringReader(input);
whoisServer.response = response; whoisServer.response = response;
whoisServer.whoisReaderFactory = whoisServer.whoisReader = new WhoisReader(new WhoisCommandFactory());
new WhoisReaderFactory(Providers.of(new WhoisCommandFactory()));
whoisServer.whoisMetrics = new WhoisMetrics(); whoisServer.whoisMetrics = new WhoisMetrics();
whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock); whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock);
whoisServer.disclaimer = "Doodle Disclaimer"; whoisServer.disclaimer = "Doodle Disclaimer";
@ -478,14 +476,9 @@ public class WhoisServerTest {
public void testRun_metricsLoggedForInternalServerError() throws Exception { public void testRun_metricsLoggedForInternalServerError() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisServer server = newWhoisServer("ns1.cat.lol"); WhoisServer server = newWhoisServer("ns1.cat.lol");
final WhoisReader reader = mock(WhoisReader.class); server.whoisReader = mock(WhoisReader.class);
when(reader.readCommand(any(Reader.class))).thenThrow(new IOException("missing cat interface")); when(server.whoisReader.readCommand(any(Reader.class), any(DateTime.class)))
.thenThrow(new IOException("missing cat interface"));
server.whoisReaderFactory = new WhoisReaderFactory(Providers.of(new WhoisCommandFactory())) {
@Override
WhoisReader create(DateTime now) {
return reader;
}};
server.whoisMetrics = mock(WhoisMetrics.class); server.whoisMetrics = mock(WhoisMetrics.class);
server.run(); server.run();