-
Notifications
You must be signed in to change notification settings - Fork 215
/
WrappingMessageMapper.java
182 lines (152 loc) · 5.91 KB
/
WrappingMessageMapper.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2017 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.mapping;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.UnaryOperator;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.ExternalMessageFactory;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.MessageMappingFailedException;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.connectivity.service.config.mapping.MapperLimitsConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.protocol.Adaptable;
import akka.actor.ActorSystem;
/**
* Enforce message size limits on a {@link MessageMapper} and adds random correlation IDs should they not be present
* in the mapped message.
*/
final class WrappingMessageMapper implements MessageMapper {
private int inboundMessageLimit;
private int outboundMessageLimit;
private final MessageMapper delegate;
private WrappingMessageMapper(final MessageMapper delegate) {
this.delegate = checkNotNull(delegate);
}
/**
* Enforces content type checking for the mapper
*
* @param mapper the mapper
* @return the wrapped mapper
*/
public static MessageMapper wrap(final MessageMapper mapper) {
return new WrappingMessageMapper(mapper);
}
@Override
public String getId() {
return delegate.getId();
}
@Override
public String getAlias() {
return delegate.getAlias();
}
@Override
public boolean isConfigurationMandatory() {
return delegate.isConfigurationMandatory();
}
@Override
public MessageMapper createNewMapperInstance() {
return delegate.createNewMapperInstance();
}
@Override
public Collection<String> getContentTypeBlocklist() {
return delegate.getContentTypeBlocklist();
}
@Override
public JsonObject getDefaultOptions() {
return delegate.getDefaultOptions();
}
@Override
public Map<String, String> getIncomingConditions() {
return delegate.getIncomingConditions();
}
@Override
public Map<String, String> getOutgoingConditions() {
return delegate.getOutgoingConditions();
}
/**
* @return the MessageMapper delegate this instance wraps.
*/
public MessageMapper getDelegate() {
return delegate;
}
@Override
public void configure(final Connection connection,
final ConnectivityConfig connectivityConfig,
final MessageMapperConfiguration configuration,
final ActorSystem actorSystem) {
final MapperLimitsConfig mapperLimitsConfig = connectivityConfig.getMappingConfig().getMapperLimitsConfig();
inboundMessageLimit = mapperLimitsConfig.getMaxMappedInboundMessages();
outboundMessageLimit = mapperLimitsConfig.getMaxMappedOutboundMessages();
delegate.configure(connection, connectivityConfig, configuration, actorSystem);
}
@Override
public List<Adaptable> map(final ExternalMessage message) {
return checkMaxMappedMessagesLimit(delegate.map(message), inboundMessageLimit, message.getInternalHeaders());
}
@Override
public DittoHeaders getAdditionalInboundHeaders(final ExternalMessage message) {
return delegate.getAdditionalInboundHeaders(message);
}
@Override
public List<ExternalMessage> map(final Adaptable adaptable) {
final var externalMessages = delegate.map(adaptable);
checkMaxMappedMessagesLimit(externalMessages, outboundMessageLimit, adaptable.getDittoHeaders());
final var isResponse = AbstractMessageMapper.isResponse(adaptable);
final UnaryOperator<ExternalMessage> markAsResponse =
externalMessage -> ExternalMessageFactory.newExternalMessageBuilder(externalMessage)
.asResponse(isResponse)
.build();
return externalMessages.stream()
.map(markAsResponse)
.toList();
}
private <T> List<T> checkMaxMappedMessagesLimit(final List<T> mappingResult, final int maxMappedMessages,
final DittoHeaders dittoHeaders) {
if (mappingResult.size() > maxMappedMessages) {
final var descFormat = "The payload mapping '%s' produced %d messages, which exceeds the limit of %d.";
throw MessageMappingFailedException.newBuilder(null)
.message("The number of messages produced by the payload mapping exceeded the limits.")
.dittoHeaders(dittoHeaders)
.description(String.format(descFormat, getId(), mappingResult.size(), maxMappedMessages))
.build();
}
return mappingResult;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final WrappingMessageMapper that = (WrappingMessageMapper) o;
return Objects.equals(delegate, that.delegate);
}
@Override
public int hashCode() {
return Objects.hash(delegate);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"delegate=" + delegate +
"]";
}
}