Accept a file from REST API

1
0
-1

Hi
I have an ASP.Net REST API that generate and download a file to browser.
My question is, how to store that file in a document variable via connectors in bonita?

1 answer

1
+2
-1
This one is the BEST answer!

You can use the below functions to store that file in a document variable via connectors in Bonita. Please note that the documentName parameter in the below functions refer to the name you provide while defining the document variable in the process

Document attachDocument(long processInstanceId,
String documentName,
String fileName,
String mimeType,
String url)
throws ProcessInstanceNotFoundException,
DocumentAttachmentException
Ref: attach document by url

Document attachDocument(long processInstanceId,
String documentName,
String fileName,
String mimeType,
byte[] documentContent)
throws ProcessInstanceNotFoundException,
DocumentAttachmentException
Ref: attach document by byte array of the file

Comments

Submitted by moh_farrag_1366291 on Sun, 04/15/2018 - 17:20

thanks Raji

your post helped me a lot, but the problem right now is how to access the file bytes from my API response in the connector.

I'm using bonita 7.6 and I have these variable in the connector output operations: bodyAsObject, bodyAsString and headers. I tried to cast the bodyAsObject to byte[] but it failed

Do you have any idea how to get the file bytes steam in the connector output operation?

Submitted by rjrjswrdvi on Sun, 04/15/2018 - 18:35

Hello,

You can leverage "bodyAsString" attribute and convert the string to bytes. I gave a quick try and this worked for me.
apiAccessor.processAPI.attachDocument(processInstanceId,
<document var name>,
<document name>,
null,
bodyAsString.getBytes());

Submitted by moh_farrag_1366291 on Mon, 04/16/2018 - 13:59

I tried that, the file(.xlsx) is attached to the task and able to download it from the task overview page, but the file is corrupted when tried to open it. I tried to request my API from the browser and the file is downloaded and opened successfully.

here is my code

processAPI.attachDocument(processInstanceId,"documentVariable","abc.xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                bodyAsString.getBytes())

do you have any idea why this happened?

Submitted by rjrjswrdvi on Mon, 04/16/2018 - 14:08
  1. Did you try ignoring the 4th parameter(pass it as null)?
  2. Did you try using the attachDocument function that takes URL instead of the byte array(unless you need a connector to invoke your connector may be for authe authentication or something else)?

I tried an audio file for my testing which worked.

Submitted by moh_farrag_1366291 on Wed, 04/18/2018 - 17:22

both didn't work for me.
I tried to pass the URL instead of the bytes array, but when I tried to download the file I got this error

The image “http://localhost:8080/bonita/portal/resource/processInstance/SettlementProcess/1.0/API/https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png” cannot be displayed because it contains errors.

its concatenating the the url of bonita + the file url that I passed to attached document
here is my code

final ProcessAPI processAPI = apiAccessor.getProcessAPI()
processAPI.attachDocument(processInstanceId, "inputfile", "logo.png",null,
"https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")

Submitted by rjrjswrdvi on Fri, 04/20/2018 - 19:47

I found your URL working too. Here is the code I used.

URL u = new URL('https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png');
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
  is = u.openStream ();
  byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
  int n;

  while ( (n = is.read(byteChunk)) > 0 ) {
    baos.write(byteChunk, 0, n);
  }
}
catch (IOException e) {
  e.printStackTrace ();
  // Perform any other exception handling that's appropriate.
}
finally {
  if (is != null) { is.close(); }
}

apiAccessor.processAPI.attachDocument(processInstanceId, <documentVarName>,<documentFileName>, null,baos.toByteArray());

P.S.:
1. If you would want to know where attachDocument by URL can be used, you might want to read this section .
2. Referred this answer to get byte array from an URL.

Notifications