use of org.opencastproject.util.MimeType in project opencast by opencast.
the class MediaInspectionServiceImplTest method testEnrichmentEmptyContainer.
@Test
public void testEnrichmentEmptyContainer() throws Exception {
final URI trackUri = getResource("/nostreams.mp4");
for (MediaInspector mi : init(trackUri)) {
Track track = mi.inspectTrack(trackUri, Options.NO_OPTION);
// make changes to metadata
Checksum cs = track.getChecksum();
track.setChecksum(null);
MimeType mt = mimeType("video", "flash");
track.setMimeType(mt);
// test the enrich scenario
Track newTrack = (Track) mi.enrich(track, false, Options.NO_OPTION);
assertEquals(newTrack.getChecksum(), cs);
assertEquals(newTrack.getMimeType(), mt);
assertNull(newTrack.getDuration());
// test the override scenario
newTrack = (Track) mi.enrich(track, true, Options.NO_OPTION);
assertEquals(newTrack.getChecksum(), cs);
assertNotSame(newTrack.getMimeType(), mt);
assertNull(newTrack.getDuration());
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class PublishOaiPmhWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Running distribution workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
// Check which tags have been configured
String downloadTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_TAGS));
String downloadFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_FLAVORS));
String streamingTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_TAGS));
String streamingFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_FLAVORS));
boolean checkAvailability = option(workflowInstance.getCurrentOperation().getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
String repository = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(REPOSITORY));
Opt<String> externalChannel = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_CHANNEL_NAME);
Opt<String> externalTempalte = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_TEMPLATE);
Opt<MimeType> externalMimetype = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_MIME_TYPE).bind(MimeTypes.toMimeType);
if (repository == null)
throw new IllegalArgumentException("No repository has been specified");
String[] sourceDownloadTags = StringUtils.split(downloadTags, ",");
String[] sourceDownloadFlavors = StringUtils.split(downloadFlavors, ",");
String[] sourceStreamingTags = StringUtils.split(streamingTags, ",");
String[] sourceStreamingFlavors = StringUtils.split(streamingFlavors, ",");
if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
logger.warn("No tags or flavors have been specified, so nothing will be published to the engage");
return createResult(mediaPackage, Action.CONTINUE);
}
final SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
for (String flavor : sourceDownloadFlavors) {
downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceDownloadTags) {
downloadElementSelector.addTag(tag);
}
final Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
final Collection<MediaPackageElement> streamingElements;
if (distributeStreaming) {
final SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
for (String flavor : sourceStreamingFlavors) {
streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceStreamingTags) {
streamingElementSelector.addTag(tag);
}
streamingElements = streamingElementSelector.select(mediaPackage, false);
} else {
streamingElements = list();
}
try {
Set<String> downloadElementIds = new HashSet<>();
Set<String> streamingElementIds = new HashSet<>();
// Look for elements matching the tag
for (MediaPackageElement elem : downloadElements) {
downloadElementIds.add(elem.getIdentifier());
}
for (MediaPackageElement elem : streamingElements) {
streamingElementIds.add(elem.getIdentifier());
}
// Also distribute the security configuration
// -----
// This was removed in the meantime by a fix for MH-8515, but could now be used again.
// -----
Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
if (securityAttachments != null && securityAttachments.length > 0) {
for (Attachment a : securityAttachments) {
downloadElementIds.add(a.getIdentifier());
streamingElementIds.add(a.getIdentifier());
}
}
Job publishJob = null;
try {
publishJob = publicationService.publish(mediaPackage, repository, downloadElementIds, streamingElementIds, checkAvailability);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Error parsing media package", e);
} catch (PublicationException e) {
throw new WorkflowOperationException("Error parsing media package", e);
}
// Wait until the publication job has returned
if (!waitForStatus(publishJob).isSuccess())
throw new WorkflowOperationException("Mediapackage " + mediaPackage.getIdentifier() + " could not be published to OAI-PMH repository " + repository);
// The job has passed
Job job = serviceRegistry.getJob(publishJob.getId());
// If there is no payload, then the item has not been published.
if (job.getPayload() == null) {
logger.warn("Publish to OAI-PMH repository '{}' failed, no payload from publication job: {}", repository, job);
return createResult(mediaPackage, Action.CONTINUE);
}
Publication newElement = null;
try {
newElement = (Publication) MediaPackageElementParser.getFromXml(job.getPayload());
} catch (MediaPackageException e) {
throw new WorkflowOperationException(e);
}
if (newElement == null) {
logger.warn("Publication to OAI-PMH repository '{}' failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", repository, job.getPayload(), job.toString());
return createResult(mediaPackage, Action.CONTINUE);
}
for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(newElement.getChannel()).toFn())) {
mediaPackage.remove(existingPublication);
}
mediaPackage.add(newElement);
if (externalChannel.isSome() && externalMimetype.isSome() && externalTempalte.isSome()) {
String template = externalTempalte.get().replace("{event}", mediaPackage.getIdentifier().compact());
if (StringUtils.isNotBlank(mediaPackage.getSeries()))
template = template.replace("{series}", mediaPackage.getSeries());
Publication externalElement = PublicationImpl.publication(UUID.randomUUID().toString(), externalChannel.get(), URI.create(template), externalMimetype.get());
for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(externalChannel.get()).toFn())) {
mediaPackage.remove(existingPublication);
}
mediaPackage.add(externalElement);
}
logger.debug("Publication to OAI-PMH repository '{}' operation completed", repository);
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
throw new WorkflowOperationException(e);
}
}
return createResult(mediaPackage, Action.CONTINUE);
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class CatalogBuilderPlugin method elementFromManifest.
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
* org.opencastproject.mediapackage.MediaPackageSerializer)
*/
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
String id = null;
String flavor = null;
URI url = null;
long size = -1;
Checksum checksum = null;
MimeType mimeType = null;
String reference = null;
try {
// id
id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
// url
url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
// flavor
flavor = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
// reference
reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
// size
String documentSize = xpath.evaluate("size/text()", elementNode).trim();
if (!"".equals(documentSize))
size = Long.parseLong(documentSize);
// checksum
String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
// mimetype
String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
if (StringUtils.isNotEmpty(mimeTypeValue))
mimeType = MimeTypes.parseMimeType(mimeTypeValue);
// create the catalog
Catalog dc = CatalogImpl.fromURI(url);
if (StringUtils.isNotEmpty(id))
dc.setIdentifier(id);
// Add url
dc.setURI(url);
// Add flavor
if (flavor != null)
dc.setFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
// Add reference
if (StringUtils.isNotEmpty(reference))
dc.referTo(MediaPackageReferenceImpl.fromString(reference));
// Set size
if (size > 0)
dc.setSize(size);
// Set checksum
if (checksum != null)
dc.setChecksum(checksum);
// Set Mimetype
if (mimeType != null)
dc.setMimeType(mimeType);
// Tags
NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
for (int i = 0; i < tagNodes.getLength(); i++) {
dc.addTag(tagNodes.item(i).getTextContent());
}
return dc;
} catch (XPathExpressionException e) {
throw new UnsupportedElementException("Error while reading catalog information from manifest: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
} catch (URISyntaxException e) {
throw new UnsupportedElementException("Error while reading dublin core catalog " + url + ": " + e.getMessage());
}
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class EncodingProfileScanner method loadProfile.
/**
* Reads the profile from the given properties
*
* @param profile
* @param properties
* @param artifact
* @return the loaded profile or null if profile
* @throws RuntimeException
*/
private EncodingProfile loadProfile(String profile, Properties properties, File artifact) throws ConfigurationException {
List<String> defaultProperties = new ArrayList<>(10);
String name = getDefaultProperty(profile, PROP_NAME, properties, defaultProperties);
if (name == null || "".equals(name))
throw new ConfigurationException("Distribution profile '" + profile + "' is missing a name (" + PROP_NAME + "). (Check web.xml profiles.)");
EncodingProfileImpl df = new EncodingProfileImpl(profile, name, artifact);
// Output Type
String type = getDefaultProperty(profile, PROP_OUTPUT, properties, defaultProperties);
if (StringUtils.isBlank(type))
throw new ConfigurationException("Output type (" + PROP_OUTPUT + ") of profile '" + profile + "' is missing");
try {
df.setOutputType(MediaType.parseString(StringUtils.trimToEmpty(type)));
} catch (IllegalArgumentException e) {
throw new ConfigurationException("Output type (" + PROP_OUTPUT + ") '" + type + "' of profile '" + profile + "' is unknwon");
}
// Suffixes with tags?
List<String> tags = getTags(profile, properties, defaultProperties);
if (tags.size() > 0) {
for (String tag : tags) {
String prop = PROP_SUFFIX + "." + tag;
String suffixObj = getDefaultProperty(profile, prop, properties, defaultProperties);
df.setSuffix(tag, StringUtils.trim(suffixObj));
}
} else {
// Suffix old stile, without tags
String suffixObj = getDefaultProperty(profile, PROP_SUFFIX, properties, defaultProperties);
if (StringUtils.isBlank(suffixObj))
throw new ConfigurationException("Suffix (" + PROP_SUFFIX + ") of profile '" + profile + "' is missing");
df.setSuffix(StringUtils.trim(suffixObj));
}
// Mimetype
String mimeTypeObj = getDefaultProperty(profile, PROP_MIMETYPE, properties, defaultProperties);
if (StringUtils.isNotBlank(mimeTypeObj)) {
MimeType mimeType;
try {
mimeType = MimeTypes.parseMimeType(mimeTypeObj);
} catch (Exception e) {
throw new ConfigurationException("Mime type (" + PROP_MIMETYPE + ") " + mimeTypeObj + " could not be parsed as a mime type! Expressions are not allowed!");
}
df.setMimeType(mimeType.toString());
}
// Applicable to the following track categories
String applicableObj = getDefaultProperty(profile, PROP_APPLICABLE, properties, defaultProperties);
if (StringUtils.isBlank(applicableObj))
throw new ConfigurationException("Input type (" + PROP_APPLICABLE + ") of profile '" + profile + "' is missing");
df.setApplicableType(MediaType.parseString(StringUtils.trimToEmpty(applicableObj)));
String jobLoad = getDefaultProperty(profile, PROP_JOBLOAD, properties, defaultProperties);
if (!StringUtils.isBlank(jobLoad)) {
df.setJobLoad(Float.valueOf(jobLoad));
logger.debug("Setting job load for profile {} to {}", profile, jobLoad);
}
// Look for extensions
String extensionKey = PROP_PREFIX + profile + ".";
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = entry.getKey().toString();
if (key.startsWith(extensionKey) && !defaultProperties.contains(key)) {
String k = key.substring(extensionKey.length());
String v = StringUtils.trimToEmpty(entry.getValue().toString());
df.addExtension(k, v);
}
}
return df;
}
use of org.opencastproject.util.MimeType in project opencast by opencast.
the class ThemeWorkflowOperationHandler method addElement.
private void addElement(MediaPackage mediaPackage, final MediaPackageElementFlavor flavor, final List<String> tags, InputStream file, String filename, Type type) throws IOException {
MediaPackageElement element = elementBuilderFactory.newElementBuilder().newElement(type, flavor);
element.setIdentifier(UUID.randomUUID().toString());
for (String tag : tags) {
element.addTag(tag);
}
URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), filename, file);
element.setURI(uri);
try {
MimeType mimeType = MimeTypes.fromString(filename);
element.setMimeType(mimeType);
} catch (UnknownFileTypeException e) {
logger.warn("Unable to detect the mime type of file {}", filename);
}
mediaPackage.add(element);
}
Aggregations