All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.devonfw.module.kafka.common.messaging.util.MessageUtil Maven / Gradle / Ivy

There is a newer version: 2023.01.001
Show newest version
package com.devonfw.module.kafka.common.messaging.util;

import java.nio.charset.Charset;
import java.util.Optional;

import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;

/**
 * An utility class with support methods.
 *
 * @deprecated The implementation of devon4j-kafka will be abandoned. It is superseeded by Springs Kafka
 *             implementation.
 */
@Deprecated
public class MessageUtil {

  private static final Charset UTF_8 = Charset.forName("UTF-8");

  private MessageUtil() {

  }

  /**
   * This method is used to add header value to the {@link Headers} with the value as bytes in the UTF_8 charset.
   *
   * @param headers the {@link Headers}
   * @param key the key
   * @param value the value.
   */
  public static void addHeaderValue(Headers headers, String key, String value) {

    if (key != null && value != null && !value.isEmpty()) {
      headers.add(key, value.getBytes(UTF_8));
    }
  }

  /**
   * This method is used to get the last header for the given traceIdName from {@link Headers}
   *
   * @param headers the {@link Headers}
   * @param traceIdName the traceId Name
   * @return the String.
   */
  public static String getHeaderValue(Headers headers, String traceIdName) {

    Header header = headers.lastHeader(traceIdName);

    return Optional.ofNullable(header).map(value -> new String(header.value(), UTF_8)).orElse(null);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy