Java Code Examples for org.activiti.engine.impl.util.IoUtil#readInputStream()

The following examples show how to use org.activiti.engine.impl.util.IoUtil#readInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeploymentBuilderImpl.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
public DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream) {
  try {
    ZipEntry entry = zipInputStream.getNextEntry();
    while (entry != null) {
      if (!entry.isDirectory()) {
        String entryName = entry.getName();
        byte[] bytes = IoUtil.readInputStream(zipInputStream, entryName);
        ResourceEntity resource = resourceEntityManager.create();
        resource.setName(entryName);
        resource.setBytes(bytes);
        deployment.addResource(resource);
      }
      entry = zipInputStream.getNextEntry();
    }
  } catch (Exception e) {
    throw new ActivitiException("problem reading zip input stream", e);
  }
  return this;
}
 
Example 2
Source File: BpmnDeploymentTest.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = { "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.bpmn20.xml",
    "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg" })
public void testProcessDiagramResource() {
  ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

  assertEquals("org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.bpmn20.xml", processDefinition.getResourceName());
  BpmnModel processModel = repositoryService.getBpmnModel(processDefinition.getId());
  List<StartEvent> startEvents = processModel.getMainProcess().findFlowElementsOfType(StartEvent.class);
  assertEquals(1, startEvents.size());
  assertEquals("someFormKey", startEvents.get(0).getFormKey());

  String diagramResourceName = processDefinition.getDiagramResourceName();
  assertEquals("org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg", diagramResourceName);

  InputStream diagramStream = repositoryService.getResourceAsStream(deploymentIdFromDeploymentAnnotation,
      "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg");
  byte[] diagramBytes = IoUtil.readInputStream(diagramStream, "diagram stream");
  assertEquals(33343, diagramBytes.length);
}
 
Example 3
Source File: BpmnDeploymentTest.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = {
        "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.bpmn20.xml",
        "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg"
})
public void testProcessDiagramResource() {
    ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();

    assertEquals("org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.bpmn20.xml", processDefinition.getResourceName());
    assertTrue(processDefinition.hasStartFormKey());

    String diagramResourceName = processDefinition.getDiagramResourceName();
    assertEquals("org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg", diagramResourceName);

    InputStream diagramStream = repositoryService.getResourceAsStream(deploymentIdFromDeploymentAnnotation, "org/activiti/engine/test/bpmn/deployment/BpmnDeploymentTest.testProcessDiagramResource.jpg");
    byte[] diagramBytes = IoUtil.readInputStream(diagramStream, "diagram stream");
    assertEquals(33343, diagramBytes.length);
}
 
Example 4
Source File: DeploymentBuilderImpl.java    From flowable-engine with Apache License 2.0 6 votes vote down vote up
@Override
public DeploymentBuilder addZipInputStream(ZipInputStream zipInputStream) {
    try {
        ZipEntry entry = zipInputStream.getNextEntry();
        while (entry != null) {
            if (!entry.isDirectory()) {
                String entryName = entry.getName();
                byte[] bytes = IoUtil.readInputStream(zipInputStream, entryName);
                ResourceEntity resource = new ResourceEntity();
                resource.setName(entryName);
                resource.setBytes(bytes);
                deployment.addResource(resource);
            }
            entry = zipInputStream.getNextEntry();
        }
    } catch (Exception e) {
        throw new ActivitiException("problem reading zip input stream", e);
    }
    return this;
}
 
Example 5
Source File: DeploymentBuilderImpl.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public DeploymentBuilder addInputStream(String resourceName, InputStream inputStream) {
  if (inputStream == null) {
    throw new ActivitiIllegalArgumentException("inputStream for resource '" + resourceName + "' is null");
  }
  byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
  ResourceEntity resource = resourceEntityManager.create();
  resource.setName(resourceName);
  resource.setBytes(bytes);
  deployment.addResource(resource);
  return this;
}
 
