/** * Extracts the service name from the given method. * * @param method The method to get the service name from. * @return The extracted service name. * @see MethodDescriptor#extractFullServiceName(String) * @see #extractMethodName(MethodDescriptor) */ public static String extractServiceName(final MethodDescriptor<?, ?> method) { return MethodDescriptor.extractFullServiceName(method.getFullMethodName()); }
/** * Extracts the service name from the given method. * * @param method The method to get the service name from. * @return The extracted service name. * @see MethodDescriptor#extractFullServiceName(String) * @see #extractMethodName(MethodDescriptor) */ public static String extractServiceName(final MethodDescriptor<?, ?> method) { return MethodDescriptor.extractFullServiceName(method.getFullMethodName()); }
/** * Note: This does not actually honor the authority provided. It will, eventually in the future. */ @Override @Nullable public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) { String serviceName = MethodDescriptor.extractFullServiceName(methodName); if (serviceName == null) { return null; } ServerServiceDefinition service = services.get(serviceName); if (service == null) { return null; } return service.getMethod(methodName); } }
/** * Note: This does not actually honor the authority provided. It will, eventually in the future. */ @Override @Nullable public ServerMethodDefinition<?, ?> lookupMethod(String methodName, @Nullable String authority) { String serviceName = MethodDescriptor.extractFullServiceName(methodName); if (serviceName == null) { return null; } ServerServiceDefinition service = services.get(serviceName); if (service == null) { return null; } return service.getMethod(methodName); } }
static void validateMethodNames(String serviceName, Collection<MethodDescriptor<?, ?>> methods) { Set<String> allNames = new HashSet<String>(methods.size()); for (MethodDescriptor<?, ?> method : methods) { checkNotNull(method, "method"); String methodServiceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName()); checkArgument(serviceName.equals(methodServiceName), "service names %s != %s", methodServiceName, serviceName); checkArgument(allNames.add(method.getFullMethodName()), "duplicate name %s", method.getFullMethodName()); } }
@CheckForNull private MethodInfo getMethodInfo(MethodDescriptor<?, ?> method) { Map<String, MethodInfo> localServiceMethodMap = serviceMethodMap.get(); MethodInfo info = null; if (localServiceMethodMap != null) { info = localServiceMethodMap.get(method.getFullMethodName()); } if (info == null) { Map<String, MethodInfo> localServiceMap = serviceMap.get(); if (localServiceMap != null) { info = localServiceMap.get( MethodDescriptor.extractFullServiceName(method.getFullMethodName())); } } return info; }
/** * format: <package>.<service>/<method> */ public static ProtoName parse(String fullName) { String fullServiceName = MethodDescriptor.extractFullServiceName(fullName); if (fullServiceName == null) { throw new IllegalArgumentException("Can't extract full service from " + fullName); } int serviceLength = fullServiceName.length(); if (serviceLength + 1 >= fullName.length() || fullName.charAt(serviceLength) != '/') { throw new IllegalArgumentException("Can't extract method name from " + fullName); } String methodName = fullName.substring(fullServiceName.length() + 1); int index = fullServiceName.lastIndexOf('.'); if (index == -1) { throw new IllegalArgumentException("Can't extract package name from " + fullServiceName); } String packageName = fullServiceName.substring(0, index); if (index + 1 >= fullServiceName.length() || fullServiceName.charAt(index) != '.') { throw new IllegalArgumentException("Can't extract service from " + fullServiceName); } String serviceName = fullServiceName.substring(index + 1); return new ProtoName(packageName, serviceName, methodName); }
/** Add a method to be supported by the service. */ public <ReqT, RespT> Builder addMethod(ServerMethodDefinition<ReqT, RespT> def) { MethodDescriptor<ReqT, RespT> method = def.getMethodDescriptor(); checkArgument( serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())), "Method name should be prefixed with service name and separated with '/'. " + "Expected service name: '%s'. Actual fully qualifed method name: '%s'.", serviceName, method.getFullMethodName()); String name = method.getFullMethodName(); checkState(!methods.containsKey(name), "Method by same name already registered: %s", name); methods.put(name, def); return this; }
static void validateMethodNames(String serviceName, Collection<MethodDescriptor<?, ?>> methods) { Set<String> allNames = new HashSet<String>(methods.size()); for (MethodDescriptor<?, ?> method : methods) { checkNotNull(method, "method"); String methodServiceName = MethodDescriptor.extractFullServiceName(method.getFullMethodName()); checkArgument(serviceName.equals(methodServiceName), "service names %s != %s", methodServiceName, serviceName); checkArgument(allNames.add(method.getFullMethodName()), "duplicate name %s", method.getFullMethodName()); } }
/** Creates a {@link ServerStats} for the supplied method. */ <ReqT, RespT> ServerStats createMetricsForMethod(MethodDescriptor<ReqT, RespT> methodDescriptor, StatsLogger statsLogger) { String fullMethodName = methodDescriptor.getFullMethodName(); String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName); String methodName = fullMethodName.substring(serviceName.length() + 1); MethodType type = methodDescriptor.getType(); return new ServerStats( statsLogger.scope(methodName), includeLatencyHistograms, type == MethodType.CLIENT_STREAMING || type == MethodType.BIDI_STREAMING, type == MethodType.SERVER_STREAMING || type == MethodType.BIDI_STREAMING); } }
/** Creates a {@link ClientStats} for the supplied method. */ <ReqT, RespT> ClientStats createMetricsForMethod(MethodDescriptor<ReqT, RespT> methodDescriptor, StatsLogger statsLogger) { String fullMethodName = methodDescriptor.getFullMethodName(); String serviceName = MethodDescriptor.extractFullServiceName(fullMethodName); String methodName = fullMethodName.substring(serviceName.length() + 1); MethodType type = methodDescriptor.getType(); return new ClientStats( statsLogger.scope(methodName), includeLatencyHistograms, type == MethodType.CLIENT_STREAMING || type == MethodType.BIDI_STREAMING, type == MethodType.SERVER_STREAMING || type == MethodType.BIDI_STREAMING); } }
/** Add a method to be supported by the service. */ public <ReqT, RespT> Builder addMethod(ServerMethodDefinition<ReqT, RespT> def) { MethodDescriptor<ReqT, RespT> method = def.getMethodDescriptor(); checkArgument( serviceName.equals(MethodDescriptor.extractFullServiceName(method.getFullMethodName())), "Method name should be prefixed with service name and separated with '/'. " + "Expected service name: '%s'. Actual fully qualifed method name: '%s'.", serviceName, method.getFullMethodName()); String name = method.getFullMethodName(); checkState(!methods.containsKey(name), "Method by same name already registered: %s", name); methods.put(name, def); return this; }
/** * Generate a JWT-specific service URI. The URI is simply an identifier with enough information * for a service to know that the JWT was intended for it. The URI will commonly be verified with * a simple string equality check. */ private static URI serviceUri(String authority, MethodDescriptor<?, ?> method) throws StatusException { // Always use HTTPS, by definition. final String scheme = "https"; final int defaultPort = 443; String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); URI uri; try { uri = new URI(scheme, authority, path, null, null); } catch (URISyntaxException e) { throw Status.UNAUTHENTICATED.withDescription("Unable to construct service URI for auth") .withCause(e).asException(); } // The default port must not be present. Alternative ports should be present. if (uri.getPort() == defaultPort) { uri = removePort(uri); } return uri; }
/** * Generate a JWT-specific service URI. The URI is simply an identifier with enough information * for a service to know that the JWT was intended for it. The URI will commonly be verified with * a simple string equality check. */ private URI serviceUri(Channel channel, MethodDescriptor<?, ?> method) throws StatusException { String authority = channel.authority(); if (authority == null) { throw Status.UNAUTHENTICATED.withDescription("Channel has no authority").asException(); } // Always use HTTPS, by definition. final String scheme = "https"; final int defaultPort = 443; String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); URI uri; try { uri = new URI(scheme, authority, path, null, null); } catch (URISyntaxException e) { throw Status.UNAUTHENTICATED.withDescription("Unable to construct service URI for auth") .withCause(e).asException(); } // The default port must not be present. Alternative ports should be present. if (uri.getPort() == defaultPort) { uri = removePort(uri); } return uri; }
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName()); URI uri; try {