From ecbe2662ac08807b2fd5dfb660dd5c4ea975a948 Mon Sep 17 00:00:00 2001 From: mmuller Date: Tue, 31 Jan 2017 07:25:25 -0800 Subject: [PATCH] Convert request factory components into a module Move all of the code to create the request factories into RequestFactoryModule. Also add the --force_http_connection flag to allow us to force the use of HTTP connections instead of HTTPOverRPC for our internal connections. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=146116640 --- .../registry/tools/AppEngineConnection.java | 6 +- .../tools/AppEngineConnectionFlags.java | 16 ++++ .../tools/AppEngineConnectionFlagsModule.java | 36 --------- .../BasicHttpRequestFactoryComponent.java | 29 ------- .../tools/DefaultRequestFactoryModule.java | 76 +++++++++++++++++++ .../tools/HttpRequestFactoryComponent.java | 28 ------- .../LocalhostRequestFactoryComponent.java | 46 ----------- java/google/registry/tools/RegistryCli.java | 12 +-- .../registry/tools/RegistryToolComponent.java | 7 +- 9 files changed, 99 insertions(+), 157 deletions(-) delete mode 100644 java/google/registry/tools/AppEngineConnectionFlagsModule.java delete mode 100644 java/google/registry/tools/BasicHttpRequestFactoryComponent.java create mode 100644 java/google/registry/tools/DefaultRequestFactoryModule.java delete mode 100644 java/google/registry/tools/HttpRequestFactoryComponent.java delete mode 100644 java/google/registry/tools/LocalhostRequestFactoryComponent.java 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("(.*?)"); @Inject HttpRequestFactory requestFactory; - @Inject HostAndPort server; + @Inject AppEngineConnectionFlags flags; @Inject AppEngineConnection() {} @@ -144,11 +144,11 @@ class AppEngineConnection implements Connection { } HostAndPort getServer() { - return server.withDefaultPort(443); // Default to HTTPS port if unspecified. + return flags.getServer().withDefaultPort(443); // Default to HTTPS port if unspecified. } boolean isLocalhost() { - return server.getHostText().equals("localhost"); + return flags.getServer().getHostText().equals("localhost"); } private String getUserId() { diff --git a/java/google/registry/tools/AppEngineConnectionFlags.java b/java/google/registry/tools/AppEngineConnectionFlags.java index ec6888f4d..e094396cb 100644 --- a/java/google/registry/tools/AppEngineConnectionFlags.java +++ b/java/google/registry/tools/AppEngineConnectionFlags.java @@ -17,6 +17,8 @@ package google.registry.tools; import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.net.HostAndPort; +import dagger.Module; +import dagger.Provides; import google.registry.config.RegistryConfig; /** @@ -34,5 +36,19 @@ class AppEngineConnectionFlags { HostAndPort getServer() { return server; } + + @Module + static class FlagsModule { + AppEngineConnectionFlags flags; + + FlagsModule(AppEngineConnectionFlags flags) { + this.flags = flags; + } + + @Provides + AppEngineConnectionFlags provideAppEngineConnectionFlags() { + return flags; + } + } } diff --git a/java/google/registry/tools/AppEngineConnectionFlagsModule.java b/java/google/registry/tools/AppEngineConnectionFlagsModule.java deleted file mode 100644 index 544f0c44e..000000000 --- a/java/google/registry/tools/AppEngineConnectionFlagsModule.java +++ /dev/null @@ -1,36 +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.common.net.HostAndPort; -import dagger.Module; -import dagger.Provides; - -/** - * Dagger module to provide communication flags for talking to App Engine. - */ -@Module -public final class AppEngineConnectionFlagsModule { - private final AppEngineConnectionFlags flags; - - AppEngineConnectionFlagsModule(AppEngineConnectionFlags flags) { - this.flags = flags; - } - - @Provides - HostAndPort provideServer() { - return flags.getServer(); - } -} diff --git a/java/google/registry/tools/BasicHttpRequestFactoryComponent.java b/java/google/registry/tools/BasicHttpRequestFactoryComponent.java deleted file mode 100644 index 5025f98ac..000000000 --- a/java/google/registry/tools/BasicHttpRequestFactoryComponent.java +++ /dev/null @@ -1,29 +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; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.http.javanet.NetHttpTransport; - -/** Creates a request factory for dealing with normal HTTP requests. */ -class BasicHttpRequestFactoryComponent implements HttpRequestFactoryComponent { - private final HttpTransport transport = new NetHttpTransport(); - - @Override - public HttpRequestFactory httpRequestFactory() { - return transport.createRequestFactory(); - } -} diff --git a/java/google/registry/tools/DefaultRequestFactoryModule.java b/java/google/registry/tools/DefaultRequestFactoryModule.java new file mode 100644 index 000000000..31191d5b7 --- /dev/null +++ b/java/google/registry/tools/DefaultRequestFactoryModule.java @@ -0,0 +1,76 @@ +// 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; +import dagger.Binds; +import dagger.Module; +import dagger.Provides; +import javax.inject.Named; + +/** + * Module for providing the default HttpRequestFactory. + * + * + *

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 {