io.quarkus.grpc.runtime.Interceptors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-grpc Show documentation
Show all versions of quarkus-grpc Show documentation
Serve and consume gRPC services
package io.quarkus.grpc.runtime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import jakarta.enterprise.inject.Any;
import jakarta.enterprise.inject.spi.Prioritized;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
public final class Interceptors {
public static final int DUPLICATE_CONTEXT = Integer.MAX_VALUE - 5;
public static final int EXCEPTION_HANDLER = Integer.MAX_VALUE - 10;
public static final int REQUEST_CONTEXT = Integer.MAX_VALUE - 50;
static List getSortedPerServiceInterceptors(String name, Set> interceptorClasses) {
if (interceptorClasses.isEmpty()) {
return Collections.emptyList();
}
List interceptors = new ArrayList<>();
for (Class> interceptorClass : interceptorClasses) {
// Note that additional qualifiers are ignored - @Any is required
InstanceHandle> interceptorInstance = Arc.container().instance(interceptorClass, Any.Literal.INSTANCE);
@SuppressWarnings("unchecked")
T interceptor = (T) interceptorInstance.get();
if (interceptor == null) {
throw new IllegalArgumentException("Interceptor class " + interceptorClass + " is not a CDI bean. " +
"Only CDI beans can be used as gRPC server/client interceptors. Add one of the scope-defining annotations"
+
" (@Singleton, @ApplicationScoped, @RequestScoped) on the interceptor class.");
}
interceptors.add(interceptor);
}
interceptors.sort(Interceptors.INTERCEPTOR_COMPARATOR);
return interceptors;
}
static List getSortedPerServiceInterceptors(Set> interceptorClasses) {
if (interceptorClasses.isEmpty()) {
return Collections.emptyList();
}
List interceptors = new ArrayList<>();
for (Class> interceptorClass : interceptorClasses) {
// Note that additional qualifiers are ignored - @Any is required
InstanceHandle> interceptorInstance = Arc.container().instance(interceptorClass, Any.Literal.INSTANCE);
@SuppressWarnings("unchecked")
T interceptor = (T) interceptorInstance.get();
if (interceptor == null) {
throw new IllegalArgumentException("Interceptor class " + interceptorClass + " is not a CDI bean. " +
"Only CDI beans can be used as gRPC server/client interceptors. Add one of the scope-defining annotations"
+
" (@Singleton, @ApplicationScoped, @RequestScoped) on the interceptor class.");
}
interceptors.add(interceptor);
}
interceptors.sort(Interceptors.INTERCEPTOR_COMPARATOR);
return interceptors;
}
static List getSortedGlobalInterceptors(Set> globalInterceptors) {
if (globalInterceptors.isEmpty()) {
return Collections.emptyList();
}
List interceptors = new ArrayList<>();
for (Class> interceptorClass : globalInterceptors) {
// Note that additional qualifiers are ignored - @Any is required
InstanceHandle> interceptorInstance = Arc.container().instance(interceptorClass, Any.Literal.INSTANCE);
@SuppressWarnings("unchecked")
T serverInterceptor = (T) interceptorInstance.get();
if (serverInterceptor == null) {
throw new IllegalArgumentException("Interceptor class " + interceptorClass + " is not a CDI bean. " +
"Only CDI beans can be used as gRPC server/client interceptors. Add one of the scope-defining annotations"
+
" (@Singleton, @ApplicationScoped, @RequestScoped) on the interceptor class.");
}
interceptors.add(serverInterceptor);
}
interceptors.sort(Interceptors.INTERCEPTOR_COMPARATOR);
return interceptors;
}
static final Comparator