Search in sources :

Example 46 with WorkflowInstance

use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.

the class WorkflowOperationSkippingTest method testIf.

@Test
public void testIf() throws Exception {
    Map<String, String> properties1 = new HashMap<String, String>();
    properties1.put("executecondition", "true");
    Map<String, String> properties2 = new HashMap<String, String>();
    properties2.put("executecondition", "false");
    WorkflowInstance instance = startAndWait(workingDefinition, mediapackage1, properties1, WorkflowState.SUCCEEDED);
    WorkflowInstance instance2 = startAndWait(workingDefinition, mediapackage1, properties2, WorkflowState.SUCCEEDED);
    WorkflowInstance instance3 = startAndWait(workingDefinition, mediapackage1, null, WorkflowState.SUCCEEDED);
    // See if the skip operation has been executed
    WorkflowInstance instanceFromDb = service.getWorkflowById(instance.getId());
    assertNotNull(instanceFromDb);
    assertEquals(OperationState.SUCCEEDED, instanceFromDb.getOperations().get(0).getState());
    // See if the skip operation has been skipped (skip value != "true")
    WorkflowInstance instance2FromDb = service.getWorkflowById(instance2.getId());
    assertNotNull(instance2FromDb);
    assertEquals(OperationState.SKIPPED, instance2FromDb.getOperations().get(0).getState());
    // See if the skip operation has been skipped (skip property is undefined)
    WorkflowInstance instance3FromDb = service.getWorkflowById(instance3.getId());
    assertNotNull(instance3FromDb);
    assertEquals(OperationState.SKIPPED, instance3FromDb.getOperations().get(0).getState());
}
Also used : HashMap(java.util.HashMap) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Example 47 with WorkflowInstance

use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.

the class WorkflowServiceImplAuthzTest method testWorkflowWithoutSecurityPolicy.

