|
| 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.listener; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | + |
| 23 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 24 | + |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +/** |
| 28 | + * A {@link RecordInterceptor} that delegates to one or more {@link RecordInterceptor} in |
| 29 | + * order. |
| 30 | + * |
| 31 | + * @param <K> the key type. |
| 32 | + * @param <V> the value type. |
| 33 | + * |
| 34 | + * @author Artem Bilan |
| 35 | + * @author Gary Russell |
| 36 | + * @since 2.3 |
| 37 | + * |
| 38 | + */ |
| 39 | +public class CompositeRecordInterceptor<K, V> implements RecordInterceptor<K, V> { |
| 40 | + |
| 41 | + private final Collection<RecordInterceptor<K, V>> delegates = new ArrayList<>(); |
| 42 | + |
| 43 | + @SafeVarargs |
| 44 | + @SuppressWarnings("varargs") |
| 45 | + public CompositeRecordInterceptor(RecordInterceptor<K, V>... delegates) { |
| 46 | + Assert.notNull(delegates, "'delegates' cannot be null"); |
| 47 | + Assert.noNullElements(delegates, "'delegates' cannot have null entries"); |
| 48 | + this.delegates.addAll(Arrays.asList(delegates)); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public ConsumerRecord<K, V> intercept(ConsumerRecord<K, V> record) { |
| 53 | + ConsumerRecord<K, V> recordToIntercept = record; |
| 54 | + for (RecordInterceptor<K, V> delegate : this.delegates) { |
| 55 | + recordToIntercept = delegate.intercept(recordToIntercept); |
| 56 | + } |
| 57 | + return recordToIntercept; |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments