Search in sources :

Example 11 with WorkflowException

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

the class WorkflowServiceSolrIndex method createDocument.

/**
 * Adds the workflow instance to the search index.
 *
 * @param instance
 *          the instance
 * @return the solr input document
 * @throws Exception
 */
protected SolrInputDocument createDocument(WorkflowInstance instance) throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField(ID_KEY, instance.getId());
    doc.addField(WORKFLOW_DEFINITION_KEY, instance.getTemplate());
    doc.addField(STATE_KEY, instance.getState().toString());
    String xml = WorkflowParser.toXml(instance);
    doc.addField(XML_KEY, xml);
    // index the current operation if there is one. If the workflow is finished, there is no current operation, so use a
    // constant
    WorkflowOperationInstance op = instance.getCurrentOperation();
    if (op == null) {
        doc.addField(OPERATION_KEY, NO_OPERATION_KEY);
    } else {
        doc.addField(OPERATION_KEY, op.getTemplate());
    }
    MediaPackage mp = instance.getMediaPackage();
    doc.addField(MEDIAPACKAGE_KEY, mp.getIdentifier().toString());
    if (mp.getSeries() != null) {
        doc.addField(SERIES_ID_KEY, mp.getSeries());
    }
    if (mp.getSeriesTitle() != null) {
        doc.addField(SERIES_TITLE_KEY, mp.getSeriesTitle());
    }
    if (mp.getDate() != null) {
        doc.addField(CREATED_KEY, mp.getDate());
    }
    if (mp.getTitle() != null) {
        doc.addField(TITLE_KEY, mp.getTitle());
    }
    if (mp.getLicense() != null) {
        doc.addField(LICENSE_KEY, mp.getLicense());
    }
    if (mp.getLanguage() != null) {
        doc.addField(LANGUAGE_KEY, mp.getLanguage());
    }
    if (mp.getContributors() != null && mp.getContributors().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String contributor : mp.getContributors()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(contributor);
        }
        doc.addField(CONTRIBUTOR_KEY, buf.toString());
    }
    if (mp.getCreators() != null && mp.getCreators().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String creator : mp.getCreators()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(creator);
        }
        doc.addField(CREATOR_KEY, buf.toString());
    }
    if (mp.getSubjects() != null && mp.getSubjects().length > 0) {
        StringBuffer buf = new StringBuffer();
        for (String subject : mp.getSubjects()) {
            if (buf.length() > 0)
                buf.append("; ");
            buf.append(subject);
        }
        doc.addField(SUBJECT_KEY, buf.toString());
    }
    User workflowCreator = instance.getCreator();
    doc.addField(WORKFLOW_CREATOR_KEY, workflowCreator.getUsername());
    doc.addField(ORG_KEY, instance.getOrganization().getId());
    WorkflowInstance.WorkflowState state = instance.getState();
    if (!(WorkflowInstance.WorkflowState.SUCCEEDED.equals(state) || WorkflowInstance.WorkflowState.FAILED.equals(state) || WorkflowInstance.WorkflowState.STOPPED.equals(state))) {
        AccessControlList acl;
        try {
            acl = authorizationService.getActiveAcl(mp).getA();
        } catch (Error e) {
            logger.error("No security xacml found on media package {}", mp);
            throw new WorkflowException(e);
        }
        addAuthorization(doc, acl);
    }
    return doc;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) User(org.opencastproject.security.api.User) WorkflowState(org.opencastproject.workflow.api.WorkflowInstance.WorkflowState) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance)

Example 12 with WorkflowException

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

the class WorkflowRestService method update.

