Skip to content

Commit

Permalink
added possibility to map raw payloads
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Jan 31, 2018
1 parent b54ce15 commit f306086
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import javax.script.Bindings;

Expand Down Expand Up @@ -100,4 +101,19 @@ static ByteBuffer convertToByteBuffer(final Object obj) {
}
return null;
}

static String convertToJsonArrayString(final ByteBuffer byteBuffer) {
final byte[] bytes = Optional.ofNullable(byteBuffer).map(ByteBuffer::array).orElse(null);
if (bytes != null) {
final StringBuilder sb = new StringBuilder("[");
for (final byte aByte : bytes) {
sb.append(aByte);
sb.append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("]");
return sb.toString();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ public Adaptable mapIncomingMessageToDittoAdaptable(final MappingTemplate templa
final PayloadMapperMessage message) {

engine.put(MAPPING_HEADERS_VAR, message.getHeaders());
engine.put(MAPPING_BYTEARRAY_VAR, message.getRawData().orElse(null));

final String bytesString = convertToJsonArrayString(message.getRawData().orElse(null));
try {
engine.eval(MAPPING_BYTEARRAY_VAR + " = " + bytesString + ";");
} catch (ScriptException e) {
e.printStackTrace();
}
engine.put(MAPPING_STRING_VAR, message.getStringData().orElse(null));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ void loadJavascriptLibrary(final Reader reader, final String libraryName) {
public Adaptable mapIncomingMessageToDittoAdaptable(final MappingTemplate template, final PayloadMapperMessage message) {

nashornSandbox.inject(MAPPING_HEADERS_VAR, message.getHeaders());
nashornSandbox.inject(MAPPING_BYTEARRAY_VAR, message.getRawData().orElse(null));
final String bytesString = convertToJsonArrayString(message.getRawData().orElse(null));
try {
nashornSandbox.eval(MAPPING_BYTEARRAY_VAR + " = " + bytesString + ";");
} catch (ScriptException e) {
e.printStackTrace();
}
nashornSandbox.inject(MAPPING_STRING_VAR, message.getStringData().orElse(null));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;

Expand All @@ -27,6 +28,7 @@
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;
Expand Down Expand Up @@ -70,7 +72,16 @@ public Adaptable mapIncomingMessageToDittoAdaptable(final MappingTemplate templa
message.getHeaders().forEach((key, value) -> headersObj.put(key, headersObj, value));
ScriptableObject.putProperty(scope, MAPPING_HEADERS_VAR, headersObj);

ScriptableObject.putProperty(scope, MAPPING_BYTEARRAY_VAR, message.getRawData().orElse(null));
if (message.getRawData().isPresent()) {
final ByteBuffer byteBuffer = message.getRawData().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_STRING_VAR, message.getStringData().orElse(null));

ScriptableObject.putProperty(scope, DITTO_PROTOCOL_JSON_VAR, new NativeObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,42 @@

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.ditto.protocoladapter.Adaptable;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* TODO doc
*/
//@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class ProtocolToRawMapperBytesTest {

private static final ImmutableMappingTemplate TEMPLATE = new ImmutableMappingTemplate("" +
private static final ImmutableMappingTemplate TEMPLATE = new ImmutableMappingTemplate(
"dittoProtocolJson.topic = 'org.eclipse.ditto/foo-bar/things/twin/commands/create';" +
"dittoProtocolJson.path = '/';" +
"dittoProtocolJson.headers = {};" +
"dittoProtocolJson.headers['correlation-id'] = mappingHeaders['correlation-id'];" +
"dittoProtocolJson.value = {};"
"dittoProtocolJson.value = String.fromCharCode.apply(String, mappingByteArray);"
);

private static final ByteBuffer PAYLOAD_BYTEBUFFER = ByteBuffer.wrap("hello binary!".getBytes(StandardCharsets.UTF_8));

private static PayloadMapper javaScriptNashornMapper;
private static PayloadMapper javaScriptNashornSandboxMapper;
private static PayloadMapper javaScriptRhinoMapper;

// @Parameterized.Parameters
// public static List<Object[]> data() {
// return Arrays.asList(new Object[20][0]);
// }
@Parameterized.Parameters
public static List<Object[]> data() {
return Arrays.asList(new Object[20][0]);
}

@BeforeClass
public static void setup() {
Expand Down Expand Up @@ -70,7 +77,7 @@ public void testNashornMapper() {

final Map<String, String> headers = new HashMap<>();
headers.put("correlation-id", "4711-foobar");
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(null, "huhu!", headers);
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(PAYLOAD_BYTEBUFFER, null, headers);

final long startTs = System.nanoTime();
final Adaptable
Expand Down Expand Up @@ -100,7 +107,7 @@ public void testNashornSandboxMapper() {

final Map<String, String> headers = new HashMap<>();
headers.put("correlation-id", "4711-foobar");
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(null, "huhu!", headers);
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(PAYLOAD_BYTEBUFFER, null, headers);

final long startTs = System.nanoTime();
final Adaptable
Expand All @@ -115,7 +122,7 @@ public void testRhinoMapper() {

final Map<String, String> headers = new HashMap<>();
headers.put("correlation-id", "4711-foobar");
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(null, "huhu!", headers);
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(PAYLOAD_BYTEBUFFER, null, headers);

final long startTs = System.nanoTime();
final Adaptable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.ditto.protocoladapter.Adaptable;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

/**
* TODO doc
*/
//@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class ProtocolToRawMapperSimpleTest {

private static final ImmutableMappingTemplate TEMPLATE = new ImmutableMappingTemplate("" +
Expand All @@ -34,16 +38,16 @@ public class ProtocolToRawMapperSimpleTest {
"dittoProtocolJson.value = mappingString;"
);

private static final String HUHU = "huhu!";
private static final String PAYLOAD_STRING = "hello!";

private static PayloadMapper javaScriptNashornMapper;
private static PayloadMapper javaScriptNashornSandboxMapper;
private static PayloadMapper javaScriptRhinoMapper;

// @Parameterized.Parameters
// public static List<Object[]> data() {
// return Arrays.asList(new Object[20][0]);
// }
@Parameterized.Parameters
public static List<Object[]> data() {
return Arrays.asList(new Object[20][0]);
}

@BeforeClass
public static void setup() {
Expand Down Expand Up @@ -72,7 +76,7 @@ public void testNashornMapper() {

final Map<String, String> headers = new HashMap<>();
headers.put("correlation-id", "4711-foobar");
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(null, HUHU, headers);
final PayloadMapperMessage message = new ImmutablePayloadMapperMessage(null, PAYLOAD_STRING, headers);

final long startTs = System.nanoTime();
final Adaptable
Expand Down

0 comments on commit f306086

Please sign in to comment.