MetadataUtil { public static void fillHeaders(Metadata metadata, HttpHeadersBuilder builder) { if (InternalMetadata.headerCount(metadata) == 0) { return; } final byte[][] serializedMetadata = InternalMetadata.serialize(metadata); assert serializedMetadata.length % 2 == 0; for (int i = 0; i < serializedMetadata.length; i += 2) { final AsciiString name = new AsciiString(serializedMetadata[i], false); if (STRIPPED_HEADERS.contains(name)) { continue; } final byte[] valueBytes = serializedMetadata[i + 1]; final String value; if (isBinary(name)) { value = BASE64_ENCODING_OMIT_PADDING.encode(valueBytes); } else if (isGrpcAscii(valueBytes)) { value = new String(valueBytes, StandardCharsets.US_ASCII); } else { logger.warn("Metadata name=" + name + ", value=" + Arrays.toString(valueBytes) + " contains invalid ASCII characters, skipping."); continue; } builder.add(name, value); } } private  MetadataUtil(); static void fillHeaders(Metadata metadata, HttpHeadersBuilder builder); static Metadata copyFromHeaders(HttpHeaders headers);  }
@Test void fillHeadersTest() { final HttpHeadersBuilder trailers = ResponseHeaders.builder() .endOfStream(true) .add(HttpHeaderNames.STATUS, HttpStatus.OK.codeAsText()) .add(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto") .add(GrpcHeaderNames.GRPC_STATUS, "3") .add(GrpcHeaderNames.GRPC_MESSAGE, "test_grpc_message"); final Metadata metadata = new Metadata(); metadata.put(TEST_ASCII_KEY, "metadata_test_string"); metadata.put(TEST_BIN_KEY, "metadata_test_string".getBytes()); metadata.put(STATUS_KEY, "200"); metadata.put(InternalStatus.CODE_KEY, Status.OK); metadata.put(InternalStatus.MESSAGE_KEY, "grpc_message_must_not_be_copied"); metadata.put(THROWABLE_PROTO_METADATA_KEY, THROWABLE_PROTO); MetadataUtil.fillHeaders(metadata, trailers); assertThat(trailers.getAll(TEST_ASCII_KEY.originalName())).containsExactly("metadata_test_string"); assertThat(Base64.getDecoder().decode(trailers.get(TEST_BIN_KEY.originalName()))) .containsExactly("metadata_test_string".getBytes()); assertThat(trailers.getAll(HttpHeaderNames.STATUS)).containsExactly(HttpStatus.OK.codeAsText()); assertThat(trailers.getAll(HttpHeaderNames.CONTENT_TYPE)).containsExactly("application/grpc+proto"); assertThat(trailers.getAll(GrpcHeaderNames.GRPC_STATUS)).containsExactly("3"); assertThat(trailers.getAll(GrpcHeaderNames.GRPC_MESSAGE)).containsOnly("test_grpc_message"); assertThat(trailers.getAll(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN)).isEmpty(); }