How can I upload a contract input file from a connector to an external API.

1
0
-1

I'm unable to use the existing REST connectors to upload a contract input file as a form parameter to an external API.

I've tried configuring the file upload widget with the external API URL instead of Bonita's file upload API and this is also failing. Screenshot%202020-02-17%20at%201.03.44%20PM.png

Screenshot%202020-02-17%20at%201.04.03%20PM.png

Please suggest the best possible way to implement this use case. sad_smile.png

1 answer

1
0
-1

you may have a look to current file uploadwidget to understand how it works.

file submission is a combination of 2 calls:

  • first call a uploadServlet that returns a temporary filename (strored on the server)

http://localhost:8080/bonita/portal/resource/process///API/formFileUpload

response is {"filename":"","tempPath":"tmp_702715417031016846.txt","contentType":"text\/plain"}

  • then the submit itself contains a json object with this name, posting this json as the contract input name

yous url is supposed to return the same response

Comments

Submitted by ronson_1420753 on Thu, 03/05/2020 - 13:57

Thank you Laurent.

I have created a automated task to upload the file to external API. I have achieved this using groovy script in external connector. This may not be the best implementation. But serves my purpose. Script below:

import org.bonitasoft.engine.api.DocumentAPI;

import org.bonitasoft.engine.api.ProcessRuntimeAPI;

import org.bonitasoft.engine.bpm.document.Document;

import org.bonitasoft.engine.bpm.document.DocumentNotFoundException;

import org.bonitasoft.engine.connector.ConnectorException;

ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();

String docName = templateDocument.getContentFileName();

String mimeType = templateDocument.getContentMimeType();

final byte[] contentByte = ((DocumentAPI) processRuntimeAPI).getDocumentContent(templateDocument.getContentStorageId());

//final ByteArrayInputStream templateDocumentByte = new ByteArrayInputStream(contentByte);

int templateDocumentByteLength = contentByte.length;

String query = String.format("category=%s&subCategory=%s&fileName=%s",

URLEncoder.encode(templateFile.getCategory(), "UTF-8"),

URLEncoder.encode(templateFile.getSubCategory(), "UTF-8"),

URLEncoder.encode(docName, "UTF-8"));

URL url = new URL(fileUploadUrl + "?" + query);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setReadTimeout(10000);

conn.setConnectTimeout(15000);

conn.setRequestMethod("POST");

conn.setUseCaches(false);

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setRequestProperty("Connection", "Keep-Alive");

conn.addRequestProperty("Content-length", templateDocumentByteLength.toString());

conn.addRequestProperty("file", mimeType);

OutputStream os = conn.getOutputStream();

os.write(contentByte)

os.close();

conn.connect();

if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {

println "File Uploaded!"

}

else{

throw new Exception("File Upload Failed!")

}

Notifications