Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024-2024 the original author or authors.
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,7 @@
* @param <T> the type of server builder
* @author David Syer
* @author Chris Bono
* @author Andrey Litvitski
* @see ServerProvider#provider()
*/
public class DefaultGrpcServerFactory<T extends ServerBuilder<T>> implements GrpcServerFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;

import io.grpc.Server;
import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;

/**
Expand Down Expand Up @@ -48,4 +49,8 @@ public interface GrpcServerFactory {
*/
void addService(ServerServiceDefinition service);

default boolean supports(ServerInterceptor interceptor, ServerServiceDefinition service) {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,48 @@

import java.util.List;

import org.springframework.grpc.server.service.ServerInterceptorFilter;
import org.springframework.lang.Nullable;

import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;
import io.grpc.inprocess.InProcessServerBuilder;

/**
* {@link GrpcServerFactory} that can be used to create an in-process gRPC server.
*
* @author Chris Bono
* @author Andrey Litvitski
*/
public class InProcessGrpcServerFactory extends DefaultGrpcServerFactory<InProcessServerBuilder> {

private ServerServiceDefinitionFilter serviceFilter;

private ServerInterceptorFilter interceptorFilter;

public InProcessGrpcServerFactory(String address,
List<ServerBuilderCustomizer<InProcessServerBuilder>> serverBuilderCustomizers) {
super(address, serverBuilderCustomizers);
List<ServerBuilderCustomizer<InProcessServerBuilder>> serverBuilderCustomizers,
@Nullable ServerInterceptorFilter interceptorFilter,
@Nullable ServerServiceDefinitionFilter serviceFilter) {
super(address, serverBuilderCustomizers, null, null, null);
this.interceptorFilter = interceptorFilter;
this.serviceFilter = serviceFilter;
}

@Override
protected InProcessServerBuilder newServerBuilder() {
return InProcessServerBuilder.forName(address());
}

@Override
public void addService(ServerServiceDefinition service) {
if (this.serviceFilter == null || this.serviceFilter.filter(service, this)) {
super.addService(service);
}
}

@Override
public boolean supports(ServerInterceptor interceptor, ServerServiceDefinition service) {
return (this.interceptorFilter == null || this.interceptorFilter.filter(interceptor, service));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024-2024 the original author or authors.
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

import org.springframework.lang.Nullable;

import io.grpc.TlsServerCredentials.ClientAuth;
import io.grpc.netty.NettyServerBuilder;
import io.netty.channel.epoll.EpollEventLoopGroup;
Expand All @@ -32,13 +34,15 @@
*
* @author David Syer
* @author Chris Bono
* @author Andrey Litvitski
*/
public class NettyGrpcServerFactory extends DefaultGrpcServerFactory<NettyServerBuilder> {

public NettyGrpcServerFactory(String address,
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers, KeyManagerFactory keyManager,
TrustManagerFactory trustManager, ClientAuth clientAuth) {
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth);
TrustManagerFactory trustManager, ClientAuth clientAuth,
@Nullable ServerServiceDefinitionFilter serviceFilter) {
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.grpc.server;

import io.grpc.ServerServiceDefinition;

/**
* Strategy to determine whether a {@link ServerServiceDefinition} should be added to a
* {@link GrpcServerFactory server factory}.
*
* @author Andrey Litvitski
*/
@FunctionalInterface
public interface ServerServiceDefinitionFilter {

/**
* Determine whether the given {@link ServerServiceDefinition} should be added to the
* given {@link GrpcServerFactory server factory}.
* @param serviceDefinition the gRPC service definition under consideration.
* @param serverFactory the server factory in use.
* @return {@code true} if the service should be included; {@code false} otherwise.
*/
boolean filter(ServerServiceDefinition serviceDefinition, GrpcServerFactory serverFactory);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024-2024 the original author or authors.
* Copyright 2024-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;

import org.springframework.lang.Nullable;

import io.grpc.TlsServerCredentials.ClientAuth;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup;
Expand All @@ -32,13 +34,15 @@
*
* @author David Syer
* @author Chris Bono
* @author Andrey Litvitski
*/
public class ShadedNettyGrpcServerFactory extends DefaultGrpcServerFactory<NettyServerBuilder> {

public ShadedNettyGrpcServerFactory(String address,
List<ServerBuilderCustomizer<NettyServerBuilder>> serverBuilderCustomizers, KeyManagerFactory keyManager,
TrustManagerFactory trustManager, ClientAuth clientAuth) {
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth);
TrustManagerFactory trustManager, ClientAuth clientAuth,
@Nullable ServerServiceDefinitionFilter serviceFilter) {
super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.grpc.internal.ApplicationContextBeanLookupUtils;
import org.springframework.grpc.server.GlobalServerInterceptor;
import org.springframework.grpc.server.GrpcServerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -57,9 +58,9 @@ public void afterPropertiesSet() {
}

@Override
public ServerServiceDefinition configure(GrpcServiceSpec serviceSpec) {
public ServerServiceDefinition configure(GrpcServiceSpec serviceSpec, GrpcServerFactory serverFactory) {
Assert.notNull(serviceSpec, () -> "serviceSpec must not be null");
return bindInterceptors(serviceSpec.service(), serviceSpec.serviceInfo());
return bindInterceptors(serviceSpec.service(), serviceSpec.serviceInfo(), serverFactory);
}

private List<ServerInterceptor> findGlobalInterceptors() {
Expand All @@ -68,13 +69,14 @@ private List<ServerInterceptor> findGlobalInterceptors() {
}

private ServerServiceDefinition bindInterceptors(BindableService bindableService,
@Nullable GrpcServiceInfo serviceInfo) {
@Nullable GrpcServiceInfo serviceInfo,
GrpcServerFactory serverFactory) {
var serviceDef = bindableService.bindService();

// Add and filter global interceptors first
List<ServerInterceptor> allInterceptors = new ArrayList<>(this.globalInterceptors);
if (this.interceptorFilter != null) {
allInterceptors.removeIf(interceptor -> !this.interceptorFilter.filter(interceptor, serviceDef));
allInterceptors.removeIf(interceptor -> !serverFactory.supports(interceptor, serviceDef));
}
if (serviceInfo == null) {
return ServerInterceptors.interceptForward(serviceDef, allInterceptors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import io.grpc.ServerServiceDefinition;

import org.springframework.grpc.server.GrpcServerFactory;

/**
* Configures and binds a {@link GrpcServiceSpec service spec} into a
* {@link ServerServiceDefinition service definition} that can then be added to a gRPC
Expand All @@ -35,6 +37,6 @@ public interface GrpcServiceConfigurer {
* @return bound and configured service definition that is ready to be added to a
* server
*/
ServerServiceDefinition configure(GrpcServiceSpec serviceSpec);
ServerServiceDefinition configure(GrpcServiceSpec serviceSpec, GrpcServerFactory serverFactory);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.grpc.server;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import java.util.List;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import io.grpc.ServerServiceDefinition;

/**
* Tests for {@link DefaultGrpcServerFactory}.
*/
class DefaultGrpcServerFactoryTests {

@Nested
class WithServiceFilter {

@Test
void whenNoFilterThenAllServicesAdded() {
ServerServiceDefinition serviceDef1 = mock();
ServerServiceDefinition serviceDef2 = mock();
DefaultGrpcServerFactory serverFactory = new DefaultGrpcServerFactory("myhost:5150", List.of(), null, null,
null, null);
serverFactory.addService(serviceDef2);
serverFactory.addService(serviceDef1);
assertThat(serverFactory)
.extracting("serviceList", InstanceOfAssertFactories.list(ServerServiceDefinition.class))
.containsExactly(serviceDef2, serviceDef1);
}

@Test
void whenFilterAllowsAllThenAllServicesAdded() {
ServerServiceDefinition serviceDef1 = mock();
ServerServiceDefinition serviceDef2 = mock();
ServerServiceDefinitionFilter serviceFilter = (serviceDef, serviceFactory) -> true;
DefaultGrpcServerFactory serverFactory = new DefaultGrpcServerFactory("myhost:5150", List.of(), null, null,
null, serviceFilter);
serverFactory.addService(serviceDef2);
serverFactory.addService(serviceDef1);
assertThat(serverFactory)
.extracting("serviceList", InstanceOfAssertFactories.list(ServerServiceDefinition.class))
.containsExactly(serviceDef2, serviceDef1);
}

@Test
void whenFilterAllowsOneThenOneServiceAdded() {
ServerServiceDefinition serviceDef1 = mock();
ServerServiceDefinition serviceDef2 = mock();
ServerServiceDefinitionFilter serviceFilter = (serviceDef, serviceFactory) -> serviceDef == serviceDef1;
DefaultGrpcServerFactory serverFactory = new DefaultGrpcServerFactory("myhost:5150", List.of(), null, null,
null, serviceFilter);
serverFactory.addService(serviceDef2);
serverFactory.addService(serviceDef1);
assertThat(serverFactory)
.extracting("serviceList", InstanceOfAssertFactories.list(ServerServiceDefinition.class))
.containsExactly(serviceDef1);
}

}

}

This file was deleted.

Loading
Loading