Example 6
Source File: ProcessDefinitionDiagramHelper.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a diagram resource for a ProcessDefinitionEntity and associated BpmnParse.  The
 * returned resource has not yet been persisted, nor attached to the ProcessDefinitionEntity.
 * This requires that the ProcessDefinitionEntity have its key and resource name already set.
 * 
 * The caller must determine whether creating a diagram for this process definition is appropriate or not,
 * for example see {@link #shouldCreateDiagram(ProcessDefinitionEntity, DeploymentEntity)}.
 */
public ResourceEntity createDiagramForProcessDefinition(ProcessDefinitionEntity processDefinition, BpmnParse bpmnParse) {
  
  if (StringUtils.isEmpty(processDefinition.getKey()) || StringUtils.isEmpty(processDefinition.getResourceName())) {
    throw new IllegalStateException("Provided process definition must have both key and resource name set.");
  }
  
  ResourceEntity resource = createResourceEntity();
  ProcessEngineConfiguration processEngineConfiguration = Context.getCommandContext().getProcessEngineConfiguration();
  try {
    byte[] diagramBytes = IoUtil.readInputStream(
        processEngineConfiguration.getProcessDiagramGenerator().generateDiagram(bpmnParse.getBpmnModel(), "png",
            processEngineConfiguration.getActivityFontName(),
            processEngineConfiguration.getLabelFontName(),
            processEngineConfiguration.getAnnotationFontName(),
            processEngineConfiguration.getClassLoader()), null);
      String diagramResourceName = ResourceNameUtil.getProcessDiagramResourceName(
          processDefinition.getResourceName(), processDefinition.getKey(), "png");
      
      resource.setName(diagramResourceName);
      resource.setBytes(diagramBytes);
      resource.setDeploymentId(processDefinition.getDeploymentId());

      // Mark the resource as 'generated'
      resource.setGenerated(true);
      
  } catch (Throwable t) { // if anything goes wrong, we don't store the image (the process will still be executable).
    log.warn("Error while generating process diagram, image will not be stored in repository", t);
    resource = null;
  }
  
  return resource;
}
 
Example 7
Source File: StartTimerEventTest.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
public void testTimerShouldNotBeRemovedWhenUndeployingOldVersion() throws Exception {
  // Deploy test process
  String processXml = new String(IoUtil.readInputStream(getClass().getResourceAsStream("StartTimerEventTest.testTimerShouldNotBeRemovedWhenUndeployingOldVersion.bpmn20.xml"), ""));
  String firstDeploymentId = repositoryService.createDeployment().addInputStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml",
          new ByteArrayInputStream(processXml.getBytes())).deploy().getId();
  
  // After process start, there should be timer created
  TimerJobQuery jobQuery = managementService.createTimerJobQuery();
  assertEquals(1, jobQuery.count());

  //we deploy new process version, with some small change
  String processChanged = processXml.replaceAll("beforeChange","changed");
  String secondDeploymentId = repositoryService.createDeployment().addInputStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml",
      new ByteArrayInputStream(processChanged.getBytes())).deploy().getId();
  assertEquals(1, jobQuery.count());

  // Remove the first deployment
  repositoryService.deleteDeployment(firstDeploymentId, true);

  // The removal of an old version should not affect timer deletion
  // ACT-1533: this was a bug, and the timer was deleted!
  assertEquals(1, jobQuery.count());

  // Cleanup
  cleanDB();
  repositoryService.deleteDeployment(secondDeploymentId, true);
}
 
Example 8
Source File: DemoDataConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void createUser(String userId, String firstName, String lastName, String password, String email, String imageResource, List<String> groups, List<String> userInfo) {

    if (identityService.createUserQuery().userId(userId).count() == 0) {

      // Following data can already be set by demo setup script

      User user = identityService.newUser(userId);
      user.setFirstName(firstName);
      user.setLastName(lastName);
      user.setPassword(password);
      user.setEmail(email);
      identityService.saveUser(user);

      if (groups != null) {
        for (String group : groups) {
          identityService.createMembership(userId, group);
        }
      }
    }

    // Following data is not set by demo setup script

    // image
    if (imageResource != null) {
      byte[] pictureBytes = IoUtil.readInputStream(this.getClass().getClassLoader().getResourceAsStream(imageResource), null);
      Picture picture = new Picture(pictureBytes, "image/jpeg");
      identityService.setUserPicture(userId, picture);
    }

    // user info
    if (userInfo != null) {
      for (int i = 0; i < userInfo.size(); i += 2) {
        identityService.setUserInfo(userId, userInfo.get(i), userInfo.get(i + 1));
      }
    }

  }
 
Example 9
Source File: StartTimerEventTest.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public void testTimerShouldNotBeRemovedWhenUndeployingOldVersion() throws Exception {
    // Deploy test process
    String processXml = new String(IoUtil.readInputStream(getClass().getResourceAsStream("StartTimerEventTest.testTimerShouldNotBeRemovedWhenUndeployingOldVersion.bpmn20.xml"), ""));
    String firstDeploymentId = repositoryService.createDeployment().addInputStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml",
            new ByteArrayInputStream(processXml.getBytes()))
            .deploymentProperty(DeploymentProperties.DEPLOY_AS_FLOWABLE5_PROCESS_DEFINITION, Boolean.TRUE)
            .deploy()
            .getId();

    // After process start, there should be timer created
    TimerJobQuery jobQuery = managementService.createTimerJobQuery();
    assertEquals(1, jobQuery.count());

    // we deploy new process version, with some small change
    String processChanged = processXml.replaceAll("beforeChange", "changed");
    String secondDeploymentId = repositoryService.createDeployment().addInputStream("StartTimerEventTest.testVersionUpgradeShouldCancelJobs.bpmn20.xml",
            new ByteArrayInputStream(processChanged.getBytes()))
            .deploymentProperty(DeploymentProperties.DEPLOY_AS_FLOWABLE5_PROCESS_DEFINITION, Boolean.TRUE)
            .deploy()
            .getId();
    assertEquals(1, jobQuery.count());

    // Remove the first deployment
    repositoryService.deleteDeployment(firstDeploymentId, true);

    // The removal of an old version should not affect timer deletion
    // ACT-1533: this was a bug, and the timer was deleted!
    assertEquals(1, jobQuery.count());

    // Cleanup
    cleanDB();
    repositoryService.deleteDeployment(secondDeploymentId, true);
}
 
