Access uploaded file in Groovy Script

cancel
Showing results for 
Search instead for 
Did you mean: 
jvaidya
Active Member II

Access uploaded file in Groovy Script

Jump to solution

I'm trying to Base64 encode a file that a user uploads in an Activiti form, and then save the resulting encoding as a string to be accessed by other parts of the business process. I thought a Groovy Script in a Script task would be the best way to do this. How do I access the file in the script? 

Would something like: 

execution.getVariable('file'); 

give me access to the raw file? 

Thanks!

1 Solution

Accepted Solutions
jvaidya
Active Member II

Re: Access uploaded file in Groovy Script

Jump to solution

Managed to solve this issue! We had to change the activiti-app.properties configuration file to set:

# Beans whitelisting
beans.whitelisting.enabled=false

# EL whitelisting
el.whitelisting.enabled=false

View solution in original post

13 Replies
gdharley
Intermediate

Re: Access uploaded file in Groovy Script

Jump to solution

Nope, When you upload a file it is registered as related content (Enterprise Edition) or as an attachment (Community Edition).

You will need to use the relatedContentService to retrieve the uploaded files for the process instance (Enterprise Edition).
More details here:  


if using Community Edition you can get to the attachments you use taskService.getTaskAttachments(taskid).

This will return a list of atachments from which you can retrieve the content.

Greg

jvaidya
Active Member II

Re: Access uploaded file in Groovy Script

Jump to solution

Thanks Greg! Very helpful! I tried calling the API from a Script Task, but I can't use jQuery or vanilla JavaScript AJAX calls. The error I get from Activiti is: 

Caused by: <eval>:5 ReferenceError: "XMLHttpRequest" is not defined

How do I access this API if I can't use XML Http Requests? 

EDIT: I tried following this : What is the best way to call REST API from Activiti? Can I use JavaScript's URL redirection?  but I get an "'importPackage' is not defined" error. 

gdharley
Intermediate

Re: Access uploaded file in Groovy Script

Jump to solution

Hi Jay,

Script tasks actually run on the server side, even though they use Javascript (via embedded Nashorn/Rhino engine) or groovy.

Therefore you dont need to access the Ajax request.

Look up script tasks here: Activiti User Guide - Script Task 

jvaidya
Active Member II

Re: Access uploaded file in Groovy Script

Jump to solution

How do I access the file using relatedContentService? The link you pointed me to in the original answer uses REST API calls, so I got a little confused! Sorry about that. 

Can you point me to the documentation for relatedContentService? 

gdharley
Intermediate

Re: Access uploaded file in Groovy Script

Jump to solution

Unfortunately there is no documentation on related content service.
You can check for example use here:
 

Greg

jvaidya
Active Member II

Re: Access uploaded file in Groovy Script

Jump to solution

Hi Greg,

Can you do something like this in a script task, or would you have to use a service task? The example you pointed me to seems to use Java, but script tasks can only take JavaScript or Groovy, correct? 

Thanks,

Jay

gdharley
Intermediate

Re: Access uploaded file in Groovy Script

Jump to solution

Yes, it is java code, but if you set the script task language to groovy then you can effectively write java code in a script task.

Obviously if the code is complex you would use a service task, but for simple things like pulling the instance/task attachments it is only a few lines of code (<10) so a script task if ok.
The only real downside to a script task is the fact that you cannot debug your code and dont have the toot support you would have with a java IDE.

Greg

jvaidya
Active Member II

Re: Access uploaded file in Groovy Script

Jump to solution

Thanks Greg. I'm a little lost without documentation, so not sure how to use relatedContentService. Would something like this work? 

fileVar = "content"

file = relatedContentService.getFieldContentForProcessInstance( execution.getProcessInstanceId(), fileVar, 1, 0).getContent(); <- what format would file be in and what methods of relatedContentService do I use? 

is = file.getStream() <- Another questionable line 
val = is.read()
output = ""
while(val != -1){
output += val
}
execution.setVariable('raw', output)

gdharley
Intermediate

Re: Access uploaded file in Groovy Script

Jump to solution

Try this in a groovy script task.

import com.activiti.service.runtime.RelatedContentService;
import com.activiti.service.runtime.RelatedContentStreamProvider;
import com.activiti.domain.runtime.RelatedContent;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import java.nio.charset.StandardCharsets;

String fileVariableName = "myFile";
List<RelatedContent> contentList = relatedContentService.getFieldContentForProcessInstance(execution.getProcessInstanceId(), fileVariableName, 1, 0).getContent();
RelatedContent content = contentList.get(0);
out.println("GDH :" + content.getName());
InputStream is = relatedContentStreamProvider.getContentStream(content);
String result = IOUtils.toString(is, StandardCharsets.UTF_8);
out.println("GDH:" + result);

Apologies but ALfresco has not documented any of these services.

Greg