Download File

how can I download a file that was uploaded by the input file, I am using the following code to access the variable id

“CoreVariableInstance variableInstance = ((ExecutionEntity) ex) .getVariableInstance (” uploadDoc ");
String id = ((VariableInstanceEntity) variableInstance) .getId (); "

but if it is running multiple instances it always takes the id of the first variable since I do the filter by the name “uploadDoc”, however that name is the same for other instances. In short, I need to download the file from the current instance.

Hi @MarceloCP.

From my short user experience in Camunda, I can tell you that you can download an uploaded file via Cockpit. If you log in Cockpit and select variable tab, you can see the value of your file variable.

Hope this helps.

Cheers,
Steve

I would like to download it by the script task, encoding a class in java. I have been using Url class but the API is with basic authentication enabled I do not know how to pass this authentication in the URL class in java

If you are inside a script task, you already have access to camunda API, so you shouldn’t need to authenticate.

Anyway, if you are going to get a variable (a file or something else) through rest API you can authenticate like this:
(This example is in groovy)

def url = new URL(endpoint)
def connection = url.openConnection()
connection.setRequestProperty( "Authorization", "Basic " + ("${user}:${pass}").getBytes().encodeBase64().toString() )

It’s not clear where you are writing the ‘dowload’ code. Are you inside a script task of the process? another process? A completely external procedure?

If you find by variable name, it’s not enough, you have to define another parameter to identify the desired instance.

1 Like

I enabled the basic authentication in the REST API, without the authentication the code in java works normally, but with authentication I get the 401 error not authorized, it follows the code used:

I am using in the flow a service task with java class implementation

public void execute(String user, String password) throws IOException{

	String auth = user+":"+password;
	byte[] authEncBytes = Base64.encodeBase64(auth.getBytes());
	String authEncoded = new String(authEncBytes);
	System.out.println(authEncoded);
	
	String destino = "C:\\TempEproc\\doc.pdf";
	
	
	URL url = new URL("http://ipservidor/engine-rest/variable-instance/b355e6bc-20b9-11e8-915b-005056ae0cf1/data");
	URLConnection connection = url.openConnection();
	connection.setRequestProperty("Authorization", "Basic "+authEncoded);
	
	InputStream is = url.openStream();		
	FileOutputStream fos = new FileOutputStream(destino);
	int umByte = 0;
	while ((umByte = is.read()) != -1){
		fos.write(umByte);
	}
	
	is.close();
	fos.close();

If you are using a Class defined in a serviceTask, you’ll have better performance injecting the execution with the Field injection feature. You shouldn’t need to use another connection like you were in a detached application.

It should work with the REST anyway. I don’t see anything wrong with the connection code you wrote.

Instead of loosing lot of time with re-deploys, you could try it outside as a standalone groovy or java procedure (the rest is accessible from anywhere). If you succeed in a simple rest query (e.g. /engine-rest/job) you should solve your authentication problem. You could even try with the browser without any code at all to test your credentials are working.

I managed to make it work with this code

public void execute(String processId, String numProcesso, String nomeDocumento, String nomeArquivo) {

String filePath = "c:\\TempEproc\\"+nomeArquivo+".pdf";
	
	try {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(
            "http://ipservidor/engine-rest/process-instance/"+processId+"/variables/"+nomeArquivo+"/data");
        request.setHeader("Authorization", "Basic aW5kcmE6IzFuZHJAIw==");
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();

        int responseCode = response.getStatusLine().getStatusCode();
        
        System.out.println("Request Url: " + request.getURI());
        System.out.println("Response Code: " + responseCode);

        InputStream is = entity.getContent();
        
        FileOutputStream fos = new FileOutputStream(new File(filePath));

        int inByte;
        while ((inByte = is.read()) != -1) {
            fos.write(inByte);
        }

        is.close();
        fos.close();

        client.close();
    } catch (ClientProtocolException e) {
        System.out.println(e);
    } catch (UnsupportedOperationException e) {
    	System.out.println(e);
    } catch (IOException e) {
    	System.out.println(e);
    }

}

, but if the service task that will do the downlod is soon after it does not locate the variable that contains the file, it seems to me that it runs the java class before putting the variable in the system follows example