org.yestech.publish.publisher.EpisodicPublisher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yespublish Show documentation
Show all versions of yespublish Show documentation
Framework to publish ascii or binary data
to different sources. Currently Episodic, Bitgravity, Local, Cloudfs,
and S3 are supported
package org.yestech.publish.publisher;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.lang.StringUtils;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.io.FileUtils.openOutputStream;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import static org.yestech.lib.util.Pair.create;
import org.yestech.lib.util.Pair;
import org.yestech.publish.objectmodel.*;
import org.yestech.publish.objectmodel.episodic.IEpisodicArtifactPersister;
import org.yestech.publish.objectmodel.episodic.IEpisodicArtifact;
import static org.yestech.publish.util.PublishUtils.generateUniqueIdentifier;
import org.yestech.publish.util.PublishUtils;
import org.yestech.publish.publisher.webdav.MkColMethod;
import org.yestech.episodic.EpisodicService;
import org.yestech.episodic.DefaultEpisodicService;
import org.yestech.episodic.objectmodel.Episodes;
import org.yestech.episodic.objectmodel.Episode;
import org.yestech.episodic.objectmodel.Player;
import java.io.*;
/**
* Publishes videos to episodic by creating a unique episode with 1 asset under a given show.
*
*
*
* - secret - The episodic secret key.
* - api_key - The episodic api key
* show_id - The episodic show to publish artifacts as episodes too.
*
*
* Optionally It is possible to save the episodeId and assetId back to the artifact if desired.
* The following must be done in order for this functionality to work:
*
* - Make your artfact object implement {@link org.yestech.publish.objectmodel.episodic.IEpisodicArtifact}
*
- Provide an implementation of {@link org.yestech.publish.objectmodel.episodic.IEpisodicArtifactPersister} to the publisher
* via the {@link #setPersister(org.yestech.publish.objectmodel.episodic.IEpisodicArtifactPersister)} method.
*
*
* @author A.J. Wright
*/
@ProducerArtifactType(type = {ArtifactType.VIDEO})
public class EpisodicPublisher extends BasePublisher implements IPublisher {
private static final Logger logger = LoggerFactory.getLogger(EpisodicPublisher.class);
private PublisherProperties properties;
private ArtifactType artifactType;
private IEpisodicArtifactPersister persister;
public EpisodicPublisher() {
properties = new PublisherProperties();
}
@Override
public void publish(IFileArtifact artifact) {
IFileArtifactMetaData metaData = artifact.getArtifactMetaData();
InputStream artifactStream = artifact.getStream();
String artifactDirectoryName = (String) metaData.getUniqueNames().getFirst();
if (isBlank(artifactDirectoryName)) {
artifactDirectoryName = generateUniqueIdentifier(metaData.getArtifactOwner());
}
String uniqueFileName = (String) metaData.getUniqueNames().getSecond();
if (isBlank(uniqueFileName)) {
uniqueFileName = generateUniqueIdentifier(metaData);
}
final File tempFile = saveToDisk(artifactDirectoryName, artifactStream, uniqueFileName);
try {
EpisodicService episodicService = buildEpisodicService();
String assetId = episodicService.createAsset(getShowId(), uniqueFileName, tempFile);
if (assetId != null || !"".equals(assetId)) {
String episodeId = episodicService.createEpisode(getShowId(), uniqueFileName, new String[]{assetId}, true, null, getPingUrl());
if (artifact instanceof IEpisodicArtifact) {
IEpisodicArtifact ea = (IEpisodicArtifact) artifact;
ea.setAssetId(assetId);
ea.setEpisodeId(episodeId);
if (persister != null) {
// load up the loacation (the embed code from episodic)
Episodes episodes = episodicService.getEpisodes(null, new String[]{episodeId}, null, null, null, null, null, null, null, null, null, null, null);
if (!episodes.getEpisode().isEmpty()) {
Episode episode = episodes.getEpisode().get(0);
if (episode != null) {
Player defaultPlayer = episode.getDefaultPlayer();
if (defaultPlayer != null) {
metaData.setLocation(defaultPlayer.getEmbedCode());
}
}
}
persister.save(ea);
} else {
logger.warn("Artifact is an IEpisodicArtfact, but no IEpisodicArtifactPersister was supplied.");
}
}
}
} catch (RuntimeException e) {
logger.error(e.getMessage(), e);
throw e;
} finally {
if (logger.isInfoEnabled()) {
logger.info("removing file: " + tempFile);
}
if (tempFile.exists()) {
//noinspection ResultOfMethodCallIgnored
tempFile.delete();
}
PublishUtils.reset(artifact);
}
}
@Required
public void setArtifactType(ArtifactType artifactType) {
this.artifactType = artifactType;
}
@Required
public void setProperties(PublisherProperties properties) {
this.properties = properties;
}
public void setPersister(IEpisodicArtifactPersister persister) {
this.persister = persister;
}
public String getSecret() {
return properties.getProperty(create(getArtifactType(), "secret"));
}
public String getApiKey() {
return properties.getProperty(create(getArtifactType(), "api_key"));
}
public String getShowId() {
return properties.getProperty(create(getArtifactType(), "show_id"));
}
public String getPingUrl() {
return properties.getProperty(create(getArtifactType(), "ping_url"));
}
public ArtifactType getArtifactType() {
return artifactType;
}
public File getTempDirectory() {
return properties.getProperty(Pair.create(getArtifactType(), "tempDirectory"));
}
private File saveToDisk(String artifactDirectoryName, InputStream artifact, String uniqueFileName) {
File fullPath = new File(getTempDirectory() + File.separator + artifactDirectoryName);
if (!fullPath.exists()) {
//noinspection ResultOfMethodCallIgnored
fullPath.mkdirs();
}
File location = new File(fullPath.getAbsolutePath(), uniqueFileName);
FileOutputStream outputStream = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Saving file: " + location);
}
outputStream = openOutputStream(location);
IOUtils.copyLarge(artifact, outputStream);
outputStream.flush();
if (logger.isDebugEnabled()) {
logger.debug("Saved file: " + location);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
finally {
IOUtils.closeQuietly(artifact);
IOUtils.closeQuietly(outputStream);
}
return location;
}
protected EpisodicService buildEpisodicService() {
return new DefaultEpisodicService(getSecret(), getApiKey());
}
}