try to receive pdf as base64
authorDavid Kaufmann <david.kaufmann@student.tuwien.ac.at>
Wed, 28 Nov 2018 20:55:31 +0000 (21:55 +0100)
committerDavid Kaufmann <david.kaufmann@student.tuwien.ac.at>
Wed, 28 Nov 2018 20:55:31 +0000 (21:55 +0100)
camunda-overlay/sentiment-analysis.bpmn

index 3a46c63a948612a2f2ac499f986d85e7e28c8dbf..50298f346a701f7c2defbb06ff87e758eb78d0c6 100644 (file)
@@ -122,11 +122,96 @@ S(response);</camunda:script>
             <camunda:inputParameter name="payload">${results.toString()}</camunda:inputParameter>
             <camunda:outputParameter name="reportPDF">
               <camunda:script scriptFormat="javascript">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</camunda:script>
+// from nodejs base64 package                            
+var lookup = []
+var revLookup = []
+var javaByteArray = Java.type('byte[]')
+
+var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
+for (var i = 0, len = code.length; i &lt; 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 &gt; 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 &gt; 0
+    ? validLen - 4
+    : validLen
+
+  for (var i = 0; i &lt; len; i += 4) {
+    tmp =
+      (revLookup[b64.charCodeAt(i)] &lt;&lt; 18) |
+      (revLookup[b64.charCodeAt(i + 1)] &lt;&lt; 12) |
+      (revLookup[b64.charCodeAt(i + 2)] &lt;&lt; 6) |
+      revLookup[b64.charCodeAt(i + 3)]
+    arr[curByte++] = (tmp &gt;&gt; 16) &amp; 0xFF
+    arr[curByte++] = (tmp &gt;&gt; 8) &amp; 0xFF
+    arr[curByte++] = tmp &amp; 0xFF
+  }
+
+  if (placeHoldersLen === 2) {
+    tmp =
+      (revLookup[b64.charCodeAt(i)] &lt;&lt; 2) |
+      (revLookup[b64.charCodeAt(i + 1)] &gt;&gt; 4)
+    arr[curByte++] = tmp &amp; 0xFF
+  }
+
+  if (placeHoldersLen === 1) {
+    tmp =
+      (revLookup[b64.charCodeAt(i)] &lt;&lt; 10) |
+      (revLookup[b64.charCodeAt(i + 1)] &lt;&lt; 4) |
+      (revLookup[b64.charCodeAt(i + 2)] &gt;&gt; 2)
+    arr[curByte++] = (tmp &gt;&gt; 8) &amp; 0xFF
+    arr[curByte++] = tmp &amp; 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
+</camunda:script>
             </camunda:outputParameter>
           </camunda:inputOutput>
           <camunda:connectorId>http-connector</camunda:connectorId>