From ce6358e57fe1ab81bd5568129beb13ab2afef129 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Wed, 24 Apr 2019 17:45:41 -0700 Subject: [PATCH] core: add getService to MethodDescriptor --- .../main/java/io/grpc/MethodDescriptor.java | 13 ++++++++++ .../java/io/grpc/ServerServiceDefinition.java | 2 +- .../main/java/io/grpc/ServiceDescriptor.java | 3 +-- .../java/io/grpc/MethodDescriptorTest.java | 25 +++++++++++++++++++ .../io/grpc/auth/ClientAuthInterceptor.java | 2 +- .../GoogleAuthLibraryCallCredentials.java | 2 +- .../internal/ServiceConfigInterceptor.java | 2 +- 7 files changed, 43 insertions(+), 6 deletions(-) diff --git a/api/src/main/java/io/grpc/MethodDescriptor.java b/api/src/main/java/io/grpc/MethodDescriptor.java index 89fc24e7bb..a05dd4a77b 100644 --- a/api/src/main/java/io/grpc/MethodDescriptor.java +++ b/api/src/main/java/io/grpc/MethodDescriptor.java @@ -41,6 +41,7 @@ public final class MethodDescriptor { private final MethodType type; private final String fullMethodName; + @Nullable private final String serviceName; private final Marshaller requestMarshaller; private final Marshaller responseMarshaller; private final @Nullable Object schemaDescriptor; @@ -226,6 +227,7 @@ public final class MethodDescriptor { this.type = Preconditions.checkNotNull(type, "type"); this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName"); + this.serviceName = extractFullServiceName(fullMethodName); this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller"); this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller"); this.schemaDescriptor = schemaDescriptor; @@ -254,6 +256,17 @@ public final class MethodDescriptor { return fullMethodName; } + /** + * A convenience method for {@code extractFullServiceName(getFullMethodName())}. + * + * @since 1.21.0 + */ + @Nullable + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635") + public String getServiceName() { + return serviceName; + } + /** * Parse a response payload from the given {@link InputStream}. * diff --git a/api/src/main/java/io/grpc/ServerServiceDefinition.java b/api/src/main/java/io/grpc/ServerServiceDefinition.java index 3bcac57bd8..75f1071820 100644 --- a/api/src/main/java/io/grpc/ServerServiceDefinition.java +++ b/api/src/main/java/io/grpc/ServerServiceDefinition.java @@ -106,7 +106,7 @@ public final class ServerServiceDefinition { public Builder addMethod(ServerMethodDefinition def) { MethodDescriptor method = def.getMethodDescriptor(); checkArgument( - serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())), + serviceName.equals(method.getServiceName()), "Method name should be prefixed with service name and separated with '/'. " + "Expected service name: '%s'. Actual fully qualifed method name: '%s'.", serviceName, method.getFullMethodName()); diff --git a/api/src/main/java/io/grpc/ServiceDescriptor.java b/api/src/main/java/io/grpc/ServiceDescriptor.java index 57a6ff12cc..a928a5d658 100644 --- a/api/src/main/java/io/grpc/ServiceDescriptor.java +++ b/api/src/main/java/io/grpc/ServiceDescriptor.java @@ -110,8 +110,7 @@ public final class ServiceDescriptor { Set allNames = new HashSet<>(methods.size()); for (MethodDescriptor method : methods) { checkNotNull(method, "method"); - String methodServiceName = - MethodDescriptor.extractFullServiceName(method.getFullMethodName()); + String methodServiceName = method.getServiceName(); checkArgument(serviceName.equals(methodServiceName), "service names %s != %s", methodServiceName, serviceName); checkArgument(allNames.add(method.getFullMethodName()), diff --git a/api/src/test/java/io/grpc/MethodDescriptorTest.java b/api/src/test/java/io/grpc/MethodDescriptorTest.java index 47f13d3573..f9b00ba8ac 100644 --- a/api/src/test/java/io/grpc/MethodDescriptorTest.java +++ b/api/src/test/java/io/grpc/MethodDescriptorTest.java @@ -20,9 +20,12 @@ import static junit.framework.TestCase.assertSame; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import io.grpc.MethodDescriptor.Marshaller; import io.grpc.MethodDescriptor.MethodType; +import io.grpc.testing.TestMethodDescriptors; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -135,6 +138,28 @@ public class MethodDescriptorTest { assertTrue(md4.isSampledToLocalTracing()); } + @Test + public void getServiceName_extractsService() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo/bar") + .build(); + + assertEquals("foo", md.getServiceName()); + } + + @Test + public void getServiceName_returnsNull() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo-bar") + .build(); + + assertNull(md.getServiceName()); + } + @Test public void toBuilderTest() { MethodDescriptor md1 = MethodDescriptor.newBuilder() diff --git a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java index adbf2cbce5..ec5b4f0fa7 100644 --- a/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java +++ b/auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java @@ -102,7 +102,7 @@ public final class ClientAuthInterceptor implements ClientInterceptor { // Always use HTTPS, by definition. final String scheme = "https"; final int defaultPort = 443; - String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); + String path = "/" + method.getServiceName(); URI uri; try { uri = new URI(scheme, authority, path, null, null); diff --git a/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java b/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java index ef8cd38627..7ca0370707 100644 --- a/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java +++ b/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java @@ -159,7 +159,7 @@ final class GoogleAuthLibraryCallCredentials extends io.grpc.CallCredentials2 { // Always use HTTPS, by definition. final String scheme = "https"; final int defaultPort = 443; - String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); + String path = "/" + method.getServiceName(); URI uri; try { uri = new URI(scheme, authority, path, null, null); diff --git a/core/src/main/java/io/grpc/internal/ServiceConfigInterceptor.java b/core/src/main/java/io/grpc/internal/ServiceConfigInterceptor.java index 0ade1de9a2..f33eaf39f7 100644 --- a/core/src/main/java/io/grpc/internal/ServiceConfigInterceptor.java +++ b/core/src/main/java/io/grpc/internal/ServiceConfigInterceptor.java @@ -195,7 +195,7 @@ final class ServiceConfigInterceptor implements ClientInterceptor { info = mcsc.getServiceMethodMap().get(method.getFullMethodName()); } if (info == null && mcsc != null) { - String serviceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName()); + String serviceName = method.getServiceName(); info = mcsc.getServiceMap().get(serviceName); } return info;