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
This commit is contained in:
mmuller 2017-01-31 07:25:25 -08:00 committed by Ben McIlwain
parent b70f57b7c7
commit ecbe2662ac
9 changed files with 99 additions and 157 deletions

View file

@ -51,7 +51,7 @@ class AppEngineConnection implements Connection {
private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>"); private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>");
@Inject HttpRequestFactory requestFactory; @Inject HttpRequestFactory requestFactory;
@Inject HostAndPort server; @Inject AppEngineConnectionFlags flags;
@Inject @Inject
AppEngineConnection() {} AppEngineConnection() {}
@ -144,11 +144,11 @@ class AppEngineConnection implements Connection {
} }
HostAndPort getServer() { 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() { boolean isLocalhost() {
return server.getHostText().equals("localhost"); return flags.getServer().getHostText().equals("localhost");
} }
private String getUserId() { private String getUserId() {

View file

@ -17,6 +17,8 @@ package google.registry.tools;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import dagger.Module;
import dagger.Provides;
import google.registry.config.RegistryConfig; import google.registry.config.RegistryConfig;
/** /**
@ -34,5 +36,19 @@ class AppEngineConnectionFlags {
HostAndPort getServer() { HostAndPort getServer() {
return server; return server;
} }
@Module
static class FlagsModule {
AppEngineConnectionFlags flags;
FlagsModule(AppEngineConnectionFlags flags) {
this.flags = flags;
}
@Provides
AppEngineConnectionFlags provideAppEngineConnectionFlags() {
return flags;
}
}
} }

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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.
*
*
* <p>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.
*
* <p>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.
*
* <p>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);
}
}

View file

@ -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.
*
* <p>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();
}

View file

@ -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.
*
* <p>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.
*
* <p>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");
}
});
}
}

View file

@ -118,19 +118,9 @@ final class RegistryCli {
} }
loggingParams.configureLogging(); // Must be called after parameters are parsed. 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. // Create the main component and use it to inject the command class.
RegistryToolComponent component = DaggerRegistryToolComponent.builder() RegistryToolComponent component = DaggerRegistryToolComponent.builder()
.httpRequestFactoryComponent(requestFactoryComponent) .flagsModule(new AppEngineConnectionFlags.FlagsModule(appEngineConnectionFlags))
.appEngineConnectionFlagsModule(
new AppEngineConnectionFlagsModule(appEngineConnectionFlags))
.build(); .build();
injectReflectively(RegistryToolComponent.class, component, command); injectReflectively(RegistryToolComponent.class, component, command);

View file

@ -38,10 +38,12 @@ import javax.inject.Singleton;
@Singleton @Singleton
@Component( @Component(
modules = { modules = {
AppEngineConnectionFlagsModule.class, AppEngineConnectionFlags.FlagsModule.class,
ConfigModule.class, ConfigModule.class,
DatastoreServiceModule.class, DatastoreServiceModule.class,
CloudDnsWriterModule.class, CloudDnsWriterModule.class,
DefaultRequestFactoryModule.class,
DefaultRequestFactoryModule.RequestFactoryModule.class,
DnsUpdateWriterModule.class, DnsUpdateWriterModule.class,
DummyKeyringModule.class, DummyKeyringModule.class,
Jackson2Module.class, Jackson2Module.class,
@ -52,9 +54,6 @@ import javax.inject.Singleton;
URLFetchServiceModule.class, URLFetchServiceModule.class,
VoidDnsWriterModule.class, VoidDnsWriterModule.class,
WhoisModule.class, WhoisModule.class,
},
dependencies = {
HttpRequestFactoryComponent.class,
} }
) )
interface RegistryToolComponent { interface RegistryToolComponent {