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;
}
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);
}
}
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();
}
}
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);
}
}
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);
}
Aggregations