Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Adven27 committed Oct 25, 2021
1 parent 98d2918 commit 6e778bf
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.adven.grpc.wiremock;

import com.google.common.io.BaseEncoding;
import io.grpc.Context;
import io.grpc.Contexts;
import io.grpc.Metadata;
Expand All @@ -10,12 +11,15 @@
import java.util.Map;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
import static io.grpc.Metadata.BINARY_BYTE_MARSHALLER;
import static io.grpc.Metadata.BINARY_HEADER_SUFFIX;
import static io.grpc.Metadata.Key.of;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;

public class HeaderPropagationInterceptor implements ServerInterceptor {
public static final Context.Key<Map<String, String>> HEADERS = Context.key("GRPC_WIREMOCK_HEADERS");
private static final BaseEncoding BASE64_ENCODING_OMIT_PADDING = BaseEncoding.base64().omitPadding();

@Override
public <Req, Resp> ServerCall.Listener<Req> interceptCall(ServerCall<Req, Resp> call, final Metadata headers, ServerCallHandler<Req, Resp> next) {
Expand All @@ -28,6 +32,13 @@ public <Req, Resp> ServerCall.Listener<Req> interceptCall(ServerCall<Req, Resp>
}

private Map<String, String> asMap(Metadata headers) {
return headers.keys().stream().collect(toMap(identity(), k -> headers.get(of(k, ASCII_STRING_MARSHALLER))));
return headers.keys().stream().collect(
toMap(
identity(),
k -> k.endsWith(BINARY_HEADER_SUFFIX)
? BASE64_ENCODING_OMIT_PADDING.encode(headers.get(of(k, BINARY_BYTE_MARSHALLER)))
: headers.get(of(k, ASCII_STRING_MARSHALLER))
)
);
}
}

0 comments on commit 6e778bf

Please sign in to comment.