|
| 1 | +/* |
| 2 | + * Copyright 2017-2018 original 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 | + * http://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 io.micronaut.configuration.kafka; |
| 18 | + |
| 19 | +import io.micronaut.core.convert.ArgumentConversionContext; |
| 20 | +import io.micronaut.core.convert.ConversionService; |
| 21 | +import io.micronaut.messaging.MessageHeaders; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.apache.kafka.common.header.Header; |
| 27 | +import org.apache.kafka.common.header.Headers; |
| 28 | + |
| 29 | +/** |
| 30 | + * A {@link MessageHeaders} implementation for Kafka. |
| 31 | + * |
| 32 | + * @author Graeme Rocher |
| 33 | + * @since 1.0 |
| 34 | + */ |
| 35 | +public class KafkaHeaders implements MessageHeaders { |
| 36 | + |
| 37 | + private final Headers headers; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructs a new instance for the given headers. |
| 41 | + * |
| 42 | + * @param headers The kafka headers |
| 43 | + */ |
| 44 | + public KafkaHeaders(Headers headers) { |
| 45 | + Objects.requireNonNull(headers, "Argument [headers] cannot be null"); |
| 46 | + this.headers = headers; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public List<String> getAll(CharSequence name) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String get(CharSequence name) { |
| 56 | + Header header = headers.lastHeader(name.toString()); |
| 57 | + if (header != null) { |
| 58 | + return new String(header.value()); |
| 59 | + } |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Set<String> names() { |
| 65 | + return Arrays.stream(headers.toArray()).map(Header::key).collect(Collectors.toSet()); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Collection<List<String>> values() { |
| 70 | + return names().stream().map(name -> { |
| 71 | + Iterable<Header> headers = KafkaHeaders.this.headers.headers(name); |
| 72 | + List<String> values = new ArrayList<>(); |
| 73 | + for (Header header : headers) { |
| 74 | + values.add(new String(header.value())); |
| 75 | + } |
| 76 | + return values; |
| 77 | + }).collect(Collectors.toList()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) { |
| 82 | + String v = get(name); |
| 83 | + if (v != null) { |
| 84 | + return ConversionService.SHARED.convert(v, conversionContext); |
| 85 | + } |
| 86 | + return Optional.empty(); |
| 87 | + } |
| 88 | +} |
0 commit comments