Search in sources :

Example 1 with MessageContext

use of org.ballerinalang.net.grpc.MessageContext in project ballerina by ballerina-lang.

the class ServerHeaderInterceptor method interceptCall.

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
    MessageContext ctx = MessageContext.DATA_KEY.get();
    // Only initialize ctx if not yet initialized
    ctx = ctx != null ? ctx : new MessageContext();
    boolean found = false;
    for (String keyName : headers.keys()) {
        if (keyName.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
            Metadata.Key<byte[]> key = Metadata.Key.of(keyName, Metadata.BINARY_BYTE_MARSHALLER);
            Iterable<byte[]> values = headers.getAll(key);
            if (values == null) {
                continue;
            }
            for (byte[] value : values) {
                ctx.put(key, value);
            }
        } else {
            Metadata.Key<String> key = Metadata.Key.of(keyName, Metadata.ASCII_STRING_MARSHALLER);
            Iterable<String> values = headers.getAll(key);
            if (values == null) {
                continue;
            }
            for (String value : values) {
                ctx.put(key, value);
            }
        }
        found = true;
    }
    if (found) {
        return Contexts.interceptCall(Context.current().withValue(MessageContext.DATA_KEY, ctx), new HeaderForwardingServerCall<>(call), headers, next);
    } else {
        // Don't attach a context if there is nothing to attach
        return next.startCall(new HeaderForwardingServerCall<>(call), headers);
    }
}
Also used : Metadata(io.grpc.Metadata) MessageContext(org.ballerinalang.net.grpc.MessageContext)

Aggregations

Metadata (io.grpc.Metadata)1 MessageContext (org.ballerinalang.net.grpc.MessageContext)1