@POST
@Path("update")
@RestQuery(name = "update", description = "Updates a workflow instance.", returnDescription = "No content.", restParameters = { @RestParameter(name = "workflow", isRequired = true, description = "The XML representation of the workflow instance.", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "Workflow instance updated.") })
public Response update(@FormParam("workflow") String workflowInstance) throws NotFoundException, UnauthorizedException {
    try {
        WorkflowInstance instance = WorkflowParser.parseWorkflowInstance(workflowInstance);
        service.update(instance);
        return Response.noContent().build();
    } catch (WorkflowException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 13 with WorkflowException

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

the class WorkflowRestService method resume.

@POST
@Path("replaceAndresume")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "replaceAndresume", description = "Replaces a suspended workflow instance with an updated version, and resumes the workflow.", returnDescription = "An XML representation of the updated and resumed workflow instance", restParameters = { @RestParameter(name = "id", isRequired = true, description = "The workflow instance identifier", type = STRING), @RestParameter(name = "mediapackage", isRequired = false, description = "The new Mediapackage", type = TEXT), @RestParameter(name = "properties", isRequired = false, description = "Properties", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the updated and resumed workflow instance."), @RestResponse(responseCode = SC_CONFLICT, description = "Can not resume workflow not in paused state"), @RestResponse(responseCode = SC_NOT_FOUND, description = "No suspended workflow instance with that identifier exists."), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to resume. Maybe you need to authenticate.") })
public Response resume(@FormParam("id") long workflowInstanceId, @FormParam("mediapackage") final String mediaPackage, @FormParam("properties") LocalHashMap properties) throws NotFoundException, UnauthorizedException {
    final Map<String, String> map;
    if (properties == null) {
        map = new HashMap<String, String>();
    } else {
        map = properties.getMap();
    }
    final Lock lock = this.lock.get(workflowInstanceId);
    lock.lock();
    try {
        WorkflowInstance workflow = service.getWorkflowById(workflowInstanceId);
        if (!WorkflowState.PAUSED.equals(workflow.getState())) {
            logger.warn("Can not resume workflow '{}', not in state paused but {}", workflow, workflow.getState());
            return Response.status(Status.CONFLICT).build();
        }
        if (mediaPackage != null) {
            MediaPackage newMp = MediaPackageParser.getFromXml(mediaPackage);
            MediaPackage oldMp = workflow.getMediaPackage();
            // Delete removed elements from workspace
            for (MediaPackageElement elem : oldMp.getElements()) {
                if (MediaPackageSupport.contains(elem.getIdentifier(), newMp))
                    continue;
                try {
                    workspace.delete(elem.getURI());
                    logger.info("Deleted removed mediapackge element {}", elem);
                } catch (NotFoundException e) {
                    logger.info("Removed mediapackage element {} is already deleted", elem);
                }
            }
            workflow.setMediaPackage(newMp);
            service.update(workflow);
        }
        workflow = service.resume(workflowInstanceId, map);
        return Response.ok(workflow).build();
    } catch (NotFoundException e) {
        return Response.status(Status.NOT_FOUND).build();
    } catch (UnauthorizedException e) {
        return Response.status(Status.UNAUTHORIZED).build();
    } catch (IllegalStateException e) {
        logger.warn(ExceptionUtils.getMessage(e));
        return Response.status(Status.CONFLICT).build();
    } catch (WorkflowException e) {
        logger.error(ExceptionUtils.getMessage(e), e);
        return Response.serverError().build();
    } catch (Exception e) {
        logger.error(ExceptionUtils.getMessage(e), e);
        return Response.serverError().build();
    } finally {
        lock.unlock();
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URISyntaxException(java.net.URISyntaxException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 14 with WorkflowException

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

the class WorkflowRestService method startWorkflow.

private WorkflowInstanceImpl startWorkflow(WorkflowDefinition workflowDefinition, MediaPackageImpl mp, String parentWorkflowId, LocalHashMap localMap) {
    Map<String, String> properties = new HashMap<String, String>();
    if (localMap != null)
        properties = localMap.getMap();
    Long parentIdAsLong = null;
    if (StringUtils.isNotEmpty(parentWorkflowId)) {
        try {
            parentIdAsLong = Long.parseLong(parentWorkflowId);
        } catch (NumberFormatException e) {
            throw new WebApplicationException(e, Status.BAD_REQUEST);
        }
    }
    try {
        return (WorkflowInstanceImpl) service.start(workflowDefinition, mp, parentIdAsLong, properties);
    } catch (WorkflowException e) {
        throw new WebApplicationException(e);
    } catch (NotFoundException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) LocalHashMap(org.opencastproject.util.LocalHashMap) WorkflowException(org.opencastproject.workflow.api.WorkflowException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 15 with WorkflowException

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

the class WorkflowServiceRemoteImpl method resume.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowService#resume(long, java.util.Map)
 */
@Override
public WorkflowInstance resume(long workflowInstanceId, Map<String, String> properties) throws NotFoundException, UnauthorizedException, WorkflowException, IllegalStateException {
    HttpPost post = new HttpPost("/resume");
    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
    params.add(new BasicNameValuePair("id", Long.toString(workflowInstanceId)));
    if (properties != null)
        params.add(new BasicNameValuePair("properties", mapToString(properties)));
    post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
    HttpResponse response = getResponse(post, SC_OK, SC_NOT_FOUND, SC_UNAUTHORIZED, SC_CONFLICT);
    try {
        if (response != null) {
            if (response.getStatusLine().getStatusCode() == SC_NOT_FOUND) {
                throw new NotFoundException("Workflow instance with id='" + workflowInstanceId + "' not found");
            } else if (response.getStatusLine().getStatusCode() == SC_UNAUTHORIZED) {
                throw new UnauthorizedException("You do not have permission to resume");
            } else if (response.getStatusLine().getStatusCode() == SC_CONFLICT) {
                throw new IllegalStateException("Can not resume a workflow where the current state is not in paused");
            } else {
                logger.info("Workflow '{}' resumed", workflowInstanceId);
                return WorkflowParser.parseWorkflowInstance(response.getEntity().getContent());
            }
        }
    } catch (NotFoundException e) {
        throw e;
    } catch (UnauthorizedException e) {
        throw e;
    } catch (IllegalStateException e) {
        throw e;
    } catch (Exception e) {
        throw new WorkflowException(e);
    } finally {
        closeConnection(response);
    }
    throw new WorkflowException("Unable to resume workflow instance " + workflowInstanceId);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) WorkflowException(org.opencastproject.workflow.api.WorkflowException) ArrayList(java.util.ArrayList) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) HttpResponse(org.apache.http.HttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) ParseException(org.apache.http.ParseException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) IOException(java.io.IOException) WorkflowParsingException(org.opencastproject.workflow.api.WorkflowParsingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

WorkflowException (org.opencastproject.workflow.api.WorkflowException)15 NotFoundException (org.opencastproject.util.NotFoundException)10 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)9 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)8 IOException (java.io.IOException)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)6 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)6 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)5 WorkflowParsingException (org.opencastproject.workflow.api.WorkflowParsingException)5 Lock (java.util.concurrent.locks.Lock)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)3 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)3 SeriesException (org.opencastproject.series.api.SeriesException)3 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)3 UndispatchableJobException (org.opencastproject.serviceregistry.api.UndispatchableJobException)3 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)3 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)3 WorkflowStateException (org.opencastproject.workflow.api.WorkflowStateException)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3