diff --git a/current/core/src/main/java/com/coresecure/brightcove/wrapper/sling/ServiceUtil.java b/current/core/src/main/java/com/coresecure/brightcove/wrapper/sling/ServiceUtil.java index a3a7284..c9f8fa9 100644 --- a/current/core/src/main/java/com/coresecure/brightcove/wrapper/sling/ServiceUtil.java +++ b/current/core/src/main/java/com/coresecure/brightcove/wrapper/sling/ServiceUtil.java @@ -875,7 +875,9 @@ private void setImages(JSONObject objObject, Asset newAsset) { //Map rendition_map = new HashMap(); newAsset.addRendition(Constants.BRC_THUMBNAIL_PNG, ris, StandardImageHandler.PNG1_MIMETYPE); } else { - newAsset.removeRendition(Constants.BRC_THUMBNAIL_PNG); + if (newAsset.getRendition(Constants.BRC_THUMBNAIL_PNG) != null) { + newAsset.removeRendition(Constants.BRC_THUMBNAIL_PNG); + } } } catch (Exception e) diff --git a/current/core/src/main/java/com/coresecure/brightcove/wrapper/utils/S3UploadUtil.java b/current/core/src/main/java/com/coresecure/brightcove/wrapper/utils/S3UploadUtil.java index c956042..50e466a 100644 --- a/current/core/src/main/java/com/coresecure/brightcove/wrapper/utils/S3UploadUtil.java +++ b/current/core/src/main/java/com/coresecure/brightcove/wrapper/utils/S3UploadUtil.java @@ -106,9 +106,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream) { if (responseCode == 200) { LOGGER.info("Successfully uploaded."); + } else { + LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode); } } catch (IOException e) { LOGGER.error("IOException",e); + } catch (Exception e) { + LOGGER.error("***************************S3UploadUtil: Error uploading asset"); } return responseCode == 200; @@ -172,9 +176,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream, String proxy if (responseCode == 200) { LOGGER.info("Successfully uploaded."); + } else { + LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode); } } catch (IOException e) { LOGGER.error("IOException",e); + } catch (Exception e) { + LOGGER.error("***************************S3UploadUtil: Error uploading asset"); } return responseCode == 200; @@ -224,9 +232,13 @@ public static boolean uploadToUrl(URL url, InputStream inputStream, Proxy proxy) if (responseCode == 200) { LOGGER.info("Successfully uploaded."); + } else { + LOGGER.error("***************************S3UploadUtil: Error uploading asset: " + responseCode); } } catch (IOException e) { LOGGER.error("IOException",e); + } catch (Exception e) { + LOGGER.error("***************************S3UploadUtil: Error uploading asset"); } return responseCode == 200; diff --git a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java index 0baac36..6fe7a60 100644 --- a/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java +++ b/current/core/src/main/java/com/coresecure/brightcove/wrapper/workflow/BrightcoveSyncAssetWorkflowStep.java @@ -34,11 +34,18 @@ import java.util.concurrent.TimeUnit; import javax.jcr.AccessDeniedException; +import javax.jcr.InvalidItemStateException; +import javax.jcr.ItemExistsException; import javax.jcr.ItemNotFoundException; import javax.jcr.Node; import javax.jcr.PathNotFoundException; +import javax.jcr.ReferentialIntegrityException; import javax.jcr.RepositoryException; import javax.jcr.ValueFormatException; +import javax.jcr.lock.LockException; +import javax.jcr.nodetype.ConstraintViolationException; +import javax.jcr.nodetype.NoSuchNodeTypeException; +import javax.jcr.version.VersionException; import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.PersistenceException; @@ -60,7 +67,7 @@ public class BrightcoveSyncAssetWorkflowStep implements WorkflowProcess{ private static final Logger LOG = LoggerFactory.getLogger(BrightcoveSyncAssetWorkflowStep.class); private static final String SERVICE_ACCOUNT_IDENTIFIER = "brightcoveWrite"; - private String brightcoveAssetId; + private Map paths = null; @Override @@ -70,6 +77,7 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData LOG.info("********************* payloadpath:" + payloadPath); ResourceResolver rr = null; + String brightcoveAssetId = null; try { @@ -119,10 +127,10 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData Asset _asset = assetResource.adaptTo(Asset.class); ServiceUtil serviceUtil = new ServiceUtil(brightcoveAccountId); // upload or modify the asset - activateAsset(rr, _asset, serviceUtil); + brightcoveAssetId = activateAsset(rr, _asset, serviceUtil); TimeUnit.SECONDS.sleep(15); if (brightcoveAssetId != null && !brightcoveAssetId.isEmpty()) { - syncBrightcoveData(serviceUtil, _asset, rr, brightcoveAccountId); + syncBrightcoveData(brightcoveAssetId, serviceUtil, _asset, rr, brightcoveAccountId); } rr.commit(); @@ -135,12 +143,12 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData } catch (LoginException e) { // there is some issue with the system user used - LOG.error("There was an error using the Brightcove system user."); + LOG.error("*************************** There was an error using the Brightcove system user."); } catch (Exception e) { // a general error - LOG.error("Error when handling the Brightcove video sync: {}", e.getMessage()); + LOG.error("*************************** Error when handling the Brightcove video sync: {}", e.getMessage()); } finally { if (rr != null && rr.isLive()) { @@ -149,7 +157,7 @@ public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaData } } - private void syncBrightcoveData(ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) { + private void syncBrightcoveData(String brightcoveAssetId, ServiceUtil serviceUtil, Asset _asset, ResourceResolver rr, String brightcoveAccountId) { try { JSONObject result = serviceUtil.getSelectedVideo(brightcoveAssetId); @@ -161,10 +169,12 @@ private void syncBrightcoveData(ServiceUtil serviceUtil, Asset _asset, ResourceR } } - private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) { + private String activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) { LOG.trace("brc_lastsync was null or zero : asset should be initialized"); LOG.info("activate new asset"); + String brightcoveAssetId = null; + try { // get the binary @@ -195,7 +205,8 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod // log the error LOG.error(Constants.REP_ACTIVATION_SUCCESS_TMPL, _asset.getName()); - + LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName()); + } } catch (Exception e) { @@ -203,13 +214,16 @@ private void activateNew(Asset _asset, ServiceUtil serviceUtil, Video video, Mod LOG.error("Error: {}", e.getMessage()); } + + return brightcoveAssetId; } - private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, + private String activateModified(Asset _asset, ServiceUtil serviceUtil, Video video, ModifiableValueMap brc_lastsync_map) { LOG.info("Entering activateModified()"); + String brightcoveAssetId = null; try { @@ -220,7 +234,7 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video boolean sent = api_resp.getBoolean(Constants.SENT); if (sent) { - + brightcoveAssetId = api_resp.getString(Constants.VIDEOID); LOG.info("Brightcove video updated successfully: {}", _asset.getPath()); serviceUtil.updateRenditions(_asset, video); LOG.info("Updated renditions for Brightcove video: {}", _asset.getPath()); @@ -235,15 +249,18 @@ private void activateModified(Asset _asset, ServiceUtil serviceUtil, Video video } else { // log the error - LOG.error("Error sending data to Brightcove: {}", _asset.getName()); - + LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName()); + } } catch (Exception e) { // log the error LOG.error("General Error: {}", _asset.getName()); + LOG.error("*************************** Error sending data to Brightcove: {}", _asset.getName()); } + + return brightcoveAssetId; } @@ -251,20 +268,35 @@ private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node asset try { LOG.trace("CHECKING PARENT FOR BRC_FOLDER_ID: " + assetNode.getParent().getPath()); Node parentNode = assetNode.getParent(); + String videoId = api_resp.getString(Constants.VIDEOID); + if (!parentNode.hasProperty("brc_folder_id")) { String folderId = serviceUtil.createFolder(assetNode.getParent().getName()); if (folderId != null && !folderId.isEmpty()) { - - parentNode.setProperty("brc_folder_id", folderId); - parentNode.getSession().save(); - LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'"); - serviceUtil.moveVideoToFolder(folderId, api_resp.getString(Constants.VIDEOID)); + setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId); + } else { + LOG.error("*************************** No folder created ***************************"); + TimeUnit.SECONDS.sleep(15); + parentNode.refresh(false); + if (!parentNode.hasProperty("brc_folder_id")) { + folderId = serviceUtil.createFolder(assetNode.getParent().getName()); + if (folderId != null && !folderId.isEmpty()) { + setFolderIdMoveAssetInBC(serviceUtil, parentNode, videoId, folderId); + } else { + LOG.error("*************************** No folder created attempt 2 ***************************"); + } + } else { + // this is in a subfolder so we need to formally move the asset to this folder + String brc_folder_id = parentNode.getProperty("brc_folder_id").getString(); + LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'"); + serviceUtil.moveVideoToFolder(brc_folder_id, videoId); + } } } else { // this is in a subfolder so we need to formally move the asset to this folder - String brc_folder_id = assetNode.getParent().getProperty("brc_folder_id").getString(); + String brc_folder_id = parentNode.getProperty("brc_folder_id").getString(); LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + brc_folder_id + "'"); - serviceUtil.moveVideoToFolder(brc_folder_id, api_resp.getString(Constants.VIDEOID)); + serviceUtil.moveVideoToFolder(brc_folder_id, videoId); } @@ -276,27 +308,35 @@ private void syncFolder(ServiceUtil serviceUtil, JSONObject api_resp, Node asset } } - private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) { + private void setFolderIdMoveAssetInBC(ServiceUtil serviceUtil, Node parentNode, String videoId, String folderId) throws Exception { + parentNode.setProperty("brc_folder_id", folderId); + parentNode.getSession().save(); + + LOG.trace("SUBFOLDER FOUND - SETTING THE FOLDER ID to '" + folderId + "'"); + serviceUtil.moveVideoToFolder(folderId, videoId); + } + + private String activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil serviceUtil) { // need to either activate a new asset or an updated existing // ServiceUtil serviceUtil = new ServiceUtil(accountId); String path = _asset.getPath(); - + String brightcoveAssetId = null; Video video = serviceUtil.createVideo(path, _asset, "ACTIVE"); Resource assetRes = _asset.adaptTo(Resource.class); if (assetRes == null) { - return; + return ""; } Resource metadataRes = assetRes.getChild(Constants.ASSET_METADATA_PATH); if (metadataRes == null) { - return; + return ""; } ModifiableValueMap brc_lastsync_map = metadataRes.adaptTo(ModifiableValueMap.class); if (brc_lastsync_map == null) { - return; + return ""; } Long jcr_lastmod = _asset.getLastModified(); @@ -308,13 +348,15 @@ private void activateAsset(ResourceResolver rr, Asset _asset, ServiceUtil servic // we need to activate a new asset here LOG.info("Activating New Brightcove Asset: {}", _asset.getPath()); - activateNew(_asset, serviceUtil, video, brc_lastsync_map); + brightcoveAssetId = activateNew(_asset, serviceUtil, video, brc_lastsync_map); } else { // we need to modify an existing asset here LOG.info("Activating Modified Brightcove Asset: {}", _asset.getPath()); - activateModified(_asset, serviceUtil, video, brc_lastsync_map); + brightcoveAssetId = activateModified(_asset, serviceUtil, video, brc_lastsync_map); } + + return brightcoveAssetId; } } \ No newline at end of file