1
1
package io .grpc .protobuf .services ;
2
2
3
+ import com .google .common .annotations .VisibleForTesting ;
4
+ import com .google .protobuf .InvalidProtocolBufferException ;
3
5
import io .grpc .BindableService ;
4
6
import io .grpc .ExperimentalApi ;
5
7
import io .grpc .protobuf .services .util .ReflectionServiceProtoAdapter ;
18
20
*/
19
21
@ ExperimentalApi ("https://github.com/grpc/grpc-java/issues/2222" )
20
22
public class ProtoReflectionService extends ServerReflectionGrpc .ServerReflectionImplBase {
21
- private final ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1 ) ProtoReflectionServiceV1 .newInstance ();
23
+ private ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1 ) ProtoReflectionServiceV1 .newInstance ();
22
24
/**
23
25
* Creates a instance of {@link ProtoReflectionServiceV1}.
24
26
*/
25
27
public static BindableService newInstance () {
26
28
return new ProtoReflectionService ();
27
29
}
28
30
31
+ private ProtoReflectionService () {}
32
+
33
+ @ VisibleForTesting
34
+ void setProtoReflectionServiceV1 (ProtoReflectionServiceV1 protoReflectionServiceV1 ) {
35
+ this .protoReflectionServiceV1 = protoReflectionServiceV1 ;
36
+ }
29
37
@ Override
30
38
public StreamObserver <ServerReflectionRequest > serverReflectionInfo (
31
39
final StreamObserver <ServerReflectionResponse > responseObserver ) {
@@ -34,8 +42,13 @@ public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
34
42
@ Override
35
43
public void onNext (
36
44
io .grpc .reflection .v1 .ServerReflectionResponse serverReflectionResponse ) {
37
- responseObserver .onNext (
38
- ReflectionServiceProtoAdapter .toV1AlphaResponse (serverReflectionResponse ));
45
+ try {
46
+ responseObserver .onNext (
47
+ ReflectionServiceProtoAdapter .toV1AlphaResponse (serverReflectionResponse ));
48
+ } catch (InvalidProtocolBufferException e ) {
49
+ // Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
50
+ throw new RuntimeException (e );
51
+ }
39
52
}
40
53
41
54
@ Override
@@ -51,7 +64,12 @@ public void onCompleted() {
51
64
return new StreamObserver <ServerReflectionRequest >() {
52
65
@ Override
53
66
public void onNext (ServerReflectionRequest serverReflectionRequest ) {
54
- v1RequestObserver .onNext (ReflectionServiceProtoAdapter .toV1Request (serverReflectionRequest ));
67
+ try {
68
+ v1RequestObserver .onNext (ReflectionServiceProtoAdapter .toV1Request (serverReflectionRequest ));
69
+ } catch (InvalidProtocolBufferException e ) {
70
+ // Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
71
+ throw new RuntimeException (e );
72
+ }
55
73
}
56
74
57
75
@ Override
0 commit comments