From 26ce84d1947f93bf7ecb5ab57ea796ff3762c69f Mon Sep 17 00:00:00 2001 From: Norbert Hoene Date: Mon, 6 May 2024 07:54:02 +0200 Subject: [PATCH] support multiple URLs as to read desc files --- .../wiremock/grpc/GrpcExtensionFactory.java | 14 ++++ .../grpc/internal/GrpcHttpServerFactory.java | 73 ++++++++++++------- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java b/src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java index 5e7bf2b..8efa923 100644 --- a/src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java +++ b/src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java @@ -18,6 +18,8 @@ import com.github.tomakehurst.wiremock.extension.Extension; import com.github.tomakehurst.wiremock.extension.ExtensionFactory; import com.github.tomakehurst.wiremock.extension.WireMockServices; + +import java.net.URL; import java.util.List; import org.wiremock.annotations.Beta; import org.wiremock.grpc.internal.GrpcHttpServerFactory; @@ -25,8 +27,20 @@ @Beta(justification = "Incubating extension: https://github.com/wiremock/wiremock/issues/2383") public class GrpcExtensionFactory implements ExtensionFactory { + private List urls; + + public GrpcExtensionFactory() { + } + + public GrpcExtensionFactory(List urls) { + this.urls = urls; + } + @Override public List create(WireMockServices services) { + if(this.urls !=null){ + return List.of(new GrpcHttpServerFactory(this.urls)); + } return List.of(new GrpcHttpServerFactory(services.getStores().getBlobStore("grpc"))); } } diff --git a/src/main/java/org/wiremock/grpc/internal/GrpcHttpServerFactory.java b/src/main/java/org/wiremock/grpc/internal/GrpcHttpServerFactory.java index 79382e8..a8f641a 100644 --- a/src/main/java/org/wiremock/grpc/internal/GrpcHttpServerFactory.java +++ b/src/main/java/org/wiremock/grpc/internal/GrpcHttpServerFactory.java @@ -26,42 +26,65 @@ import com.google.protobuf.DescriptorProtos; import com.google.protobuf.Descriptors; import jakarta.servlet.DispatcherType; +import org.eclipse.jetty.servlet.FilterHolder; +import org.eclipse.jetty.servlet.ServletContextHandler; + +import java.net.URL; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Optional; -import org.eclipse.jetty.servlet.FilterHolder; -import org.eclipse.jetty.servlet.ServletContextHandler; +import java.util.stream.Collectors; public class GrpcHttpServerFactory implements HttpServerFactory { private final List fileDescriptors = new ArrayList<>(); public GrpcHttpServerFactory(BlobStore protoDescriptorStore) { - protoDescriptorStore - .getAllKeys() - .filter(key -> key.endsWith(".dsc") || key.endsWith(".desc")) - .map( - key -> - protoDescriptorStore - .get(key) - .map( - data -> + List descriptorProtos = protoDescriptorStore + .getAllKeys() + .filter(key -> key.endsWith(".dsc") || key.endsWith(".desc")) + .map( + key -> + protoDescriptorStore + .get(key) + .map( + data -> + Exceptions.uncheck( + () -> DescriptorProtos.FileDescriptorSet.parseFrom(data), + DescriptorProtos.FileDescriptorSet.class))) + .filter(Optional::isPresent) + .map(Optional::get) + .flatMap(fileDescriptorSet -> fileDescriptorSet.getFileList().stream()) + .collect(Collectors.toList()); + + convertToFileDescriptors(descriptorProtos); + } + + public GrpcHttpServerFactory(List protoUrls) { + List protos = protoUrls.stream() + .filter(key -> key.getPath().endsWith(".dsc") || key.getPath().endsWith(".desc")) + .map( + data -> Exceptions.uncheck( - () -> DescriptorProtos.FileDescriptorSet.parseFrom(data), - DescriptorProtos.FileDescriptorSet.class))) - .filter(Optional::isPresent) - .map(Optional::get) - .flatMap(fileDescriptorSet -> fileDescriptorSet.getFileList().stream()) - .forEach( - fileDescriptorProto -> - Exceptions.uncheck( - () -> - fileDescriptors.add( - Descriptors.FileDescriptor.buildFrom( - fileDescriptorProto, - fileDescriptors.toArray(Descriptors.FileDescriptor[]::new), - true)))); + () -> DescriptorProtos.FileDescriptorSet.parseFrom(data.openStream()), + DescriptorProtos.FileDescriptorSet.class)) + .flatMap(fileDescriptorSet -> fileDescriptorSet.getFileList().stream()) + .collect(Collectors.toList()); + + convertToFileDescriptors(protos); + } + + private void convertToFileDescriptors(List protos){ + protos + .forEach( + fileDescriptorProto -> + Exceptions.uncheck(() -> + fileDescriptors.add( + Descriptors.FileDescriptor.buildFrom( + fileDescriptorProto, + fileDescriptors.toArray(Descriptors.FileDescriptor[]::new), + true)))); } @Override