Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple URLs as to read desc files #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/org/wiremock/grpc/GrpcExtensionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@
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;

@Beta(justification = "Incubating extension: https://github.com/wiremock/wiremock/issues/2383")
public class GrpcExtensionFactory implements ExtensionFactory {

private List<URL> urls;

public GrpcExtensionFactory() {
}

public GrpcExtensionFactory(List<URL> urls) {
this.urls = urls;
}

@Override
public List<Extension> create(WireMockServices services) {
if(this.urls !=null){
return List.of(new GrpcHttpServerFactory(this.urls));
}
return List.of(new GrpcHttpServerFactory(services.getStores().getBlobStore("grpc")));
}
}
73 changes: 48 additions & 25 deletions src/main/java/org/wiremock/grpc/internal/GrpcHttpServerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Descriptors.FileDescriptor> 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.FileDescriptorProto> 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<URL> protoUrls) {
List<DescriptorProtos.FileDescriptorProto> 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<DescriptorProtos.FileDescriptorProto> protos){
protos
.forEach(
fileDescriptorProto ->
Exceptions.uncheck(() ->
fileDescriptors.add(
Descriptors.FileDescriptor.buildFrom(
fileDescriptorProto,
fileDescriptors.toArray(Descriptors.FileDescriptor[]::new),
true))));
}

@Override
Expand Down