/
RhinoJavaScriptPayloadMapper.java
235 lines (199 loc) · 10.6 KB
/
RhinoJavaScriptPayloadMapper.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
/*
* Copyright (c) 2017 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
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.amqpbridge.mapping.mapper.javascript;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.script.Bindings;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.amqpbridge.AmqpBridgeModelFactory;
import org.eclipse.ditto.model.amqpbridge.ExternalMessage;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.JsonifiableAdaptable;
import org.eclipse.ditto.protocoladapter.ProtocolFactory;
import org.eclipse.ditto.services.amqpbridge.mapping.mapper.MessageMapper;
import org.eclipse.ditto.services.amqpbridge.mapping.mapper.MessageMapperConfiguration;
import org.mozilla.javascript.Callable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeJSON;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
/**
* This mapper executes its mapping methods on the <b>current thread</b>. The caller should be aware of that.
*/
final class RhinoJavaScriptPayloadMapper implements MessageMapper {
private static final String WEBJARS_PATH = "/META-INF/resources/webjars";
private static final String WEBJARS_BYTEBUFFER = WEBJARS_PATH + "/bytebuffer/5.0.0/dist/bytebuffer.min.js";
private static final String WEBJARS_LONG = WEBJARS_PATH + "/long/3.2.0/dist/long.min.js";
private static final String WEBJARS_MUSTACHE = WEBJARS_PATH + "/mustache/2.3.0/mustache.min.js";
private static final String DITTO_PROTOCOL_JSON_VAR = "ditto_protocolJson";
private static final String MAPPING_CONTENT_TYPE_VAR = "ditto_mappingContentType";
private static final String MAPPING_STRING_VAR = "ditto_mappingString";
private static final String MAPPING_BYTEARRAY_VAR = "ditto_mappingByteArray";
private static final String MAPPING_HEADERS_VAR = "ditto_mappingHeaders";
private static final String INIT_VARS_TEMPLATE = "var " +
MAPPING_CONTENT_TYPE_VAR + "=undefined," +
MAPPING_STRING_VAR + "=undefined," +
MAPPING_BYTEARRAY_VAR + "=undefined," +
MAPPING_HEADERS_VAR + "=undefined," +
DITTO_PROTOCOL_JSON_VAR + "={}" +
";";
@Nullable
private ContextFactory contextFactory;
@Nullable
private Scriptable scope;
@Nullable private JavaScriptMessageMapperConfiguration configuration;
RhinoJavaScriptPayloadMapper() {
}
@Override
public Optional<String> getContentType() {
return Optional.empty();
}
@Override
public void configure(final MessageMapperConfiguration options) {
this.configuration = new ImmutableJavaScriptMessageMapperConfiguration.Builder(options.getProperties()).build();
contextFactory = new RhinoContextFactory();
// create scope once and load the required libraries in order to get best performance:
scope = (Scriptable) contextFactory.call(cx -> {
final Scriptable scope = cx.initSafeStandardObjects();
cx.evaluateString(scope, INIT_VARS_TEMPLATE, "init-vars", 1, null);
initLibraries(cx, scope);
return scope;
});
}
@Override
public Adaptable map(final ExternalMessage message) {
return (Adaptable) contextFactory.call(cx -> {
final NativeObject headersObj = new NativeObject();
message.getHeaders().forEach((key, value) -> headersObj.put(key, headersObj, value));
ScriptableObject.putProperty(scope, MAPPING_HEADERS_VAR, headersObj);
if (message.getBytePayload().isPresent()) {
final ByteBuffer byteBuffer = message.getBytePayload().get();
final byte[] array = byteBuffer.array();
final NativeArray newArray = new NativeArray(array.length);
for (int a = 0; a < array.length; a++) {
ScriptableObject.putProperty(newArray, a, array[a]);
}
ScriptableObject.putProperty(scope, MAPPING_BYTEARRAY_VAR, newArray);
}
ScriptableObject.putProperty(scope, MAPPING_CONTENT_TYPE_VAR, message.getHeaders().get("content-type"));
ScriptableObject.putProperty(scope, MAPPING_STRING_VAR, message.getTextPayload().orElse(null));
ScriptableObject.putProperty(scope, DITTO_PROTOCOL_JSON_VAR, new NativeObject());
cx.evaluateString(scope, getConfiguration().flatMap(JavaScriptMessageMapperConfiguration::getIncomingMappingScript)
.orElse(""), "template", 1, null);
final Object dittoProtocolJson = ScriptableObject.getProperty(scope, DITTO_PROTOCOL_JSON_VAR);
final String dittoProtocolJsonStr =
(String) NativeJSON.stringify(cx, scope, dittoProtocolJson, null, null);
final JsonObject jsonObject = JsonFactory.readFrom(dittoProtocolJsonStr).asObject();
return ProtocolFactory.jsonifiableAdaptableFromJson(jsonObject);
});
}
@Override
public ExternalMessage map(final Adaptable adaptable) {
final JsonifiableAdaptable jsonifiableAdaptable =
ProtocolFactory.wrapAsJsonifiableAdaptable(adaptable);
return (ExternalMessage) contextFactory.call(cx ->{
final Object nativeJsonObject =
NativeJSON.parse(cx, scope, jsonifiableAdaptable.toJsonString(), new NullCallable());
ScriptableObject.putProperty(scope, DITTO_PROTOCOL_JSON_VAR, nativeJsonObject);
cx.evaluateString(scope, getConfiguration().flatMap(JavaScriptMessageMapperConfiguration::getOutgoingMappingScript)
.orElse(""), "template", 1, null);
final String contentType = ScriptableObject.getTypedProperty(scope, MAPPING_CONTENT_TYPE_VAR, String.class);
final String mappingString = ScriptableObject.getTypedProperty(scope, MAPPING_STRING_VAR, String.class);
final Object mappingByteArray = ScriptableObject.getProperty(scope, MAPPING_BYTEARRAY_VAR);
final Object mappingHeaders = ScriptableObject.getProperty(scope, MAPPING_HEADERS_VAR);
final Map<String, String> headers = !(mappingHeaders instanceof Undefined) ? null : Collections.emptyMap();
//TODO pm evaulate if bytes or text payload
return AmqpBridgeModelFactory.newExternalMessageBuilder(headers, ExternalMessage.MessageType.RESPONSE)
.withAdditionalHeaders("content-type", contentType).withText(mappingString).build();
});
}
private void initLibraries(final Context cx, final Scriptable scope) {
if (getConfiguration().map(JavaScriptMessageMapperConfiguration::isLoadBytebufferJS).orElse(false)) {
loadJavascriptLibrary(cx, scope, new InputStreamReader(getClass().getResourceAsStream(WEBJARS_BYTEBUFFER)),
"bytebuffer.js");
}
if (getConfiguration().map(JavaScriptMessageMapperConfiguration::isLoadLongJS).orElse(false)) {
loadJavascriptLibrary(cx, scope, new InputStreamReader(getClass().getResourceAsStream(WEBJARS_LONG)),
"long.js");
}
if (getConfiguration().map(JavaScriptMessageMapperConfiguration::isLoadMustacheJS).orElse(false)) {
loadJavascriptLibrary(cx, scope, new InputStreamReader(getClass().getResourceAsStream(WEBJARS_MUSTACHE)),
"mustache.js");
}
}
private Optional<JavaScriptMessageMapperConfiguration> getConfiguration() {
return Optional.ofNullable(configuration);
}
private void loadJavascriptLibrary(final Context cx, final Scriptable scope, final Reader reader, final String libraryName) {
try {
cx.evaluateReader(scope, reader, libraryName, 1, null);
} catch (final IOException e) {
throw new IllegalStateException("Could not load script <" + libraryName + ">", e);
}
}
private static ByteBuffer convertToByteBuffer(final Object obj) {
if (obj instanceof Bindings) {
try {
final Class<?> cls = Class.forName("jdk.nashorn.api.scripting.ScriptObjectMirror");
if (cls.isAssignableFrom(obj.getClass())) {
final Method isArray = cls.getMethod("isArray");
final Object result = isArray.invoke(obj);
if (result != null && result.equals(true)) {
final Method values = cls.getMethod("values");
final Object vals = values.invoke(obj);
if (vals instanceof Collection) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Collection coll = (Collection) vals;
coll.forEach(e -> baos.write(((Number) e).intValue()));
return ByteBuffer.wrap(baos.toByteArray());
}
}
}
} catch (final ClassNotFoundException | NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException("Could not retrieve array values", e);
}
}
if (obj instanceof List<?>) {
final List<?> list = (List<?>) obj;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
list.forEach(e -> baos.write(((Number) e).intValue()));
return ByteBuffer.wrap(baos.toByteArray());
}
return null;
}
/**
*
*/
private static class NullCallable implements Callable {
@Override
public Object call(Context context, Scriptable scope, Scriptable holdable, Object[] objects) {
return objects[1];
}
}
}