/
ScriptedIncomingMapping.java
111 lines (95 loc) · 5.06 KB
/
ScriptedIncomingMapping.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
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.mapping.javascript;
import java.nio.ByteBuffer;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.MessageMappingFailedException;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.ProtocolFactory;
import org.eclipse.ditto.services.models.connectivity.ExternalMessage;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.NativeJSON;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.RhinoException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.typedarrays.NativeArrayBuffer;
/**
* Mapping function for incoming messages based on JavaScript.
*/
public class ScriptedIncomingMapping implements MappingFunction<ExternalMessage, Optional<Adaptable>> {
private static final String EXTERNAL_MESSAGE_HEADERS = "headers";
private static final String EXTERNAL_MESSAGE_CONTENT_TYPE = "contentType";
private static final String EXTERNAL_MESSAGE_TEXT_PAYLOAD = "textPayload";
private static final String EXTERNAL_MESSAGE_BYTE_PAYLOAD = "bytePayload";
private static final String INCOMING_FUNCTION_NAME = "mapToDittoProtocolMsgWrapper";
@Nullable
private ContextFactory contextFactory;
@Nullable
private Scriptable scope;
ScriptedIncomingMapping(@Nullable final ContextFactory contextFactory, @Nullable final Scriptable scope) {
this.contextFactory = contextFactory;
this.scope = scope;
}
@Override
public Optional<Adaptable> apply(final ExternalMessage message) {
try {
return Optional.ofNullable((Adaptable) contextFactory.call(cx -> {
final NativeObject headersObj = new NativeObject();
message.getHeaders().forEach((key, value) -> headersObj.put(key, headersObj, value));
final NativeArrayBuffer bytePayload;
if (message.getBytePayload().isPresent()) {
final ByteBuffer byteBuffer = message.getBytePayload().get();
final byte[] array = byteBuffer.array();
bytePayload = new NativeArrayBuffer(array.length);
for (int a = 0; a < array.length; a++) {
bytePayload.getBuffer()[a] = array[a];
}
} else {
bytePayload = null;
}
final String contentType = message.getHeaders().get(ExternalMessage.CONTENT_TYPE_HEADER);
final String textPayload = message.getTextPayload().orElse(null);
final NativeObject externalMessage = new NativeObject();
externalMessage.put(EXTERNAL_MESSAGE_HEADERS, externalMessage, headersObj);
externalMessage.put(EXTERNAL_MESSAGE_TEXT_PAYLOAD, externalMessage, textPayload);
externalMessage.put(EXTERNAL_MESSAGE_BYTE_PAYLOAD, externalMessage, bytePayload);
externalMessage.put(EXTERNAL_MESSAGE_CONTENT_TYPE, externalMessage, contentType);
final org.mozilla.javascript.Function
mapToDittoProtocolMsgWrapper = (org.mozilla.javascript.Function) scope.get(INCOMING_FUNCTION_NAME, scope);
final Object result = mapToDittoProtocolMsgWrapper.call(cx, scope, scope, new Object[] {externalMessage});
if (result == null) {
// return null if result is null causing the wrapping Optional to be empty
return null;
}
final String dittoProtocolJsonStr = (String) NativeJSON.stringify(cx, scope, result, null, null);
return DittoJsonException.wrapJsonRuntimeException(() -> {
final JsonObject jsonObject = JsonFactory.readFrom(dittoProtocolJsonStr).asObject();
return ProtocolFactory.jsonifiableAdaptableFromJson(jsonObject);
});
}));
} catch (final RhinoException e) {
throw buildMessageMappingFailedException(e, message.findContentType().orElse(""),
DittoHeaders.of(message.getHeaders()));
} catch (final Throwable e) {
throw MessageMappingFailedException.newBuilder(message.findContentType().orElse(null))
.description(e.getMessage())
.dittoHeaders(DittoHeaders.of(message.getHeaders()))
.cause(e)
.build();
}
}
}