Search in sources :

Example 6 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class IngestDownloadWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String baseUrl = workspace.getBaseUri().toString();
    List<URI> externalUris = new ArrayList<URI>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (element.getElementType() == MediaPackageElement.Type.Publication) {
            logger.debug("Skipping downloading media package element {} from media package {} " + "because it is a publication: {}", element.getIdentifier(), mediaPackage.getIdentifier().compact(), element.getURI());
            continue;
        }
        URI originalElementUri = element.getURI();
        if (originalElementUri.toString().startsWith(baseUrl)) {
            logger.info("Skipping downloading already existing element {}", originalElementUri);
            continue;
        }
        // Download the external URI
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (Exception e) {
            logger.warn("Unable to download the external element {}", element.getURI());
            throw new WorkflowOperationException("Unable to download the external element " + element.getURI(), e);
        }
        // Put to working file repository and rewrite URI on element
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), FilenameUtils.getName(element.getURI().getPath()), in);
            element.setURI(uri);
        } catch (Exception e) {
            logger.warn("Unable to store downloaded element '{}': {}", element.getURI(), e.getMessage());
            throw new WorkflowOperationException("Unable to store downloaded element " + element.getURI(), e);
        } finally {
            IOUtils.closeQuietly(in);
            try {
                workspace.delete(originalElementUri);
            } catch (Exception e) {
                logger.warn("Unable to delete ingest-downloaded element {}: {}", element.getURI(), e);
            }
        }
        logger.info("Downloaded the external element {}", originalElementUri);
        // Store origianl URI for deletion
        externalUris.add(originalElementUri);
    }
    if (!deleteExternal || externalUris.size() == 0)
        return createResult(mediaPackage, Action.CONTINUE);
    // Find all external working file repository base Urls
    logger.debug("Assembling list of external working file repositories");
    List<String> externalWfrBaseUrls = new ArrayList<String>();
    try {
        for (ServiceRegistration reg : serviceRegistry.getServiceRegistrationsByType(WorkingFileRepository.SERVICE_TYPE)) {
            if (baseUrl.startsWith(reg.getHost())) {
                logger.trace("Skpping local working file repository");
                continue;
            }
            externalWfrBaseUrls.add(UrlSupport.concat(reg.getHost(), reg.getPath()));
        }
        logger.debug("{} external working file repositories found", externalWfrBaseUrls.size());
    } catch (ServiceRegistryException e) {
        logger.error("Unable to load WFR services from service registry: {}", e.getMessage());
        throw new WorkflowOperationException(e);
    }
    for (URI uri : externalUris) {
        String elementUri = uri.toString();
        // Delete external working file repository URI's
        String wfrBaseUrl = null;
        for (String url : externalWfrBaseUrls) {
            if (elementUri.startsWith(url)) {
                wfrBaseUrl = url;
                break;
            }
        }
        if (wfrBaseUrl == null) {
            logger.info("Unable to delete external URI {}, no working file repository found", elementUri);
            continue;
        }
        HttpDelete delete;
        if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX))) {
            String wfrDeleteUrl = elementUri.substring(0, elementUri.lastIndexOf("/"));
            delete = new HttpDelete(wfrDeleteUrl);
        } else if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.COLLECTION_PATH_PREFIX))) {
            delete = new HttpDelete(elementUri);
        } else {
            logger.info("Unable to handle working file repository URI {}", elementUri);
            continue;
        }
        HttpResponse response = null;
        try {
            response = client.execute(delete);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_OK) {
                logger.info("Sucessfully deleted external URI {}", delete.getURI());
            } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
                logger.info("External URI {} has already been deleted", delete.getURI());
            } else {
                logger.info("Unable to delete external URI {}, status code '{}' returned", delete.getURI(), statusCode);
            }
        } catch (TrustedHttpClientException e) {
            logger.warn("Unable to execute DELETE request on external URI {}", delete.getURI());
            throw new WorkflowOperationException(e);
        } finally {
            client.close(response);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) File(java.io.File) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 7 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class MediaInspectionServiceImpl method process.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
 */
