-
Notifications
You must be signed in to change notification settings - Fork 215
/
MessageMapper.java
149 lines (132 loc) · 5.49 KB
/
MessageMapper.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
/*
* 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.Optional;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.api.ExternalMessage;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.MessageMapperConfigurationInvalidException;
import org.eclipse.ditto.connectivity.service.config.ConnectivityConfig;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.protocol.Adaptable;
import akka.actor.ActorSystem;
/**
* Defines a message mapper which maps a {@link ExternalMessage} to a {@link Adaptable} and vice versa.
* <p>
* Usually a mapper is bound to a content type.
* </p>
* A message mapper is considered to be dynamically instantiated at runtime, it therefore can only be configured at
* runtime.
*/
public interface MessageMapper {
/**
* Returns a unique ID of this mapper that can be used in sources and targets to reference this mapper.
*
* @return a unique ID of this mapper.
*/
String getId();
/**
* Returns a blocklist of content-types which shall not be handled by this message mapper.
* Is determined from the passed in {@code MessageMapperConfiguration} in
* {@link #configure(Connection, ConnectivityConfig, MessageMapperConfiguration, ActorSystem)}
*
* @return a blocklist of content-types which shall not be handled by this message mapper.
*/
Collection<String> getContentTypeBlocklist();
/**
* Applies configuration for this MessageMapper.
*
* @param connection the connection
* @param connectivityConfig the connectivity config related to the given connection.
* @param configuration the configuration to apply.
* @param actorSystem the actor system.
* @throws MessageMapperConfigurationInvalidException if configuration is invalid.
* @throws org.eclipse.ditto.connectivity.model.MessageMapperConfigurationFailedException if the configuration
* failed for a mapper specific reason.
*/
void configure(Connection connection, ConnectivityConfig connectivityConfig,
MessageMapperConfiguration configuration, ActorSystem actorSystem);
/**
* Maps an {@link ExternalMessage} to an {@link Adaptable}
*
* @param message the ExternalMessage to map
* @return the mapped Adaptable or an empty List if the ExternalMessage should not be mapped after all
* @throws org.eclipse.ditto.connectivity.model.MessageMappingFailedException if the given message can not be mapped
* @throws org.eclipse.ditto.base.model.exceptions.DittoRuntimeException if anything during Ditto Adaptable creation
* went wrong
*/
List<Adaptable> map(ExternalMessage message);
/**
* Lets the mapper implementation calculate additional DittoHeaders to set for an incoming inbound
* {@code externalMessage}.
*
* @return the additional DittoHeaders to inject.
*/
DittoHeaders getAdditionalInboundHeaders(ExternalMessage message);
/**
* Maps an {@link Adaptable} to an {@link ExternalMessage}
*
* @param adaptable the Adaptable to map
* @return the ExternalMessage or an empty List if the Adaptable should not be mapped after all
* @throws org.eclipse.ditto.connectivity.model.MessageMappingFailedException if the given adaptable can not be mapped
*/
List<ExternalMessage> map(Adaptable adaptable);
/**
* @return a map of default options for this mapper
*/
default JsonObject getDefaultOptions() {
return JsonObject.empty();
}
/**
* Returns the conditions to be checked before mapping incoming messages.
*
* @return the conditions.
* @since 1.3.0
*/
Map<String, String> getIncomingConditions();
/**
* Returns the conditions to be checked before mapping outgoing messages.
*
* @return the conditions.
* @since 1.3.0
*/
Map<String, String> getOutgoingConditions();
/**
* Finds the content-type header from the passed ExternalMessage.
*
* @param externalMessage the ExternalMessage to look for the content-type header in
* @return the optional content-type value
*/
static Optional<String> findContentType(final ExternalMessage externalMessage) {
checkNotNull(externalMessage);
return externalMessage.findHeaderIgnoreCase(ExternalMessage.CONTENT_TYPE_HEADER);
}
/**
* Finds the content-type header from the passed Adaptable.
*
* @param adaptable the Adaptable to look for the content-type header in
* @return the optional content-type value
*/
static Optional<String> findContentType(final Adaptable adaptable) {
checkNotNull(adaptable);
return adaptable.getDittoHeaders().entrySet()
.stream()
.filter(e -> ExternalMessage.CONTENT_TYPE_HEADER.equalsIgnoreCase(e.getKey()))
.findFirst()
.map(Map.Entry::getValue);
}
}