Hi Ramesh,
I had the exact same requirement in one of my projects and I ended up doing this in a service task at the very of my process flow. Please find below the Java Delegate which I used to solve this requirement.
<java>
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import com.activiti.domain.runtime.RelatedContent;
import com.activiti.service.runtime.RelatedContentService;
import com.activiti.service.runtime.RelatedContentStreamProvider;
@Component("backupAllDocumentsJavaDelegate")
public class BackupAllDocumentsJavaDelegate implements JavaDelegate {
private static final Logger log = LoggerFactory
.getLogger(BackupAllDocumentsJavaDelegate.class);
@Autowired
private RelatedContentService relatedContentService;
@Autowired
private RelatedContentStreamProvider relatedContentStreamProvider;
//back up all documents in the process
@Override
public void execute(DelegateExecution execution) throws BpmnError {
List<RelatedContent> relatedContent = new ArrayList<RelatedContent>();
Page<RelatedContent> page = null;
int pageNumber = 0;
try {
while ((page == null) || (page.hasNext())) {
page = relatedContentService
.getAllFieldContentForProcessInstance(
execution.getProcessInstanceId(), 50,
pageNumber);
relatedContent.addAll(page.getContent());
pageNumber++;
}
for (RelatedContent rc : relatedContent) {
InputStream inputStream = relatedContentStreamProvider
.getContentStream(rc);
byte[] streamByteArray = IOUtils.toByteArray(inputStream);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", streamByteArray,
ContentType.APPLICATION_OCTET_STREAM, rc.getName());
String fileName = rc.getName();
builder.addTextBody("filename", fileName);
HttpEntity multipart = builder.build();
//Logic to invoke your external REST API can be added here
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
</java>