Use Dagger to @Inject WhoisReader instances

This is a precursor to adding metrics to WHOIS queries (as I'd like
to be able to @Inject the metrics builders).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149418018
This commit is contained in:
mcilwain 2017-03-07 07:47:38 -08:00 committed by Ben McIlwain
parent 53785c2fc7
commit bd7db61606
8 changed files with 51 additions and 51 deletions

View file

@ -16,6 +16,7 @@ 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,24 +26,25 @@ public final class Whois {
private final Clock clock; private final Clock clock;
private final String disclaimer; private final String disclaimer;
private final WhoisCommandFactory commandFactory; private final WhoisReaderFactory whoisReaderFactory;
@Inject @Inject
public Whois( public Whois(
Clock clock, Clock clock,
@Config("whoisDisclaimer") String disclaimer, @Config("whoisDisclaimer") String disclaimer,
@Config("whoisCommandFactory") WhoisCommandFactory commandFactory) { WhoisReaderFactory whoisReader) {
this.clock = clock; this.clock = clock;
this.disclaimer = disclaimer; this.disclaimer = disclaimer;
this.commandFactory = commandFactory; this.whoisReaderFactory = 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 new WhoisReader(new StringReader(query), commandFactory, now) return whoisReaderFactory
.readCommand() .create(now)
.readCommand(new StringReader(query))
.executeQuery(now) .executeQuery(now)
.getPlainTextOutput(preferUnicode, disclaimer); .getPlainTextOutput(preferUnicode, disclaimer);
} catch (WhoisException e) { } catch (WhoisException e) {

View file

@ -34,7 +34,6 @@ import google.registry.request.Response;
import google.registry.util.Clock; import google.registry.util.Clock;
import google.registry.util.FormattingLogger; import google.registry.util.FormattingLogger;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
@ -132,7 +131,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 @Config("whoisCommandFactory") WhoisCommandFactory commandFactory; @Inject WhoisReaderFactory whoisReaderFactory;
@Inject @RequestPath String requestPath; @Inject @RequestPath String requestPath;
@Inject WhoisHttpServer() {} @Inject WhoisHttpServer() {}
@ -143,10 +142,10 @@ public final class WhoisHttpServer implements Runnable {
try { try {
// Extremely permissive parsing that turns stuff like "/hello/world/" into "hello world". // Extremely permissive parsing that turns stuff like "/hello/world/" into "hello world".
String command = decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n"; String command = decode(JOINER.join(SLASHER.split(path.substring(PATH.length())))) + "\r\n";
Reader reader = new StringReader(command);
DateTime now = clock.nowUtc(); DateTime now = clock.nowUtc();
sendResponse( sendResponse(
SC_OK, new WhoisReader(reader, commandFactory, now).readCommand().executeQuery(now)); SC_OK,
whoisReaderFactory.create(now).readCommand(new StringReader(command)).executeQuery(now));
} catch (WhoisException e) { } catch (WhoisException e) {
sendResponse(e.getStatus(), e); sendResponse(e.getStatus(), e);
} catch (IOException e) { } catch (IOException e) {

View file

@ -20,15 +20,19 @@ 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;
import com.google.common.net.InetAddresses; import com.google.common.net.InetAddresses;
import com.google.common.net.InternetDomainName; import com.google.common.net.InternetDomainName;
import google.registry.config.RegistryConfig.Config;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import org.joda.time.DateTime; import org.joda.time.DateTime;
/** /**
@ -58,6 +62,7 @@ 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
class WhoisReader { class WhoisReader {
/** /**
@ -68,15 +73,15 @@ 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 Reader reader;
private final DateTime now; 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. */
WhoisReader(Reader reader, WhoisCommandFactory commandFactory, DateTime now) { @Inject
this.reader = checkNotNull(reader, "reader"); WhoisReader(
this.commandFactory = checkNotNull(commandFactory, "commandFactory"); @Provided @Config("whoisCommandFactory") WhoisCommandFactory commandFactory, DateTime now) {
this.now = checkNotNull(now, "now"); this.commandFactory = commandFactory;
this.now = now;
} }
/** /**
@ -86,8 +91,8 @@ 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() throws IOException, WhoisException { WhoisCommand readCommand(Reader reader) throws IOException, WhoisException {
return parseCommand(CharStreams.toString(reader)); return parseCommand(CharStreams.toString(checkNotNull(reader, "reader")));
} }
/** /**
@ -97,7 +102,6 @@ class WhoisReader {
private WhoisCommand parseCommand(String command) throws WhoisException { private WhoisCommand parseCommand(String command) 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()) {
throw new WhoisException(now, SC_BAD_REQUEST, "No WHOIS command specified."); throw new WhoisException(now, SC_BAD_REQUEST, "No WHOIS command specified.");
} }

View file

@ -58,7 +58,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 @Config("whoisCommandFactory") WhoisCommandFactory commandFactory; @Inject WhoisReaderFactory whoisReaderFactory;
@Inject @Config("whoisDisclaimer") String disclaimer; @Inject @Config("whoisDisclaimer") String disclaimer;
@Inject WhoisServer() {} @Inject WhoisServer() {}
@ -68,8 +68,9 @@ public class WhoisServer implements Runnable {
DateTime now = clock.nowUtc(); DateTime now = clock.nowUtc();
try { try {
responseText = responseText =
new WhoisReader(input, commandFactory, now) whoisReaderFactory
.readCommand() .create(now)
.readCommand(input)
.executeQuery(now) .executeQuery(now)
.getPlainTextOutput(PREFER_UNICODE, disclaimer); .getPlainTextOutput(PREFER_UNICODE, disclaimer);
} catch (WhoisException e) { } catch (WhoisException e) {

View file

@ -34,6 +34,7 @@ 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 org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.Duration; import org.joda.time.Duration;
import org.junit.After; import org.junit.After;
@ -53,26 +54,22 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class WhoisHttpServerTest { public class WhoisHttpServerTest {
@Rule @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
public final AppEngineRule appEngine = AppEngineRule.builder() @Rule public final InjectRule inject = new InjectRule();
.withDatastore()
.build();
@Rule
public final InjectRule inject = new InjectRule();
private final FakeResponse response = new FakeResponse(); private final FakeResponse response = new FakeResponse();
private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z")); private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z"));
private WhoisHttpServer newWhoisHttpServer(String pathInfo) { private WhoisHttpServer newWhoisHttpServer(String pathInfo) {
WhoisHttpServer result = new WhoisHttpServer(); WhoisHttpServer whoisServer = new WhoisHttpServer();
result.clock = clock; whoisServer.clock = clock;
result.expires = Duration.standardHours(1); whoisServer.expires = Duration.standardHours(1);
result.requestPath = WhoisHttpServer.PATH + pathInfo; whoisServer.requestPath = WhoisHttpServer.PATH + pathInfo;
result.response = response; whoisServer.response = response;
result.disclaimer = "Doodle Disclaimer"; whoisServer.whoisReaderFactory =
result.commandFactory = new WhoisCommandFactory(); new WhoisReaderFactory(Providers.of(new WhoisCommandFactory()));
return result; whoisServer.disclaimer = "Doodle Disclaimer";
return whoisServer;
} }
@Before @Before

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 StringReader(commandStr), new WhoisCommandFactory(), clock.nowUtc()) new WhoisReader(new WhoisCommandFactory(), clock.nowUtc())
.readCommand(); .readCommand(new StringReader(commandStr));
} }
void assertLoadsExampleTld(String commandString) throws Exception { void assertLoadsExampleTld(String commandString) throws Exception {

View file

@ -36,6 +36,7 @@ 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 java.io.StringReader; import java.io.StringReader;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
@ -49,25 +50,21 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class WhoisServerTest { public class WhoisServerTest {
@Rule @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
public final AppEngineRule appEngine = AppEngineRule.builder() @Rule public final InjectRule inject = new InjectRule();
.withDatastore()
.build();
@Rule
public final InjectRule inject = new InjectRule();
private final FakeResponse response = new FakeResponse(); private final FakeResponse response = new FakeResponse();
private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z")); private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z"));
private WhoisServer newWhoisServer(String input) { private WhoisServer newWhoisServer(String input) {
WhoisServer result = new WhoisServer(); WhoisServer whoisServer = new WhoisServer();
result.clock = clock; whoisServer.clock = clock;
result.input = new StringReader(input); whoisServer.input = new StringReader(input);
result.response = response; whoisServer.response = response;
result.disclaimer = "Doodle Disclaimer"; whoisServer.whoisReaderFactory =
result.commandFactory = new WhoisCommandFactory(); new WhoisReaderFactory(Providers.of(new WhoisCommandFactory()));
return result; whoisServer.disclaimer = "Doodle Disclaimer";
return whoisServer;
} }
@Before @Before