From 337ecde775a9ea01934220ab4df91055923e463e Mon Sep 17 00:00:00 2001 From: David Kaufmann Date: Wed, 28 Nov 2018 21:55:31 +0100 Subject: [PATCH] try to receive pdf as base64 --- camunda-overlay/sentiment-analysis.bpmn | 95 +++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 5 deletions(-) diff --git a/camunda-overlay/sentiment-analysis.bpmn b/camunda-overlay/sentiment-analysis.bpmn index 3a46c63..50298f3 100644 --- a/camunda-overlay/sentiment-analysis.bpmn +++ b/camunda-overlay/sentiment-analysis.bpmn @@ -122,11 +122,96 @@ S(response); ${results.toString()} var response = connector.getVariable("response"); -print ("response: ") -print (response) -var file = Java.type('org.camunda.bpm.engine.variable.Variables').fileValue("pdfTest").file(response.getBytes("utf-8")).mimeType('application/pdf').create() -//response.getBytes("utf-8") -file +// from nodejs base64 package +var lookup = [] +var revLookup = [] +var javaByteArray = Java.type('byte[]') + +var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' +for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i] + revLookup[code.charCodeAt(i)] = i +} + +// Support decoding URL-safe base64 strings, as Node.js does. +// See: https://en.wikipedia.org/wiki/Base64#URL_applications +revLookup['-'.charCodeAt(0)] = 62 +revLookup['_'.charCodeAt(0)] = 63 + +function getLens (b64) { + var len = b64.length + + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // Trim off extra bytes after placeholder bytes are found + // See: https://github.com/beatgammit/base64-js/issues/42 + var validLen = b64.indexOf('=') + if (validLen === -1) validLen = len + + var placeHoldersLen = validLen === len + ? 0 + : 4 - (validLen % 4) + + return [validLen, placeHoldersLen] +} + +function _byteLength (b64, validLen, placeHoldersLen) { + return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen +} + +function toByteArray (b64) { + var tmp + var lens = getLens(b64) + var validLen = lens[0] + var placeHoldersLen = lens[1] + + var arr = new javaByteArray(_byteLength(b64, validLen, placeHoldersLen)) + + var curByte = 0 + + // if there are placeholders, only get up to the last complete 4 chars + var len = placeHoldersLen > 0 + ? validLen - 4 + : validLen + + for (var i = 0; i < len; i += 4) { + tmp = + (revLookup[b64.charCodeAt(i)] << 18) | + (revLookup[b64.charCodeAt(i + 1)] << 12) | + (revLookup[b64.charCodeAt(i + 2)] << 6) | + revLookup[b64.charCodeAt(i + 3)] + arr[curByte++] = (tmp >> 16) & 0xFF + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 2) { + tmp = + (revLookup[b64.charCodeAt(i)] << 2) | + (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[curByte++] = tmp & 0xFF + } + + if (placeHoldersLen === 1) { + tmp = + (revLookup[b64.charCodeAt(i)] << 10) | + (revLookup[b64.charCodeAt(i + 1)] << 4) | + (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[curByte++] = (tmp >> 8) & 0xFF + arr[curByte++] = tmp & 0xFF + } + + return arr +} + +var source = "kP/+kA=="; +var decoded = toByteArray(source); +//decoded +var file = Java.type('org.camunda.bpm.engine.variable.Variables').fileValue("pdfTest").file(toByteArray(response)).mimeType('application/pdf').create() +file + http-connector -- 2.43.0