mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Convert AppEngineConnection to use HttpTransport
Make AppEngineConnection use HttpTransport through HttpRequestFactory and create factory factories for localhost and HTTPOverRPC. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=143680257
This commit is contained in:
parent
0cf62be403
commit
270734fd9b
8 changed files with 255 additions and 67 deletions
|
@ -15,51 +15,46 @@
|
|||
package google.registry.tools;
|
||||
|
||||
import static com.google.common.base.Suppliers.memoize;
|
||||
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
|
||||
import static com.google.common.net.HttpHeaders.X_REQUESTED_WITH;
|
||||
import static com.google.common.net.MediaType.JSON_UTF_8;
|
||||
import static google.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
|
||||
import static google.registry.security.XsrfTokenManager.X_CSRF_TOKEN;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.api.client.http.ByteArrayContent;
|
||||
import com.google.api.client.http.GenericUrl;
|
||||
import com.google.api.client.http.HttpHeaders;
|
||||
import com.google.api.client.http.HttpRequest;
|
||||
import com.google.api.client.http.HttpRequestFactory;
|
||||
import com.google.api.client.http.HttpResponse;
|
||||
import com.google.api.client.http.HttpUnsuccessfulResponseHandler;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.google.common.net.HostAndPort;
|
||||
import com.google.common.net.MediaType;
|
||||
import com.google.re2j.Matcher;
|
||||
import com.google.re2j.Pattern;
|
||||
import google.registry.config.RegistryEnvironment;
|
||||
import google.registry.security.XsrfTokenManager;
|
||||
import google.registry.tools.ServerSideCommand.Connection;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import javax.inject.Inject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
/** An http connection to the appengine server. */
|
||||
@Parameters(separators = " =")
|
||||
class AppEngineConnection implements Connection {
|
||||
|
||||
/** Pattern to heuristically extract title tag contents in HTML responses. */
|
||||
private static final Pattern HTML_TITLE_TAG_PATTERN = Pattern.compile("<title>(.*?)</title>");
|
||||
|
||||
@Inject HttpRequestFactory requestFactory;
|
||||
@Inject HostAndPort server;
|
||||
|
||||
@Parameter(
|
||||
names = "--server",
|
||||
description = "HOST[:PORT] to which remote commands are sent.")
|
||||
private HostAndPort server = RegistryEnvironment.get().config().getServer();
|
||||
|
||||
@Inject
|
||||
AppEngineConnection() {}
|
||||
|
||||
/**
|
||||
* Memoized XSRF security token.
|
||||
|
@ -86,55 +81,50 @@ class AppEngineConnection implements Connection {
|
|||
}
|
||||
|
||||
/** Returns the HTML from the connection error stream, if any, otherwise the empty string. */
|
||||
private static String getErrorHtmlAsString(HttpURLConnection connection) throws IOException {
|
||||
return connection.getErrorStream() != null
|
||||
? CharStreams.toString(new InputStreamReader(connection.getErrorStream(), UTF_8))
|
||||
: "";
|
||||
private static String getErrorHtmlAsString(HttpResponse response) throws IOException {
|
||||
return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String send(
|
||||
String endpoint, Map<String, ?> params, MediaType contentType, byte[] payload)
|
||||
throws IOException {
|
||||
HttpURLConnection connection = getHttpURLConnection(
|
||||
new URL(String.format("%s%s?%s", getServerUrl(), endpoint, encodeParams(params))));
|
||||
connection.setRequestMethod("POST");
|
||||
// Disable following redirects, which we shouldn't normally encounter.
|
||||
connection.setInstanceFollowRedirects(false);
|
||||
connection.setUseCaches(false);
|
||||
connection.setRequestProperty(CONTENT_TYPE, contentType.toString());
|
||||
connection.setRequestProperty(X_CSRF_TOKEN, xsrfToken.get());
|
||||
connection.setRequestProperty(X_REQUESTED_WITH, "RegistryTool");
|
||||
connection.setDoOutput(true);
|
||||
connection.connect();
|
||||
try (OutputStream output = connection.getOutputStream()) {
|
||||
output.write(payload);
|
||||
}
|
||||
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
||||
String errorTitle = extractHtmlTitle(getErrorHtmlAsString(connection));
|
||||
throw new IOException(String.format(
|
||||
GenericUrl url = new GenericUrl(String.format("%s%s", getServerUrl(), endpoint));
|
||||
url.putAll(params);
|
||||
HttpRequest request =
|
||||
requestFactory.buildPostRequest(url, new ByteArrayContent(contentType.toString(), payload));
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
headers.setCacheControl("no-cache");
|
||||
headers.put(X_CSRF_TOKEN, ImmutableList.of(xsrfToken.get()));
|
||||
headers.put(X_REQUESTED_WITH, ImmutableList.of("RegistryTool"));
|
||||
request.setHeaders(headers);
|
||||
request.setFollowRedirects(false);
|
||||
request.setThrowExceptionOnExecuteError(false);
|
||||
request.setUnsuccessfulResponseHandler(
|
||||
new HttpUnsuccessfulResponseHandler() {
|
||||
@Override
|
||||
public boolean handleResponse(
|
||||
HttpRequest request, HttpResponse response, boolean supportsRetry)
|
||||
throws IOException {
|
||||
String errorTitle = extractHtmlTitle(getErrorHtmlAsString(response));
|
||||
throw new IOException(
|
||||
String.format(
|
||||
"Error from %s: %d %s%s",
|
||||
connection.getURL(),
|
||||
connection.getResponseCode(),
|
||||
connection.getResponseMessage(),
|
||||
request.getUrl().toString(),
|
||||
response.getStatusCode(),
|
||||
response.getStatusMessage(),
|
||||
(errorTitle == null ? "" : ": " + errorTitle)));
|
||||
}
|
||||
return CharStreams.toString(new InputStreamReader(connection.getInputStream(), UTF_8));
|
||||
}
|
||||
|
||||
private String encodeParams(Map<String, ?> params) {
|
||||
return Joiner.on('&').join(Iterables.transform(
|
||||
params.entrySet(),
|
||||
new Function<Entry<String, ?>, String>() {
|
||||
@Override
|
||||
public String apply(Entry<String, ?> entry) {
|
||||
});
|
||||
HttpResponse response = null;
|
||||
try {
|
||||
return entry.getKey()
|
||||
+ "=" + URLEncoder.encode(entry.getValue().toString(), UTF_8.name());
|
||||
} catch (Exception e) { // UnsupportedEncodingException
|
||||
throw new RuntimeException(e);
|
||||
response = request.execute();
|
||||
return CharStreams.toString(new InputStreamReader(response.getContent(), UTF_8));
|
||||
} finally {
|
||||
if (response != null) {
|
||||
response.disconnect();
|
||||
}
|
||||
}
|
||||
}}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -148,11 +138,6 @@ class AppEngineConnection implements Connection {
|
|||
return (Map<String, Object>) JSONValue.parse(response.substring(JSON_SAFETY_PREFIX.length()));
|
||||
}
|
||||
|
||||
private HttpURLConnection getHttpURLConnection(URL remoteUrl) throws IOException {
|
||||
// TODO(b/28219927): Figure out authentication.
|
||||
return (HttpURLConnection) remoteUrl.openConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerUrl() {
|
||||
return (isLocalhost() ? "http://" : "https://") + getServer().toString();
|
||||
|
|
38
java/google/registry/tools/AppEngineConnectionFlags.java
Normal file
38
java/google/registry/tools/AppEngineConnectionFlags.java
Normal file
|
@ -0,0 +1,38 @@
|
|||
// Copyright 2016 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.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
import com.google.common.net.HostAndPort;
|
||||
import google.registry.config.RegistryEnvironment;
|
||||
|
||||
/**
|
||||
* Class to contain the configuration flags for AppEngineConnection.
|
||||
*
|
||||
* <p>This is broken out into its own class to make it cleaner to extract these from the dagger
|
||||
* module, where these values are injected.
|
||||
*/
|
||||
@Parameters(separators = " =")
|
||||
class AppEngineConnectionFlags {
|
||||
|
||||
@Parameter(names = "--server", description = "HOST[:PORT] to which remote commands are sent.")
|
||||
private static HostAndPort server = RegistryEnvironment.get().config().getServer();
|
||||
|
||||
HostAndPort getServer() {
|
||||
return server;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2016 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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2016 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();
|
||||
}
|
||||
}
|
28
java/google/registry/tools/HttpRequestFactoryComponent.java
Normal file
28
java/google/registry/tools/HttpRequestFactoryComponent.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2016 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();
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2016 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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -49,7 +49,9 @@ final class RegistryCli {
|
|||
|
||||
// Do not make this final - compile-time constant inlining may interfere with JCommander.
|
||||
@ParametersDelegate
|
||||
private AppEngineConnection connection = new AppEngineConnection();
|
||||
private AppEngineConnectionFlags appEngineConnectionFlags =
|
||||
new AppEngineConnectionFlags();
|
||||
|
||||
|
||||
// Do not make this final - compile-time constant inlining may interfere with JCommander.
|
||||
@ParametersDelegate
|
||||
|
@ -115,13 +117,29 @@ final class RegistryCli {
|
|||
return;
|
||||
}
|
||||
loggingParams.configureLogging(); // Must be called after parameters are parsed.
|
||||
injectReflectively(RegistryToolComponent.class, DaggerRegistryToolComponent.create(), command);
|
||||
|
||||
// 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))
|
||||
.build();
|
||||
injectReflectively(RegistryToolComponent.class, component, command);
|
||||
|
||||
if (!(command instanceof RemoteApiCommand)) {
|
||||
command.run();
|
||||
return;
|
||||
}
|
||||
|
||||
AppEngineConnection connection = component.appEngineConnection();
|
||||
if (command instanceof ServerSideCommand) {
|
||||
((ServerSideCommand) command).setConnection(connection);
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import google.registry.request.Modules.Jackson2Module;
|
|||
import google.registry.request.Modules.URLFetchServiceModule;
|
||||
import google.registry.util.SystemClock.SystemClockModule;
|
||||
import google.registry.util.SystemSleeper.SystemSleeperModule;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Dagger component for Registry Tool.
|
||||
|
@ -33,8 +34,10 @@ import google.registry.util.SystemSleeper.SystemSleeperModule;
|
|||
* <p>Any command class with {@code @Inject} fields <i>must</i> be listed as a method here.
|
||||
* Otherwise {@link RegistryCli} will not be able to populate those fields after its instantiation.
|
||||
*/
|
||||
@Singleton
|
||||
@Component(
|
||||
modules = {
|
||||
AppEngineConnectionFlagsModule.class,
|
||||
ConfigModule.class,
|
||||
DatastoreServiceModule.class,
|
||||
CloudDnsWriterModule.class,
|
||||
|
@ -47,6 +50,9 @@ import google.registry.util.SystemSleeper.SystemSleeperModule;
|
|||
SystemSleeperModule.class,
|
||||
URLFetchServiceModule.class,
|
||||
VoidDnsWriterModule.class,
|
||||
},
|
||||
dependencies = {
|
||||
HttpRequestFactoryComponent.class,
|
||||
}
|
||||
)
|
||||
interface RegistryToolComponent {
|
||||
|
@ -68,4 +74,6 @@ interface RegistryToolComponent {
|
|||
void inject(UpdateTldCommand command);
|
||||
void inject(ValidateEscrowDepositCommand command);
|
||||
void inject(WhoisQueryCommand command);
|
||||
|
||||
AppEngineConnection appEngineConnection();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue