Skip to content
Closed
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 @@ -28,6 +28,7 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.grpc.internal.GrpcUtils;
import org.springframework.lang.Nullable;

import com.google.common.collect.Lists;
import io.grpc.Grpc;
Expand All @@ -49,6 +50,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 All @@ -68,17 +70,21 @@ public class DefaultGrpcServerFactory<T extends ServerBuilder<T>> implements Grp

private ClientAuth clientAuth;

private ServerServiceDefinitionFilter serviceFilter;

public DefaultGrpcServerFactory(String address, List<ServerBuilderCustomizer<T>> serverBuilderCustomizers) {
this.address = address;
this.serverBuilderCustomizers = Objects.requireNonNull(serverBuilderCustomizers, "serverBuilderCustomizers");
}

public DefaultGrpcServerFactory(String address, List<ServerBuilderCustomizer<T>> serverBuilderCustomizers,
KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth) {
KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth,
@Nullable ServerServiceDefinitionFilter serviceFilter) {
this(address, serverBuilderCustomizers);
this.keyManager = keyManager;
this.trustManager = trustManager;
this.clientAuth = clientAuth;
this.serviceFilter = serviceFilter;
}

protected String address() {
Expand All @@ -94,7 +100,9 @@ public Server createServer() {

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

import java.util.List;

import org.springframework.lang.Nullable;

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> {

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

@Override
Expand Down
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
@@ -0,0 +1,45 @@
/*
* 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.
* 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.Server;
import io.grpc.ServerServiceDefinition;

/**
* Marker interface for {@link GrpcServerFactory} that is to be handled by the servlet
* container.
*
* @author Chris Bono
*/
public class ServletGrpcServerFactory implements GrpcServerFactory {

/**
* Default instance of marker interface.
*/
public static ServletGrpcServerFactory INSTANCE = new ServletGrpcServerFactory();

@Override
public Server createServer() {
throw new UnsupportedOperationException("Marker interface only");
}

@Override
public void addService(ServerServiceDefinition service) {
throw new UnsupportedOperationException("Marker interface only");
}

}
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,7 +23,9 @@
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;

import io.grpc.BindableService;
import io.grpc.ServerInterceptor;
Expand All @@ -42,8 +44,12 @@ public class DefaultGrpcServiceConfigurer implements GrpcServiceConfigurer, Init

private List<ServerInterceptor> globalInterceptors;

public DefaultGrpcServiceConfigurer(ApplicationContext applicationContext) {
private ServerInterceptorFilter interceptorFilter;

public DefaultGrpcServiceConfigurer(ApplicationContext applicationContext,
@Nullable ServerInterceptorFilter interceptorFilter) {
this.applicationContext = applicationContext;
this.interceptorFilter = interceptorFilter;
}

@Override
Expand All @@ -52,8 +58,10 @@ public void afterPropertiesSet() {
}

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

private List<ServerInterceptor> findGlobalInterceptors() {
Expand All @@ -62,13 +70,18 @@ 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, serverFactory));
}
if (serviceInfo == null) {
return ServerInterceptors.interceptForward(serviceDef, this.globalInterceptors);
return ServerInterceptors.interceptForward(serviceDef, allInterceptors);
}
// Add global interceptors first
List<ServerInterceptor> allInterceptors = new ArrayList<>(this.globalInterceptors);
// Add interceptors by type
Arrays.stream(serviceInfo.interceptors())
.forEachOrdered(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.grpc.server.service;

import org.springframework.grpc.server.GrpcServerFactory;

import io.grpc.ServerServiceDefinition;

/**
Expand All @@ -29,12 +31,14 @@
public interface GrpcServiceConfigurer {

/**
* Configure and bind a gRPC service spec resulting in a service definition that can
* Configure and bind a gRPC server spec resulting in a service definition that can
* then be added to a gRPC server.
* @param serviceSpec the spec containing the info about the service
* @return bound and configured service definition that is ready to be added to the
* @param serverFactory the factory that provides the server that the service will be
* added to
* @return bound and configured service definition that is ready to be added to a
* server
*/
ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceSpec);
ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceSpec, GrpcServerFactory serverFactory);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.service;

import org.springframework.grpc.server.GrpcServerFactory;

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

/**
* Strategy to determine whether a global {@link ServerInterceptor server interceptor}
* should be applied to {@link ServerServiceDefinition gRPC service}.
*
* @author Chris Bono
*/
@FunctionalInterface
public interface ServerInterceptorFilter {

/**
* Determine whether an interceptor should be applied to a service when the service is
* running on a server provided by the given server factory.
* @param interceptor the server interceptor under consideration.
* @param service the service being added.
* @param serverFactory the server factory in use.
* @return {@code true} if the interceptor should be included; {@code false}
* otherwise.
*/
boolean filter(ServerInterceptor interceptor, ServerServiceDefinition service, GrpcServerFactory serverFactory);

}
Loading