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