Clean up some code quality issues in GCP proxy

All changes are suggested by IntelliJ code inspection.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189586104
This commit is contained in:
jianglai 2018-03-19 08:27:04 -07:00
parent 22b575b17d
commit c72e01f75e
13 changed files with 27 additions and 32 deletions

View file

@ -73,8 +73,8 @@ public class CertificateModule {
ImmutableList<Object> objects, Class<T> clazz, Function<T, E> converter) {
return objects
.stream()
.filter(obj -> clazz.isInstance(obj))
.map(obj -> clazz.cast(obj))
.filter(clazz::isInstance)
.map(clazz::cast)
.map(converter)
.collect(toImmutableList());
}

View file

@ -16,7 +16,6 @@ package google.registry.proxy;
import static google.registry.util.ResourceUtils.readResourceBytes;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import dagger.Module;
import dagger.Provides;
@ -43,6 +42,7 @@ import io.netty.handler.timeout.ReadTimeoutHandler;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Supplier;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Qualifier;
@ -56,7 +56,7 @@ public class EppProtocolModule {
/** Dagger qualifier to provide epp protocol related handlers and other bindings. */
@Qualifier
public @interface EppProtocol {};
public @interface EppProtocol {}
private static final String PROTOCOL_NAME = "epp";

View file

@ -26,8 +26,6 @@ import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.services.cloudkms.v1.CloudKMS;
import com.google.api.services.cloudkms.v1.model.DecryptRequest;
import com.google.common.base.Optional;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.monitoring.metrics.MetricReporter;
@ -48,10 +46,12 @@ import io.netty.handler.ssl.OpenSsl;
import io.netty.handler.ssl.SslProvider;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Supplier;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
@ -96,9 +96,9 @@ public class ProxyModule {
private void configureLogging() {
// Remove all other handlers on the root logger to avoid double logging.
Logger rootLogger = Logger.getLogger("");
Arrays.asList(rootLogger.getHandlers()).forEach(h -> rootLogger.removeHandler(h));
Arrays.asList(rootLogger.getHandlers()).forEach(rootLogger::removeHandler);
// If running on in a non-local environment, use GCP JSON formater.
// If running on in a non-local environment, use GCP JSON formatter.
Handler rootHandler = new ConsoleHandler();
rootHandler.setLevel(Level.FINE);
if (env != Environment.LOCAL) {
@ -129,19 +129,19 @@ public class ProxyModule {
@Provides
@WhoisProtocol
int provideWhoisPort(ProxyConfig config) {
return Optional.fromNullable(whoisPort).or(config.whois.port);
return Optional.ofNullable(whoisPort).orElse(config.whois.port);
}
@Provides
@EppProtocol
int provideEppPort(ProxyConfig config) {
return Optional.fromNullable(eppPort).or(config.epp.port);
return Optional.ofNullable(eppPort).orElse(config.epp.port);
}
@Provides
@HealthCheckProtocol
int provideHealthCheckPort(ProxyConfig config) {
return Optional.fromNullable(healthCheckPort).or(config.healthCheck.port);
return Optional.ofNullable(healthCheckPort).orElse(config.healthCheck.port);
}
@Provides
@ -295,7 +295,7 @@ public class ProxyModule {
private final byte[] bytes;
static final PemBytes create(byte[] bytes) {
static PemBytes create(byte[] bytes) {
return new PemBytes(bytes);
}

View file

@ -14,7 +14,6 @@
package google.registry.proxy;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import dagger.Module;
import dagger.Provides;
@ -36,6 +35,7 @@ import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Supplier;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Qualifier;
@ -47,7 +47,7 @@ public class WhoisProtocolModule {
/** Dagger qualifier to provide whois protocol related handlers and other bindings. */
@Qualifier
public @interface WhoisProtocol {};
public @interface WhoisProtocol {}
private static final String PROTOCOL_NAME = "whois";

View file

@ -20,7 +20,6 @@ import static google.registry.proxy.handler.ProxyProtocolHandler.REMOTE_ADDRESS_
import static google.registry.proxy.handler.SslServerInitializer.CLIENT_CERTIFICATE_PROMISE_KEY;
import static google.registry.util.X509Utils.getCertificateHash;
import com.google.common.base.Supplier;
import google.registry.proxy.metric.FrontendMetrics;
import google.registry.util.FormattingLogger;
import io.netty.buffer.ByteBuf;
@ -36,6 +35,7 @@ import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.Promise;
import java.security.cert.X509Certificate;
import java.util.function.Supplier;
/** Handler that processes EPP protocol logic. */
public class EppServiceHandler extends HttpsRelayServiceHandler {

View file

@ -17,7 +17,6 @@ package google.registry.proxy.handler;
import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.base.Supplier;
import google.registry.proxy.metric.FrontendMetrics;
import google.registry.util.FormattingLogger;
import io.netty.buffer.ByteBuf;
@ -38,6 +37,7 @@ import io.netty.handler.codec.http.cookie.Cookie;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
* Handler that relays a single (framed) ByteBuf message to an HTTPS server.

View file

@ -14,13 +14,13 @@
package google.registry.proxy.handler;
import com.google.common.base.Supplier;
import google.registry.proxy.metric.FrontendMetrics;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import java.util.function.Supplier;
/** Handler that processes WHOIS protocol logic. */
public final class WhoisServiceHandler extends HttpsRelayServiceHandler {

View file

@ -78,7 +78,7 @@ public class MetricParameters {
this(ImmutableMap.copyOf(System.getenv()), MetricParameters::gceConnectionFactory);
}
private static final HttpURLConnection gceConnectionFactory(String path) {
private static HttpURLConnection gceConnectionFactory(String path) {
String url = GCE_METADATA_URL_BASE + path;
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
@ -91,7 +91,7 @@ public class MetricParameters {
logger.warningfmt(e, "Incorrect GCE metadata server URL: %s", url);
throw new RuntimeException(e);
}
};
}
private String readEnvVar(String envVar) {
return envVarMap.getOrDefault(envVar, "");

View file

@ -37,16 +37,13 @@ public class GcpJsonFormatterTest {
private final LogRecord logRecord = new LogRecord(Level.WARNING, MESSAGE);
private static String makeJson(String severity, String source, String message) {
StringBuilder builder = new StringBuilder();
builder.append("{");
builder.append(
Joiner.on(",")
return "{"
+ Joiner.on(",")
.join(
makeJsonField("severity", severity),
makeJsonField("source", source),
makeJsonField("message", "\\n" + message)));
builder.append("}\n");
return builder.toString();
makeJsonField("message", "\\n" + message))
+ "}\n";
}
private static String makeJsonField(String name, String content) {

View file

@ -18,7 +18,6 @@ import static com.google.common.collect.ImmutableList.toImmutableList;
import static google.registry.proxy.ProxyConfig.Environment.TEST;
import static google.registry.proxy.ProxyConfig.getProxyConfig;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@ -55,6 +54,7 @@ import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.inject.Named;
import javax.inject.Provider;
import javax.inject.Singleton;

View file

@ -51,7 +51,7 @@ public class ProxyProtocolHandlerTest {
@Test
public void testSuccess_proxyHeaderMalformed_singleFrame() {
header = String.format("PROXY UNKNOWN\r\n");
header = "PROXY UNKNOWN\r\n";
String message = "some message";
// Header processed, rest of the message passed along.
assertThat(channel.writeInbound(Unpooled.wrappedBuffer((header + message).getBytes(UTF_8))))

View file

@ -62,8 +62,7 @@ public class MetricParametersTest {
ZONE_PATH,
zoneConnection);
private final HashMap<String, String> fakeEnvVarMap = new HashMap<>();
private final Function<String, HttpURLConnection> fakeConnectionFactory =
path -> mockConnections.get(path);
private final Function<String, HttpURLConnection> fakeConnectionFactory = mockConnections::get;
private final MetricParameters metricParameters =
new MetricParameters(fakeEnvVarMap, fakeConnectionFactory);

View file

@ -46,7 +46,6 @@ public class QuotaManagerTest {
new QuotaManager(tokenStore, MoreExecutors.newDirectExecutorService());
private QuotaRequest request;
private QuotaResponse response;
private QuotaRebate rebate;
@Test
public void testSuccess_requestApproved() {
@ -74,7 +73,7 @@ public class QuotaManagerTest {
public void testSuccess_rebate() throws Exception {
DateTime grantedTokenRefillTime = clock.nowUtc();
response = QuotaResponse.create(true, USER_ID, grantedTokenRefillTime);
rebate = QuotaRebate.create(response);
QuotaRebate rebate = QuotaRebate.create(response);
Future<?> unusedFuture = quotaManager.releaseQuota(rebate);
verify(tokenStore).scheduleRefresh();
verify(tokenStore).put(USER_ID, grantedTokenRefillTime);