@Override
protected String process(Job job) throws Exception {
    Operation op = null;
    String operation = job.getOperation();
    List<String> arguments = job.getArguments();
    try {
        op = Operation.valueOf(operation);
        MediaPackageElement inspectedElement = null;
        Map<String, String> options = null;
        switch(op) {
            case Inspect:
                URI uri = URI.create(arguments.get(0));
                options = Options.fromJson(arguments.get(1));
                inspectedElement = inspector.inspectTrack(uri, options);
                break;
            case Enrich:
                MediaPackageElement element = MediaPackageElementParser.getFromXml(arguments.get(0));
                boolean overwrite = Boolean.parseBoolean(arguments.get(1));
                options = Options.fromJson(arguments.get(2));
                inspectedElement = inspector.enrich(element, overwrite, options);
                break;
            default:
                throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
        }
        return MediaPackageElementParser.getAsXml(inspectedElement);
    } catch (IllegalArgumentException e) {
        throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
    } catch (IndexOutOfBoundsException e) {
        throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
    } catch (Exception e) {
        throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException)

Example 8 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class ServicesListProvider method getList.

@Override
public Map<String, String> getList(String listName, ResourceListQuery query, Organization organization) throws ListProviderException {
    ServicesListQuery servicesQuery;
    try {
        servicesQuery = (ServicesListQuery) query;
    } catch (ClassCastException ex) {
        servicesQuery = new ServicesListQuery(query);
    }
    Map<String, String> result = new HashMap<String, String>();
    if (LIST_STATUS.equals(listName)) {
        for (ServiceState s : ServiceState.values()) {
            result.put(s.toString(), SERVICE_STATUS_FILTER_PREFIX + s.toString());
        }
        return result;
    }
    List<ServiceRegistration> serviceRegistrations;
    try {
        serviceRegistrations = serviceRegistry.getServiceRegistrations();
    } catch (ServiceRegistryException ex) {
        throw new ListProviderException("Failed to get service registrations.", ex);
    }
    for (ServiceRegistration serviceRegistration : serviceRegistrations) {
        if (servicesQuery.getHostname().isSome() && !StringUtils.equals(servicesQuery.getHostname().get(), serviceRegistration.getHost()))
            continue;
        if (servicesQuery.getActions().isSome() && servicesQuery.getActions().get() && serviceRegistration.getServiceState() == ServiceState.NORMAL)
            continue;
        result.put(serviceRegistration.getServiceType(), serviceRegistration.getServiceType());
    }
    if (servicesQuery.getLimit().isSome() || servicesQuery.getLimit().isSome()) {
        int limit = servicesQuery.getLimit().getOrElse(0);
        int offset = servicesQuery.getOffset().getOrElse(0);
        result = new SmartIterator(limit, offset).applyLimitAndOffset(result);
    }
    return result;
}
Also used : ServiceState(org.opencastproject.serviceregistry.api.ServiceState) SmartIterator(org.opencastproject.util.SmartIterator) HashMap(java.util.HashMap) ListProviderException(org.opencastproject.index.service.exception.ListProviderException) ServicesListQuery(org.opencastproject.index.service.resources.list.query.ServicesListQuery) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 9 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class ServiceRegistryEndpoint method removeParentlessJobs.

@POST
@Path("removejobs")
@RestQuery(name = "removejobs", description = "Removes all given jobs and their child jobs", returnDescription = "No data is returned, just the HTTP status code", restParameters = { @RestParameter(name = "jobIds", isRequired = true, description = "The IDs of the jobs to delete", type = Type.TEXT) }, reponses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "Jobs successfully removed"), @RestResponse(responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR, description = "Error while removing jobs") })
public Response removeParentlessJobs(@FormParam("jobIds") String jobIds) throws NotFoundException {
    try {
        final JSONArray array = (JSONArray) JSONValue.parse(jobIds);
        final List<Long> jobIdList = Arrays.asList((Long[]) array.toArray(new Long[0]));
        serviceRegistry.removeJobs(jobIdList);
        return Response.noContent().build();
    } catch (ServiceRegistryException e) {
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JSONArray(org.json.simple.JSONArray) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 10 with ServiceRegistryException

use of org.opencastproject.serviceregistry.api.ServiceRegistryException in project opencast by opencast.

the class ServiceRegistryJpaImpl method countOfAbnormalServices.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#countOfAbnormalServices()
 */
@Override
public long countOfAbnormalServices() throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query query = em.createNamedQuery("ServiceRegistration.countNotNormal");
        Number count = (Number) query.getSingleResult();
        return count.longValue();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) URISyntaxException(java.net.URISyntaxException) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) NotFoundException(org.opencastproject.util.NotFoundException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Aggregations

ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)99 NotFoundException (org.opencastproject.util.NotFoundException)61 ConfigurationException (org.osgi.service.cm.ConfigurationException)41 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)30 URISyntaxException (java.net.URISyntaxException)29 Job (org.opencastproject.job.api.Job)29 PersistenceException (javax.persistence.PersistenceException)26 RollbackException (javax.persistence.RollbackException)26 TrustedHttpClientException (org.opencastproject.security.api.TrustedHttpClientException)26 NoResultException (javax.persistence.NoResultException)25 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)24 EntityManager (javax.persistence.EntityManager)22 MediaPackage (org.opencastproject.mediapackage.MediaPackage)20 URI (java.net.URI)16 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)14 DistributionException (org.opencastproject.distribution.api.DistributionException)13 Attachment (org.opencastproject.mediapackage.Attachment)12 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)12