use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler 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 engage publication workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
// Check which tags have been configured
String downloadSourceTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_TAGS));
String downloadTargetTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_TARGET_TAGS));
String downloadSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_FLAVORS));
String downloadTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(DOWNLOAD_TARGET_SUBFLAVOR));
String streamingSourceTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_TAGS));
String streamingTargetTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_TARGET_TAGS));
String streamingSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_FLAVORS));
String streamingTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(STREAMING_TARGET_SUBFLAVOR));
String republishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
boolean checkAvailability = option(op.getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
String[] sourceDownloadTags = StringUtils.split(downloadSourceTags, ",");
String[] targetDownloadTags = StringUtils.split(downloadTargetTags, ",");
String[] sourceDownloadFlavors = StringUtils.split(downloadSourceFlavors, ",");
String[] sourceStreamingTags = StringUtils.split(streamingSourceTags, ",");
String[] targetStreamingTags = StringUtils.split(streamingTargetTags, ",");
String[] sourceStreamingFlavors = StringUtils.split(streamingSourceFlavors, ",");
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 publication channel");
return createResult(mediaPackage, Action.CONTINUE);
}
// Parse the download target flavor
MediaPackageElementFlavor downloadSubflavor = null;
if (downloadTargetSubflavor != null) {
try {
downloadSubflavor = MediaPackageElementFlavor.parseFlavor(downloadTargetSubflavor);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException(e);
}
}
// Parse the streaming target flavor
MediaPackageElementFlavor streamingSubflavor = null;
if (streamingTargetSubflavor != null) {
try {
streamingSubflavor = MediaPackageElementFlavor.parseFlavor(streamingTargetSubflavor);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException(e);
}
}
// Configure the download element selector
SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
for (String flavor : sourceDownloadFlavors) {
downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceDownloadTags) {
downloadElementSelector.addTag(tag);
}
// Configure the streaming element selector
SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
for (String flavor : sourceStreamingFlavors) {
streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceStreamingTags) {
streamingElementSelector.addTag(tag);
}
// Select the appropriate elements for download and streaming
Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
Collection<MediaPackageElement> streamingElements = streamingElementSelector.select(mediaPackage, false);
try {
Set<String> downloadElementIds = new HashSet<String>();
Set<String> streamingElementIds = new HashSet<String>();
// 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());
}
}
removePublicationElement(mediaPackage);
switch(republishStrategy) {
case ("merge"):
// nothing to do here. other publication strategies can be added to this list later on
break;
default:
retractFromEngage(mediaPackage);
}
List<Job> jobs = new ArrayList<Job>();
// distribute Elements
try {
if (downloadElementIds.size() > 0) {
Job job = downloadDistributionService.distribute(CHANNEL_ID, mediaPackage, downloadElementIds, checkAvailability);
if (job != null) {
jobs.add(job);
}
}
if (distributeStreaming) {
for (String elementId : streamingElementIds) {
Job job = streamingDistributionService.distribute(CHANNEL_ID, mediaPackage, elementId);
if (job != null) {
jobs.add(job);
}
}
}
} catch (DistributionException e) {
throw new WorkflowOperationException(e);
}
if (jobs.size() < 1) {
logger.info("No mediapackage element was found for distribution to engage");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait until all distribution jobs have returned
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess())
throw new WorkflowOperationException("One of the distribution jobs did not complete successfully");
logger.debug("Distribute of mediapackage {} completed", mediaPackage);
String engageUrlString = null;
try {
MediaPackage mediaPackageForSearch = getMediaPackageForSearchIndex(mediaPackage, jobs, downloadSubflavor, targetDownloadTags, downloadElementIds, streamingSubflavor, streamingElementIds, targetStreamingTags);
// MH-10216, check if only merging into existing mediapackage
removePublicationElement(mediaPackage);
switch(republishStrategy) {
case ("merge"):
// merge() returns merged mediapackage or null mediaPackage is not published
mediaPackageForSearch = merge(mediaPackageForSearch);
if (mediaPackageForSearch == null) {
logger.info("Skipping republish for {} since it is not currently published", mediaPackage.getIdentifier().toString());
return createResult(mediaPackage, Action.SKIP);
}
break;
default:
}
if (!isPublishable(mediaPackageForSearch))
throw new WorkflowOperationException("Media package does not meet criteria for publication");
logger.info("Publishing media package {} to search index", mediaPackageForSearch);
URL engageBaseUrl = null;
engageUrlString = StringUtils.trimToNull(workflowInstance.getOrganization().getProperties().get(ENGAGE_URL_PROPERTY));
if (engageUrlString != null) {
engageBaseUrl = new URL(engageUrlString);
} else {
engageBaseUrl = serverUrl;
logger.info("Using 'server.url' as a fallback for the non-existing organization level key '{}' for the publication url", ENGAGE_URL_PROPERTY);
}
// create the publication URI (used by Admin UI for event details link)
URI engageUri = this.createEngageUri(engageBaseUrl.toURI(), mediaPackage);
// Create new distribution element
Publication publicationElement = PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_ID, engageUri, MimeTypes.parseMimeType("text/html"));
mediaPackage.add(publicationElement);
// Adding media package to the search index
Job publishJob = null;
try {
publishJob = searchService.add(mediaPackageForSearch);
if (!waitForStatus(publishJob).isSuccess()) {
throw new WorkflowOperationException("Mediapackage " + mediaPackageForSearch.getIdentifier() + " could not be published");
}
} catch (SearchException e) {
throw new WorkflowOperationException("Error publishing media package", e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Error parsing media package", e);
}
logger.debug("Publishing of mediapackage {} completed", mediaPackage);
return createResult(mediaPackage, Action.CONTINUE);
} catch (MalformedURLException e) {
logger.error("{} is malformed: {}", ENGAGE_URL_PROPERTY, engageUrlString);
throw new WorkflowOperationException(e);
} catch (Throwable t) {
if (t instanceof WorkflowOperationException)
throw (WorkflowOperationException) t;
else
throw new WorkflowOperationException(t);
}
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
throw new WorkflowOperationException(e);
}
}
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class XACMLAuthorizationService method getAcl.
/**
* Get the ACL of the given flavor from a media package.
*/
private Option<AccessControlList> getAcl(final MediaPackage mp, final List<MediaPackageElementFlavor> flavors) {
Option<AccessControlList> result = Option.none();
Set<Attachment> attachments = new HashSet<>();
for (MediaPackageElementFlavor flavor : flavors) {
Attachment[] attachmentsArray = mp.getAttachments(flavor);
attachments.addAll(Arrays.asList(attachmentsArray));
}
if (attachments.size() == 1) {
logger.debug("One security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
for (Attachment attachment : attachments) {
result = loadAcl(attachment.getURI());
}
} else if (attachments.size() < 1) {
logger.debug("No security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
} else if (attachments.size() > 1) {
logger.warn("More than one security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
}
return result;
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class IndexServiceImpl method updateMpAssetFlavor.
/**
* Update the flavor of newly added asset with the passed metadata
*
* @param assetList
* the list of assets to update
* @param mp
* the mediapackage to update
* @param assetMetadata
* a set of mapping metadata for the asset list
* @param overwriteExisting
* true if the existing asset of the same flavor should be overwritten
* @return mediapackage updated with assets
*/
@SuppressWarnings("unchecked")
protected MediaPackage updateMpAssetFlavor(List<String> assetList, MediaPackage mp, JSONArray assetMetadata, Boolean overwriteExisting) {
// Create JSONObject data map
JSONObject assetDataMap = new JSONObject();
for (int i = 0; i < assetMetadata.size(); i++) {
try {
assetDataMap.put(((JSONObject) assetMetadata.get(i)).get("id"), assetMetadata.get(i));
} catch (Exception e) {
throw new IllegalArgumentException("Unable to parse metadata", e);
}
}
// Find the correct flavor for each asset.
for (String assetOrig : assetList) {
// expecting file assets to contain postfix "track_trackpart.0"
String asset = assetOrig;
String assetNumber = null;
String[] assetNameParts = asset.split(Pattern.quote("."));
if (assetNameParts.length > 1) {
asset = assetNameParts[0];
assetNumber = assetNameParts[1];
}
try {
if ((assetMetadata != null) && (assetDataMap.get(asset) != null)) {
String type = (String) ((JSONObject) assetDataMap.get(asset)).get("type");
String flavorType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorType");
String flavorSubType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorSubType");
if (patternNumberedAsset.matcher(flavorSubType).matches() && (assetNumber != null)) {
flavorSubType = assetNumber;
}
MediaPackageElementFlavor newElemflavor = new MediaPackageElementFlavor(flavorType, flavorSubType);
if (patternAttachment.matcher(type).matches()) {
if (overwriteExisting) {
// remove existing attachments of the new flavor
Attachment[] existing = mp.getAttachments(newElemflavor);
for (int i = 0; i < existing.length; i++) {
mp.remove(existing[i]);
logger.info("Overwriting existing asset {} {}", type, newElemflavor);
}
}
// correct the flavor of the new attachment
Attachment[] elArray = mp.getAttachments(new MediaPackageElementFlavor(assetOrig, "*"));
elArray[0].setFlavor(newElemflavor);
logger.info("Updated asset {} {}", type, newElemflavor);
} else if (patternCatalog.matcher(type).matches()) {
if (overwriteExisting) {
// remove existing catalogs of the new flavor
Catalog[] existing = mp.getCatalogs(newElemflavor);
for (int i = 0; i < existing.length; i++) {
mp.remove(existing[i]);
logger.info("Overwriting existing asset {} {}", type, newElemflavor);
}
}
Catalog[] catArray = mp.getCatalogs(new MediaPackageElementFlavor(assetOrig, "*"));
if (catArray.length > 1) {
throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
}
catArray[0].setFlavor(newElemflavor);
logger.info("Update asset {} {}", type, newElemflavor);
} else if (patternTrack.matcher(type).matches()) {
// Overwriting of existing tracks of same flavor is currently not allowed.
// TODO: allow overwriting of existing tracks of same flavor
Track[] trackArray = mp.getTracks(new MediaPackageElementFlavor(assetOrig, "*"));
if (trackArray.length > 1) {
throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
}
trackArray[0].setFlavor(newElemflavor);
logger.info("Update asset {} {}", type, newElemflavor);
} else {
logger.warn("Unknown asset type {} {} for field {}", type, newElemflavor, asset);
}
}
} catch (Exception e) {
// Assuming a parse error versus a file error and logging the error type
logger.warn("Unable to process asset metadata {}", assetMetadata.toJSONString(), e);
throw new IllegalArgumentException("Unable to parse metadata", e);
}
}
return mp;
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class EventIndexUtils method generatePublicationDoc.
/**
* Generate the document structure for the publication element
*
* @param publication
* the source publication element
* @return a map representing the ES document structure of the publication element
*/
private static HashMap<String, Object> generatePublicationDoc(Publication publication) {
HashMap<String, Object> pMap = new HashMap<String, Object>();
// Add first level elements
pMap.put(PublicationIndexSchema.CHANNEL, publication.getChannel());
addObjectStringtToMap(pMap, PublicationIndexSchema.MIMETYPE, publication.getMimeType());
// Attachments
Attachment[] attachments = publication.getAttachments();
HashMap<String, Object>[] attachmentsArray = new HashMap[attachments.length];
for (int i = 0; i < attachmentsArray.length; i++) {
Attachment attachment = attachments[i];
HashMap<String, Object> element = new HashMap<String, Object>();
element.put(PublicationIndexSchema.ELEMENT_ID, attachment.getIdentifier());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, attachment.getMimeType());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, attachment.getElementType());
element.put(PublicationIndexSchema.ELEMENT_TAG, attachment.getTags());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, attachment.getURI());
element.put(PublicationIndexSchema.ELEMENT_SIZE, attachment.getSize());
attachmentsArray[i] = element;
}
pMap.put(PublicationIndexSchema.ATTACHMENT, attachmentsArray);
// Catalogs
Catalog[] catalogs = publication.getCatalogs();
HashMap<String, Object>[] catalogsArray = new HashMap[catalogs.length];
for (int i = 0; i < catalogsArray.length; i++) {
Catalog catalog = catalogs[i];
HashMap<String, Object> element = new HashMap<String, Object>();
element.put(PublicationIndexSchema.ELEMENT_ID, catalog.getIdentifier());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, catalog.getMimeType());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, catalog.getElementType());
element.put(PublicationIndexSchema.ELEMENT_TAG, catalog.getTags());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, catalog.getURI());
element.put(PublicationIndexSchema.ELEMENT_SIZE, catalog.getSize());
catalogsArray[i] = element;
}
pMap.put(PublicationIndexSchema.CATALOG, catalogsArray);
// Tracks
Track[] tracks = publication.getTracks();
HashMap<String, Object>[] tracksArray = new HashMap[tracks.length];
for (int i = 0; i < tracksArray.length; i++) {
Track track = tracks[i];
HashMap<String, Object> element = new HashMap<String, Object>();
element.put(PublicationIndexSchema.ELEMENT_ID, track.getIdentifier());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_MIMETYPE, track.getMimeType());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_TYPE, track.getElementType());
element.put(PublicationIndexSchema.ELEMENT_TAG, track.getTags());
addObjectStringtToMap(element, PublicationIndexSchema.ELEMENT_URL, track.getURI());
element.put(PublicationIndexSchema.ELEMENT_SIZE, track.getSize());
element.put(PublicationIndexSchema.TRACK_DURATION, track.getDuration());
tracksArray[i] = element;
}
pMap.put(PublicationIndexSchema.TRACK, tracksArray);
return pMap;
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class ComposerServiceImpl method composite.
/**
* {@inheritDoc}
*/
@Override
public Job composite(Dimension compositeTrackSize, Option<LaidOutElement<Track>> upperTrack, LaidOutElement<Track> lowerTrack, Option<LaidOutElement<Attachment>> watermark, String profileId, String background) throws EncoderException, MediaPackageException {
List<String> arguments = new ArrayList<>(9);
arguments.add(PROFILE_ID_INDEX, profileId);
arguments.add(LOWER_TRACK_INDEX, MediaPackageElementParser.getAsXml(lowerTrack.getElement()));
arguments.add(LOWER_TRACK_LAYOUT_INDEX, Serializer.json(lowerTrack.getLayout()).toJson());
if (upperTrack.isNone()) {
arguments.add(UPPER_TRACK_INDEX, NOT_AVAILABLE);
arguments.add(UPPER_TRACK_LAYOUT_INDEX, NOT_AVAILABLE);
} else {
arguments.add(UPPER_TRACK_INDEX, MediaPackageElementParser.getAsXml(upperTrack.get().getElement()));
arguments.add(UPPER_TRACK_LAYOUT_INDEX, Serializer.json(upperTrack.get().getLayout()).toJson());
}
arguments.add(COMPOSITE_TRACK_SIZE_INDEX, Serializer.json(compositeTrackSize).toJson());
arguments.add(BACKGROUND_COLOR_INDEX, background);
if (watermark.isSome()) {
LaidOutElement<Attachment> watermarkLaidOutElement = watermark.get();
arguments.add(WATERMARK_INDEX, MediaPackageElementParser.getAsXml(watermarkLaidOutElement.getElement()));
arguments.add(WATERMARK_LAYOUT_INDEX, Serializer.json(watermarkLaidOutElement.getLayout()).toJson());
}
try {
final EncodingProfile profile = profileScanner.getProfile(profileId);
return serviceRegistry.createJob(JOB_TYPE, Operation.Composite.toString(), arguments, profile.getJobLoad());
} catch (ServiceRegistryException e) {
throw new EncoderException("Unable to create composite job", e);
}
}
Aggregations