Activiti工作流中工作流配置资源的上传实现

           在activiti工作流中的将工作流文件通过activit-probe的deployment部署文件。实现原理如下。通过activiti-probe中调用commons-fileupload将文件上传到并部署。源代码如下:

 

 

 

 

package org.activiti.rest.api.repository;

import java.util.List;

/**
 * @author Tijs Rademakers
 */
public class DeploymentUploadResource extends SecuredResource {
  
  @Post
  public void uploadDeployment(Representation entity) {
    try {
      if(authenticate(SecuredResource.ADMIN) == false) return;
      
      RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
      List<FileItem> items = upload.parseRepresentation(entity);
      
      FileItem uploadItem = null;
      String successMethod = null;
      for (FileItem fileItem : items) {
        if(fileItem.getName() != null) {
          uploadItem = fileItem;
        } else if(fileItem.getFieldName().equals("success")){
          successMethod = fileItem.getString();
        }
      }
      
      DeploymentBuilder deploymentBuilder = ActivitiUtil.getRepositoryService().createDeployment();
      String fileName = uploadItem.getName();
      if (fileName.endsWith(".bpmn20.xml")) {
        deploymentBuilder.addInputStream(fileName, uploadItem.getInputStream());
      } else if (fileName.endsWith(".bar") || fileName.endsWith(".zip")) {
        deploymentBuilder.addZipInputStream(new ZipInputStream(uploadItem.getInputStream()));
      } else {
        throw new ActivitiException("File must be of type .bpmn20.xml, .bar or .zip");
      }
      deploymentBuilder.name(fileName);
      deploymentBuilder.deploy();
      StringBuilder html = new StringBuilder();
      html.append("<html>\n");
      html.append("  <script type=\"text/javascript\">\n");
      html.append("    " + successMethod + "()\n");
      html.append("   </script>\n");
      html.append("</html>\n");
      getResponse().setEntity(html.toString(), MediaType.TEXT_HTML);
      
    } catch (Exception e) {
      throw new ActivitiException(e.getMessage(), e);
    }
  }
}

 

你可能感兴趣的:(Activiti)