From 460a4b670b83c7ed15e26377428f586eec1db02f Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 7 Jun 2024 09:59:49 -0400 Subject: [PATCH] GH-9191: Support `byte[]` for headers in the `MessageReceiverContext` Fixes: #9191 Not all inbound protocol handlers convert header from native representation. Sometimes they just come in as `byte[]`. * Fix `MessageReceiverContext` to support `byte[]` for headers used in the observation, e.g. `B3-*`, `traceparent` etc. (cherry picked from commit b29ff9a7108b5b63e8f109e9deb531f9b2100ed5) --- .../observation/MessageReceiverContext.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/MessageReceiverContext.java b/spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/MessageReceiverContext.java index f3bb801de1..c7b26164a4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/MessageReceiverContext.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/MessageReceiverContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2022-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.integration.support.management.observation; +import java.nio.charset.StandardCharsets; + import io.micrometer.observation.transport.ReceiverContext; import org.springframework.lang.Nullable; @@ -35,7 +37,7 @@ public class MessageReceiverContext extends ReceiverContext> { private final String handlerName; public MessageReceiverContext(Message message, @Nullable String handlerName) { - super((carrier, key) -> carrier.getHeaders().get(key, String.class)); + super(MessageReceiverContext::getHeader); this.message = message; this.handlerName = handlerName != null ? handlerName : "unknown"; } @@ -49,4 +51,12 @@ public class MessageReceiverContext extends ReceiverContext> { return this.handlerName; } + @Nullable + private static String getHeader(Message message, String key) { + Object value = message.getHeaders().get(key); + return value instanceof byte[] bytes + ? new String(bytes, StandardCharsets.UTF_8) + : (value != null ? value.toString() : null); + } + }