diff --git a/java/google/registry/tools/AppEngineConnection.java b/java/google/registry/tools/AppEngineConnection.java index c5949b8f9..139a73dd5 100644 --- a/java/google/registry/tools/AppEngineConnection.java +++ b/java/google/registry/tools/AppEngineConnection.java @@ -51,7 +51,7 @@ class AppEngineConnection implements Connection { private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("
This module provides a standard NetHttpTransport-based HttpRequestFactory binding. + * The binding is qualified with the name named "default" and is not consumed directly. The + * RequestFactoryModule module binds the "default" HttpRequestFactory to the unqualified + * HttpRequestFactory, allowing users to override the actual, unqualified HttpRequestFactory + * binding by replacing RequestFactoryfModule with their own module, optionally providing + * the "default" factory in some circumstances. + * + *
Localhost connections go to the App Engine dev server. The dev server differs from most HTTP + * connections in that they don't require OAuth2 credentials, but instead require a special cookie. + */ +@Module +class DefaultRequestFactoryModule { + + @Provides + @Named("default") + public HttpRequestFactory provideHttpRequestFactory(AppEngineConnectionFlags connectionFlags) { + if (connectionFlags.getServer().getHostText().equals("localhost")) { + return new NetHttpTransport() + .createRequestFactory( + new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest request) { + request + .getHeaders() + .setCookie("dev_appserver_login=test@example.com:true:1858047912411"); + } + }); + } else { + return new NetHttpTransport().createRequestFactory(); + } + } + + /** + * Module for providing HttpRequestFactory. + * + *
Localhost connections go to the App Engine dev server. The dev server differs from most HTTP + * connections in that they don't require OAuth2 credentials, but instead require a special + * cookie. + */ + @Module + abstract class RequestFactoryModule { + + @Binds + public abstract HttpRequestFactory provideHttpRequestFactory( + @Named("default") HttpRequestFactory requestFactory); + } +} diff --git a/java/google/registry/tools/HttpRequestFactoryComponent.java b/java/google/registry/tools/HttpRequestFactoryComponent.java deleted file mode 100644 index 1b5eacc57..000000000 --- a/java/google/registry/tools/HttpRequestFactoryComponent.java +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.tools; - -import com.google.api.client.http.HttpRequestFactory; - -/** - * This is a Dagger component interface for providing request factories. - * - *
It is not annotated as a component because it's just an interface used as a dependency in - * other components. We provide our own concrete implementations of this for creating specific - * connection types. - */ -interface HttpRequestFactoryComponent { - public HttpRequestFactory httpRequestFactory(); -} diff --git a/java/google/registry/tools/LocalhostRequestFactoryComponent.java b/java/google/registry/tools/LocalhostRequestFactoryComponent.java deleted file mode 100644 index 5c8e88bad..000000000 --- a/java/google/registry/tools/LocalhostRequestFactoryComponent.java +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2017 The Nomulus Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package google.registry.tools; - -import com.google.api.client.http.HttpRequest; -import com.google.api.client.http.HttpRequestFactory; -import com.google.api.client.http.HttpRequestInitializer; -import com.google.api.client.http.javanet.NetHttpTransport; - -/** - * Request factory for dealing with a "localhost" connection. - * - *
Localhost connections go to the App Engine dev server. The dev server differs from most HTTP - * connections in that they don't require OAuth2 credentials, but instead require a special cookie. - * - *
This is an immplementation of HttpRequestFactoryComponent which can be a component dependency - * in a Dagger graph used for providing a request factory. - */ -class LocalhostRequestFactoryComponent implements HttpRequestFactoryComponent { - @Override - public HttpRequestFactory httpRequestFactory() { - return new NetHttpTransport() - .createRequestFactory( - new HttpRequestInitializer() { - @Override - public void initialize(HttpRequest request) { - request - .getHeaders() - .setCookie("dev_appserver_login=test@example.com:true:1858047912411"); - } - }); - } -} - diff --git a/java/google/registry/tools/RegistryCli.java b/java/google/registry/tools/RegistryCli.java index 397353a74..96281397a 100644 --- a/java/google/registry/tools/RegistryCli.java +++ b/java/google/registry/tools/RegistryCli.java @@ -118,19 +118,9 @@ final class RegistryCli { } loggingParams.configureLogging(); // Must be called after parameters are parsed. - // Decide which HTTP connection to use for App Engine - HttpRequestFactoryComponent requestFactoryComponent; - if (appEngineConnectionFlags.getServer().getHostText().equals("localhost")) { - requestFactoryComponent = new LocalhostRequestFactoryComponent(); - } else { - requestFactoryComponent = new BasicHttpRequestFactoryComponent(); - } - // Create the main component and use it to inject the command class. RegistryToolComponent component = DaggerRegistryToolComponent.builder() - .httpRequestFactoryComponent(requestFactoryComponent) - .appEngineConnectionFlagsModule( - new AppEngineConnectionFlagsModule(appEngineConnectionFlags)) + .flagsModule(new AppEngineConnectionFlags.FlagsModule(appEngineConnectionFlags)) .build(); injectReflectively(RegistryToolComponent.class, component, command); diff --git a/java/google/registry/tools/RegistryToolComponent.java b/java/google/registry/tools/RegistryToolComponent.java index 5c5f46b42..23bf71156 100644 --- a/java/google/registry/tools/RegistryToolComponent.java +++ b/java/google/registry/tools/RegistryToolComponent.java @@ -38,10 +38,12 @@ import javax.inject.Singleton; @Singleton @Component( modules = { - AppEngineConnectionFlagsModule.class, + AppEngineConnectionFlags.FlagsModule.class, ConfigModule.class, DatastoreServiceModule.class, CloudDnsWriterModule.class, + DefaultRequestFactoryModule.class, + DefaultRequestFactoryModule.RequestFactoryModule.class, DnsUpdateWriterModule.class, DummyKeyringModule.class, Jackson2Module.class, @@ -52,9 +54,6 @@ import javax.inject.Singleton; URLFetchServiceModule.class, VoidDnsWriterModule.class, WhoisModule.class, - }, - dependencies = { - HttpRequestFactoryComponent.class, } ) interface RegistryToolComponent {