Refactor out creation of server TRIDs so they can be tested

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=152837185
This commit is contained in:
mcilwain 2017-04-11 11:28:11 -07:00 committed by Ben McIlwain
parent af942774f4
commit 8653d2f204
29 changed files with 174 additions and 62 deletions

View file

@ -50,6 +50,7 @@ public final class EppController {
@Inject EppMetric.Builder eppMetricBuilder;
@Inject EppMetrics eppMetrics;
@Inject BigQueryMetricsEnqueuer bigQueryMetricsEnqueuer;
@Inject ServerTridProvider serverTridProvider;
@Inject EppController() {}
/** Reads EPP XML, executes the matching flow, and returns an {@link EppOutput}. */
@ -83,7 +84,8 @@ public final class EppController {
Strings.repeat("=", 40));
// Return early by sending an error message, with no clTRID since we couldn't unmarshal it.
eppMetricBuilder.setStatus(e.getResult().getCode());
return getErrorResponse(e.getResult(), Trid.create(null));
return getErrorResponse(
e.getResult(), Trid.create(null, serverTridProvider.createServerTrid()));
}
if (!eppInput.getTargetIds().isEmpty()) {
eppMetricBuilder.setEppTarget(Joiner.on(',').join(eppInput.getTargetIds()));

View file

@ -152,6 +152,13 @@ public class FlowModule {
return credentials;
}
@Provides
@FlowScope
Trid provideTrid(EppInput eppInput, ServerTridProvider serverTridProvider) {
return Trid.create(
eppInput.getCommandWrapper().getClTrid(), serverTridProvider.createServerTrid());
}
@Provides
@FlowScope
@ClientId
@ -161,12 +168,6 @@ public class FlowModule {
return Strings.nullToEmpty(sessionMetadata.getClientId());
}
@Provides
@FlowScope
static Trid provideTrid(EppInput eppInput) {
return Trid.create(eppInput.getCommandWrapper().getClTrid());
}
@Provides
@FlowScope
static Class<? extends Flow> provideFlowClass(EppInput eppInput) {

View file

@ -0,0 +1,28 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.flows;
import google.registry.model.eppcommon.Trid;
/**
* An interface that provides server Trids.
*
* @see Trid
*/
public interface ServerTridProvider {
/** Creates a new server Trid. */
public String createServerTrid();
}

View file

@ -0,0 +1,50 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.flows;
import static com.google.common.primitives.Longs.BYTES;
import com.google.common.io.BaseEncoding;
import java.nio.ByteBuffer;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import javax.inject.Inject;
/** A server Trid provider that generates globally incrementing UUIDs. */
public class ServerTridProviderImpl implements ServerTridProvider {
private static final String SERVER_ID = getServerId();
private static final AtomicLong idCounter = new AtomicLong();
@Inject public ServerTridProviderImpl() {}
/** Creates a unique id for this server instance, as a base64 encoded UUID. */
private static String getServerId() {
UUID uuid = UUID.randomUUID();
ByteBuffer buffer =
ByteBuffer.allocate(BYTES * 2)
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits());
return BaseEncoding.base64().encode(buffer.array());
}
@Override
public String createServerTrid() {
// The server id can be at most 64 characters. The SERVER_ID is at most 22 characters (128
// bits in base64), plus the dash. That leaves 41 characters, so we just append the counter in
// hex.
return String.format("%s-%x", SERVER_ID, idCounter.incrementAndGet());
}
}

View file

@ -0,0 +1,28 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.flows;
import dagger.Module;
import dagger.Provides;
/** Dagger module for the server Trid provider. */
@Module
public class ServerTridProviderModule {
@Provides
static ServerTridProvider provideServerTridProvider(ServerTridProviderImpl defaultProvider) {
return defaultProvider;
}
}