com.brightcove.uploader.input.Batch.java Source code

Java tutorial

Introduction

Here is the source code for com.brightcove.uploader.input.Batch.java

Source

/**
 * Copyright (C) 2011 Brightcove Inc. All Rights Reserved. No use, copying or distribution of this
 * work may be made except in accordance with a valid license agreement from Brightcove Inc. This
 * notice must be included on all copies, modifications and derivatives of this work.
 * 
 * Brightcove Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * "Brightcove" is a registered trademark of Brightcove Inc.
 */
package com.brightcove.uploader.input;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import org.apache.commons.net.ftp.FTPClient;
import org.json.JSONException;

import com.brightcove.common.io.FTPHelper;
import com.brightcove.common.io.FTPHelperBuilder;
import com.brightcove.common.logging.BrightcoveLog;
import com.brightcove.uploader.config.Account;
import com.brightcove.uploader.config.Environment;
import com.brightcove.uploader.exception.BadEnvironmentException;
import com.brightcove.uploader.exception.MediaAPIError;
import com.brightcove.uploader.upload.IngestFile;
import com.brightcove.uploader.upload.TitleMetaData;

public class Batch {
    private BrightcoveLog mLog = BrightcoveLog.getLogger(this.getClass());

    /**
     * Creates videos via Batch
     * @param pVideoFiles[] files with options to be created
     * @param pAccount account to create the title in
     * @param pEnvironment environment to run api call against
     * @return Video Ref ID of created video.
     * @throws IOException 
     */
    public ArrayList<String> createVideos(IngestFile[] pVideoFiles, Account pAccount, Environment pEnvironment)
            throws BadEnvironmentException, MediaAPIError, JSONException, URISyntaxException, IOException {
        return createVideos(pVideoFiles, pAccount, pEnvironment, true);

    }

