-
Notifications
You must be signed in to change notification settings - Fork 215
/
Resolvers.java
245 lines (219 loc) · 10.7 KB
/
Resolvers.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2019 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 java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.WithEntityId;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.Signal;
import org.eclipse.ditto.base.model.signals.WithFeatureId;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.api.placeholders.ConnectivityPlaceholders;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.placeholders.ExpressionResolver;
import org.eclipse.ditto.placeholders.Placeholder;
import org.eclipse.ditto.placeholders.PlaceholderFactory;
import org.eclipse.ditto.placeholders.PlaceholderResolver;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.TopicPath;
/**
* Creator of expression resolvers for incoming and outgoing messages.
*/
public final class Resolvers {
private Resolvers() {
throw new AssertionError();
}
/**
* Placeholder resolver creators for incoming and outgoing messages.
*/
private static final List<ResolverCreator<?>> RESOLVER_CREATORS = Arrays.asList(
// For incoming messages, header mapping injects headers of external messages into Ditto headers.
ResolverCreator.of(PlaceholderFactory.newHeadersPlaceholder(), (e, s, t, a, c) -> e),
ResolverCreator.of(ConnectivityPlaceholders.newEntityPlaceholder(),
(e, s, t, a, c) -> WithEntityId.getEntityIdOfType(EntityId.class, s).orElse(null)),
ResolverCreator.of(ConnectivityPlaceholders.newThingPlaceholder(),
(e, s, t, a, c) -> WithEntityId.getEntityIdOfType(EntityId.class, s).orElse(null)),
ResolverCreator.of(ConnectivityPlaceholders.newFeaturePlaceholder(), (e, s, t, a, c) -> {
if (s instanceof WithFeatureId) {
return ((WithFeatureId) s).getFeatureId();
} else {
return null;
}
}),
ResolverCreator.of(ConnectivityPlaceholders.newTopicPathPlaceholder(), (e, s, t, a, c) -> t),
ResolverCreator.of(ConnectivityPlaceholders.newResourcePlaceholder(), (e, s, t, a, c) -> s),
ResolverCreator.of(ConnectivityPlaceholders.newTimePlaceholder(), (e, s, t, a, c) -> new Object()),
ResolverCreator.of(ConnectivityPlaceholders.newRequestPlaceholder(), (e, s, t, a, c) -> a),
ResolverCreator.of(ConnectivityPlaceholders.newConnectionIdPlaceholder(), (e, s, t, a, c) -> c)
);
private static final List<Placeholder<?>> PLACEHOLDERS = Collections.unmodifiableList(
RESOLVER_CREATORS.stream()
.map(ResolverCreator::getPlaceholder)
.collect(Collectors.toList())
);
/**
* @return Array of all placeholders for target address and source/target header mappings.
*/
@SuppressWarnings({"rawtypes", "java:S3740"})
public static Placeholder[] getPlaceholders() {
return PLACEHOLDERS.toArray(new Placeholder[0]);
}
/**
* Create an expression resolver for an outbound message.
*
* @param mappedOutboundSignal the mapped external message.
* @param sendingConnectionId the ID of the connection sending the message.
* @return the expression resolver.
*/
public static ExpressionResolver forOutbound(final OutboundSignal.Mapped mappedOutboundSignal,
final ConnectionId sendingConnectionId) {
final Signal<?> signal = mappedOutboundSignal.getSource();
final ExternalMessage externalMessage = mappedOutboundSignal.getExternalMessage();
final Adaptable adaptable = mappedOutboundSignal.getAdaptable();
return PlaceholderFactory.newExpressionResolver(
RESOLVER_CREATORS.stream()
.map(creator -> creator.create(adaptable.getDittoHeaders(), signal,
externalMessage.getTopicPath().orElse(null),
signal.getDittoHeaders().getAuthorizationContext(),
sendingConnectionId))
.toArray(PlaceholderResolver[]::new)
);
}
/**
* Create an expression resolver for an signal.
*
* @param signal the signal.
* @param connectionId the ID of the connection that handles the signal
* @return the expression resolver.
* @since 1.3.0
*/
public static ExpressionResolver forSignal(final Signal<?> signal,
final ConnectionId connectionId) {
return PlaceholderFactory.newExpressionResolver(
RESOLVER_CREATORS.stream()
.map(creator -> creator.create(signal.getDittoHeaders(), signal,
null,
signal.getDittoHeaders().getAuthorizationContext(),
connectionId))
.toArray(PlaceholderResolver[]::new)
);
}
/**
* Create an expression resolver for an external Message.
*
* @param message the external message.
* @param receivingConnectionId the ID of the connection receiving the message.
* @return the expression resolver.
* @since 1.3.0
*/
public static ExpressionResolver forExternalMessage(final ExternalMessage message,
final ConnectionId receivingConnectionId) {
return PlaceholderFactory.newExpressionResolver(
RESOLVER_CREATORS.stream()
.map(creator -> creator.create(makeCaseInsensitive(message.getHeaders()), null,
message.getTopicPath().orElse(null),
message.getAuthorizationContext().orElse(null),
receivingConnectionId))
.toArray(PlaceholderResolver[]::new)
);
}
/**
* Create an expression resolver for an mappable outbound signal.
*
* @param outboundSignal the outbound signal.
* @param sendingConnectionId the ID of the connection sending the message.
* @return the expression resolver.
* @since 1.2.0
*/
public static ExpressionResolver forOutboundSignal(final OutboundSignal.Mappable outboundSignal, final
ConnectionId sendingConnectionId) {
return PlaceholderFactory.newExpressionResolver(
RESOLVER_CREATORS.stream()
.map(creator -> creator.create(outboundSignal.getSource().getDittoHeaders(),
outboundSignal.getSource(),
null,
outboundSignal.getSource().getDittoHeaders().getAuthorizationContext(),
sendingConnectionId))
.toArray(PlaceholderResolver[]::new)
);
}
/**
* Create an expression resolver for an inbound message.
*
* @param externalMessage the inbound external message.
* @param signal the mapped Ditto signal without internal headers.
* @param topicPath the topic path of the inbound message, or null if it cannot be determined.
* @param authorizationContext the authorization context of the inbound message, or null if it cannot be determined.
* @param connectionId The ID of the connection that received the inbound message.
* @return the expression resolver.
*/
public static ExpressionResolver forInbound(final ExternalMessage externalMessage, final Signal<?> signal,
@Nullable final TopicPath topicPath, @Nullable final AuthorizationContext authorizationContext,
@Nullable final ConnectionId connectionId) {
return PlaceholderFactory.newExpressionResolver(
RESOLVER_CREATORS.stream()
.map(creator ->
creator.create(makeCaseInsensitive(externalMessage.getHeaders()), signal, topicPath,
authorizationContext, connectionId))
.toArray(PlaceholderResolver[]::new)
);
}
private static DittoHeaders makeCaseInsensitive(final Map<String, String> externalHeaders) {
return DittoHeaders.of(externalHeaders);
}
/**
* Extract data for a placeholder.
*
* @param <T> the data required by the placeholder.
*/
@FunctionalInterface
private interface ResolverDataExtractor<T> {
@Nullable
T extract(Map<String, String> inputHeaders, @Nullable Signal<?> signal, @Nullable TopicPath topicPath,
@Nullable AuthorizationContext authorizationContext, @Nullable final ConnectionId connectionId);
}
/**
* Creator of a placeholder resolver.
* Must be an inner class due to the lack of existential types in Java 8.
*
* @param <T> the data required by the placeholder.
*/
private static final class ResolverCreator<T> {
private final Placeholder<T> placeholder;
private final ResolverDataExtractor<T> dataExtractor;
private ResolverCreator(final Placeholder<T> placeholder, final ResolverDataExtractor<T> dataExtractor) {
this.placeholder = placeholder;
this.dataExtractor = dataExtractor;
}
private static <T> ResolverCreator<T> of(final Placeholder<T> placeholder,
final ResolverDataExtractor<T> dataExtractor) {
return new ResolverCreator<>(placeholder, dataExtractor);
}
private PlaceholderResolver<T> create(final Map<String, String> inputHeaders, @Nullable final Signal<?> signal,
@Nullable final TopicPath topicPath, @Nullable final AuthorizationContext authorizationContext,
@Nullable ConnectionId connectionId) {
return PlaceholderFactory.newPlaceholderResolver(placeholder,
dataExtractor.extract(inputHeaders, signal, topicPath, authorizationContext, connectionId));
}
private Placeholder<T> getPlaceholder() {
return placeholder;
}
}
}