Skip to content

Commit 1234070

Browse files
author
James Smith
committed
added support for server interceptors
1 parent 167a3e0 commit 1234070

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 33 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,31 @@ 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+
try {
79+
interceptors.add(clazz.newInstance());
80+
} catch (InstantiationException | IllegalAccessException e) {
81+
// TODO
82+
}
83+
}
84+
85+
return interceptors;
86+
}
87+
5688
private void startDaemonAwaitThread() {
5789
Thread awaitThread = new Thread() {
5890
@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)