    public ArrayList<String> createVideos(IngestFile[] pVideoFiles, Account pAccount, Environment pEnvironment,
            boolean waitForPickup)
            throws BadEnvironmentException, MediaAPIError, JSONException, URISyntaxException, IOException {

        String manifest = constructManifest(pVideoFiles, pAccount, pEnvironment);
        String delManifest = constructCleanupManifest(pVideoFiles, pAccount, pEnvironment);

        String ftphost = pEnvironment.getFTPServer();
        String ftpRootDir = pEnvironment.getFTPRootDir();
        String ftpuser = pAccount.getFtpLogin();
        String ftppass = pAccount.getFtpPassword();

        FTPHelperBuilder ftpHB = new FTPHelperBuilder(ftphost, ftpuser, ftppass,
                FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        FTPHelper ftpH = ftpHB.createFTPHelper();
        boolean loggedin = ftpH.connect();
        mLog.info("connected: " + loggedin + " to " + ftphost);
        ArrayList<String> refids = new ArrayList<String>();
        for (IngestFile video : pVideoFiles) {
            ftpH.put(ftpRootDir, video.getUniqueID() + video.getFile().getName(), video.getFile());
            refids.add("title-" + video.getUniqueID());
        }
        String manifestName = System.currentTimeMillis() + "batchSystemTest.xml";
        ftpH.put(ftpRootDir, manifestName, new ByteArrayInputStream(manifest.getBytes()));
        mLog.info("Uploading " + manifestName + " manifest");
        String delManifestName = System.currentTimeMillis() + "batchSystemTestCleanup.xml.del";
        ftpH.put(ftpRootDir, delManifestName, new ByteArrayInputStream(delManifest.getBytes()));

        if (waitForPickup) {
        }
        ftpH.disconnect();

        return refids;
    }

    private String constructManifest(IngestFile[] pVideoFiles, Account pAccount, Environment pEnvironment) {
        String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

        manifest += getHeader(pAccount);
        for (IngestFile video : pVideoFiles) {
            manifest += getAssetXML(video);
            if (!video.isAssetOnly()) {
                manifest += getTitleXML(video);
            }
        }

        //add closing
        manifest += "</publisher-upload-manifest>";

        manifest = formatManifest(manifest);
        return manifest;
    }

    private String formatManifest(String manifest) {
        manifest = manifest.replace("&", "&amp;");
        manifest = manifest.replace(">", ">\n");
        return manifest;
    }

    private String constructCleanupManifest(IngestFile[] pVideoFiles, Account pAccount, Environment pEnvironment) {
        String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

        manifest += getHeader(pAccount);
        for (IngestFile video : pVideoFiles) {
            manifest += getDeleteFLVFULLAssetXML(video);
            if (!video.isAssetOnly()) {
                manifest += getDeleteTitleXML(video);
            }
        }

        //add closing
        manifest += "</publisher-upload-manifest>";
        return manifest;
    }

    public String getTitleXML(IngestFile pVideoFile) {
        String manifest = "  <title name=\"" + pVideoFile.getDisplayName() + "\" refid=\"" + pVideoFile.getRefId();
        if (pVideoFile.getMetaData() == null || pVideoFile.getMetaData().isActive()) {
            manifest += "\" active=\"TRUE\"";
        } else {
            manifest += "\" active=\"FALSE\"";
        }
        manifest += " video-full-refid=\"" + pVideoFile.getRefId() + "\">";
        manifest += getTitleMetaDataXML(pVideoFile);
        manifest += "  </title>";
        return manifest;
    }

    private String getTitleMetaDataXML(IngestFile pVideoFile) {
        String manifest = "";
        TitleMetaData metaData = pVideoFile.getMetaData();
        if (metaData != null) {
            if (metaData.getCustomStringFields() != null) {
                Iterator<String> keyIter = metaData.getCustomStringFields().keySet().iterator();
                while (keyIter.hasNext()) {
                    String key = keyIter.next();
                    manifest += "<custom-string-value name=\"" + key + "\">"
                            + metaData.getCustomStringFields().get(key) + "</custom-string-value>";
                }
            }
            if (metaData.getCustomEnumFields() != null) {
                Iterator<String> keyIter = metaData.getCustomEnumFields().keySet().iterator();
                while (keyIter.hasNext()) {
                    String key = keyIter.next();
                    manifest += "<custom-enum-value name=\"" + key + "\">" + metaData.getCustomEnumFields().get(key)
                            + "</custom-enum-value>";
                }
            }
            if (metaData.getTags() != null) {
                Iterator<String> tagIter = metaData.getTags().iterator();
                while (tagIter.hasNext()) {
                    manifest += "<tag>" + tagIter.next() + "</tag>";
                }
            }
            if (metaData.getShortDescription() != null) {
                manifest += "<short-description>" + metaData.getShortDescription() + "</short-description>";
            }
            if (metaData.getLongDescription() != null) {
                manifest += "<long-description>" + metaData.getLongDescription() + "</long-description>";
            }
            if (metaData.getLinkURL() != null) {
                manifest += "<related-link-url>" + metaData.getLinkURL() + "</related-link-url>";
            }
            if (metaData.getLinkText() != null) {
                manifest += "<related-link-text>" + metaData.getLinkText() + "</related-link-text>";
            }

        }
        return manifest;
    }

    public String getDeleteTitleXML(IngestFile pVideoFile) {
        String manifest = "<delete-title refid==\"title-" + pVideoFile.getUniqueID() + "\" />";
        return manifest;
    }

    private String getDeleteFLVFULLAssetXML(IngestFile pVideoFile) {
        String refid = pVideoFile.getUniqueID();
        String manifest = "  <delete-asset refid=\"" + refid + "\" />";
        return manifest;

    }

    public String getHeader(Account pAccount) {
        String manifest = "<publisher-upload-manifest publisher-id=\"" + pAccount.getId() + "\" preparer=\""
                + pAccount.getLogin() + "\" report-success=\"TRUE\" >";
        manifest += "  <notify email=\"zartan-dev@brightcove.com\" />";
        manifest += "<callback entity-url=\"http://storpey-ws.vidmark.local:8282/rtcallback\"/>";
        return manifest;
    }

    /**
     * Creates video via Batch
     * @param pVideoFiles[] files with options to be created
     * @param pAccount account to create the title in
     * @param pEnvironment environment to run api call against
     * @return Video Ref ID of created video.
     * @throws IOException 
     */
    public String createVideo(IngestFile pVideoFiles, Account pAccount, Environment pEnvironment)
            throws BadEnvironmentException, MediaAPIError, JSONException, URISyntaxException, IOException {
        ArrayList<String> results = createVideos(new IngestFile[] { pVideoFiles }, pAccount, pEnvironment);
        return results.size() > 0 ? results.get(0) : null;
    }

    public String retranscodeVideo(IngestFile pVideoFiles, Account pAccount, Environment pEnvironment,
            String retranscodeType) throws IOException {
        return retranscodeVideo(pVideoFiles, pAccount, pEnvironment, retranscodeType, null);
    }

    public String retranscodeVideo(IngestFile pVideoFiles, Account pAccount, Environment pEnvironment,
            String retranscodeType, IngestFile newAssetFile) throws IOException {
        String manifest = constructRetranscodeManifest(pVideoFiles, pAccount, pEnvironment, retranscodeType,
                newAssetFile);
        String ftphost = pEnvironment.getFTPServer();
        String ftpRootDir = pEnvironment.getFTPRootDir();
        String ftpuser = pAccount.getFtpLogin();
        String ftppass = pAccount.getFtpPassword();

        FTPHelperBuilder ftpHB = new FTPHelperBuilder(ftphost, ftpuser, ftppass,
                FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
        FTPHelper ftpH = ftpHB.createFTPHelper();
        boolean loggedin = ftpH.connect();
        System.out.print(loggedin);
        if (!(newAssetFile == null)) {
            if (!newAssetFile.getFilename().isEmpty()) {
                ftpH.put(ftpRootDir, newAssetFile.getFile().getName(), newAssetFile.getFile());
            }
        }
        String manifestName = System.currentTimeMillis() + "-" + pVideoFiles.getRefId()
                + "-RetranscodebatchSystemTest.xml";
        ftpH.put(ftpRootDir, manifestName, new ByteArrayInputStream(manifest.getBytes()));
        return pVideoFiles.getRefId();

    }

    /**
     * Returns Asset XML
     * @param pVideoFile
     * @return
     */
    private String getAssetXML(IngestFile pVideoFile) {
        String manifest = "  <asset filename=\"" + pVideoFile.getUniqueID() + pVideoFile.getFile().getName()
                + "\" refid=\"" + pVideoFile.getRefId() + "\" ";
        if (pVideoFile.getDisplayName() != null) {
            manifest += " display-name=\"" + pVideoFile.getDisplayName() + "\" ";
        }
        manifest += " size=\"" + pVideoFile.getFile().length() + "\" type=\""
                + pVideoFile.getOptions().getVideoType() + "\"";
        manifest += " encode-to=\"" + pVideoFile.getOptions().getTargetCodec() + "\" ";
        manifest += " encode-multiple=\"" + pVideoFile.getOptions().isMBR() + "\"";
        if (pVideoFile.getOptions().isPreserveSource()) {
            manifest += " h264-preserve-as-rendition=\"" + pVideoFile.getOptions().isPreserveSource() + "\"";
        }
        manifest += "/>";
        return manifest;
    }

    /**
     * Create xml manifest for the retranscode
     * @param pVideoFile
     * @param pAccount
     * @param pEnvironment
     * @param retranscodeType
     * @param newAssetFile
     * @return
     */
    private String constructRetranscodeManifest(IngestFile pVideoFile, Account pAccount, Environment pEnvironment,
            String retranscodeType, IngestFile newAssetFile) {
        String manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        manifest += getHeader(pAccount);
        if (retranscodeType.equals("reencode-from-existing-source")) {
            manifest += getReencodeFromExistingSource(pVideoFile);
        } else if (retranscodeType.equals("reencode-from-new-source")) {
            manifest += getAssetXML(newAssetFile);
            manifest += getReencodeFromNewSource(pVideoFile, newAssetFile);
        } else if (retranscodeType.equals("reencode-from-no-assets")) {
            manifest += getReencodeFromNoAssetSource(pVideoFile, newAssetFile);
        }
        //add closing
        manifest += "</publisher-upload-manifest>";
        return manifest;
    }

    private String getReencodeFromExistingSource(IngestFile pVideoFile) {
        String manifest = "  <reencode-from-existing-source";
        manifest += " title-refid=\"" + pVideoFile.getRefId() + "\"";
        manifest += " encode-to=\"" + pVideoFile.getOptions().getTargetCodec() + "\" ";
        manifest += " encode-multiple=\"" + pVideoFile.getOptions().isMBR() + "\"";
        if (pVideoFile.getOptions().isPreserveSource()) {
            manifest += " preserve-source-as-rendition=\"" + pVideoFile.getOptions().isPreserveSource() + "\"";
        }
        manifest += " />";
        return manifest;
    }

    private String getReencodeFromNoAssetSource(IngestFile pVideoFile, IngestFile newAsset) {
        String manifest = "  <reencode-from-existing-source";
        manifest += " title-refid=\"" + pVideoFile.getRefId() + "\"";
        manifest += " new-source-refid=\"" + newAsset.getRefId() + "\"";
        manifest += " encode-to=\"" + pVideoFile.getOptions().getTargetCodec() + "\" ";
        manifest += " encode-multiple=\"" + pVideoFile.getOptions().isMBR() + "\"";
        if (pVideoFile.getOptions().isPreserveSource()) {
            manifest += " preserve-source-as-rendition=\"" + pVideoFile.getOptions().isPreserveSource() + "\"";
        }
        manifest += " />";
        return manifest;
    }

    private String getReencodeFromNewSource(IngestFile pVideoFile, IngestFile newAsset) {
        String manifest = "  <reencode-from-new-source";
        manifest += " title-refid=\"" + pVideoFile.getRefId() + "\"";
        manifest += " new-source-refid =\"" + newAsset.getRefId() + "\"";
        manifest += " />";
        return manifest;
    }
}