Skip to content

Commit 3cf7c16

Browse files
authored
Merge pull request #25 from jimmysmith95/master
Server Interceptor support
2 parents 167a3e0 + c5b69ed commit 3cf7c16

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import io.grpc.Metadata;
4+
import io.grpc.ServerCall;
5+
import io.grpc.ServerCallHandler;
6+
import io.grpc.ServerInterceptor;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.lognet.springboot.grpc.GRpcInterceptor;
9+
10+
/**
11+
* Created by jamessmith on 9/7/16.
12+
*/
13+
@Slf4j
14+
@GRpcInterceptor
15+
public class DemoInterceptor implements ServerInterceptor {
16+
17+
@Override
18+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers,
19+
ServerCallHandler<ReqT, RespT> next) {
20+
log.info("Demo interceptor invoked!");
21+
return next.startCall(call, headers);
22+
}
23+
}

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/GreeterApp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@SpringBootApplication
1818
public class GreeterApp {
1919

20-
@GRpcService
20+
@GRpcService(interceptors = { DemoInterceptor.class })
2121
public static class GreeterService extends GreeterGrpc.GreeterImplBase {
2222
@Override
2323
public void sayHello(GreeterOuterClass.HelloRequest request, StreamObserver<GreeterOuterClass.HelloReply> responseObserver) {
@@ -32,4 +32,3 @@ public static void main(String[] args) {
3232
}
3333

3434
}
35-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.springframework.stereotype.Service;
10+
11+
/**
12+
* Created by jamessmith on 9/7/16.
13+
*/
14+
@Target(ElementType.TYPE)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Documented
17+
@Service
18+
public @interface GRpcInterceptor {
19+
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcServerRunner.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package org.lognet.springboot.grpc;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import java.util.Optional;
46

7+
import io.grpc.ServerInterceptor;
8+
import io.grpc.ServerInterceptors;
9+
import io.grpc.ServerServiceDefinition;
510
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
611
import org.springframework.beans.factory.DisposableBean;
712
import org.springframework.beans.factory.annotation.Autowired;
@@ -40,7 +45,9 @@ public void run(String... args) throws Exception {
4045
if (BindableService.class.isAssignableFrom(grpcService.getClass())) {
4146

4247
BindableService bindableService = BindableService.class.cast(grpcService);
43-
serverBuilder.addService(bindableService);
48+
49+
ServerServiceDefinition definition = bindInterceptors(bindableService);
50+
serverBuilder.addService(definition);
4451
log.info("'{}' service has been registered.", bindableService.getClass().getName());
4552
} else {
4653
throw new IllegalArgumentException(String.format("%s should be of type %s" ,grpcService.getClass().getName(), BindableService.class.getName()));
@@ -53,6 +60,27 @@ public void run(String... args) throws Exception {
5360

5461
}
5562

63+
private ServerServiceDefinition bindInterceptors(BindableService bindableService) {
64+
Class<? extends ServerInterceptor>[] interceptorClasses = bindableService.getClass().getAnnotation(GRpcService.class).interceptors();
65+
66+
if(interceptorClasses.length == 0) {
67+
bindableService.bindService();
68+
}
69+
70+
List<ServerInterceptor> interceptors = buildInterceptors(interceptorClasses);
71+
return ServerInterceptors.intercept(bindableService, interceptors);
72+
}
73+
74+
private List<ServerInterceptor> buildInterceptors(Class<? extends ServerInterceptor>[] classes) {
75+
List<ServerInterceptor> interceptors = new ArrayList<>();
76+
77+
for(Class<? extends ServerInterceptor> clazz : classes) {
78+
interceptors.add(applicationContext.getBean(clazz));
79+
}
80+
81+
return interceptors;
82+
}
83+
5684
private void startDaemonAwaitThread() {
5785
Thread awaitThread = new Thread() {
5886
@Override

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
88

9+
import io.grpc.ServerInterceptor;
910
import org.springframework.stereotype.Service;
1011

1112
/**
@@ -19,5 +20,5 @@
1920
@Documented
2021
@Service
2122
public @interface GRpcService {
22-
23+
Class<? extends ServerInterceptor>[] interceptors() default {};
2324
}

0 commit comments

Comments
 (0)