mirror of
https://github.com/google/nomulus.git
synced 2025-06-27 06:44:51 +02:00
mv com/google/domain/registry google/registry
This change renames directories in preparation for the great package rename. The repository is now in a broken state because the code itself hasn't been updated. However this should ensure that git correctly preserves history for each file.
This commit is contained in:
parent
a41677aea1
commit
5012893c1d
2396 changed files with 0 additions and 0 deletions
179
java/google/registry/tools/AppEngineConnection.java
Normal file
179
java/google/registry/tools/AppEngineConnection.java
Normal file
|
@ -0,0 +1,179 @@
|
|||
// Copyright 2016 The Domain Registry 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 com.google.domain.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 com.google.domain.registry.security.JsonHttp.JSON_SAFETY_PREFIX;
|
||||
import static com.google.domain.registry.security.XsrfTokenManager.X_CSRF_TOKEN;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Supplier;
|
||||
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.domain.registry.config.RegistryEnvironment;
|
||||
import com.google.domain.registry.security.XsrfTokenManager;
|
||||
import com.google.domain.registry.tools.ServerSideCommand.Connection;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
|
||||
import org.joda.time.Duration;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
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 java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** 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>");
|
||||
|
||||
|
||||
@Parameter(
|
||||
names = "--server",
|
||||
description = "HOST[:PORT] to which remote commands are sent.")
|
||||
private HostAndPort server = RegistryEnvironment.get().config().getServer();
|
||||
|
||||
@Parameter(
|
||||
names = "--remote_server_spec",
|
||||
description = "Combined server spec for the backend to connect to for remote logging.")
|
||||
private String remoteServerSpec = "gslb:apphosting-frontend:4";
|
||||
|
||||
@Parameter(
|
||||
names = "--remote_connection_timeout",
|
||||
description = "How long to wait for the remote logger server before giving up.")
|
||||
private Duration remoteConnectionTimeout = Duration.standardSeconds(30);
|
||||
|
||||
/**
|
||||
* Memoized XSRF security token.
|
||||
*
|
||||
* <p>Computing this is expensive since it needs to load {@code ServerSecret} so do it once.
|
||||
*/
|
||||
private final Supplier<String> xsrfToken =
|
||||
memoize(new Supplier<String>() {
|
||||
@Override
|
||||
public String get() {
|
||||
return XsrfTokenManager.generateToken("admin", getUserId());
|
||||
}});
|
||||
|
||||
@Override
|
||||
public void prefetchXsrfToken() throws IOException {
|
||||
// Cause XSRF token to be fetched, and then stay resident in cache (since it's memoized).
|
||||
xsrfToken.get();
|
||||
}
|
||||
|
||||
/** Returns the contents of the title tag in the given HTML, or null if not found. */
|
||||
private static String extractHtmlTitle(String html) {
|
||||
Matcher matcher = HTML_TITLE_TAG_PATTERN.matcher(html);
|
||||
return (matcher.find() ? matcher.group(1) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String send(
|
||||
String endpoint, Map<String, ?> params, MediaType contentType, byte[] payload)
|
||||
throws IOException {
|
||||
HttpURLConnection connection = getHttpURLConnection(
|
||||
new URL(String.format("http://%s%s?%s", getServer(), endpoint, encodeParams(params))));
|
||||
connection.setRequestMethod("POST");
|
||||
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(
|
||||
CharStreams.toString(new InputStreamReader(connection.getErrorStream(), UTF_8)));
|
||||
throw new IOException(String.format(
|
||||
"Error from %s: %d %s%s",
|
||||
connection.getURL(),
|
||||
connection.getResponseCode(),
|
||||
connection.getResponseMessage(),
|
||||
(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) {
|
||||
try {
|
||||
return entry.getKey()
|
||||
+ "=" + URLEncoder.encode(entry.getValue().toString(), UTF_8.name());
|
||||
} catch (Exception e) { // UnsupportedEncodingException
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}}));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> sendJson(String endpoint, Map<String, ?> object) throws IOException {
|
||||
String response = send(
|
||||
endpoint,
|
||||
ImmutableMap.<String, Object>of(),
|
||||
JSON_UTF_8,
|
||||
JSONValue.toJSONString(object).getBytes(UTF_8));
|
||||
return (Map<String, Object>) JSONValue.parse(response.substring(JSON_SAFETY_PREFIX.length()));
|
||||
}
|
||||
|
||||
private HttpURLConnection getHttpURLConnection(URL remoteUrl) throws IOException {
|
||||
// TODO: Figure out authentication.
|
||||
return (HttpURLConnection) remoteUrl.openConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerUrl() {
|
||||
return "https://" + getServer().toString().replaceFirst("\\.", "-dot-");
|
||||
}
|
||||
|
||||
HostAndPort getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
boolean isLocalhost() {
|
||||
return server.getHostText().equals("localhost");
|
||||
}
|
||||
|
||||
private String getUserId() {
|
||||
return isLocalhost()
|
||||
? UserIdProvider.getTestUserId()
|
||||
: UserIdProvider.getProdUserId();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue