Class AbstractKafkaHeaderMapper

java.lang.Object
org.springframework.kafka.support.AbstractKafkaHeaderMapper
All Implemented Interfaces:
KafkaHeaderMapper
Direct Known Subclasses:
DefaultKafkaHeaderMapper, SimpleKafkaHeaderMapper

public abstract class AbstractKafkaHeaderMapper extends Object implements KafkaHeaderMapper
Base for Kafka header mappers.
Since:
2.1.3
Author:
Gary Russell, Artem Bilan, Soby Chacko
  • Field Details

  • Constructor Details

    • AbstractKafkaHeaderMapper

      public AbstractKafkaHeaderMapper(String... patterns)
      Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound). For outbound mapping, certain internal framework headers are never mapped.
      Parameters:
      patterns - the patterns.
    • AbstractKafkaHeaderMapper

      protected AbstractKafkaHeaderMapper(boolean outbound, String... patterns)
      Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound). For outbound mapping, certain internal framework headers are never mapped.
      Parameters:
      outbound - true for an outbound mapper.
      patterns - the patterns.
  • Method Details

    • addMatchers

      protected final void addMatchers(AbstractKafkaHeaderMapper.HeaderMatcher... matchersToAdd)
      Subclasses can invoke this to add custom AbstractKafkaHeaderMapper.HeaderMatchers.
      Parameters:
      matchersToAdd - the matchers to add.
      Since:
      2.3
    • setMapAllStringsOut

      public void setMapAllStringsOut(boolean mapAllStringsOut)
      Set to true to map all String valued outbound headers to byte[]. To map to a String for inbound, there must be an entry in the rawMappedHeaders map.
      Parameters:
      mapAllStringsOut - true to map all strings.
      Since:
      2.2.5
      See Also:
    • getCharset

      protected Charset getCharset()
    • setCharset

      public void setCharset(Charset charset)
      Set the charset to use when mapping String-valued headers to/from byte[]. Default UTF-8.
      Parameters:
      charset - the charset.
      Since:
      2.2.5
      See Also:
    • setRawMappedHeaders

      public void setRawMappedHeaders(Map<String,Boolean> rawMappedHeaders)
      Set the headers to not perform any conversion on (except String to byte[] for outbound). Inbound headers that match will be mapped as byte[] unless the corresponding boolean in the map value is true, in which case it will be mapped as a String. Headers mapped by default are: kafka_listenerInfo, b3, traceparent, X-B3-TraceId, X-B3-SpanId, X-B3-Sampled and X-B3-Flags.
      Parameters:
      rawMappedHeaders - the header names to not convert and
      Since:
      2.2.5
      See Also:
    • addRawMappedHeader

      public void addRawMappedHeader(String name, boolean toString)
      Add a raw mapped header.
      Parameters:
      name - the header name.
      toString - convert to string on inbound when true.
      Since:
      2.7.1
      See Also:
    • matches

      protected boolean matches(String header, Object value)
    • matches

      protected boolean matches(String header)
    • matchesForInbound

      protected boolean matchesForInbound(String header)
      Matches header names for inbound mapping when configured as an inbound mapper.
      Parameters:
      header - the header name.
      Returns:
      true if it can be mapped.
      Since:
      2.8.8
    • headerValueToAddOut

      protected Object headerValueToAddOut(String key, Object value)
      Check if the value is a String and convert to byte[], if so configured.
      Parameters:
      key - the header name.
      value - the header value.
      Returns:
      the value to add.
      Since:
      2.2.5
    • headerValueToAddIn

      protected Object headerValueToAddIn(org.apache.kafka.common.header.Header header)
      Check if the header value should be mapped to a String, if so configured.
      Parameters:
      header - the header.
      Returns:
      the value to add.