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

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. Upload error in console

Network trace

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

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/<PROCESS_NAME>/<PROCESS_VERSION>/API/formFileUpload

response is {"filename":"<YOUR_FILE>","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

 

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!")

}