CleanupWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { cleanUpJobArgument(workflowInstance); MediaPackage mediaPackage = workflowInstance.getMediaPackage(); WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation(); String flavors = currentOperation.getConfiguration(PRESERVE_FLAVOR_PROPERTY); final List<MediaPackageElementFlavor> flavorsToPreserve = new ArrayList<MediaPackageElementFlavor>(); boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL)); String delayStr = currentOperation.getConfiguration(DELAY); int delay = 1; if (delayStr != null) { try { delay = Integer.parseInt(delayStr); } catch (NumberFormatException e) { logger.warn("Invalid value '{}' for delay in workflow operation configuration (should be integer)", delayStr); } } if (delay > 0) { try { logger.debug("Sleeping {}s before removing workflow files", delay); Thread.sleep(delay * 1000); } catch (InterruptedException e) { } } for (String flavor : asList(flavors)) flavorsToPreserve.add(MediaPackageElementFlavor.parseFlavor(flavor)); List<MediaPackageElement> elementsToRemove = new ArrayList<>(); for (MediaPackageElement element : mediaPackage.getElements()) { if (element.getURI() == null) continue; if (!isPreserved(element, flavorsToPreserve)) elementsToRemove.add(element); } List<String> externalBaseUrls = null; if (deleteExternal) { externalBaseUrls = getAllWorkingFileRepositoryUrls(); externalBaseUrls.remove(workspace.getBaseUri().toString()); } for (MediaPackageElement elementToRemove : elementsToRemove) { if (deleteExternal) { for (String repository : externalBaseUrls) { logger.debug("Removing {} from repository {}", elementToRemove.getURI(), repository); try { removeElementFromRepository(elementToRemove, repository); } catch (TrustedHttpClientException ex) { logger.debug("Removing media package element {} from repository {} failed: {}", elementToRemove.getURI(), repository, ex.getMessage()); } } } logger.debug("Removing {} from the workspace", elementToRemove.getURI()); try { mediaPackage.remove(elementToRemove); workspace.delete(elementToRemove.getURI()); } catch (NotFoundException ex) { logger.debug("Workspace doesn't contain element with Id '{}' from media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().toString(), ex.getMessage()); } catch (IOException ex) { logger.warn("Unable to remove element with Id '{}' from the media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().toString(), ex.getMessage()); } } return createResult(mediaPackage, Action.CONTINUE); }  void setWorkspace(Workspace workspace); void setTrustedHttpClient(TrustedHttpClient client); void cleanUpJobArgument(WorkflowInstance workflowInstance); @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context); static final String PRESERVE_FLAVOR_PROPERTY; static final String DELETE_EXTERNAL; static final String DELAY; }
@Test public void testCreanupWOHwithPreservedFlavorAndMediaPackagePathPrefix() throws WorkflowOperationException, MediaPackageException { Map<String, String> wfInstConfig = new Hashtable<>(); wfInstConfig.put(CleanupWorkflowOperationHandler.PRESERVE_FLAVOR_PROPERTY, "*/source,smil/trimmed,securitysource"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor '*/source'", elementSelector.select(mp, false).isEmpty()); elementSelector = new SimpleElementSelector(); elementSelector.addFlavor("smil/trimmed"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'smil/trimmed'", elementSelector.select(mp, false).isEmpty()); elementSelector = new SimpleElementSelector(); elementSelector.addFlavor("security/*"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'security/*'", elementSelector.select(mp, false).isEmpty()); Assert.assertEquals("At least one file wasn't deleted on remote repository", 3, deletedFilesURIs.size()); }
@Test public void testCreanupWOHwithPreservedFlavorAndCollectionPathPrefix() throws WorkflowOperationException, MediaPackageException { Map<String, String> wfInstConfig = new Hashtable<>(); wfInstConfig.put(CleanupWorkflowOperationHandler.PRESERVE_FLAVOR_PROPERTY, "*/source,smil/trimmed,securitysource"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor '*/source'", elementSelector.select(mp, false).isEmpty()); elementSelector = new SimpleElementSelector(); elementSelector.addFlavor("smil/trimmed"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'smil/trimmed'", elementSelector.select(mp, false).isEmpty()); elementSelector = new SimpleElementSelector(); elementSelector.addFlavor("security/*"); Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'security/*'", elementSelector.select(mp, false).isEmpty()); Assert.assertEquals("At least one file wasn't deleted on remote repository", 3, deletedFilesURIs.size()); }
@Test public void testCreanupWOHwithoutPreservedFlavor() throws WorkflowOperationException, MediaPackageException { Map<String, String> wfInstConfig = new Hashtable<>(); wfInstConfig.put(CleanupWorkflowOperationHandler.DELETE_EXTERNAL, "true"); MediaPackageBuilder mpBuilder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder(); MediaPackage mp = mpBuilder.createNew(); MediaPackageElement track1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Track, "presenter", "source", null); track1.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.COLLECTION_PATH_PREFIX, "asset", mp.getIdentifier().toString(), track1.getIdentifier(), "track.mp4")); cleanupWOH.start(createWorkflowInstance(wfInstConfig, mp), null); Assert.assertEquals("Media package shouldn't contain any elements", 0, mp.getElements().length); Assert.assertEquals("One file wasn't deleted on remote repository", 1, deletedFilesURIs.size()); }
@Test public void testCreanupWOHwithoutPreservedFlavorAndWithoutDeleteExternal() throws WorkflowOperationException, MediaPackageException { Map<String, String> wfInstConfig = new Hashtable<>(); MediaPackageBuilder mpBuilder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder(); MediaPackage mp = mpBuilder.createNew(); MediaPackageElement track1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Track, "presenter", "source", null); track1.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.COLLECTION_PATH_PREFIX, "asset", mp.getIdentifier().toString(), track1.getIdentifier(), "track.mp4")); cleanupWOH.start(createWorkflowInstance(wfInstConfig, mp), null); Assert.assertEquals("Media package shouldn't contain any elements", 0, mp.getElements().length); Assert.assertEquals("Delete on remote repository not allowed", 0, deletedFilesURIs.size()); }
@Test public void testCreanupWOHwithsomeUnknowenUrl() throws WorkflowOperationException, MediaPackageException { Map<String, String> wfInstConfig = new Hashtable<>(); MediaPackageBuilder mpBuilder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder(); MediaPackage mp = mpBuilder.createNew(); MediaPackageElement track1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Track, "presenter", "source", null); track1.setURI(UrlSupport.uri(HOSTNAME_NODE1, "asset", "asset", mp.getIdentifier().toString(), track1.getIdentifier(), 0, "track.mp4")); cleanupWOH.start(createWorkflowInstance(wfInstConfig, mp), null); Assert.assertEquals("Media package shouldn't contain any elements", 0, mp.getElements().length); Assert.assertEquals("Delete on remote repository not allowed", 0, deletedFilesURIs.size()); }