Java Code Examples for org.apache.kafka.common.header.Headers#lastHeader()

The following examples show how to use org.apache.kafka.common.header.Headers#lastHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonSchemaKafkaDeserializer.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the message type from the headers.  Throws if not found.
 *
 * @param headers the headers
 */
@SuppressWarnings("unchecked")
protected Class<T> getMessageType(Headers headers) {
    Header header = headers.lastHeader(JsonSchemaSerDeConstants.HEADER_MSG_TYPE);
    if (header == null) {
        throw new RuntimeException("Message Type not found in headers.");
    }
    String msgTypeName = IoUtil.toString(header.value());
    
    try {
        return (Class<T>) Thread.currentThread().getContextClassLoader().loadClass(msgTypeName);
    } catch (ClassNotFoundException ignored) {
    }
    try {
        return (Class<T>) Class.forName(msgTypeName);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: BinderHeaderMapper.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Nullable
private Map<String, String> decodeJsonTypes(Headers source) {
	Map<String, String> types = null;
	Header jsonTypes = source.lastHeader(JSON_TYPES);
	if (jsonTypes != null) {
		ObjectMapper headerObjectMapper = getObjectMapper();
		try {
			types = headerObjectMapper.readValue(jsonTypes.value(), Map.class);
		}
		catch (IOException e) {
			logger.error(e, () -> "Could not decode json types: " + new String(jsonTypes.value()));
		}
	}
	return types;
}
 
Example 3
Source File: JsonSchemaKafkaDeserializer.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the global id from the headers.  Returns null if not found.
 *
 * @param headers the headers
 */
protected Long getGlobalId(Headers headers) {
    Header header = headers.lastHeader(JsonSchemaSerDeConstants.HEADER_GLOBAL_ID);
    if (header == null) {
        return null;
    }
    return ByteBuffer.wrap(header.value()).getLong();
}
 
Example 4
Source File: JsonSchemaKafkaDeserializer.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the artifact id from the headers.  Throws if not found.
 *
 * @param headers the headers
 */
protected String getArtifactId(Headers headers) {
    Header header = headers.lastHeader(JsonSchemaSerDeConstants.HEADER_ARTIFACT_ID);
    if (header == null) {
        throw new RuntimeException("ArtifactId not found in headers.");
    }
    return IoUtil.toString(header.value());
}
 
Example 5
Source File: JsonSchemaKafkaDeserializer.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the artifact version from the headers.  Returns null if not found.
 *
 * @param headers the headers
 */
protected Integer getVersion(Headers headers) {
    Header header = headers.lastHeader(JsonSchemaSerDeConstants.HEADER_VERSION);
    if (header == null) {
        return null;
    }
    return ByteBuffer.wrap(header.value()).getInt();
}
 
Example 6
Source File: CryptoSerializer.java    From kafka-encryption with Apache License 2.0 5 votes vote down vote up
/**
 * serialize data with encryption (if needed). Key reference will be looked for exclusively in the Kafka Header
 * {@link KafkaCryptoConstants#KEY_REF_HEADER}
 *
 * @param topic
 * @param headers
 * @param data
 * @return
 */
@Override
public byte[] serialize(String topic, Headers headers, T data) {
    byte[] serializedData = rawSerializer.serialize(topic, headers, data);
    if (serializedData == null) {
        return null;
    }
    Header keyReferenceHeader = headers.lastHeader(KEY_REF_HEADER);
    return encrypt(serializedData, keyReferenceHeader == null ? null : keyReferenceHeader.value());
}
 
Example 7
Source File: KafkaHeaders.java    From sdk-java with Apache License 2.0 5 votes vote down vote up
public static String getParsedKafkaHeader(Headers headers, String key) {
    Header h = headers.lastHeader(key);
    if (h == null) {
        return null;
    }
    return new String(h.value(), StandardCharsets.UTF_8);
}
 
Example 8
Source File: KafkaAvroDeserializer.java    From registry with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(String topic, Headers headers, byte[] data) {
    if (headers != null) {
        final Header header = headers.lastHeader(isKey ? keySchemaVersionIdHeaderName : valueSchemaVersionIdHeaderName);
        if (header != null) {
            return messageAndMetadataAvroDeserializer.deserialize(new MessageAndMetadata(header.value(), data), readerVersions.get(topic));
        }
    }
    return deserialize(topic, data);
}
 
Example 9
Source File: DefaultKafkaMessageConverter.java    From extension-kafka with Apache License 2.0 4 votes vote down vote up
private Optional<EventMessage<?>> buildMessage(Headers headers, SerializedMessage<?> message) {
    long timestamp = valueAsLong(headers, MESSAGE_TIMESTAMP);
    return headers.lastHeader(AGGREGATE_ID) != null
            ? buildDomainEvent(headers, message, timestamp)
            : buildEvent(message, timestamp);
}
 
Example 10
Source File: KafkaDecoder.java    From synapse with Apache License 2.0 4 votes vote down vote up
private String lastHeader(final Headers headers, final String key) {
    final Header header = headers.lastHeader(key);
    return header != null ? toString(header.value()) : null;
}
 
Example 11
Source File: KafkaHeaders.java    From brave with Apache License 2.0 4 votes vote down vote up
@Nullable static String lastStringHeader(Headers headers, String key) {
  Header header = headers.lastHeader(key);
  if (header == null || header.value() == null) return null;
  return new String(header.value(), UTF_8);
}
 
Example 12
Source File: KafkaHeaders.java    From brave with Apache License 2.0 4 votes vote down vote up
@Nullable static String lastStringHeader(Headers headers, String key) {
  Header header = headers.lastHeader(key);
  if (header == null || header.value() == null) return null;
  return new String(header.value(), UTF_8);
}
 
Example 13
Source File: HeaderUtils.java    From extension-kafka with Apache License 2.0 3 votes vote down vote up
/**
 * Return the {@code value} stored under a given {@code key} inside the {@link Headers}. In case of missing entry
 * {@code null} is returned.
 *
 * @param headers the Kafka {@code headers} to pull the value from
 * @param key     the key corresponding to the expected value
 * @return the value corresponding to the given {@code key} in the {@code headers}
 */
@SuppressWarnings("ConstantConditions") // Null check performed by `Assert.isTrue`
public static byte[] value(Headers headers, String key) {
    isTrue(headers != null, () -> "Headers may not be null");
    Header header = headers.lastHeader(key);
    return header != null ? header.value() : null;
}