mirror of
https://github.com/google/nomulus.git
synced 2025-05-30 01:10:14 +02:00
Refactor RDE import stuff into its own rde.imports package
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=141566236
This commit is contained in:
parent
58b7babc2b
commit
a56a0677ba
66 changed files with 97 additions and 9248 deletions
37
java/google/registry/rde/imports/BUILD
Normal file
37
java/google/registry/rde/imports/BUILD
Normal file
|
@ -0,0 +1,37 @@
|
|||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
licenses(["notice"]) # Apache 2.0
|
||||
|
||||
java_library(
|
||||
name = "imports",
|
||||
srcs = glob(["*.java"]),
|
||||
deps = [
|
||||
"//java/com/google/common/annotations",
|
||||
"//java/com/google/common/base",
|
||||
"//java/com/google/common/collect",
|
||||
"//java/com/google/common/html",
|
||||
"//java/com/google/common/io",
|
||||
"//java/com/google/common/math",
|
||||
"//java/com/google/common/net",
|
||||
"//java/google/registry/config",
|
||||
"//java/google/registry/gcs",
|
||||
"//java/google/registry/mapreduce",
|
||||
"//java/google/registry/model",
|
||||
"//java/google/registry/request",
|
||||
"//java/google/registry/util",
|
||||
"//java/google/registry/xjc",
|
||||
"//third_party/java/appengine:appengine-api",
|
||||
"//third_party/java/appengine_gcs_client",
|
||||
"//third_party/java/appengine_mapreduce2:appengine_mapreduce",
|
||||
"//third_party/java/auto:auto_factory",
|
||||
"//third_party/java/auto:auto_value",
|
||||
"//third_party/java/dagger",
|
||||
"//third_party/java/joda_time",
|
||||
"//third_party/java/jsr305_annotations",
|
||||
"//third_party/java/jsr330_inject",
|
||||
"//third_party/java/objectify:objectify-v4_1",
|
||||
"//third_party/java/servlet/servlet_api",
|
||||
],
|
||||
)
|
127
java/google/registry/rde/imports/RdeContactImportAction.java
Normal file
127
java/google/registry/rde/imports/RdeContactImportAction.java
Normal file
|
@ -0,0 +1,127 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static google.registry.mapreduce.MapreduceRunner.PARAM_MAP_SHARDS;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.PipelineUtils.createJobPath;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.Mapper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.config.ConfigModule.Config;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.mapreduce.MapreduceRunner;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.request.Action;
|
||||
import google.registry.request.Parameter;
|
||||
import google.registry.request.Response;
|
||||
import google.registry.util.SystemClock;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* A mapreduce that imports contacts from an escrow file.
|
||||
*
|
||||
* <p>Specify the escrow file to import with the "path" parameter.
|
||||
*/
|
||||
@Action(path = "/_dr/task/importRdeContacts")
|
||||
public class RdeContactImportAction implements Runnable {
|
||||
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
protected final MapreduceRunner mrRunner;
|
||||
protected final Response response;
|
||||
protected final String importBucketName;
|
||||
protected final String importFileName;
|
||||
protected final Optional<Integer> mapShards;
|
||||
|
||||
@Inject
|
||||
public RdeContactImportAction(
|
||||
MapreduceRunner mrRunner,
|
||||
Response response,
|
||||
@Config("rdeImportBucket") String importBucketName,
|
||||
@Parameter("path") String importFileName,
|
||||
@Parameter(PARAM_MAP_SHARDS) Optional<Integer> mapShards) {
|
||||
this.mrRunner = mrRunner;
|
||||
this.response = response;
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
this.mapShards = mapShards;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
response.sendJavaScriptRedirect(createJobPath(mrRunner
|
||||
.setJobName("Import contacts from escrow file")
|
||||
.setModuleName("backend")
|
||||
.runMapOnly(
|
||||
createMapper(),
|
||||
ImmutableList.of(createInput()))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link RdeContactInput}
|
||||
*/
|
||||
private RdeContactInput createInput() {
|
||||
return new RdeContactInput(mapShards, importBucketName, importFileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link RdeContactImportMapper}
|
||||
*/
|
||||
private RdeContactImportMapper createMapper() {
|
||||
return new RdeContactImportMapper(importBucketName);
|
||||
}
|
||||
|
||||
/** Mapper to import contacts from an escrow file. */
|
||||
public static class RdeContactImportMapper extends Mapper<ContactResource, Void, Void> {
|
||||
|
||||
private static final long serialVersionUID = -7645091075256589374L;
|
||||
private final String importBucketName;
|
||||
private transient RdeImportUtils importUtils;
|
||||
|
||||
public RdeContactImportMapper(String importBucketName) {
|
||||
this.importBucketName = importBucketName;
|
||||
}
|
||||
|
||||
private RdeImportUtils getImportUtils() {
|
||||
if (importUtils == null) {
|
||||
importUtils = createRdeImportUtils();
|
||||
}
|
||||
return importUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of RdeImportUtils.
|
||||
*/
|
||||
private RdeImportUtils createRdeImportUtils() {
|
||||
return new RdeImportUtils(
|
||||
ofy(),
|
||||
new SystemClock(),
|
||||
importBucketName,
|
||||
new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void map(ContactResource contact) {
|
||||
getImportUtils().importContact(contact);
|
||||
}
|
||||
}
|
||||
}
|
132
java/google/registry/rde/imports/RdeContactInput.java
Normal file
132
java/google/registry/rde/imports/RdeContactInput.java
Normal file
|
@ -0,0 +1,132 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static com.google.common.math.IntMath.divide;
|
||||
import static java.math.RoundingMode.CEILING;
|
||||
import static java.math.RoundingMode.FLOOR;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.Input;
|
||||
import com.google.appengine.tools.mapreduce.InputReader;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.rde.imports.RdeParser.RdeHeader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A MapReduce {@link Input} that imports {@link ContactResource} objects from an escrow file.
|
||||
*
|
||||
* <p>If a mapShards parameter has been specified, up to that many readers will be created
|
||||
* so that each map shard has one reader. If a mapShards parameter has not been specified, a
|
||||
* default number of readers will be created.
|
||||
*/
|
||||
public class RdeContactInput extends Input<ContactResource> {
|
||||
|
||||
private static final long serialVersionUID = -366966393494008712L;
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
/**
|
||||
* Default number of readers if map shards are not specified.
|
||||
*/
|
||||
private static final int DEFAULT_READERS = 50;
|
||||
|
||||
/**
|
||||
* Minimum number of records per reader.
|
||||
*/
|
||||
private static final int MINIMUM_RECORDS_PER_READER = 100;
|
||||
|
||||
/**
|
||||
* Optional argument to explicitly specify the number of readers.
|
||||
*/
|
||||
private final int numReaders;
|
||||
private final String importBucketName;
|
||||
private final String importFileName;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RdeContactInput}
|
||||
*
|
||||
* @param mapShards Number of readers that should be created
|
||||
* @param importBucketName Name of GCS bucket for escrow file imports
|
||||
* @param importFileName Name of escrow file in GCS
|
||||
*/
|
||||
public RdeContactInput(Optional<Integer> mapShards, String importBucketName,
|
||||
String importFileName) {
|
||||
this.numReaders = mapShards.or(DEFAULT_READERS);
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends InputReader<ContactResource>> createReaders() throws IOException {
|
||||
int numReaders = this.numReaders;
|
||||
RdeHeader header = newParser().getHeader();
|
||||
int numberOfContacts = header.getContactCount().intValue();
|
||||
if (numberOfContacts / numReaders < MINIMUM_RECORDS_PER_READER) {
|
||||
numReaders = divide(numberOfContacts, MINIMUM_RECORDS_PER_READER, FLOOR);
|
||||
// use at least one reader
|
||||
numReaders = Math.max(numReaders, 1);
|
||||
}
|
||||
ImmutableList.Builder<RdeContactReader> builder = new ImmutableList.Builder<>();
|
||||
int contactsPerReader =
|
||||
Math.max(MINIMUM_RECORDS_PER_READER, divide(numberOfContacts, numReaders, CEILING));
|
||||
int offset = 0;
|
||||
for (int i = 0; i < numReaders; i++) {
|
||||
builder = builder.add(newReader(offset, contactsPerReader));
|
||||
offset += contactsPerReader;
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeContactReader}
|
||||
*/
|
||||
private RdeContactReader newReader(int offset, int maxResults) {
|
||||
return new RdeContactReader(importBucketName, importFileName, offset, maxResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeParser}
|
||||
*/
|
||||
private RdeParser newParser() {
|
||||
GcsUtils utils = new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize());
|
||||
GcsFilename filename = new GcsFilename(importBucketName, importFileName);
|
||||
try (InputStream xmlInput = utils.openInputStream(filename)) {
|
||||
return new RdeParser(xmlInput);
|
||||
} catch (Exception e) {
|
||||
throw new InitializationException(
|
||||
String.format("Error opening rde file %s/%s", importBucketName, importFileName), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when the input cannot initialize properly.
|
||||
*/
|
||||
private static class InitializationException extends RuntimeException {
|
||||
|
||||
public InitializationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
107
java/google/registry/rde/imports/RdeContactReader.java
Normal file
107
java/google/registry/rde/imports/RdeContactReader.java
Normal file
|
@ -0,0 +1,107 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.InputReader;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.NoSuchElementException;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
/** Mapreduce {@link InputReader} for reading contacts from escrow files */
|
||||
@NotThreadSafe
|
||||
public class RdeContactReader extends InputReader<ContactResource> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -3688793834175577691L;
|
||||
|
||||
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
final String importBucketName;
|
||||
final String importFileName;
|
||||
final int offset;
|
||||
final int maxResults;
|
||||
|
||||
private int count = 0;
|
||||
|
||||
transient RdeParser parser;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeParser}
|
||||
*/
|
||||
private RdeParser newParser() {
|
||||
GcsUtils utils = new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize());
|
||||
GcsFilename filename = new GcsFilename(importBucketName, importFileName);
|
||||
InputStream xmlInput = utils.openInputStream(filename);
|
||||
try {
|
||||
RdeParser parser = new RdeParser(xmlInput);
|
||||
// skip the file offset and count
|
||||
// if count is greater than 0, the reader has been rehydrated after doing some work.
|
||||
// skip any already processed records.
|
||||
parser.skipContacts(offset + count);
|
||||
return parser;
|
||||
} catch (Exception e) {
|
||||
logger.severefmt(e, "Error opening rde file %s/%s", importBucketName, importFileName);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public RdeContactReader(
|
||||
String importBucketName,
|
||||
String importFileName,
|
||||
int offset,
|
||||
int maxResults) {
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
this.offset = offset;
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactResource next() throws IOException {
|
||||
if (count < maxResults) {
|
||||
if (parser == null) {
|
||||
parser = newParser();
|
||||
if (parser.isAtContact()) {
|
||||
count++;
|
||||
return XjcToContactResourceConverter.convertContact(parser.getContact());
|
||||
}
|
||||
}
|
||||
if (parser.nextContact()) {
|
||||
count++;
|
||||
return XjcToContactResourceConverter.convertContact(parser.getContact());
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endSlice() throws IOException {
|
||||
super.endSlice();
|
||||
if (parser != null) {
|
||||
parser.close();
|
||||
}
|
||||
}
|
||||
}
|
113
java/google/registry/rde/imports/RdeHostImportAction.java
Normal file
113
java/google/registry/rde/imports/RdeHostImportAction.java
Normal file
|
@ -0,0 +1,113 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static google.registry.mapreduce.MapreduceRunner.PARAM_MAP_SHARDS;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.PipelineUtils.createJobPath;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.Mapper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.config.ConfigModule.Config;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.mapreduce.MapreduceRunner;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.request.Action;
|
||||
import google.registry.request.Parameter;
|
||||
import google.registry.request.Response;
|
||||
import google.registry.util.SystemClock;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* A mapreduce that imports hosts from an escrow file.
|
||||
*
|
||||
* <p>Specify the escrow file to import with the "path" parameter.
|
||||
*/
|
||||
@Action(path = "/_dr/task/importRdeHosts")
|
||||
public class RdeHostImportAction implements Runnable {
|
||||
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
private final MapreduceRunner mrRunner;
|
||||
private final Response response;
|
||||
private final String importBucketName;
|
||||
private final String importFileName;
|
||||
private final Optional<Integer> mapShards;
|
||||
|
||||
@Inject
|
||||
public RdeHostImportAction(
|
||||
MapreduceRunner mrRunner,
|
||||
Response response,
|
||||
@Config("rdeImportBucket") String importBucketName,
|
||||
@Parameter("path") String importFileName,
|
||||
@Parameter(PARAM_MAP_SHARDS) Optional<Integer> mapShards) {
|
||||
this.mrRunner = mrRunner;
|
||||
this.response = response;
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
this.mapShards = mapShards;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
response.sendJavaScriptRedirect(createJobPath(mrRunner
|
||||
.setJobName("Import hosts from escrow file")
|
||||
.setModuleName("backend")
|
||||
.runMapOnly(
|
||||
new RdeHostImportMapper(importBucketName),
|
||||
ImmutableList.of(new RdeHostInput(mapShards, importBucketName, importFileName)))));
|
||||
}
|
||||
|
||||
/** Mapper to import hosts from an escrow file. */
|
||||
public static class RdeHostImportMapper extends Mapper<HostResource, Void, Void> {
|
||||
|
||||
private static final long serialVersionUID = -2898753709127134419L;
|
||||
private final String importBucketName;
|
||||
private transient RdeImportUtils importUtils;
|
||||
|
||||
public RdeHostImportMapper(String importBucketName) {
|
||||
this.importBucketName = importBucketName;
|
||||
}
|
||||
|
||||
private RdeImportUtils getImportUtils() {
|
||||
if (importUtils == null) {
|
||||
importUtils = createRdeImportUtils();
|
||||
}
|
||||
return importUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of RdeImportUtils.
|
||||
*/
|
||||
private RdeImportUtils createRdeImportUtils() {
|
||||
return new RdeImportUtils(
|
||||
ofy(),
|
||||
new SystemClock(),
|
||||
importBucketName,
|
||||
new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void map(HostResource host) {
|
||||
getImportUtils().importHost(host);
|
||||
}
|
||||
}
|
||||
}
|
132
java/google/registry/rde/imports/RdeHostInput.java
Normal file
132
java/google/registry/rde/imports/RdeHostInput.java
Normal file
|
@ -0,0 +1,132 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.Input;
|
||||
import com.google.appengine.tools.mapreduce.InputReader;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.rde.imports.RdeParser.RdeHeader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A MapReduce {@link Input} that imports {@link HostResource} objects from an escrow file.
|
||||
*
|
||||
* <p>If a mapShards parameter has been specified, up to that many readers will be created
|
||||
* so that each map shard has one reader. If a mapShards parameter has not been specified, a
|
||||
* default number of readers will be created.
|
||||
*/
|
||||
public class RdeHostInput extends Input<HostResource> {
|
||||
|
||||
private static final long serialVersionUID = 9218225041307602452L;
|
||||
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
/**
|
||||
* Default number of readers if map shards are not specified.
|
||||
*/
|
||||
private static final int DEFAULT_READERS = 50;
|
||||
|
||||
/**
|
||||
* Minimum number of records per reader.
|
||||
*/
|
||||
private static final int MINIMUM_RECORDS_PER_READER = 100;
|
||||
|
||||
/**
|
||||
* Optional argument to explicitly specify the number of readers.
|
||||
*/
|
||||
private final int numReaders;
|
||||
private final String importBucketName;
|
||||
private final String importFileName;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RdeHostInput}
|
||||
*
|
||||
* @param mapShards Number of readers that should be created
|
||||
* @param importBucketName Name of GCS bucket for escrow file imports
|
||||
* @param importFileName Name of escrow file in GCS
|
||||
*/
|
||||
public RdeHostInput(Optional<Integer> mapShards, String importBucketName,
|
||||
String importFileName) {
|
||||
this.numReaders = mapShards.or(DEFAULT_READERS);
|
||||
checkArgument(numReaders > 0, "Number of shards must be greater than zero");
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends InputReader<HostResource>> createReaders() throws IOException {
|
||||
int numReaders = this.numReaders;
|
||||
RdeHeader header = createParser().getHeader();
|
||||
int numberOfHosts = header.getHostCount().intValue();
|
||||
if (numberOfHosts / numReaders < MINIMUM_RECORDS_PER_READER) {
|
||||
numReaders = numberOfHosts / MINIMUM_RECORDS_PER_READER;
|
||||
// use at least one reader
|
||||
numReaders = Math.max(numReaders, 1);
|
||||
}
|
||||
ImmutableList.Builder<RdeHostReader> builder = new ImmutableList.Builder<>();
|
||||
int hostsPerReader =
|
||||
Math.max(MINIMUM_RECORDS_PER_READER, (int) Math.ceil((double) numberOfHosts / numReaders));
|
||||
int offset = 0;
|
||||
for (int i = 0; i < numReaders; i++) {
|
||||
builder = builder.add(createReader(offset, hostsPerReader));
|
||||
offset += hostsPerReader;
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeHostReader}
|
||||
*/
|
||||
private RdeHostReader createReader(int offset, int maxResults) {
|
||||
return new RdeHostReader(importBucketName, importFileName, offset, maxResults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeParser}
|
||||
*/
|
||||
private RdeParser createParser() {
|
||||
GcsUtils utils = new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize());
|
||||
GcsFilename filename = new GcsFilename(importBucketName, importFileName);
|
||||
InputStream xmlInput = utils.openInputStream(filename);
|
||||
try {
|
||||
return new RdeParser(xmlInput);
|
||||
} catch (Exception e) {
|
||||
throw new InitializationException(
|
||||
String.format("Error opening rde file %s/%s", importBucketName, importFileName), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown when the input cannot initialize properly.
|
||||
*/
|
||||
private static class InitializationException extends RuntimeException {
|
||||
public InitializationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
107
java/google/registry/rde/imports/RdeHostReader.java
Normal file
107
java/google/registry/rde/imports/RdeHostReader.java
Normal file
|
@ -0,0 +1,107 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.google.appengine.tools.cloudstorage.GcsService;
|
||||
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
|
||||
import com.google.appengine.tools.cloudstorage.RetryParams;
|
||||
import com.google.appengine.tools.mapreduce.InputReader;
|
||||
import google.registry.config.ConfigModule;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.NoSuchElementException;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
/** Mapreduce {@link InputReader} for reading hosts from escrow files */
|
||||
@NotThreadSafe
|
||||
public class RdeHostReader extends InputReader<HostResource> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3037264959150412846L;
|
||||
|
||||
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
||||
private static final GcsService GCS_SERVICE =
|
||||
GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
|
||||
|
||||
final String importBucketName;
|
||||
final String importFileName;
|
||||
final int offset;
|
||||
final int maxResults;
|
||||
|
||||
private int count = 0;
|
||||
|
||||
transient RdeParser parser;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeParser}
|
||||
*/
|
||||
private RdeParser newParser() {
|
||||
GcsUtils utils = new GcsUtils(GCS_SERVICE, ConfigModule.provideGcsBufferSize());
|
||||
GcsFilename filename = new GcsFilename(importBucketName, importFileName);
|
||||
InputStream xmlInput = utils.openInputStream(filename);
|
||||
try {
|
||||
RdeParser parser = new RdeParser(xmlInput);
|
||||
// skip the file offset and count
|
||||
// if count is greater than 0, the reader has been rehydrated after doing some work.
|
||||
// skip any already processed records.
|
||||
parser.skipHosts(offset + count);
|
||||
return parser;
|
||||
} catch (Exception e) {
|
||||
logger.severefmt(e, "Error opening rde file %s/%s", importBucketName, importFileName);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public RdeHostReader(
|
||||
String importBucketName,
|
||||
String importFileName,
|
||||
int offset,
|
||||
int maxResults) {
|
||||
this.importBucketName = importBucketName;
|
||||
this.importFileName = importFileName;
|
||||
this.offset = offset;
|
||||
this.maxResults = maxResults;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HostResource next() throws IOException {
|
||||
if (count < maxResults) {
|
||||
if (parser == null) {
|
||||
parser = newParser();
|
||||
if (parser.isAtHost()) {
|
||||
count++;
|
||||
return XjcToHostResourceConverter.convert(parser.getHost());
|
||||
}
|
||||
}
|
||||
if (parser.nextHost()) {
|
||||
count++;
|
||||
return XjcToHostResourceConverter.convert(parser.getHost());
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endSlice() throws IOException {
|
||||
super.endSlice();
|
||||
if (parser != null) {
|
||||
parser.close();
|
||||
}
|
||||
}
|
||||
}
|
179
java/google/registry/rde/imports/RdeImportUtils.java
Normal file
179
java/google/registry/rde/imports/RdeImportUtils.java
Normal file
|
@ -0,0 +1,179 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
|
||||
import com.google.appengine.tools.cloudstorage.GcsFilename;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.Work;
|
||||
import google.registry.config.ConfigModule.Config;
|
||||
import google.registry.gcs.GcsUtils;
|
||||
import google.registry.model.EppResource;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.model.index.EppResourceIndex;
|
||||
import google.registry.model.index.ForeignKeyIndex;
|
||||
import google.registry.model.ofy.Ofy;
|
||||
import google.registry.model.registrar.Registrar;
|
||||
import google.registry.model.registry.Registry;
|
||||
import google.registry.model.registry.Registry.RegistryNotFoundException;
|
||||
import google.registry.model.registry.Registry.TldState;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.FormattingLogger;
|
||||
import google.registry.xjc.rderegistrar.XjcRdeRegistrar;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
/**
|
||||
* Utility functions for escrow file import.
|
||||
*/
|
||||
public class RdeImportUtils {
|
||||
|
||||
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
|
||||
|
||||
private final Ofy ofy;
|
||||
private final Clock clock;
|
||||
private final String escrowBucketName;
|
||||
private final GcsUtils gcsUtils;
|
||||
|
||||
@Inject
|
||||
public RdeImportUtils(
|
||||
Ofy ofy, Clock clock, @Config("rdeImportBucket") String escrowBucketName, GcsUtils gcsUtils) {
|
||||
this.ofy = ofy;
|
||||
this.clock = clock;
|
||||
this.gcsUtils = gcsUtils;
|
||||
this.escrowBucketName = escrowBucketName;
|
||||
}
|
||||
|
||||
private <T extends EppResource> boolean importEppResource(final T resource, final String type) {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Class<T> resourceClass = (Class<T>) resource.getClass();
|
||||
return ofy.transact(
|
||||
new Work<Boolean>() {
|
||||
@Override
|
||||
public Boolean run() {
|
||||
EppResource existing = ofy.load().key(Key.create(resource)).now();
|
||||
if (existing == null) {
|
||||
ForeignKeyIndex<T> existingForeignKeyIndex =
|
||||
ForeignKeyIndex.load(
|
||||
resourceClass, resource.getForeignKey(), clock.nowUtc());
|
||||
// foreign key index should not exist, since existing resource was not found.
|
||||
checkState(
|
||||
existingForeignKeyIndex == null,
|
||||
"New %s resource has existing foreign key index; foreignKey=%s, repoId=%s",
|
||||
type,
|
||||
resource.getForeignKey(),
|
||||
resource.getRepoId());
|
||||
ofy.save().entity(resource);
|
||||
ofy.save().entity(ForeignKeyIndex.create(resource, resource.getDeletionTime()));
|
||||
ofy.save().entity(EppResourceIndex.create(Key.create(resource)));
|
||||
logger.infofmt(
|
||||
"Imported %s resource - ROID=%s, id=%s",
|
||||
type, resource.getRepoId(), resource.getForeignKey());
|
||||
return true;
|
||||
} else if (!existing.getRepoId().equals(resource.getRepoId())) {
|
||||
logger.warningfmt(
|
||||
"Existing %s with same id but different ROID. "
|
||||
+ "id=%s, existing ROID=%s, new ROID=%s",
|
||||
type, resource.getForeignKey(), existing.getRepoId(), resource.getRepoId());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a host from an escrow file.
|
||||
*
|
||||
* <p>The host will only be imported if it has not been previously imported.
|
||||
*
|
||||
* <p>If the host is imported, {@link ForeignKeyIndex} and {@link EppResourceIndex} are also
|
||||
* created.
|
||||
*
|
||||
* @return true if the host was created or updated, false otherwise.
|
||||
*/
|
||||
public boolean importHost(final HostResource resource) {
|
||||
return importEppResource(resource, "host");
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports a contact from an escrow file.
|
||||
*
|
||||
* <p>The contact will only be imported if it has not been previously imported.
|
||||
*
|
||||
* <p>If the contact is imported, {@link ForeignKeyIndex} and {@link EppResourceIndex} are also
|
||||
* created.
|
||||
*
|
||||
* @return true if the contact was created or updated, false otherwise.
|
||||
*/
|
||||
public boolean importContact(final ContactResource resource) {
|
||||
return importEppResource(resource, "contact");
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates an escrow file for import.
|
||||
*
|
||||
* <p>Before an escrow file is imported into the registry, the following conditions must be met:
|
||||
*
|
||||
* <ul>
|
||||
* <li>The TLD must already exist in the registry
|
||||
* <li>The TLD must be in the PREDELEGATION state
|
||||
* <li>Each registrar must already exist in the registry
|
||||
* <li>Each IDN table referenced must already exist in the registry
|
||||
* </ul>
|
||||
*
|
||||
* <p>If any of the above conditions is not true, an {@link IllegalStateException} will be thrown.
|
||||
*
|
||||
* @param escrowFilePath Path to the escrow file to validate
|
||||
* @throws IOException If the escrow file cannot be read
|
||||
* @throws IllegalArgumentException if the escrow file cannot be imported
|
||||
*/
|
||||
public void validateEscrowFileForImport(String escrowFilePath) throws IOException {
|
||||
// TODO (wolfgang): Add validation method for IDN tables
|
||||
try (InputStream input =
|
||||
gcsUtils.openInputStream(new GcsFilename(escrowBucketName, escrowFilePath))) {
|
||||
try (RdeParser parser = new RdeParser(input)) {
|
||||
// validate that tld exists and is in PREDELEGATION state
|
||||
String tld = parser.getHeader().getTld();
|
||||
try {
|
||||
Registry registry = Registry.get(tld);
|
||||
TldState currentState = registry.getTldState(clock.nowUtc());
|
||||
checkArgument(
|
||||
currentState == TldState.PREDELEGATION,
|
||||
String.format("Tld '%s' is in state %s and cannot be imported", tld, currentState));
|
||||
} catch (RegistryNotFoundException e) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Tld '%s' not found in the registry", tld));
|
||||
}
|
||||
// validate that all registrars exist
|
||||
while (parser.nextRegistrar()) {
|
||||
XjcRdeRegistrar registrar = parser.getRegistrar();
|
||||
if (Registrar.loadByClientId(registrar.getId()) == null) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Registrar '%s' not found in the registry", registrar.getId()));
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException | JAXBException e) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Invalid XML file: '%s'", escrowFilePath), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
java/google/registry/rde/imports/RdeImportsModule.java
Normal file
36
java/google/registry/rde/imports/RdeImportsModule.java
Normal file
|
@ -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.rde.imports;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
import google.registry.request.Parameter;
|
||||
import google.registry.request.RequestParameters;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Dagger module for RDE imports package.
|
||||
*
|
||||
* @see "google.registry.module.backend.BackendRequestComponent"
|
||||
*/
|
||||
@Module
|
||||
public final class RdeImportsModule {
|
||||
|
||||
@Provides
|
||||
@Parameter("path")
|
||||
static String providePath(HttpServletRequest req) {
|
||||
return RequestParameters.extractRequiredParameter(req, "path");
|
||||
}
|
||||
}
|
592
java/google/registry/rde/imports/RdeParser.java
Normal file
592
java/google/registry/rde/imports/RdeParser.java
Normal file
|
@ -0,0 +1,592 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import google.registry.xjc.rdecontact.XjcRdeContact;
|
||||
import google.registry.xjc.rdecontact.XjcRdeContactElement;
|
||||
import google.registry.xjc.rdedomain.XjcRdeDomain;
|
||||
import google.registry.xjc.rdedomain.XjcRdeDomainElement;
|
||||
import google.registry.xjc.rdeeppparams.XjcRdeEppParams;
|
||||
import google.registry.xjc.rdeeppparams.XjcRdeEppParamsElement;
|
||||
import google.registry.xjc.rdeheader.XjcRdeHeader;
|
||||
import google.registry.xjc.rdeheader.XjcRdeHeaderCount;
|
||||
import google.registry.xjc.rdeheader.XjcRdeHeaderElement;
|
||||
import google.registry.xjc.rdehost.XjcRdeHost;
|
||||
import google.registry.xjc.rdehost.XjcRdeHostElement;
|
||||
import google.registry.xjc.rdeidn.XjcRdeIdn;
|
||||
import google.registry.xjc.rdeidn.XjcRdeIdnElement;
|
||||
import google.registry.xjc.rdenndn.XjcRdeNndn;
|
||||
import google.registry.xjc.rdenndn.XjcRdeNndnElement;
|
||||
import google.registry.xjc.rderegistrar.XjcRdeRegistrar;
|
||||
import google.registry.xjc.rderegistrar.XjcRdeRegistrarElement;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
/**
|
||||
* RDE escrow deposit file parser
|
||||
*
|
||||
* <p>{@link RdeParser} parses escrow deposit files as a sequence of elements. The parser will first
|
||||
* parse and cache the RDE header before any other elements, so all calls to {@link #getHeader} will
|
||||
* return the header even if the parser has advanced beyond it.
|
||||
*
|
||||
* <p>{@link RdeParser} currently supports parsing the following rde elements as jaxb objects:
|
||||
* <ul>
|
||||
* <li>Contact</li>
|
||||
* <li>Host</li>
|
||||
* <li>Domain</li>
|
||||
* <li>Registrar</li>
|
||||
* <li>Tld</li>
|
||||
* <li>IDN table reference</li>
|
||||
* <li>EPP Params</li>
|
||||
* <li>NNDN</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Any calls to {@link #nextDomain}, {@link #nextHost}, etc. will advance the parser to the next
|
||||
* element in the file, if any additional elements of that type exist. Since the order of these
|
||||
* elements is not known at the time the file is read, client code should only try to parse one type
|
||||
* of element at a time. Parsing of additional element types should be performed by creating a new
|
||||
* parser.
|
||||
*/
|
||||
@NotThreadSafe
|
||||
public class RdeParser implements Closeable {
|
||||
|
||||
private static final String RDE_DOMAIN_URI = "urn:ietf:params:xml:ns:rdeDomain-1.0";
|
||||
private static final String RDE_HOST_URI = "urn:ietf:params:xml:ns:rdeHost-1.0";
|
||||
private static final String RDE_CONTACT_URI = "urn:ietf:params:xml:ns:rdeContact-1.0";
|
||||
private static final String RDE_REGISTRAR_URI = "urn:ietf:params:xml:ns:rdeRegistrar-1.0";
|
||||
private static final String RDE_IDN_URI = "urn:ietf:params:xml:ns:rdeIDN-1.0";
|
||||
private static final String RDE_NNDN_URI = "urn:ietf:params:xml:ns:rdeNNDN-1.0";
|
||||
private static final String RDE_EPP_PARAMS_URI = "urn:ietf:params:xml:ns:rdeEppParams-1.0";
|
||||
private static final String RDE_HEADER_URI = "urn:ietf:params:xml:ns:rdeHeader-1.0";
|
||||
|
||||
/** List of packages to initialize JAXBContext. **/
|
||||
private static final String JAXB_CONTEXT_PACKAGES = Joiner.on(":")
|
||||
.join(Arrays.asList(
|
||||
"google.registry.xjc.contact",
|
||||
"google.registry.xjc.domain",
|
||||
"google.registry.xjc.host",
|
||||
"google.registry.xjc.mark",
|
||||
"google.registry.xjc.rde",
|
||||
"google.registry.xjc.rdecontact",
|
||||
"google.registry.xjc.rdedomain",
|
||||
"google.registry.xjc.rdeeppparams",
|
||||
"google.registry.xjc.rdeheader",
|
||||
"google.registry.xjc.rdeidn",
|
||||
"google.registry.xjc.rdenndn",
|
||||
"google.registry.xjc.rderegistrar",
|
||||
"google.registry.xjc.smd"));
|
||||
|
||||
/**
|
||||
* Convenient immutable java representation of an RDE header
|
||||
*/
|
||||
public static class RdeHeader {
|
||||
|
||||
private final ImmutableMap<String, Long> counts;
|
||||
private final String tld;
|
||||
|
||||
public String getTld() {
|
||||
return tld;
|
||||
}
|
||||
|
||||
public Long getDomainCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_DOMAIN_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getHostCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_HOST_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getContactCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_CONTACT_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getRegistrarCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_REGISTRAR_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getIdnCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_IDN_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getNndnCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_NNDN_URI)).or(0L);
|
||||
}
|
||||
|
||||
public Long getEppParamsCount() {
|
||||
return Optional.fromNullable(counts.get(RDE_EPP_PARAMS_URI)).or(0L);
|
||||
}
|
||||
|
||||
private RdeHeader(XjcRdeHeader header) {
|
||||
this.tld = header.getTld();
|
||||
ImmutableMap.Builder<String, Long> builder = new ImmutableMap.Builder<>();
|
||||
for (XjcRdeHeaderCount count : header.getCounts()) {
|
||||
builder = builder.put(count.getUri(), count.getValue());
|
||||
}
|
||||
counts = builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
private final InputStream xmlInput;
|
||||
private final XMLStreamReader reader;
|
||||
private final Unmarshaller unmarshaller;
|
||||
|
||||
private RdeHeader header;
|
||||
|
||||
/**
|
||||
* Creates a new instance of {@link RdeParser}
|
||||
*
|
||||
* @param xmlInput Contents of the escrow deposit file
|
||||
* @throws JAXBException
|
||||
*/
|
||||
public RdeParser(InputStream xmlInput) throws XMLStreamException, JAXBException {
|
||||
this.xmlInput = xmlInput;
|
||||
this.unmarshaller = JAXBContext.newInstance(JAXB_CONTEXT_PACKAGES).createUnmarshaller();
|
||||
this.reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlInput);
|
||||
this.header = new RdeHeader(readHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to read the RDE header as a jaxb object.
|
||||
*
|
||||
* @throws IllegalStateException if no RDE header is found in the file
|
||||
*/
|
||||
private XjcRdeHeader readHeader() {
|
||||
if (!nextElement(RDE_HEADER_URI, "header")) {
|
||||
throw new IllegalStateException("No RDE Header found");
|
||||
}
|
||||
XjcRdeHeaderElement element = (XjcRdeHeaderElement) unmarshalElement(RDE_HEADER_URI, "header");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unmarshals the current element into a Jaxb element.
|
||||
*
|
||||
* @param uri Element URI
|
||||
* @param name Element Name
|
||||
* @return Jaxb Element
|
||||
* @throws IllegalStateException if the parser is not at the specified element
|
||||
*/
|
||||
private Object unmarshalElement(String uri, String name) {
|
||||
checkArgumentNotNull(name, "name cannot be null");
|
||||
checkArgumentNotNull(uri, "uri cannot be null");
|
||||
try {
|
||||
if (isAtElement(uri, name)) {
|
||||
Object element = unmarshaller.unmarshal(reader);
|
||||
return element;
|
||||
} else {
|
||||
throw new IllegalStateException(String.format("Not at element %s:%s", uri, name));
|
||||
}
|
||||
} catch (IllegalStateException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at an instance of the specified element.
|
||||
*
|
||||
* @param uri Element URI
|
||||
* @param name Element Name
|
||||
* @return true if the parser is at an instance of the element, false otherwise
|
||||
*/
|
||||
private boolean isAtElement(String uri, String name) {
|
||||
return reader.getEventType() == XMLStreamReader.START_ELEMENT
|
||||
&& uri.equals(reader.getNamespaceURI()) && name.equals(reader.getName().getLocalPart());
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to advance to the next instance of the specified element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next instance of the
|
||||
* specified element.
|
||||
*
|
||||
* @param uri Element URI
|
||||
* @param name Element Name
|
||||
* @return true if the parser advanced to the element, false otherwise.
|
||||
*/
|
||||
private boolean nextElement(String uri, String name) {
|
||||
try {
|
||||
while (reader.hasNext()) {
|
||||
reader.next();
|
||||
if (isAtElement(uri, name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public RdeHeader getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to skip over a number of specified elements.
|
||||
*
|
||||
* <p>If the parser is not currently at one of the specified elements, it will advance to the next
|
||||
* instance before skipping any.
|
||||
*
|
||||
* <p>In the process of skipping over a specific type of element, other elements may be skipped as
|
||||
* well. Elements of types other than that specified by the uri and name will not be counted.
|
||||
*
|
||||
* @param numberOfElements Number of elements to skip
|
||||
* @param uri Element URI
|
||||
* @param name Element Name
|
||||
* @return Number of elements that were skipped
|
||||
*/
|
||||
private int skipElements(int numberOfElements, String uri, String name) {
|
||||
checkArgument(
|
||||
numberOfElements >= 0, "number of elements must be greater than or equal to zero");
|
||||
// don't do any skipping if numberOfElements is 0
|
||||
if (numberOfElements == 0) {
|
||||
return 0;
|
||||
}
|
||||
// unless the parser is at one of the specified elements,
|
||||
// the first call to nextElement() will advance to the first
|
||||
// element to be skipped
|
||||
int skipped = 0;
|
||||
if (isAtElement(uri, name)) {
|
||||
skipped = 1;
|
||||
}
|
||||
while (nextElement(uri, name) && skipped < numberOfElements) {
|
||||
skipped++;
|
||||
}
|
||||
return skipped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next contact element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next contact
|
||||
* element.
|
||||
*
|
||||
* @return true if the parser advanced to a contact element, false otherwise
|
||||
*/
|
||||
public boolean nextContact() {
|
||||
return nextElement(RDE_CONTACT_URI, "contact");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a contact element.
|
||||
*
|
||||
* @return true if the parser is at a contact element, false otherwise
|
||||
*/
|
||||
public boolean isAtContact() {
|
||||
return isAtElement(RDE_CONTACT_URI, "contact");
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to skip over a number of contacts.
|
||||
*
|
||||
* <p>If the parser is not currently at a contact element, it will advance to the next instance
|
||||
* before skipping any.
|
||||
*
|
||||
* <p>In the process of skipping over a contact element, other elements may be skipped as well.
|
||||
* Elements of types other than contact elements will not be counted.
|
||||
*
|
||||
* @return Number of contact elements that were skipped
|
||||
*/
|
||||
public int skipContacts(int numberOfContacts) {
|
||||
return skipElements(numberOfContacts, RDE_CONTACT_URI, "contact");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current contact.
|
||||
*
|
||||
* <p>The parser must be at a contact element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtContact} or
|
||||
* {@link #nextContact} to determine if the parser is at a contact element.
|
||||
*
|
||||
* @return Jaxb Contact
|
||||
* @throws IllegalStateException if the parser is not at a contact element
|
||||
*/
|
||||
public XjcRdeContact getContact() {
|
||||
XjcRdeContactElement element =
|
||||
(XjcRdeContactElement) unmarshalElement(RDE_CONTACT_URI, "contact");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next domain element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next domain element.
|
||||
*
|
||||
* @return true if the parser advanced to a domain element, false otherwise
|
||||
*/
|
||||
public boolean nextDomain() {
|
||||
return nextElement(RDE_DOMAIN_URI, "domain");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a domain element.
|
||||
*
|
||||
* @return true if the parser is at a domain element, false otherwise
|
||||
*/
|
||||
public boolean isAtDomain() {
|
||||
return isAtElement(RDE_DOMAIN_URI, "domain");
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to skip over a number of domains.
|
||||
*
|
||||
* <p>If the parser is not currently at a domain element, it will advance to the next instance
|
||||
* before skipping any.
|
||||
*
|
||||
* <p>In the process of skipping over a domain element, other elements may be skipped as well.
|
||||
* Elements of types other than domain elements will not be counted.
|
||||
*
|
||||
* @return Number of domain elements that were skipped
|
||||
*/
|
||||
public int skipDomains(int numberOfDomains) {
|
||||
return skipElements(numberOfDomains, RDE_DOMAIN_URI, "domain");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current domain.
|
||||
*
|
||||
* <p>The parser must be at a domain element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtDomain} or
|
||||
* {@link #nextDomain} to determine if the parser is at a domain element.
|
||||
*
|
||||
* @return Jaxb Domain
|
||||
* @throws IllegalStateException if the parser is not at a domain element
|
||||
*/
|
||||
public XjcRdeDomain getDomain() {
|
||||
XjcRdeDomainElement element = (XjcRdeDomainElement) unmarshalElement(RDE_DOMAIN_URI, "domain");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next host element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next host element.
|
||||
*
|
||||
* @return true if the parser advanced to a host element, false otherwise
|
||||
*/
|
||||
public boolean nextHost() {
|
||||
return nextElement(RDE_HOST_URI, "host");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a host element.
|
||||
*
|
||||
* @return true if the parser is at a host element, false otherwise
|
||||
*/
|
||||
public boolean isAtHost() {
|
||||
return isAtElement(RDE_HOST_URI, "host");
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to skip over a number of hosts.
|
||||
*
|
||||
* <p>If the parser is not currently at a host element, it will advance to the next instance
|
||||
* before skipping any.
|
||||
*
|
||||
* <p>In the process of skipping over a host element, other elements may be skipped as well.
|
||||
* Elements of types other than host elements will not be counted.
|
||||
*
|
||||
* @return Number of host elements that were skipped
|
||||
*/
|
||||
public int skipHosts(int numberOfHosts) {
|
||||
return skipElements(numberOfHosts, RDE_HOST_URI, "host");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current host.
|
||||
*
|
||||
* <p>The parser must be at a host element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtHost} or
|
||||
* {@link #nextHost} to determine if the parser is at a host element.
|
||||
*
|
||||
* @return Jaxb Host
|
||||
* @throws IllegalStateException if the parser is not at a host element
|
||||
*/
|
||||
public XjcRdeHost getHost() {
|
||||
XjcRdeHostElement element = (XjcRdeHostElement) unmarshalElement(RDE_HOST_URI, "host");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next registrar element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next registrar
|
||||
* element.
|
||||
*
|
||||
* @return true if the parser advanced to a registrar element, false otherwise
|
||||
*/
|
||||
public boolean nextRegistrar() {
|
||||
return nextElement(RDE_REGISTRAR_URI, "registrar");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a registrar element.
|
||||
*
|
||||
* @return true if the parser is at a registrar element, false otherwise
|
||||
*/
|
||||
public boolean isAtRegistrar() {
|
||||
return isAtElement(RDE_REGISTRAR_URI, "registrar");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current registrar.
|
||||
*
|
||||
* <p>The parser must be at a registrar element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtRegistrar}
|
||||
* or {@link #nextRegistrar} to determine if the parser is at a registrar element.
|
||||
*
|
||||
* @return Jaxb Registrar
|
||||
* @throws IllegalStateException if the parser is not at a registrar element
|
||||
*/
|
||||
public XjcRdeRegistrar getRegistrar() {
|
||||
XjcRdeRegistrarElement element =
|
||||
(XjcRdeRegistrarElement) unmarshalElement(RDE_REGISTRAR_URI, "registrar");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next IDN element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next IDN element.
|
||||
*
|
||||
* @return true if the parser advanced to a IDN element, false otherwise
|
||||
*/
|
||||
public boolean nextIdn() {
|
||||
return nextElement(RDE_IDN_URI, "idnTableRef");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a IDN element.
|
||||
*
|
||||
* @return true if the parser is at a IDN element, false otherwise
|
||||
*/
|
||||
public boolean isAtIdn() {
|
||||
return isAtElement(RDE_IDN_URI, "idnTableRef");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current IDN.
|
||||
*
|
||||
* <p>The parser must be at a IDN element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtIdn} or
|
||||
* {@link #nextIdn} to determine if the parser is at a IDN element.
|
||||
*
|
||||
* @return Jaxb IDN
|
||||
* @throws IllegalStateException if the parser is not at a IDN element
|
||||
*/
|
||||
public XjcRdeIdn getIdn() {
|
||||
XjcRdeIdnElement element = (XjcRdeIdnElement) unmarshalElement(RDE_IDN_URI, "idnTableRef");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next NNDN element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next NNDN element.
|
||||
*
|
||||
* @return true if the parser advanced to a NNDN element, false otherwise
|
||||
*/
|
||||
public boolean nextNndn() {
|
||||
return nextElement(RDE_NNDN_URI, "NNDN");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a NNDN element.
|
||||
*
|
||||
* @return true if the parser is at a NNDN element, false otherwise
|
||||
*/
|
||||
public boolean isAtNndn() {
|
||||
return isAtElement(RDE_NNDN_URI, "NNDN");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current NNDN.
|
||||
*
|
||||
* <p>The parser must be at a NNDN element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtNndn} or
|
||||
* {@link #nextNndn} to determine if the parser is at a NNDN element.
|
||||
*
|
||||
* @return Jaxb NNDN
|
||||
* @throws IllegalStateException if the parser is not at a NNDN element
|
||||
*/
|
||||
public XjcRdeNndn getNndn() {
|
||||
XjcRdeNndnElement element = (XjcRdeNndnElement) unmarshalElement(RDE_NNDN_URI, "NNDN");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances parser to the next eppParams element.
|
||||
*
|
||||
* <p>The parser may skip over other types of elements while advancing to the next eppParams
|
||||
* element.
|
||||
*
|
||||
* @return true if the parser advanced to a eppParams element, false otherwise
|
||||
*/
|
||||
public boolean nextEppParams() {
|
||||
return nextElement(RDE_EPP_PARAMS_URI, "eppParams");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the parser is at a eppParams element.
|
||||
*
|
||||
* @return true if the parser is at a eppParams element, false otherwise
|
||||
*/
|
||||
public boolean isAtEppParams() {
|
||||
return isAtElement(RDE_EPP_PARAMS_URI, "eppParams");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current eppParams.
|
||||
*
|
||||
* <p>The parser must be at a eppParams element before this method is called, or an
|
||||
* {@link IllegalStateException} will be thrown. Check the return value of {@link #isAtEppParams}
|
||||
* or {@link #nextEppParams} to determine if the parser is at a eppParams element.
|
||||
*
|
||||
* @return Jaxb EppParams
|
||||
* @throws IllegalStateException if the parser is not at a eppParams element
|
||||
*/
|
||||
public XjcRdeEppParams getEppParams() {
|
||||
XjcRdeEppParamsElement element =
|
||||
(XjcRdeEppParamsElement) unmarshalElement(RDE_EPP_PARAMS_URI, "eppParams");
|
||||
return element.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the underlying InputStream
|
||||
*
|
||||
* @throws IOException if the underlying stream throws {@link IOException} on close.
|
||||
*/
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
xmlInput.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,180 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import google.registry.model.contact.ContactAddress;
|
||||
import google.registry.model.contact.ContactPhoneNumber;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.model.contact.Disclose;
|
||||
import google.registry.model.contact.Disclose.PostalInfoChoice;
|
||||
import google.registry.model.contact.PostalInfo;
|
||||
import google.registry.model.eppcommon.StatusValue;
|
||||
import google.registry.model.transfer.TransferData;
|
||||
import google.registry.model.transfer.TransferStatus;
|
||||
import google.registry.util.XmlToEnumMapper;
|
||||
import google.registry.xjc.contact.XjcContactAddrType;
|
||||
import google.registry.xjc.contact.XjcContactDiscloseType;
|
||||
import google.registry.xjc.contact.XjcContactE164Type;
|
||||
import google.registry.xjc.contact.XjcContactIntLocType;
|
||||
import google.registry.xjc.contact.XjcContactPostalInfoEnumType;
|
||||
import google.registry.xjc.contact.XjcContactPostalInfoType;
|
||||
import google.registry.xjc.contact.XjcContactStatusType;
|
||||
import google.registry.xjc.rdecontact.XjcRdeContact;
|
||||
import google.registry.xjc.rdecontact.XjcRdeContactTransferDataType;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/** Utility class that converts an {@link XjcRdeContact} into a {@link ContactResource}. */
|
||||
final class XjcToContactResourceConverter {
|
||||
|
||||
private static final XmlToEnumMapper<PostalInfo.Type> POSTAL_INFO_TYPE_MAPPER =
|
||||
XmlToEnumMapper.create(PostalInfo.Type.values());
|
||||
private static final XmlToEnumMapper<TransferStatus> TRANSFER_STATUS_MAPPER =
|
||||
XmlToEnumMapper.create(TransferStatus.values());
|
||||
|
||||
private static final Function<XjcContactIntLocType, PostalInfoChoice> choiceConverter =
|
||||
new Function<XjcContactIntLocType, PostalInfoChoice>() {
|
||||
@Override
|
||||
public PostalInfoChoice apply(XjcContactIntLocType choice) {
|
||||
return convertPostalInfoChoice(choice);
|
||||
}
|
||||
};
|
||||
|
||||
private static final Function<XjcContactStatusType, StatusValue> STATUS_CONVERTER =
|
||||
new Function<XjcContactStatusType, StatusValue>() {
|
||||
@Override
|
||||
public StatusValue apply(XjcContactStatusType status) {
|
||||
return convertStatusValue(status);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/** Converts {@link XjcRdeContact} to {@link ContactResource}. */
|
||||
static ContactResource convertContact(XjcRdeContact contact) {
|
||||
return new ContactResource.Builder()
|
||||
.setRepoId(contact.getRoid())
|
||||
.setStatusValues(
|
||||
ImmutableSet.copyOf(Iterables.transform(contact.getStatuses(), STATUS_CONVERTER)))
|
||||
.setLocalizedPostalInfo(
|
||||
getPostalInfoOfType(contact.getPostalInfos(), XjcContactPostalInfoEnumType.LOC))
|
||||
.setInternationalizedPostalInfo(
|
||||
getPostalInfoOfType(contact.getPostalInfos(), XjcContactPostalInfoEnumType.INT))
|
||||
.setContactId(contact.getId())
|
||||
.setCurrentSponsorClientId(contact.getClID())
|
||||
.setCreationClientId(contact.getCrRr() == null ? null : contact.getCrRr().getValue())
|
||||
.setLastEppUpdateClientId(contact.getUpRr() == null ? null : contact.getUpRr().getValue())
|
||||
.setCreationTime(contact.getCrDate())
|
||||
.setLastEppUpdateTime(contact.getUpDate())
|
||||
.setLastTransferTime(contact.getTrDate())
|
||||
.setVoiceNumber(convertPhoneNumber(contact.getVoice()))
|
||||
.setFaxNumber(convertPhoneNumber(contact.getFax()))
|
||||
.setEmailAddress(contact.getEmail())
|
||||
.setDisclose(convertDisclose(contact.getDisclose()))
|
||||
.setTransferData(convertTransferData(contact.getTrnData()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a {@link PostalInfo} from an {@link Iterable} of {@link XjcContactPostalInfoEnumType}.
|
||||
*/
|
||||
@Nullable
|
||||
private static PostalInfo getPostalInfoOfType(
|
||||
Iterable<XjcContactPostalInfoType> postalInfos, XjcContactPostalInfoEnumType type) {
|
||||
for (XjcContactPostalInfoType postalInfo : postalInfos) {
|
||||
if (postalInfo.getType() == type) {
|
||||
return convertPostalInfo(postalInfo);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Converts {@link XjcRdeContactTransferDataType} to {@link TransferData}. */
|
||||
private static TransferData convertTransferData(
|
||||
@Nullable XjcRdeContactTransferDataType transferData) {
|
||||
if (transferData == null) {
|
||||
return TransferData.EMPTY;
|
||||
}
|
||||
return new TransferData.Builder()
|
||||
.setTransferStatus(TRANSFER_STATUS_MAPPER.xmlToEnum(transferData.getTrStatus().value()))
|
||||
.setGainingClientId(transferData.getReRr().getValue())
|
||||
.setLosingClientId(transferData.getAcRr().getValue())
|
||||
.setTransferRequestTime(transferData.getReDate())
|
||||
.setPendingTransferExpirationTime(transferData.getAcDate())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcContactAddrType} to {@link ContactAddress}. */
|
||||
private static ContactAddress convertAddress(XjcContactAddrType address) {
|
||||
return new ContactAddress.Builder()
|
||||
.setStreet(ImmutableList.copyOf(address.getStreets()))
|
||||
.setCity(address.getCity())
|
||||
.setState(address.getSp())
|
||||
.setZip(address.getPc())
|
||||
.setCountryCode(address.getCc())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcContactDiscloseType} to {@link Disclose}. */
|
||||
@Nullable
|
||||
private static Disclose convertDisclose(@Nullable XjcContactDiscloseType disclose) {
|
||||
if (disclose == null) {
|
||||
return null;
|
||||
}
|
||||
return new Disclose.Builder()
|
||||
.setFlag(disclose.isFlag())
|
||||
.setNames(ImmutableList.copyOf(Lists.transform(disclose.getNames(), choiceConverter)))
|
||||
.setOrgs(ImmutableList.copyOf(Lists.transform(disclose.getOrgs(), choiceConverter)))
|
||||
.setAddrs(ImmutableList.copyOf(Lists.transform(disclose.getAddrs(), choiceConverter)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcContactE164Type} to {@link ContactPhoneNumber}. */
|
||||
@Nullable
|
||||
private static ContactPhoneNumber convertPhoneNumber(@Nullable XjcContactE164Type phoneNumber) {
|
||||
if (phoneNumber == null) {
|
||||
return null;
|
||||
}
|
||||
return new ContactPhoneNumber.Builder()
|
||||
.setPhoneNumber(phoneNumber.getValue())
|
||||
.setExtension(phoneNumber.getX())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link PostalInfoChoice} to {@link XjcContactIntLocType}. */
|
||||
private static PostalInfoChoice convertPostalInfoChoice(XjcContactIntLocType choice) {
|
||||
return PostalInfoChoice.create(POSTAL_INFO_TYPE_MAPPER.xmlToEnum(choice.getType().value()));
|
||||
}
|
||||
|
||||
/** Converts {@link XjcContactPostalInfoType} to {@link PostalInfo}. */
|
||||
private static PostalInfo convertPostalInfo(XjcContactPostalInfoType postalInfo) {
|
||||
return new PostalInfo.Builder()
|
||||
.setName(postalInfo.getName())
|
||||
.setOrg(postalInfo.getOrg())
|
||||
.setAddress(convertAddress(postalInfo.getAddr()))
|
||||
.setType(POSTAL_INFO_TYPE_MAPPER.xmlToEnum(postalInfo.getType().value()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcContactStatusType} to {@link StatusValue}. */
|
||||
private static StatusValue convertStatusValue(XjcContactStatusType statusType) {
|
||||
return StatusValue.fromXmlName(statusType.getS().value());
|
||||
}
|
||||
|
||||
private XjcToContactResourceConverter() {}
|
||||
}
|
|
@ -0,0 +1,307 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static com.google.common.collect.Iterables.transform;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
import static google.registry.util.DateTimeUtils.END_OF_TIME;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
|
||||
|
||||
import com.google.common.base.Ascii;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.net.InternetDomainName;
|
||||
import com.googlecode.objectify.Key;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.billing.BillingEvent;
|
||||
import google.registry.model.billing.BillingEvent.Flag;
|
||||
import google.registry.model.billing.BillingEvent.Reason;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.model.domain.DesignatedContact;
|
||||
import google.registry.model.domain.DomainResource;
|
||||
import google.registry.model.domain.GracePeriod;
|
||||
import google.registry.model.domain.rgp.GracePeriodStatus;
|
||||
import google.registry.model.domain.secdns.DelegationSignerData;
|
||||
import google.registry.model.eppcommon.StatusValue;
|
||||
import google.registry.model.eppcommon.Trid;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.model.index.ForeignKeyIndex;
|
||||
import google.registry.model.poll.PollMessage;
|
||||
import google.registry.model.registry.Registries;
|
||||
import google.registry.model.registry.Registry;
|
||||
import google.registry.model.reporting.HistoryEntry;
|
||||
import google.registry.model.transfer.TransferData;
|
||||
import google.registry.model.transfer.TransferStatus;
|
||||
import google.registry.util.XmlToEnumMapper;
|
||||
import google.registry.xjc.domain.XjcDomainContactType;
|
||||
import google.registry.xjc.domain.XjcDomainNsType;
|
||||
import google.registry.xjc.domain.XjcDomainStatusType;
|
||||
import google.registry.xjc.rdedomain.XjcRdeDomain;
|
||||
import google.registry.xjc.rdedomain.XjcRdeDomainElement;
|
||||
import google.registry.xjc.rdedomain.XjcRdeDomainTransferDataType;
|
||||
import google.registry.xjc.rgp.XjcRgpStatusType;
|
||||
import google.registry.xjc.secdns.XjcSecdnsDsDataType;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/** Utility class that converts an {@link XjcRdeDomainElement} into a {@link DomainResource}. */
|
||||
final class XjcToDomainResourceConverter extends XjcToEppResourceConverter {
|
||||
|
||||
private static final XmlToEnumMapper<TransferStatus> TRANSFER_STATUS_MAPPER =
|
||||
XmlToEnumMapper.create(TransferStatus.values());
|
||||
|
||||
private static final Function<XjcDomainStatusType, StatusValue> STATUS_CONVERTER =
|
||||
new Function<XjcDomainStatusType, StatusValue>() {
|
||||
@Override
|
||||
public StatusValue apply(XjcDomainStatusType status) {
|
||||
return convertStatusType(status);
|
||||
}
|
||||
};
|
||||
|
||||
private static final Function<String, Key<HostResource>> HOST_OBJ_CONVERTER =
|
||||
new Function<String, Key<HostResource>>() {
|
||||
@Override
|
||||
public Key<HostResource> apply(String fullyQualifiedHostName) {
|
||||
Key<HostResource> key =
|
||||
ForeignKeyIndex.loadAndGetKey(
|
||||
HostResource.class, fullyQualifiedHostName, DateTime.now());
|
||||
checkState(
|
||||
key != null,
|
||||
String.format("HostResource not found with name '%s'", fullyQualifiedHostName));
|
||||
return key;
|
||||
}
|
||||
};
|
||||
|
||||
private static final Function<XjcDomainContactType, DesignatedContact> CONTACT_CONVERTER =
|
||||
new Function<XjcDomainContactType, DesignatedContact>() {
|
||||
@Override
|
||||
public DesignatedContact apply(XjcDomainContactType contact) {
|
||||
return convertContactType(contact);
|
||||
}
|
||||
};
|
||||
|
||||
private static final Function<XjcSecdnsDsDataType, DelegationSignerData> SECDNS_CONVERTER =
|
||||
new Function<XjcSecdnsDsDataType, DelegationSignerData>() {
|
||||
@Override
|
||||
public DelegationSignerData apply(XjcSecdnsDsDataType secdns) {
|
||||
return convertSecdnsDsDataType(secdns);
|
||||
}
|
||||
};
|
||||
|
||||
/** Converts {@link XjcRgpStatusType} to {@link GracePeriod} */
|
||||
private static class GracePeriodConverter implements Function<XjcRgpStatusType, GracePeriod> {
|
||||
|
||||
private final XjcRdeDomain domain;
|
||||
private final Key<BillingEvent.Recurring> autoRenewBillingEvent;
|
||||
private final Registry tld;
|
||||
|
||||
GracePeriodConverter(XjcRdeDomain domain, Key<BillingEvent.Recurring> autoRenewBillingEvent) {
|
||||
this.domain = domain;
|
||||
this.autoRenewBillingEvent = autoRenewBillingEvent;
|
||||
this.tld =
|
||||
Registry.get(
|
||||
Registries.findTldForNameOrThrow(InternetDomainName.from(domain.getName()))
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public GracePeriod apply(XjcRgpStatusType gracePeriodStatus) {
|
||||
// TODO: (wolfgang) address these items in code review:
|
||||
// verify that this logic is correct
|
||||
switch (gracePeriodStatus.getS()) {
|
||||
case ADD_PERIOD:
|
||||
return GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.ADD,
|
||||
domain.getCrDate().plus(this.tld.getAddGracePeriodLength()),
|
||||
domain.getCrRr().getClient());
|
||||
case AUTO_RENEW_PERIOD:
|
||||
return GracePeriod.createForRecurring(
|
||||
GracePeriodStatus.AUTO_RENEW,
|
||||
domain.getUpDate().plus(this.tld.getAutoRenewGracePeriodLength()),
|
||||
domain.getClID(),
|
||||
autoRenewBillingEvent);
|
||||
case PENDING_DELETE:
|
||||
return GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.PENDING_DELETE,
|
||||
domain.getUpDate().plus(this.tld.getPendingDeleteLength()),
|
||||
domain.getClID());
|
||||
case REDEMPTION_PERIOD:
|
||||
return GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.REDEMPTION,
|
||||
domain.getUpDate().plus(this.tld.getRedemptionGracePeriodLength()),
|
||||
domain.getClID());
|
||||
case RENEW_PERIOD:
|
||||
return GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.RENEW,
|
||||
domain.getUpDate().plus(this.tld.getRenewGracePeriodLength()),
|
||||
domain.getClID());
|
||||
case TRANSFER_PERIOD:
|
||||
return GracePeriod.createWithoutBillingEvent(
|
||||
GracePeriodStatus.TRANSFER,
|
||||
domain.getUpDate().plus(this.tld.getTransferGracePeriodLength()),
|
||||
domain.getTrnData().getReRr().getValue());
|
||||
default:
|
||||
throw new IllegalArgumentException(
|
||||
"Unsupported grace period status: " + gracePeriodStatus.getS());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Converts {@link XjcRdeDomain} to {@link DomainResource}. */
|
||||
static DomainResource convertDomain(XjcRdeDomain domain) {
|
||||
// First create history entry and autorenew billing event/poll message
|
||||
// Autorenew billing event is required for creating AUTO_RENEW grace period
|
||||
HistoryEntry historyEntry = createHistoryEntry(domain);
|
||||
BillingEvent.Recurring autoRenewBillingEvent =
|
||||
createAutoRenewBillingEvent(domain, historyEntry);
|
||||
PollMessage.Autorenew pollMessage = createAutoRenewPollMessage(domain, historyEntry);
|
||||
ofy().save().<ImmutableObject>entities(historyEntry, autoRenewBillingEvent, pollMessage);
|
||||
GracePeriodConverter gracePeriodConverter =
|
||||
new GracePeriodConverter(domain, Key.create(autoRenewBillingEvent));
|
||||
DomainResource.Builder builder =
|
||||
new DomainResource.Builder()
|
||||
.setFullyQualifiedDomainName(domain.getName())
|
||||
.setRepoId(domain.getRoid())
|
||||
.setIdnTableName(domain.getIdnTableId())
|
||||
.setCurrentSponsorClientId(domain.getClID())
|
||||
.setCreationClientId(domain.getCrRr().getValue())
|
||||
.setCreationTime(domain.getCrDate())
|
||||
.setRegistrationExpirationTime(domain.getExDate())
|
||||
.setLastEppUpdateTime(domain.getUpDate())
|
||||
.setLastEppUpdateClientId(domain.getUpRr() == null ? null : domain.getUpRr().getValue())
|
||||
.setLastTransferTime(domain.getTrDate())
|
||||
.setStatusValues(ImmutableSet.copyOf(transform(domain.getStatuses(), STATUS_CONVERTER)))
|
||||
.setNameservers(convertNameservers(domain.getNs()))
|
||||
.setGracePeriods(
|
||||
ImmutableSet.copyOf(transform(domain.getRgpStatuses(), gracePeriodConverter)))
|
||||
.setContacts(ImmutableSet.copyOf(transform(domain.getContacts(), CONTACT_CONVERTER)))
|
||||
.setDsData(
|
||||
domain.getSecDNS() == null
|
||||
? ImmutableSet.<DelegationSignerData>of()
|
||||
: ImmutableSet.copyOf(
|
||||
transform(domain.getSecDNS().getDsDatas(), SECDNS_CONVERTER)))
|
||||
.setTransferData(convertDomainTransferData(domain.getTrnData()));
|
||||
checkArgumentNotNull(
|
||||
domain.getRegistrant(), "Registrant is missing for domain '%s'", domain.getName());
|
||||
builder = builder.setRegistrant(convertRegistrant(domain.getRegistrant()));
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private static BillingEvent.Recurring createAutoRenewBillingEvent(
|
||||
XjcRdeDomain domain, HistoryEntry historyEntry) {
|
||||
final BillingEvent.Recurring billingEvent =
|
||||
new BillingEvent.Recurring.Builder()
|
||||
.setReason(Reason.RENEW)
|
||||
.setFlags(ImmutableSet.of(Flag.AUTO_RENEW))
|
||||
.setTargetId(domain.getRoid())
|
||||
.setClientId(domain.getClID())
|
||||
.setEventTime(domain.getExDate())
|
||||
.setRecurrenceEndTime(END_OF_TIME)
|
||||
.setParent(historyEntry)
|
||||
.build();
|
||||
return billingEvent;
|
||||
}
|
||||
|
||||
private static PollMessage.Autorenew createAutoRenewPollMessage(
|
||||
XjcRdeDomain domain, HistoryEntry historyEntry) {
|
||||
final PollMessage.Autorenew pollMessage =
|
||||
new PollMessage.Autorenew.Builder()
|
||||
.setTargetId(domain.getRoid())
|
||||
.setClientId(domain.getClID())
|
||||
.setEventTime(domain.getExDate())
|
||||
.setMsg("Domain was auto-renewed.")
|
||||
.setParent(historyEntry)
|
||||
.build();
|
||||
return pollMessage;
|
||||
}
|
||||
|
||||
private static HistoryEntry createHistoryEntry(XjcRdeDomain domain) {
|
||||
XjcRdeDomainElement element = new XjcRdeDomainElement(domain);
|
||||
final HistoryEntry historyEntry =
|
||||
new HistoryEntry.Builder()
|
||||
.setType(HistoryEntry.Type.RDE_IMPORT)
|
||||
.setClientId(domain.getClID())
|
||||
.setTrid(Trid.create(null))
|
||||
.setModificationTime(DateTime.now())
|
||||
.setXmlBytes(getObjectXml(element))
|
||||
.setBySuperuser(true)
|
||||
.setReason("RDE Import")
|
||||
.setRequestedByRegistrar(false)
|
||||
.setParent(Key.create(null, DomainResource.class, domain.getRoid()))
|
||||
.build();
|
||||
return historyEntry;
|
||||
}
|
||||
|
||||
/** Returns {@link Key} for registrant from foreign key */
|
||||
private static Key<ContactResource> convertRegistrant(String contactId) {
|
||||
Key<ContactResource> key =
|
||||
ForeignKeyIndex.loadAndGetKey(ContactResource.class, contactId, DateTime.now());
|
||||
checkState(key != null, "Registrant not found: '%s'", contactId);
|
||||
return key;
|
||||
}
|
||||
|
||||
/** Converts {@link XjcDomainNsType} to <code>ImmutableSet<Key<HostResource>></code>. */
|
||||
private static ImmutableSet<Key<HostResource>> convertNameservers(XjcDomainNsType ns) {
|
||||
// If no hosts are specified, return an empty set
|
||||
if (ns == null || (ns.getHostAttrs() == null && ns.getHostObjs() == null)) {
|
||||
return ImmutableSet.of();
|
||||
}
|
||||
// Domain linked hosts must be specified by host object, not host attributes.
|
||||
checkArgument(
|
||||
ns.getHostAttrs() == null || ns.getHostAttrs().isEmpty(),
|
||||
"Host attributes are not yet supported");
|
||||
return ImmutableSet.copyOf(Iterables.transform(ns.getHostObjs(), HOST_OBJ_CONVERTER));
|
||||
}
|
||||
|
||||
/** Converts {@link XjcRdeDomainTransferDataType} to {@link TransferData}. */
|
||||
private static TransferData convertDomainTransferData(XjcRdeDomainTransferDataType data) {
|
||||
if (data == null) {
|
||||
return TransferData.EMPTY;
|
||||
}
|
||||
return new TransferData.Builder()
|
||||
.setTransferStatus(TRANSFER_STATUS_MAPPER.xmlToEnum(data.getTrStatus().value()))
|
||||
.setGainingClientId(data.getReRr().getValue())
|
||||
.setLosingClientId(data.getAcRr().getValue())
|
||||
.setTransferRequestTime(data.getReDate())
|
||||
.setPendingTransferExpirationTime(data.getAcDate())
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcDomainStatusType} to {@link StatusValue}. */
|
||||
private static StatusValue convertStatusType(XjcDomainStatusType type) {
|
||||
return StatusValue.fromXmlName(type.getS().value());
|
||||
}
|
||||
|
||||
/** Converts {@link XjcSecdnsDsDataType} to {@link DelegationSignerData}. */
|
||||
private static DelegationSignerData convertSecdnsDsDataType(XjcSecdnsDsDataType secdns) {
|
||||
return DelegationSignerData.create(
|
||||
secdns.getKeyTag(), secdns.getAlg(), secdns.getDigestType(), secdns.getDigest());
|
||||
}
|
||||
|
||||
/** Converts {@link XjcDomainContactType} to {@link DesignatedContact}. */
|
||||
private static DesignatedContact convertContactType(XjcDomainContactType contact) {
|
||||
String contactId = contact.getValue();
|
||||
Key<ContactResource> key =
|
||||
ForeignKeyIndex.loadAndGetKey(ContactResource.class, contactId, DateTime.now());
|
||||
checkState(key != null, "Contact not found: '%s'", contactId);
|
||||
DesignatedContact.Type type =
|
||||
DesignatedContact.Type.valueOf(Ascii.toUpperCase(contact.getType().toString()));
|
||||
return DesignatedContact.create(type, key);
|
||||
}
|
||||
|
||||
private XjcToDomainResourceConverter() {}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import google.registry.model.EppResource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
|
||||
/**
|
||||
* Base class for Jaxb object to {@link EppResource} converters
|
||||
*/
|
||||
public abstract class XjcToEppResourceConverter {
|
||||
|
||||
/** List of packages to initialize JAXBContext. **/
|
||||
private static final String JAXB_CONTEXT_PACKAGES = Joiner.on(":")
|
||||
.join(Arrays.asList(
|
||||
"google.registry.xjc.contact",
|
||||
"google.registry.xjc.domain",
|
||||
"google.registry.xjc.host",
|
||||
"google.registry.xjc.mark",
|
||||
"google.registry.xjc.rde",
|
||||
"google.registry.xjc.rdecontact",
|
||||
"google.registry.xjc.rdedomain",
|
||||
"google.registry.xjc.rdeeppparams",
|
||||
"google.registry.xjc.rdeheader",
|
||||
"google.registry.xjc.rdeidn",
|
||||
"google.registry.xjc.rdenndn",
|
||||
"google.registry.xjc.rderegistrar",
|
||||
"google.registry.xjc.smd"));
|
||||
|
||||
/** Creates a {@link Marshaller} for serializing Jaxb objects */
|
||||
private static Marshaller createMarshaller() throws JAXBException {
|
||||
return JAXBContext.newInstance(JAXB_CONTEXT_PACKAGES).createMarshaller();
|
||||
}
|
||||
|
||||
protected static byte[] getObjectXml(JAXBElement<?> jaxbElement) {
|
||||
try {
|
||||
Marshaller marshaller = createMarshaller();
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
marshaller.marshal(jaxbElement, bout);
|
||||
return bout.toByteArray();
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
// 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.rde.imports;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import google.registry.model.eppcommon.StatusValue;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.xjc.host.XjcHostAddrType;
|
||||
import google.registry.xjc.host.XjcHostStatusType;
|
||||
import google.registry.xjc.rdehost.XjcRdeHost;
|
||||
import java.net.InetAddress;
|
||||
|
||||
/** Utility class that converts an {@link XjcRdeHost} into a {@link HostResource}. */
|
||||
public class XjcToHostResourceConverter {
|
||||
|
||||
private static final Function<XjcHostStatusType, StatusValue> STATUS_VALUE_CONVERTER =
|
||||
new Function<XjcHostStatusType, StatusValue>() {
|
||||
@Override
|
||||
public StatusValue apply(XjcHostStatusType status) {
|
||||
return convertStatusType(status);
|
||||
}
|
||||
};
|
||||
|
||||
private static final Function<XjcHostAddrType, InetAddress> ADDR_CONVERTER =
|
||||
new Function<XjcHostAddrType, InetAddress>() {
|
||||
@Override
|
||||
public InetAddress apply(XjcHostAddrType addr) {
|
||||
return convertAddrType(addr);
|
||||
}
|
||||
};
|
||||
|
||||
static HostResource convert(XjcRdeHost host) {
|
||||
return new HostResource.Builder()
|
||||
.setFullyQualifiedHostName(host.getName())
|
||||
.setRepoId(host.getRoid())
|
||||
.setCurrentSponsorClientId(host.getClID())
|
||||
.setLastTransferTime(host.getTrDate())
|
||||
.setCreationTime(host.getCrDate())
|
||||
.setLastEppUpdateTime(host.getUpDate())
|
||||
.setCreationClientId(host.getCrRr().getValue())
|
||||
.setLastEppUpdateClientId(host.getUpRr() == null ? null : host.getUpRr().getValue())
|
||||
.setStatusValues(
|
||||
ImmutableSet.copyOf(Lists.transform(host.getStatuses(), STATUS_VALUE_CONVERTER)))
|
||||
.setInetAddresses(ImmutableSet.copyOf(Lists.transform(host.getAddrs(), ADDR_CONVERTER)))
|
||||
.build();
|
||||
}
|
||||
|
||||
/** Converts {@link XjcHostStatusType} to {@link StatusValue}. */
|
||||
private static StatusValue convertStatusType(XjcHostStatusType status) {
|
||||
return StatusValue.fromXmlName(status.getS().value());
|
||||
}
|
||||
|
||||
/** Converts {@link XjcHostAddrType} to {@link InetAddress}. */
|
||||
private static InetAddress convertAddrType(XjcHostAddrType addr) {
|
||||
return InetAddresses.forString(addr.getValue());
|
||||
}
|
||||
|
||||
private XjcToHostResourceConverter() {}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue