Search in sources :

Example 1 with Incident

use of org.opencastproject.job.api.Incident in project opencast by opencast.

the class AbstractIncidentService method getIncidentsOfJob.

@Override
public IncidentTree getIncidentsOfJob(long jobId, boolean cascade) throws NotFoundException, IncidentServiceException {
    List<Incident> incidents = getIncidentsOfJob(jobId);
    List<IncidentTree> childIncidents = new ArrayList<IncidentTree>();
    try {
        Job job = getServiceRegistry().getJob(jobId);
        if (cascade && !"START_WORKFLOW".equals(job.getOperation())) {
            childIncidents = getChildIncidents(jobId);
        } else if (cascade && "START_WORKFLOW".equals(job.getOperation())) {
            for (WorkflowOperationInstance operation : getWorkflowService().getWorkflowById(jobId).getOperations()) {
                if (operation.getState().equals(OperationState.INSTANTIATED))
                    continue;
                IncidentTree operationResult = getIncidentsOfJob(operation.getId(), true);
                if (hasIncidents(Collections.list(operationResult)))
                    childIncidents.add(operationResult);
            }
        }
        return new IncidentTreeImpl(incidents, childIncidents);
    } catch (NotFoundException ignore) {
        // Workflow deleted
        return new IncidentTreeImpl(incidents, childIncidents);
    } catch (Exception e) {
        logger.error("Error loading child jobs of {}: {}", jobId);
        throw new IncidentServiceException(e);
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) IncidentTreeImpl(org.opencastproject.job.api.IncidentTreeImpl) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) Incident(org.opencastproject.job.api.Incident) Job(org.opencastproject.job.api.Job) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) NotFoundException(org.opencastproject.util.NotFoundException) IncidentTree(org.opencastproject.job.api.IncidentTree)

Example 2 with Incident

use of org.opencastproject.job.api.Incident in project opencast by opencast.

the class IncidentServiceEndpoint method postIncident.

