cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve Attachments from Java Class

eramesh
Champ on-the-rise
Champ on-the-rise
Hi,

We are using Enterprise Activiti 1.5, in the final step of our review process we have to call external system REST service to send them all the approved documents that are attached to our process.

I'm trying to implement this in service task, can anyone please give me details how I can get the list of documents that are attached to the process and get their content.

Thanks in advance!

-Ramesh
4 REPLIES 4

cjose
Elite Collaborator
Elite Collaborator
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>

bharathisathya
Champ in-the-making
Champ in-the-making

Hi Ciju,

i am using the same code in custom task listener in delegate task. but not able to get the content, throwing null pointer when invoking the relatedcontentservice

eramesh
Champ on-the-rise
Champ on-the-rise
Thank you very much Ciju!!

It worked great!

Thanks again for posting the code!!


cjose
Elite Collaborator
Elite Collaborator
Smiley Happy