Skip to content

Commit 3f7ca9b

Browse files
author
Mohit Agarwal
committed
Added parsing of binaryheaders in RequestContext
1 parent 5a184ef commit 3f7ca9b

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/RequestContextAsCreds.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
12+
import static io.grpc.Metadata.BINARY_BYTE_MARSHALLER;
1213

1314
public abstract class RequestContextAsCreds extends CallCredentials {
1415
private static Logger LOGGER = LoggerFactory.getLogger(RequestContextAsCreds.class);
@@ -31,7 +32,12 @@ protected void applyRequestContext(MetadataApplier applier, RequestContext reque
3132
for (Map.Entry<String, String> entry : requestContext.getAll().entrySet()) {
3233
// Exclude null headers
3334
if (entry.getValue() != null) {
34-
metadata.put(Metadata.Key.of(entry.getKey(), ASCII_STRING_MARSHALLER), entry.getValue());
35+
String key = entry.getKey();
36+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
37+
metadata.put(Metadata.Key.of(entry.getKey(), BINARY_BYTE_MARSHALLER), entry.getValue().getBytes());
38+
} else {
39+
metadata.put(Metadata.Key.of(entry.getKey(), ASCII_STRING_MARSHALLER), entry.getValue());
40+
}
3541
}
3642
}
3743
}

grpc-server-utils/src/main/java/org/hypertrace/core/grpcutils/server/RequestContextServerInterceptor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.hypertrace.core.grpcutils.context.RequestContext;
1010
import org.hypertrace.core.grpcutils.context.RequestContextConstants;
1111

12+
import java.nio.charset.StandardCharsets;
13+
1214
/**
1315
* Interceptor which intercepts the request headers to extract request context and sets it in the context so that the
1416
* server logic can use the context and they can be passed onto other downstream services.
@@ -41,7 +43,14 @@ RequestContext createRequestContextFromMetadata(Metadata metadata) {
4143
RequestContextConstants.HEADER_PREFIXES_TO_BE_PROPAGATED.stream()
4244
.anyMatch(prefix -> k.toLowerCase().startsWith(prefix.toLowerCase())))
4345
.forEach(k -> {
44-
String value = metadata.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
46+
String value;
47+
//check if key ends with binary suffix
48+
if (k.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
49+
byte[] bytes = metadata.get(Metadata.Key.of(k, Metadata.BINARY_BYTE_MARSHALLER));
50+
value = new String(bytes, StandardCharsets.UTF_8);
51+
} else {
52+
value = metadata.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
53+
}
4554
// The value could be null or empty for some keys so validate that.
4655
if (value != null && !value.isEmpty()) {
4756
requestContext.add(k, value);

0 commit comments

Comments
 (0)