use of org.opencastproject.publication.api.PublicationException in project opencast by opencast.
the class YouTubePublicationServiceRemoteImpl method retract.
@Override
public Job retract(MediaPackage mediaPackage) throws PublicationException {
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("mediapackage", MediaPackageParser.getAsXml(mediaPackage)));
HttpPost post = new HttpPost("/retract");
HttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(params));
response = getResponse(post);
if (response != null) {
logger.info("Retracting {} from youtube", mediaPackage);
return JobParser.parseJob(response.getEntity().getContent());
}
} catch (Exception e) {
throw new PublicationException("Unable to retract mediapackage " + mediaPackage + " using a remote youtube publication service", e);
} finally {
closeConnection(response);
}
throw new PublicationException("Unable to retract mediapackage " + mediaPackage + " using a remote youtube publication service");
}
use of org.opencastproject.publication.api.PublicationException in project opencast by opencast.
the class YouTubePublicationServiceRemoteImpl method publish.
@Override
public Job publish(MediaPackage mediaPackage, Track track) throws PublicationException {
final String trackId = track.getIdentifier();
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("mediapackage", MediaPackageParser.getAsXml(mediaPackage)));
params.add(new BasicNameValuePair("elementId", trackId));
HttpPost post = new HttpPost();
HttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(params));
response = getResponse(post);
if (response != null) {
logger.info("Publishing {} to youtube", trackId);
return JobParser.parseJob(response.getEntity().getContent());
}
} catch (Exception e) {
throw new PublicationException("Unable to publish track " + trackId + " from mediapackage " + mediaPackage + " using a remote youtube publication service", e);
} finally {
closeConnection(response);
}
throw new PublicationException("Unable to publish track " + trackId + " from mediapackage " + mediaPackage + " using a remote youtube publication service");
}
use of org.opencastproject.publication.api.PublicationException in project opencast by opencast.
the class YouTubeV3PublicationServiceImpl method publish.
/**
* Publishes the element to the publication channel and returns a reference to the published version of the element.
*
* @param job
* the associated job
* @param mediaPackage
* the mediapackage
* @param elementId
* the mediapackage element id to publish
* @return the published element
* @throws PublicationException
* if publication fails
*/
private Publication publish(final Job job, final MediaPackage mediaPackage, final String elementId) throws PublicationException {
if (mediaPackage == null) {
throw new IllegalArgumentException("Mediapackage must be specified");
} else if (elementId == null) {
throw new IllegalArgumentException("Mediapackage ID must be specified");
}
final MediaPackageElement element = mediaPackage.getElementById(elementId);
if (element == null) {
throw new IllegalArgumentException("Mediapackage element must be specified");
}
if (element.getIdentifier() == null) {
throw new IllegalArgumentException("Mediapackage element must have an identifier");
}
if (element.getMimeType().toString().matches("text/xml")) {
throw new IllegalArgumentException("Mediapackage element cannot be XML");
}
try {
// create context strategy for publication
final YouTubePublicationAdapter c = new YouTubePublicationAdapter(mediaPackage, workspace);
final File file = workspace.get(element.getURI());
final String episodeName = c.getEpisodeName();
final UploadProgressListener operationProgressListener = new UploadProgressListener(mediaPackage, file);
final String privacyStatus = makeVideosPrivate ? "private" : "public";
final VideoUpload videoUpload = new VideoUpload(truncateTitleToMaxFieldLength(episodeName, false), c.getEpisodeDescription(), privacyStatus, file, operationProgressListener, tags);
final Video video = youTubeService.addVideoToMyChannel(videoUpload);
final int timeoutMinutes = 60;
final long startUploadMilliseconds = new Date().getTime();
while (!operationProgressListener.isComplete()) {
Thread.sleep(POLL_MILLISECONDS);
final long howLongWaitingMinutes = (new Date().getTime() - startUploadMilliseconds) / 60000;
if (howLongWaitingMinutes > timeoutMinutes) {
throw new PublicationException("Upload to YouTube exceeded " + timeoutMinutes + " minutes for episode " + episodeName);
}
}
String playlistName = StringUtils.trimToNull(truncateTitleToMaxFieldLength(mediaPackage.getSeriesTitle(), true));
playlistName = (playlistName == null) ? this.defaultPlaylist : playlistName;
final Playlist playlist;
final Playlist existingPlaylist = youTubeService.getMyPlaylistByTitle(playlistName);
if (existingPlaylist == null) {
playlist = youTubeService.createPlaylist(playlistName, c.getContextDescription(), mediaPackage.getSeries());
} else {
playlist = existingPlaylist;
}
youTubeService.addPlaylistItem(playlist.getId(), video.getId());
// Create new publication element
final URL url = new URL("http://www.youtube.com/watch?v=" + video.getId());
return PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_NAME, url.toURI(), MimeTypes.parseMimeType(MIME_TYPE));
} catch (Exception e) {
logger.error("failed publishing to Youtube", e);
logger.warn("Error publishing {}, {}", element, e.getMessage());
if (e instanceof PublicationException) {
throw (PublicationException) e;
} else {
throw new PublicationException("YouTube publish failed on job: " + ToStringBuilder.reflectionToString(job, ToStringStyle.MULTI_LINE_STYLE), e);
}
}
}
use of org.opencastproject.publication.api.PublicationException 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.publication.api.PublicationException in project opencast by opencast.
the class PublishYouTubeWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Running youtube publication workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
// Check which tags have been configured
String sourceTags = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-tags"));
String sourceFlavors = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-flavors"));
AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector;
if (sourceTags == null && sourceFlavors == null) {
logger.warn("No tags or flavor have been specified");
return createResult(mediaPackage, Action.CONTINUE);
}
elementSelector = new SimpleElementSelector();
if (sourceFlavors != null) {
for (String flavor : asList(sourceFlavors)) {
elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
}
if (sourceTags != null) {
for (String tag : asList(sourceTags)) {
elementSelector.addTag(tag);
}
}
try {
// Look for elements matching the tag
final Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
if (elements.size() > 1) {
logger.warn("More than one element has been found for publishing to youtube: {}", elements);
return createResult(mediaPackage, Action.SKIP);
}
if (elements.size() < 1) {
logger.info("No mediapackage element was found for publishing");
return createResult(mediaPackage, Action.CONTINUE);
}
Job youtubeJob;
try {
Track track = mediaPackage.getTrack(elements.iterator().next().getIdentifier());
youtubeJob = publicationService.publish(mediaPackage, track);
} catch (PublicationException e) {
throw new WorkflowOperationException(e);
}
// Wait until the youtube publication job has returned
if (!waitForStatus(youtubeJob).isSuccess())
throw new WorkflowOperationException("The youtube publication jobs did not complete successfully");
// All the jobs have passed
Job job = serviceRegistry.getJob(youtubeJob.getId());
// If there is no payload, then the item has not been published.
if (job.getPayload() == null) {
logger.warn("Publish to youtube failed, no payload from publication job: {}", 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 youtube failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", job.getPayload(), job);
return createResult(mediaPackage, Action.CONTINUE);
}
mediaPackage.add(newElement);
logger.debug("Publication to youtube operation completed");
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
throw new WorkflowOperationException(e);
}
}
return createResult(mediaPackage, Action.CONTINUE);
}
Aggregations