Example 10
Source File: DeploymentBuilderImpl.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public DeploymentBuilder addInputStream(String resourceName, InputStream inputStream) {
    if (inputStream == null) {
        throw new ActivitiIllegalArgumentException("inputStream for resource '" + resourceName + "' is null");
    }
    byte[] bytes = IoUtil.readInputStream(inputStream, resourceName);
    ResourceEntity resource = new ResourceEntity();
    resource.setName(resourceName);
    resource.setBytes(bytes);
    deployment.addResource(resource);
    return this;
}
 
Example 11
Source File: DemoDataGenerator.java    From maven-framework-project with MIT License 5 votes vote down vote up
protected void createUser(String userId, String firstName, String lastName, String password,
                          String email, String imageResource, List<String> groups, List<String> userInfo) {

    if (identityService.createUserQuery().userId(userId).count() == 0) {

        // Following data can already be set by demo setup script

        User user = identityService.newUser(userId);
        user.setFirstName(firstName);
        user.setLastName(lastName);
        user.setPassword(password);
        user.setEmail(email);
        identityService.saveUser(user);

        if (groups != null) {
            for (String group : groups) {
                identityService.createMembership(userId, group);
            }
        }
    }

    // Following data is not set by demo setup script

    // image
    if (imageResource != null) {
        byte[] pictureBytes = IoUtil.readInputStream(this.getClass().getClassLoader().getResourceAsStream(imageResource), null);
        Picture picture = new Picture(pictureBytes, "image/jpeg");
        identityService.setUserPicture(userId, picture);
    }

    // user info
    if (userInfo != null) {
        for (int i = 0; i < userInfo.size(); i += 2) {
            identityService.setUserInfo(userId, userInfo.get(i), userInfo.get(i + 1));
        }
    }

}
 
Example 12
Source File: BpmnDeploymentTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
private String readInputStreamToString(InputStream inputStream) {
  byte[] bytes = IoUtil.readInputStream(inputStream, "input stream");
  return new String(bytes);
}
 
Example 13
Source File: BpmnDeploymentTest.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
private String readInputStreamToString(InputStream inputStream) {
    byte[] bytes = IoUtil.readInputStream(inputStream, "input stream");
    return new String(bytes);
}
 
Example 14
Source File: CreateAttachmentCmd.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public Attachment execute(CommandContext commandContext) {

    verifyParameters(commandContext);

    AttachmentEntity attachment = new AttachmentEntity();
    attachment.setName(attachmentName);
    attachment.setDescription(attachmentDescription);
    attachment.setType(attachmentType);
    attachment.setTaskId(taskId);
    attachment.setProcessInstanceId(processInstanceId);
    attachment.setUrl(url);
    attachment.setUserId(Authentication.getAuthenticatedUserId());
    attachment.setTime(commandContext.getProcessEngineConfiguration().getClock().getCurrentTime());

    DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
    dbSqlSession.insert(attachment);

    if (content != null) {
        byte[] bytes = IoUtil.readInputStream(content, attachmentName);
        ByteArrayEntity byteArray = ByteArrayEntity.createAndInsert(bytes);
        attachment.setContentId(byteArray.getId());
        attachment.setContent(byteArray);
    }

    commandContext.getHistoryManager()
            .createAttachmentComment(taskId, processInstanceId, attachmentName, true);

    if (commandContext.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {
        // Forced to fetch the process-instance to associate the right process definition
        String processDefinitionId = null;
        if (attachment.getProcessInstanceId() != null) {
            ExecutionEntity process = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);
            if (process != null) {
                processDefinitionId = process.getProcessDefinitionId();
            }
        }

        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_CREATED, attachment, processInstanceId, processInstanceId, processDefinitionId));
        commandContext.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(
                ActivitiEventBuilder.createEntityEvent(FlowableEngineEventType.ENTITY_INITIALIZED, attachment, processInstanceId, processInstanceId, processDefinitionId));
    }

    return attachment;
}