Java tutorial
/** * 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.test.upload; import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.codehaus.jackson.JsonNode; import org.json.JSONException; import com.brightcove.com.uploader.helper.EnvironmentRegistry; import com.brightcove.com.uploader.helper.FileFinder; import com.brightcove.com.uploader.verifier.ExistenceCheck; import com.brightcove.com.uploader.verifier.RetrieveJSONNodeHelper; import com.brightcove.common.logging.BrightcoveLog; import com.brightcove.test.upload.helpers.IntegrationTestHelper; 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.input.MediaAPI; import com.brightcove.uploader.upload.IngestFile; public class MediaAPIParentClass { //public Account mAccountInfo; //public Environment mEnvironment; MediaAPI mMediaApi = new MediaAPI(); private BrightcoveLog mLog = BrightcoveLog.getLogger(this.getClass()); private long mSleepTime = 120000l; public Account setAccountInfo(String userName, String password, String publisherId, String defaultReadToken, String defaultWriteToken) { Account mAccountInfo = new Account(userName, password, Long.parseLong(publisherId)); mAccountInfo.setDefaultReadToken(defaultReadToken); mAccountInfo.setDefaultWriteToken(defaultWriteToken); Map<String, String> settings = new HashMap<String, String>(); settings.put("progressivedownload", "false"); mAccountInfo.setSettings(settings); return mAccountInfo; } public Environment setEnvironmentsInfo(String envPropertyName, String serverName) throws URISyntaxException, IOException { IntegrationTestHelper.loadEnvironments(); String envName = System.getProperty(envPropertyName, serverName); Environment mEnvironment = EnvironmentRegistry.getEnvironment(envName); ExistenceCheck mExistenceCheck = new ExistenceCheck(); mExistenceCheck.setmEnvironment(mEnvironment); return mEnvironment; } public String getAbsoluteVideoFilePath(String sourceFileName) throws URISyntaxException { return FileFinder.findFileInClasspath(sourceFileName).getAbsolutePath(); } /** * Upload the video via Media API * @param videoFile * @param accountInfo * @param envInfo * @return * @throws MediaAPIError * @throws URISyntaxException * @throws JSONException * @throws BadEnvironmentException * @throws MalformedURLException */ public Long uploadVideo(IngestFile videoFile, Account accountInfo, Environment envInfo) throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException, MalformedURLException { //upload a file Long videoId = mMediaApi.createVideo(videoFile, accountInfo, envInfo); return videoId; } /** * Create query parameter * @param videoId * @param mediaDeliveryType * @return */ public List createListParam(Account accInfo, String propertyName, String id, String command, String mediaDeliveryType, String fields) { List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("command", command)); parameters.add(new BasicNameValuePair(propertyName, id)); parameters.add(new BasicNameValuePair("token", accInfo.getDefaultReadToken())); if (!mediaDeliveryType.equals("")) { parameters.add(new BasicNameValuePair("media_delivery", mediaDeliveryType)); } if (!fields.equals("")) { parameters.add(new BasicNameValuePair("fields", fields)); } return parameters; } /** * Create query parameter for find by videoId * @param accInfo * @param id * @param mediaDeliveryType * @return */ public List createListParamForFindByRefId(Account accInfo, String id, String mediaDeliveryType) { return createListParam(accInfo, "reference_id", id, "find_video_by_reference_id", mediaDeliveryType, ""); } public List createListParamForFindByRefId(Account accInfo, String id, String mediaDeliveryType, String fields) { return createListParam(accInfo, "reference_id", id, "find_video_by_reference_id", mediaDeliveryType, fields); } /** * Create query parameter for find video by id * @param videoId * @param mediaDeliveryType * @return */ public List createListParamForFindVideoByIdCommand(Account accInfo, Long videoId, String mediaDeliveryType) { return createListParam(accInfo, "video_id", videoId.toString(), "find_video_by_id", mediaDeliveryType, ""); } public List createListParamForFindVideoByIdCommand(Account accInfo, Long videoId, String mediaDeliveryType, String fields) { return createListParam(accInfo, "video_id", videoId.toString(), "find_video_by_id", mediaDeliveryType, fields); } /** * Retrieve particular property value from API Json response * @param videoId * @param mediaDeliveryType : For default enter "" or default as the value * @param propertyName * @return * @throws MediaAPIError */ public String retrivePropertyValueFromJSONResponse(Account accInfo, Environment envInfo, long videoId, String mediaDeliveryType, String propertyName) throws MediaAPIError { RetrieveJSONNodeHelper jsonResponse = new RetrieveJSONNodeHelper( createListParamForFindVideoByIdCommand(accInfo, videoId, mediaDeliveryType), envInfo); return jsonResponse.retrievePropertyFromJSONResponse(propertyName); } /** * Retrieve particular property value from API Json response * @param accInfo * @param envInfo * @param refId * @param mediaDeliveryType * @param propertyName * @return * @throws MediaAPIError */ public String retrivePropertyValueFromJSONResponse(Account accInfo, Environment envInfo, String refId, String mediaDeliveryType, String propertyName) throws MediaAPIError { RetrieveJSONNodeHelper jsonResponse = new RetrieveJSONNodeHelper( createListParamForFindByRefId(accInfo, refId, mediaDeliveryType), envInfo); return jsonResponse.retrievePropertyFromJSONResponse(propertyName); } /** * Verify if the video already exist * @param videoId * @return * @throws MediaAPIError */ public Boolean videoExistByVideoId(Account accInfo, Environment envInfo, long videoId) throws MediaAPIError { return verifyNull(createListParamForFindVideoByIdCommand(accInfo, videoId, ""), envInfo); } /** * Verify if the video already exist * @param videoId * @return * @throws MediaAPIError */ public Boolean videoExistByRefId(Account accInfo, Environment envInfo, String refId) throws MediaAPIError { return verifyNull(createListParamForFindByRefId(accInfo, refId, ""), envInfo); } private Boolean verifyNull(List parameters, Environment envInfo) throws MediaAPIError { RetrieveJSONNodeHelper jsonResponse = new RetrieveJSONNodeHelper(parameters, envInfo); try { if (!jsonResponse.retrievePropertyFromJSONResponse("id").equals(null)) { return true; } } catch (Exception e) { return false; } return false; } }