/
KafkaMessageTransformer.java
114 lines (100 loc) · 5.31 KB
/
KafkaMessageTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.kafka;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.header.Header;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.model.EnforcementFilterFactory;
import org.eclipse.ditto.connectivity.model.Source;
import org.eclipse.ditto.connectivity.service.messaging.monitoring.ConnectionMonitor;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLogger;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import scala.util.Either;
import scala.util.Left;
import scala.util.Right;
@Immutable
final class KafkaMessageTransformer {
private static final DittoLogger LOGGER = DittoLoggerFactory.getLogger(KafkaMessageTransformer.class);
private final Source source;
private final String sourceAddress;
private final EnforcementFilterFactory<Map<String, String>, Signal<?>> headerEnforcementFilterFactory;
private final ConnectionMonitor inboundMonitor;
public KafkaMessageTransformer(final Source source, final String sourceAddress,
final EnforcementFilterFactory<Map<String, String>, Signal<?>> headerEnforcementFilterFactory,
final ConnectionMonitor inboundMonitor) {
this.source = source;
this.sourceAddress = sourceAddress;
this.headerEnforcementFilterFactory = headerEnforcementFilterFactory;
this.inboundMonitor = inboundMonitor;
}
/**
* Takes incoming kafka record and transforms the value to an {@link ExternalMessage}.
*
* @param consumerRecord the kafka record.
* @return A value that is either an {@link ExternalMessage} in case the
* transformation succeeded, or a {@link DittoRuntimeException} if it failed. Could also be null if an unexpected
* Exception occurred which should result in the message being dropped as automated recovery is expected.
*/
@Nullable
public Either<ExternalMessage, DittoRuntimeException> transform(
final ConsumerRecord<String, String> consumerRecord) {
final String key = consumerRecord.key();
final String value = consumerRecord.value();
final Map<String, String> messageHeaders = StreamSupport.stream(consumerRecord.headers().spliterator(), false)
.collect(Collectors.toMap(Header::key, header -> new String(header.value())));
final String correlationId = messageHeaders
.getOrDefault(DittoHeaderDefinition.CORRELATION_ID.getKey(), UUID.randomUUID().toString());
try {
final DittoLogger correlationIdScopedLogger = LOGGER.withCorrelationId(correlationId);
correlationIdScopedLogger.info(
"Transforming incoming kafka message with headers <{}> for thing with ID <{}>.",
messageHeaders, key);
if (correlationIdScopedLogger.isDebugEnabled()) {
correlationIdScopedLogger.debug(
"Transforming incoming kafka message <{}> with headers <{}> for thing with ID <{}>.",
value, messageHeaders, key);
}
final ExternalMessage externalMessage = ExternalMessageFactory.newExternalMessageBuilder(messageHeaders)
.withTextAndBytes(value, value == null ? null : value.getBytes())
.withAuthorizationContext(source.getAuthorizationContext())
.withEnforcement(headerEnforcementFilterFactory.getFilter(messageHeaders))
.withHeaderMapping(source.getHeaderMapping())
.withSourceAddress(sourceAddress)
.withPayloadMapping(source.getPayloadMapping())
.build();
inboundMonitor.success(externalMessage);
return new Left<>(externalMessage);
} catch (final DittoRuntimeException e) {
LOGGER.withCorrelationId(e)
.info("Got DittoRuntimeException '{}' when command was parsed: {}", e.getErrorCode(),
e.getMessage());
return new Right<>(e.setDittoHeaders(DittoHeaders.of(messageHeaders)));
} catch (final Exception e) {
inboundMonitor.exception(messageHeaders, e);
LOGGER.withCorrelationId(correlationId)
.error(String.format("Unexpected {%s}: {%s}", e.getClass().getName(), e.getMessage()), e);
return null; // Drop message
}
}
}