|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.webserver.grpc; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | + |
| 26 | +import com.google.protobuf.ByteString; |
| 27 | +import com.google.protobuf.DescriptorProtos; |
| 28 | +import com.google.protobuf.Descriptors; |
| 29 | +import io.grpc.Status; |
| 30 | +import io.grpc.reflection.v1alpha.ErrorResponse; |
| 31 | +import io.grpc.reflection.v1alpha.FileDescriptorResponse; |
| 32 | +import io.grpc.reflection.v1alpha.ListServiceResponse; |
| 33 | +import io.grpc.reflection.v1alpha.ServerReflectionProto; |
| 34 | +import io.grpc.reflection.v1alpha.ServerReflectionRequest; |
| 35 | +import io.grpc.reflection.v1alpha.ServerReflectionResponse; |
| 36 | +import io.grpc.reflection.v1alpha.ServiceResponse; |
| 37 | +import io.grpc.stub.StreamObserver; |
| 38 | + |
| 39 | +/** |
| 40 | + * Grpc reflection service version v1alpha. Some tools such as Postman still do not |
| 41 | + * support version v1. Once more tools support the new version we can remove support |
| 42 | + * for version v1alpha. Note the code in this class is almost identical to |
| 43 | + * {@link io.helidon.webserver.grpc.GrpcReflectionService} except for the code-generated |
| 44 | + * protobuf types. |
| 45 | + * |
| 46 | + * @see io.helidon.webserver.grpc.GrpcReflectionService |
| 47 | + */ |
| 48 | +class GrpcReflectionServiceV1Alpha implements GrpcService { |
| 49 | + |
| 50 | + /** |
| 51 | + * Caches FileDescriptorProto representations as byte strings to avoid serialization |
| 52 | + * on every reflection request. |
| 53 | + */ |
| 54 | + private static final Map<String, ByteString> FILE_DESCRIPTOR_CACHE = new ConcurrentHashMap<>(); |
| 55 | + |
| 56 | + private final String socket; |
| 57 | + |
| 58 | + GrpcReflectionServiceV1Alpha(String socket) { |
| 59 | + this.socket = socket; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Descriptors.FileDescriptor proto() { |
| 64 | + return ServerReflectionProto.getDescriptor(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String serviceName() { |
| 69 | + List<Descriptors.ServiceDescriptor> services = proto().getServices(); |
| 70 | + return services.getFirst().getFullName(); // only one service |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void update(Routing router) { |
| 75 | + router.bidi("ServerReflectionInfo", this::serverReflectionInfo); |
| 76 | + } |
| 77 | + |
| 78 | + private StreamObserver<ServerReflectionRequest> serverReflectionInfo(StreamObserver<ServerReflectionResponse> res) { |
| 79 | + return new StreamObserver<>() { |
| 80 | + @Override |
| 81 | + public void onNext(ServerReflectionRequest req) { |
| 82 | + res.onNext(processRequest(req)); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onError(Throwable t) { |
| 87 | + res.onError(t); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onCompleted() { |
| 92 | + res.onCompleted(); |
| 93 | + } |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + private ServerReflectionResponse processRequest(ServerReflectionRequest req) { |
| 98 | + return switch (req.getMessageRequestCase().getNumber()) { |
| 99 | + case ServerReflectionRequest.LIST_SERVICES_FIELD_NUMBER -> listServices(); |
| 100 | + case ServerReflectionRequest.FILE_BY_FILENAME_FIELD_NUMBER -> findFile(req); |
| 101 | + case ServerReflectionRequest.FILE_CONTAINING_SYMBOL_FIELD_NUMBER -> findSymbol(req); |
| 102 | + case ServerReflectionRequest.FILE_CONTAINING_EXTENSION_FIELD_NUMBER -> findExtensionField(req); |
| 103 | + default -> notImplemented(); |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + private ServerReflectionResponse listServices() { |
| 108 | + List<GrpcRouting> grpcRoutings = GrpcReflectionFeature.socketGrpcRoutings().get(socket); |
| 109 | + |
| 110 | + ListServiceResponse.Builder builder = ListServiceResponse.newBuilder(); |
| 111 | + for (GrpcRouting grpcRouting : grpcRoutings) { |
| 112 | + for (GrpcRoute grpcRoute : grpcRouting.routes()) { |
| 113 | + builder.addService(ServiceResponse.newBuilder().setName(grpcRoute.serviceName()).build()); |
| 114 | + } |
| 115 | + } |
| 116 | + return ServerReflectionResponse.newBuilder().setListServicesResponse(builder).build(); |
| 117 | + } |
| 118 | + |
| 119 | + private ServerReflectionResponse findFile(ServerReflectionRequest req) { |
| 120 | + String fileName = req.getFileByFilename(); |
| 121 | + String cachedFileNameKey = "/" + fileName; // not a legal identifier |
| 122 | + ByteString byteString = FILE_DESCRIPTOR_CACHE.get(cachedFileNameKey); |
| 123 | + if (byteString != null) { |
| 124 | + return fileDescResponse(byteString); |
| 125 | + } |
| 126 | + |
| 127 | + List<GrpcRouting> grpcRoutings = GrpcReflectionFeature.socketGrpcRoutings().get(socket); |
| 128 | + for (GrpcRouting grpcRouting : grpcRoutings) { |
| 129 | + for (GrpcRoute grpcRoute : grpcRouting.routes()) { |
| 130 | + Descriptors.FileDescriptor fileDesc = grpcRoute.proto(); |
| 131 | + if (fileDesc.getFile().getFullName().equals(fileName)) { |
| 132 | + return symbolFound(fileDesc, cachedFileNameKey); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return notFound("Unable to find file name " + fileName); |
| 137 | + } |
| 138 | + |
| 139 | + private ServerReflectionResponse findSymbol(ServerReflectionRequest req) { |
| 140 | + String symbol = req.getFileContainingSymbol(); |
| 141 | + ByteString byteString = FILE_DESCRIPTOR_CACHE.get(symbol); |
| 142 | + if (byteString != null) { |
| 143 | + return fileDescResponse(byteString); |
| 144 | + } |
| 145 | + |
| 146 | + List<GrpcRouting> grpcRoutings = GrpcReflectionFeature.socketGrpcRoutings().get(socket); |
| 147 | + for (GrpcRouting grpcRouting : grpcRoutings) { |
| 148 | + for (GrpcRoute grpcRoute : grpcRouting.routes()) { |
| 149 | + Descriptors.FileDescriptor fileDesc = grpcRoute.proto(); |
| 150 | + |
| 151 | + // scan through services and methods |
| 152 | + List<Descriptors.ServiceDescriptor> services = fileDesc.getServices(); |
| 153 | + for (Descriptors.ServiceDescriptor service : services) { |
| 154 | + if (service.getFullName().equals(symbol)) { |
| 155 | + return symbolFound(fileDesc, symbol); |
| 156 | + } |
| 157 | + List<Descriptors.MethodDescriptor> methods = service.getMethods(); |
| 158 | + for (Descriptors.MethodDescriptor method : methods) { |
| 159 | + if (method.getFullName().equals(symbol)) { |
| 160 | + return symbolFound(fileDesc, symbol); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // scan through message types |
| 166 | + List<Descriptors.Descriptor> types = fileDesc.getMessageTypes(); |
| 167 | + for (Descriptors.Descriptor type : types) { |
| 168 | + if (type.getFullName().equals(symbol)) { |
| 169 | + return symbolFound(fileDesc, symbol); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return notFound("Unable to find proto file for " + symbol); |
| 175 | + } |
| 176 | + |
| 177 | + private ServerReflectionResponse findExtensionField(ServerReflectionRequest req) { |
| 178 | + String type = req.getFileContainingExtension().getContainingType(); |
| 179 | + int number = req.getFileContainingExtension().getExtensionNumber(); |
| 180 | + String cachedFileNameKey = number + type; // not a legal identifier |
| 181 | + ByteString byteString = FILE_DESCRIPTOR_CACHE.get(cachedFileNameKey); |
| 182 | + if (byteString != null) { |
| 183 | + return fileDescResponse(byteString); |
| 184 | + } |
| 185 | + |
| 186 | + List<GrpcRouting> grpcRoutings = GrpcReflectionFeature.socketGrpcRoutings().get(socket); |
| 187 | + for (GrpcRouting grpcRouting : grpcRoutings) { |
| 188 | + for (GrpcRoute grpcRoute : grpcRouting.routes()) { |
| 189 | + Descriptors.FileDescriptor fileDesc = grpcRoute.proto(); |
| 190 | + List<Descriptors.FieldDescriptor> extensions = fileDesc.getExtensions(); |
| 191 | + for (Descriptors.FieldDescriptor extension : extensions) { |
| 192 | + if (extension.getContainingType().getFullName().equals(type) |
| 193 | + && extension.toProto().getNumber() == number) { |
| 194 | + return symbolFound(fileDesc, cachedFileNameKey); |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + return notFound("Unable to find proto file for " + type); |
| 200 | + } |
| 201 | + |
| 202 | + private ServerReflectionResponse symbolFound(Descriptors.FileDescriptor fileDesc, String symbol) { |
| 203 | + ByteString byteString; |
| 204 | + DescriptorProtos.FileDescriptorProto proto = fileDesc.toProto(); |
| 205 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 206 | + proto.writeTo(baos); |
| 207 | + byteString = ByteString.copyFrom(baos.toByteArray()); |
| 208 | + } catch (IOException e) { |
| 209 | + throw new UncheckedIOException(e); |
| 210 | + } |
| 211 | + ByteString cachedValue = FILE_DESCRIPTOR_CACHE.putIfAbsent(symbol, byteString); |
| 212 | + return fileDescResponse(cachedValue != null ? cachedValue : byteString); |
| 213 | + } |
| 214 | + |
| 215 | + private ServerReflectionResponse fileDescResponse(ByteString byteString) { |
| 216 | + FileDescriptorResponse.Builder builder = FileDescriptorResponse.newBuilder(); |
| 217 | + builder.addFileDescriptorProto(byteString); |
| 218 | + return ServerReflectionResponse.newBuilder().setFileDescriptorResponse(builder.build()).build(); |
| 219 | + } |
| 220 | + |
| 221 | + private ServerReflectionResponse notImplemented() { |
| 222 | + return ServerReflectionResponse.newBuilder().setErrorResponse( |
| 223 | + ErrorResponse.newBuilder().setErrorCode(Status.UNIMPLEMENTED.getCode().value()) |
| 224 | + .setErrorMessage("Reflection request not implemented").build()).build(); |
| 225 | + } |
| 226 | + |
| 227 | + private ServerReflectionResponse notFound(String message) { |
| 228 | + return ServerReflectionResponse.newBuilder().setErrorResponse( |
| 229 | + ErrorResponse.newBuilder().setErrorCode(Status.NOT_FOUND.getCode().value()) |
| 230 | + .setErrorMessage(message).build()).build(); |
| 231 | + } |
| 232 | +} |
0 commit comments