|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.streams; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.kafka.common.header.Headers; |
| 24 | +import org.apache.kafka.common.header.internals.RecordHeader; |
| 25 | +import org.apache.kafka.streams.KeyValue; |
| 26 | +import org.apache.kafka.streams.kstream.Transformer; |
| 27 | +import org.apache.kafka.streams.processor.ProcessorContext; |
| 28 | + |
| 29 | +import org.springframework.expression.Expression; |
| 30 | + |
| 31 | +/** |
| 32 | + * Manipulate the headers. |
| 33 | + * |
| 34 | + * @param <K> the key type. |
| 35 | + * @param <V> the value type. |
| 36 | + * |
| 37 | + * @author Gary Russell |
| 38 | + * @since 2.3 |
| 39 | + * |
| 40 | + */ |
| 41 | +public class HeaderEnricher<K, V> implements Transformer<K, V, KeyValue<K, V>> { |
| 42 | + |
| 43 | + private final Map<String, Expression> headerExpressions = new HashMap<>(); |
| 44 | + |
| 45 | + private ProcessorContext processorContext; |
| 46 | + |
| 47 | + public HeaderEnricher(Map<String, Expression> headerExpressions) { |
| 48 | + this.headerExpressions.putAll(headerExpressions); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void init(ProcessorContext context) { |
| 53 | + this.processorContext = context; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public KeyValue<K, V> transform(K key, V value) { |
| 58 | + Headers headers = this.processorContext.headers(); |
| 59 | + Container<K, V> container = new Container<>(this.processorContext, key, value); |
| 60 | + this.headerExpressions.forEach((name, expression) -> { |
| 61 | + Object headerValue = expression.getValue(container); |
| 62 | + if (headerValue instanceof String) { |
| 63 | + headerValue = ((String) headerValue).getBytes(StandardCharsets.UTF_8); |
| 64 | + } |
| 65 | + else if (!(headerValue instanceof byte[])) { |
| 66 | + throw new IllegalStateException("Invalid header value type" + headerValue.getClass()); |
| 67 | + } |
| 68 | + headers.add(new RecordHeader(name, (byte[]) headerValue)); |
| 69 | + }); |
| 70 | + return new KeyValue<>(key, value); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void close() { |
| 75 | + // NO-OP |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Container object for SpEL evaluation. |
| 80 | + * |
| 81 | + * @param <K> the key type. |
| 82 | + * @param <V> the value type. |
| 83 | + * |
| 84 | + */ |
| 85 | + public static final class Container<K, V> { |
| 86 | + |
| 87 | + private final ProcessorContext context; |
| 88 | + |
| 89 | + private final K key; |
| 90 | + |
| 91 | + private final V value; |
| 92 | + |
| 93 | + private Container(ProcessorContext context, K key, V value) { |
| 94 | + this.context = context; |
| 95 | + this.key = key; |
| 96 | + this.value = value; |
| 97 | + } |
| 98 | + |
| 99 | + public ProcessorContext getContext() { |
| 100 | + return this.context; |
| 101 | + } |
| 102 | + |
| 103 | + public K getKey() { |
| 104 | + return this.key; |
| 105 | + } |
| 106 | + |
| 107 | + public V getValue() { |
| 108 | + return this.value; |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments