-
Notifications
You must be signed in to change notification settings - Fork 214
/
OutboundSignalToExternalMessage.java
108 lines (93 loc) · 4.35 KB
/
OutboundSignalToExternalMessage.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
/*
* Copyright (c) 2020 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;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.model.HeaderMapping;
import org.eclipse.ditto.internal.utils.pekko.logging.DittoLoggerFactory;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.placeholders.ExpressionResolver;
import org.eclipse.ditto.placeholders.PlaceholderFilter;
/**
* Applies an optional "header mapping" potentially configured on a target on a passed {@link OutboundSignal.Mapped}.
*
* @since 1.3.0
*/
@Immutable
final class OutboundSignalToExternalMessage implements Supplier<ExternalMessage> {
private final OutboundSignal.Mapped outboundMappedSignal;
private final ExpressionResolver expressionResolver;
private final Map<String, String> headerMapping;
private final ThreadSafeDittoLogger logger;
private OutboundSignalToExternalMessage(final OutboundSignal.Mapped outboundMappedSignal,
final ExpressionResolver expressionResolver, final Map<String, String> headerMapping) {
this.outboundMappedSignal = checkNotNull(outboundMappedSignal, "outboundMappedSignal");
this.expressionResolver = checkNotNull(expressionResolver, "expressionResolver");
this.headerMapping = headerMapping;
logger = DittoLoggerFactory.getThreadSafeLogger(getClass()).withCorrelationId(outboundMappedSignal.getSource());
}
/**
* Returns a new instance of OutboundSignalToExternalMessage.
*
* @param outboundMappedSignal the OutboundSignal containing the {@link ExternalMessage} with headers potentially
* containing placeholders
* @param expressionResolver the expression-resolver used to resolve placeholders and optionally pipeline stages
* (functions).
* @param headerMapping optional header mappings to apply.
* @return the instance.
* @throws NullPointerException if {@code outboundMappedSignal} or {@code expressionResolver} is {@code null}.
*/
static OutboundSignalToExternalMessage newInstance(final OutboundSignal.Mapped outboundMappedSignal,
final ExpressionResolver expressionResolver, @Nullable final HeaderMapping headerMapping) {
final Map<String, String> mapping;
if (null != headerMapping) {
mapping = headerMapping.getMapping();
} else {
mapping = Map.of();
}
return new OutboundSignalToExternalMessage(outboundMappedSignal, expressionResolver, mapping);
}
@Override
public ExternalMessage get() {
final ExternalMessage originalMessage = outboundMappedSignal.getExternalMessage();
final ExternalMessage result;
if (headerMapping.isEmpty()) {
result = originalMessage;
} else {
result = ExternalMessageFactory.newExternalMessageBuilder(originalMessage)
.withAdditionalHeaders(mapHeaders())
.build();
}
return result;
}
private Map<String, String> mapHeaders() {
final Map<String, String> result = new HashMap<>();
headerMapping.forEach(
(key, value) -> mapHeaderByResolver(value).ifPresent(resolvedValue -> result.put(key, resolvedValue)));
if (logger.isDebugEnabled()) {
logger.debug("Result of header mapping <{}> are these headers to be published: {}", headerMapping, result);
}
return result;
}
private Optional<String> mapHeaderByResolver(final String value) {
return PlaceholderFilter.applyOrElseDelete(value, expressionResolver);
}
}