@POST
@Produces(MediaType.APPLICATION_XML)
@Path("/")
@RestQuery(name = "postincident", description = "Creates a new job incident and returns it as XML", returnDescription = "Returns the created job incident as XML", restParameters = { @RestParameter(name = "job", isRequired = true, description = "The job on where to create the incident", type = Type.TEXT), @RestParameter(name = "date", isRequired = true, description = "The incident creation date", type = Type.STRING), @RestParameter(name = "code", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "severity", isRequired = true, description = "The incident error code", type = Type.STRING), @RestParameter(name = "details", isRequired = false, description = "The incident details", type = Type.TEXT), @RestParameter(name = "params", isRequired = false, description = "The incident parameters", type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "New job incident has been created"), @RestResponse(responseCode = SC_BAD_REQUEST, description = "Unable to parse the one of the form params"), @RestResponse(responseCode = SC_CONFLICT, description = "No job incident related job exists") })
public Response postIncident(@FormParam("job") String jobXml, @FormParam("date") String date, @FormParam("code") String code, @FormParam("severity") String severityString, @FormParam("details") String details, @FormParam("params") LocalHashMap params) {
    Job job;
    Date timestamp;
    Severity severity;
    Map<String, String> map = new HashMap<String, String>();
    List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
    try {
        job = JobParser.parseJob(jobXml);
        timestamp = new Date(DateTimeSupport.fromUTC(date));
        severity = Severity.valueOf(severityString);
        if (params != null)
            map = params.getMap();
        if (StringUtils.isNotBlank(details)) {
            final JSONArray array = (JSONArray) JSONValue.parse(details);
            for (int i = 0; i < array.size(); i++) {
                JSONObject tuple = (JSONObject) array.get(i);
                list.add(Tuple.tuple((String) tuple.get("title"), (String) tuple.get("content")));
            }
        }
    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
    try {
        Incident incident = svc.storeIncident(job, timestamp, code, severity, map, list);
        String uri = UrlSupport.concat(serverUrl, serviceUrl, Long.toString(incident.getId()), ".xml");
        return Response.created(new URI(uri)).entity(new JaxbIncident(incident)).build();
    } catch (IllegalStateException e) {
        return Response.status(Status.CONFLICT).build();
    } catch (Exception e) {
        logger.warn("Unable to post incident for job {}: {}", job.getId(), e);
        throw new WebApplicationException(INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) LocalHashMap(org.opencastproject.util.LocalHashMap) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Severity(org.opencastproject.job.api.Incident.Severity) URI(java.net.URI) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) NotFoundException(org.opencastproject.util.NotFoundException) JSONObject(org.json.simple.JSONObject) JaxbIncident(org.opencastproject.job.api.JaxbIncident) JaxbIncident(org.opencastproject.job.api.JaxbIncident) Incident(org.opencastproject.job.api.Incident) Job(org.opencastproject.job.api.Job) Tuple(org.opencastproject.util.data.Tuple) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with Incident

use of org.opencastproject.job.api.Incident in project opencast by opencast.

the class IncidentServiceEndpoint method getIncidentsOfJobAsList.

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("job/incidents.{type:xml|json}")
@RestQuery(name = "incidentsofjobaslist", description = "Returns the job incidents with the given identifiers.", returnDescription = "Returns the job incidents.", pathParameters = { @RestParameter(name = "type", isRequired = true, description = "The media type of the response [xml|json]", defaultValue = "xml", type = Type.STRING) }, restParameters = { @RestParameter(name = "id", isRequired = true, description = "The job identifiers.", type = Type.INTEGER), @RestParameter(name = "format", isRequired = false, description = "The response format [full|digest|sys]. Defaults to sys", defaultValue = "sys", type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "The job incidents.") })
public Response getIncidentsOfJobAsList(@Context HttpServletRequest request, @QueryParam("id") final List<Long> jobIds, @QueryParam("format") @DefaultValue(FMT_DEFAULT) final String format, @PathParam("type") final String type) {
    try {
        final List<Incident> incidents = svc.getIncidentsOfJob(jobIds);
        final MediaType mt = getResponseType(type);
        if (eq(FMT_SYS, format)) {
            return ok(mt, new JaxbIncidentList(incidents));
        } else if (eq(FMT_DIGEST, format)) {
            return ok(mt, new JaxbIncidentDigestList(svc, request.getLocale(), incidents));
        } else if (eq(FMT_FULL, format)) {
            return ok(mt, new JaxbIncidentFullList(svc, request.getLocale(), incidents));
        } else {
            return unknownFormat();
        }
    } catch (NotFoundException e) {
        // should not happen
        logger.error("Unable to get job incident for id {}! Consistency issue!");
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    } catch (IncidentServiceException e) {
        logger.warn("Unable to get job incident for id {}: {}", jobIds, e.getMessage());
        throw new WebApplicationException(INTERNAL_SERVER_ERROR);
    }
}
Also used : JaxbIncidentDigestList(org.opencastproject.job.api.JaxbIncidentDigestList) WebApplicationException(javax.ws.rs.WebApplicationException) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) MediaType(javax.ws.rs.core.MediaType) JaxbIncidentList(org.opencastproject.job.api.JaxbIncidentList) NotFoundException(org.opencastproject.util.NotFoundException) JaxbIncident(org.opencastproject.job.api.JaxbIncident) Incident(org.opencastproject.job.api.Incident) JaxbIncidentFullList(org.opencastproject.job.api.JaxbIncidentFullList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 4 with Incident

use of org.opencastproject.job.api.Incident in project opencast by opencast.

the class JobEndpoint method getIncidentsAsJSON.

/**
 * Returns the list of incidents for a given workflow instance
 *
 * @param jobId
 *          the workflow instance id
 * @param locale
 *          the language in which title and description shall be returned
 * @param cascade
 *          if true, return the incidents of the given job and those of of its descendants
 * @return the list incidents as JSON array
 * @throws JobEndpointException
 * @throws NotFoundException
 */
public JValue getIncidentsAsJSON(long jobId, final Locale locale, boolean cascade) throws JobEndpointException, NotFoundException {
    final List<Incident> incidents;
    try {
        final IncidentTree it = incidentService.getIncidentsOfJob(jobId, cascade);
        incidents = cascade ? flatten(it) : it.getIncidents();
    } catch (IncidentServiceException e) {
        throw new JobEndpointException(String.format("Not able to get the incidents for the job %d from the incident service : %s", jobId, e), e.getCause());
    }
    final Stream<JValue> json = $(incidents).map(new Fn<Incident, JValue>() {

        @Override
        public JValue apply(Incident i) {
            return obj(f("id", v(i.getId())), f("severity", v(i.getSeverity(), Jsons.BLANK)), f("timestamp", v(toUTC(i.getTimestamp().getTime()), Jsons.BLANK))).merge(localizeIncident(i, locale));
        }
    });
    return arr(json);
}
Also used : JobEndpointException(org.opencastproject.adminui.exception.JobEndpointException) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) JValue(com.entwinemedia.fn.data.json.JValue) Incident(org.opencastproject.job.api.Incident) IncidentTree(org.opencastproject.job.api.IncidentTree)

Example 5 with Incident

use of org.opencastproject.job.api.Incident in project opencast by opencast.

the class EmailTemplateServiceImpl method applyTemplate.

/**
 * Apply the template to the workflow instance.
 *
 * @param templateName
 *          template name
 * @param templateContent
 *          template content
 * @param workflowInstance
 *          workflow
 * @return text with applied template
 */
@Override
public String applyTemplate(String templateName, String templateContent, WorkflowInstance workflowInstance) {
    if (templateContent == null && templateScannerRef.get() != null) {
        templateContent = templateScannerRef.get().getTemplate(templateName);
    }
    if (templateContent == null) {
        logger.warn("E-mail template not found: {}", templateName);
        // it's probably missing
        return "TEMPLATE NOT FOUND: " + templateName;
    }
    // Build email data structure and apply the template
    HashMap<String, HashMap<String, String>> catalogs = initCatalogs(workflowInstance.getMediaPackage());
    WorkflowOperationInstance failed = findFailedOperation(workflowInstance);
    List<Incident> incidentList = null;
    if (failed != null) {
        try {
            IncidentTree incidents = incidentService.getIncidentsOfJob(failed.getId(), true);
            incidentList = generateIncidentList(incidents);
        } catch (Exception e) {
            logger.error("Error when populating template with incidents", e);
        // Incidents in email will be empty
        }
    }
    return DocUtil.generate(new EmailData(templateName, workflowInstance, catalogs, failed, incidentList), templateContent);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) HashMap(java.util.HashMap) Incident(org.opencastproject.job.api.Incident) IncidentTree(org.opencastproject.job.api.IncidentTree)

Aggregations

Incident (org.opencastproject.job.api.Incident)12 IncidentTree (org.opencastproject.job.api.IncidentTree)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 Job (org.opencastproject.job.api.Job)4 IncidentServiceException (org.opencastproject.serviceregistry.api.IncidentServiceException)4 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)4 URI (java.net.URI)3 LinkedList (java.util.LinkedList)3 NotFoundException (org.opencastproject.util.NotFoundException)3 Tuple (org.opencastproject.util.data.Tuple)3 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Before (org.junit.Before)2 IncidentTreeImpl (org.opencastproject.job.api.IncidentTreeImpl)2 JaxbIncident (org.opencastproject.job.api.JaxbIncident)2 MediaPackageBuilder (org.opencastproject.mediapackage.MediaPackageBuilder)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 WorkflowDefinitionImpl (org.opencastproject.workflow.api.WorkflowDefinitionImpl)2