grpcstarter.server.GrpcContextKeys Maven / Gradle / Ivy
package grpcstarter.server;
import io.grpc.Context;
import io.grpc.Metadata;
import jakarta.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import lombok.experimental.UtilityClass;
/**
* @author Freeman
* @since 3.2.3
*/
@UtilityClass
public class GrpcContextKeys {
/**
* Modify gRPC response metadata, e.g. add custom headers/trailers.
*/
public static class ResponseMetadataModifier {
static final Context.Key INSTANCE =
Context.key("GrpcContextKeys.ResponseMetadataModifier");
final List> consumers = Collections.synchronizedList(new ArrayList<>());
/**
* Get {@link ResponseMetadataModifier} bound to current gRPC request.
*
* @return {@link ResponseMetadataModifier} bound to current gRPC request
*/
@Nullable
static ResponseMetadataModifier get() {
return INSTANCE.get();
}
/**
* Add a consumer to modify response headers/trailers.
*
* This method is thread-safe.
*
* @param consumer {@link Consumer} to modify response headers/trailers
*/
public static void addConsumer(Consumer consumer) {
ResponseMetadataModifier key = get();
if (key != null) {
key.consumers.add(consumer);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy