@Override public void operationComplete(Future<Object> future) throws Exception { if (future.await(1, TimeUnit.SECONDS)) { Object body = future.getNow(); if (body == null) { return; } if (!(body instanceof ScriptObjectMirror)) { returnError(request, "The script must return an object or array {collection: '', properties: {}}", BAD_REQUEST); } ScriptObjectMirror json = (ScriptObjectMirror) ((ScriptObjectMirror) body).eval("JSON"); Object stringify = json.callMember("stringify", body); request.response(stringify.toString()).end(); } else { byte[] bytes = JsonHelper.encodeAsBytes(errorMessage("Webhook code timeouts.", INTERNAL_SERVER_ERROR)); request.response(bytes, INTERNAL_SERVER_ERROR).end(); } } });
ScriptObjectMirror json = (ScriptObjectMirror) ((ScriptObjectMirror) body).eval("JSON"); Object stringify = json.callMember("stringify", body);
@Override public Object transform(ScriptObjectMirror jsObject) { if (jsObject == null) { return null; } final JSObject JSON = (JSObject) jsObject.eval("JSON"); final JSObject stringify = (JSObject) JSON.getMember("stringify"); final String encoded = (String) stringify.call(JSON, jsObject); char c = encoded.charAt(0); // encoded messages are expected not to be pretty printed if (c == '{') { return new JsonObject(encoded); } if (c == '[') { return new JsonArray(encoded); } throw new ClassCastException("type is not Object or Array"); } }
@Override public void encodeToWire(Buffer buffer, ScriptObjectMirror jsObject) { if (jsObject == null) { buffer.appendInt(0); return; } final JSObject JSON = (JSObject) jsObject.eval("JSON"); final JSObject stringify = (JSObject) JSON.getMember("stringify"); Buffer encoded = Buffer.buffer((String) stringify.call(JSON, jsObject)); buffer.appendInt(encoded.length()); buffer.appendBuffer(buffer); }