@Test
public void testWorkflowWithoutSecurityPolicy() throws Exception {
    // Mock up an authorization service that always returns "false" for hasPermission()
    AuthorizationService authzService = EasyMock.createNiceMock(AuthorizationService.class);
    EasyMock.expect(authzService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(new AccessControlList(), AclScope.Series)).anyTimes();
    EasyMock.expect(authzService.hasPermission((MediaPackage) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(false).anyTimes();
    EasyMock.replay(authzService);
    service.setAuthorizationService(authzService);
    dao.setAuthorizationService(authzService);
    // Create the workflow and its dependent object graph
    WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
    def.add(new WorkflowOperationDefinitionImpl("op1", "op1", null, true));
    MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    // As an instructor, create a workflow
    userResponder.setResponse(instructor1);
    WorkflowInstance workflow = service.start(def, mp);
    service.suspend(workflow.getId());
    // Ensure that this instructor can access the workflow
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the organization admin can access that workflow
    userResponder.setResponse(DEFAULT_ORG_ADMIN);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the global admin can access that workflow
    userResponder.setResponse(globalAdmin);
    try {
        service.getWorkflowById(workflow.getId());
        assertEquals(1, service.countWorkflowInstances());
    } catch (Exception e) {
        fail(e.getMessage());
    }
    // Ensure the other instructor can not see the workflow, since there is no security policy granting access
    userResponder.setResponse(instructor2);
    try {
        service.getWorkflowById(workflow.getId());
        fail();
    } catch (UnauthorizedException e) {
    // expected
    }
    assertEquals(0, service.countWorkflowInstances());
    // Ensure the instructor from a different org can not see the workflow, even though they share a role
    organizationResponder.setResponse(otherOrganization);
    userResponder.setResponse(instructorFromDifferentOrg);
    try {
        service.getWorkflowById(workflow.getId());
        fail();
    } catch (Exception e) {
    // expected
    }
    assertEquals(0, service.countWorkflowInstances());
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) WorkflowOperationDefinitionImpl(org.opencastproject.workflow.api.WorkflowOperationDefinitionImpl) WorkflowDefinitionImpl(org.opencastproject.workflow.api.WorkflowDefinitionImpl) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) Test(org.junit.Test)

Example 48 with WorkflowInstance

use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.

the class IngestRestService method ingestZippedMediaPackage.

private Response ingestZippedMediaPackage(HttpServletRequest request, String wdID, String wiID) {
    if (isIngestLimitEnabled()) {
        setIngestLimit(getIngestLimit() - 1);
        logger.debug("An ingest has started so remaining ingest limit is " + getIngestLimit());
    }
    InputStream in = null;
    Date started = new Date();
    logger.info("Received new request from {} to ingest a zipped mediapackage", request.getRemoteHost());
    try {
        String workflowDefinitionId = wdID;
        String workflowIdAsString = wiID;
        Long workflowInstanceIdAsLong = null;
        Map<String, String> workflowConfig = new HashMap<>();
        if (ServletFileUpload.isMultipartContent(request)) {
            boolean isDone = false;
            for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String value = Streams.asString(item.openStream(), "UTF-8");
                    logger.trace("{}: {}", fieldName, value);
                    if (WORKFLOW_INSTANCE_ID_PARAM.equals(fieldName)) {
                        workflowIdAsString = value;
                        continue;
                    } else if (WORKFLOW_DEFINITION_ID_PARAM.equals(fieldName)) {
                        workflowDefinitionId = value;
                        continue;
                    } else {
                        logger.debug("Processing form field: " + fieldName);
                        workflowConfig.put(fieldName, value);
                    }
                } else {
                    logger.debug("Processing file item");
                    // once the body gets read iter.hasNext must not be invoked or the stream can not be read
                    // MH-9579
                    in = item.openStream();
                    isDone = true;
                }
                if (isDone)
                    break;
            }
        } else {
            logger.debug("Processing file item");
            in = request.getInputStream();
        }
        // Adding ingest start time to workflow configuration
        DateFormat formatter = new SimpleDateFormat(IngestService.UTC_DATE_FORMAT);
        workflowConfig.put(IngestService.START_DATE_KEY, formatter.format(started));
        /* Legacy support: Try to convert the workflowId to integer */
        if (!StringUtils.isBlank(workflowIdAsString)) {
            try {
                workflowInstanceIdAsLong = Long.parseLong(workflowIdAsString);
            } catch (NumberFormatException e) {
                // The workflowId is not a long value and might be the media package identifier
                workflowConfig.put(IngestServiceImpl.LEGACY_MEDIAPACKAGE_ID_KEY, workflowIdAsString);
            }
        }
        if (StringUtils.isBlank(workflowDefinitionId)) {
            workflowDefinitionId = defaultWorkflowDefinitionId;
        }
        WorkflowInstance workflow;
        if (workflowInstanceIdAsLong != null) {
            workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig, workflowInstanceIdAsLong);
        } else {
            workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig);
        }
        return Response.ok(WorkflowParser.toXml(workflow)).build();
    } catch (NotFoundException e) {
        logger.info(e.getMessage());
        return Response.status(Status.NOT_FOUND).build();
    } catch (MediaPackageException e) {
        logger.warn(e.getMessage());
        return Response.serverError().status(Status.BAD_REQUEST).build();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        IOUtils.closeQuietly(in);
        if (isIngestLimitEnabled()) {
            setIngestLimit(getIngestLimit() + 1);
            logger.debug("An ingest has finished so increased ingest limit to " + getIngestLimit());
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Date(java.util.Date) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IngestException(org.opencastproject.ingest.api.IngestException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) SimpleDateFormat(java.text.SimpleDateFormat)

Example 49 with WorkflowInstance

use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.

the class IngestRestService method ingest.

private Response ingest(MultivaluedMap<String, String> formData, String wdID) {
    /**
     * Note: We use a MultivaluedMap here to ensure that we can get any arbitrary form parameters. This is required to
     * enable things like holding for trim or distributing to YouTube.
     */
    final Map<String, String> wfConfig = getWorkflowConfig(formData);
    if (StringUtils.isNotBlank(wdID))
        wfConfig.put(WORKFLOW_DEFINITION_ID_PARAM, wdID);
    final MediaPackage mp;
    try {
        mp = factory.newMediaPackageBuilder().loadFromXml(formData.getFirst("mediaPackage"));
        if (MediaPackageSupport.sanityCheck(mp).isSome()) {
            logger.warn("Rejected ingest with invalid mediapackage {}", mp);
            return Response.status(Status.BAD_REQUEST).build();
        }
    } catch (Exception e) {
        logger.warn("Rejected ingest without mediapackage");
        return Response.status(Status.BAD_REQUEST).build();
    }
    final String workflowInstance = wfConfig.get(WORKFLOW_INSTANCE_ID_PARAM);
    final String workflowDefinition = wfConfig.get(WORKFLOW_DEFINITION_ID_PARAM);
    // Adding ingest start time to workflow configuration
    wfConfig.put(IngestService.START_DATE_KEY, formatter.format(startCache.asMap().get(mp.getIdentifier().toString())));
    final X<WorkflowInstance> ingest = new X<WorkflowInstance>() {

        @Override
        public WorkflowInstance xapply() throws Exception {
            /* Legacy support: Try to convert the workflowInstance to integer */
            Long workflowInstanceId = null;
            if (StringUtils.isNotBlank(workflowInstance)) {
                try {
                    workflowInstanceId = Long.parseLong(workflowInstance);
                } catch (NumberFormatException e) {
                    // The workflowId is not a long value and might be the media package identifier
                    wfConfig.put(IngestServiceImpl.LEGACY_MEDIAPACKAGE_ID_KEY, workflowInstance);
                }
            }
            if (workflowInstanceId != null) {
                return ingestService.ingest(mp, trimToNull(workflowDefinition), wfConfig, workflowInstanceId);
            } else {
                return ingestService.ingest(mp, trimToNull(workflowDefinition), wfConfig);
            }
        }
    };
    try {
        WorkflowInstance workflow = ingest.apply();
        startCache.asMap().remove(mp.getIdentifier().toString());
        return Response.ok(WorkflowParser.toXml(workflow)).build();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) X(org.opencastproject.util.data.Function0.X) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IngestException(org.opencastproject.ingest.api.IngestException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 50 with WorkflowInstance

use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.

the class IngestServiceImplTest method testThickClient.

@Test
public void testThickClient() throws Exception {
    FileUtils.copyURLToFile(urlPackage.toURL(), packageFile);
    InputStream packageStream = null;
    try {
        packageStream = urlPackage.toURL().openStream();
        WorkflowInstance instance = service.addZippedMediaPackage(packageStream);
        // Assert.assertEquals(2, mediaPackage.getTracks().length);
        // Assert.assertEquals(3, mediaPackage.getCatalogs().length);
        Assert.assertEquals(workflowInstanceID, instance.getId());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(packageStream);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Test(org.junit.Test)

Aggregations

WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)94 Test (org.junit.Test)48 MediaPackage (org.opencastproject.mediapackage.MediaPackage)40 NotFoundException (org.opencastproject.util.NotFoundException)26 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)24 HashMap (java.util.HashMap)22 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)20 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)19 ArrayList (java.util.ArrayList)16 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)16 IOException (java.io.IOException)15 Organization (org.opencastproject.security.api.Organization)15 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)14 WorkflowException (org.opencastproject.workflow.api.WorkflowException)13 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)12 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)12 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)11 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)10 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)9 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)9