Search in sources :

Example 6 with DistributionException

use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.

the class AwsS3DistributionServiceImpl method restoreElement.

protected MediaPackageElement restoreElement(String channelId, MediaPackage mediaPackage, String elementId, String fileName) throws DistributionException {
    String objectName = null;
    if (StringUtils.isNotBlank(fileName)) {
        objectName = buildObjectName(channelId, mediaPackage.getIdentifier().toString(), elementId, fileName);
    } else {
        objectName = buildObjectName(channelId, mediaPackage.getIdentifier().toString(), mediaPackage.getElementById(elementId));
    }
    // Get the latest version of the file
    // Note that this should be the delete marker for the file. We'll check, but if there is more than one delete marker
    // we'll have probs
    ListVersionsRequest lv = new ListVersionsRequest().withBucketName(bucketName).withPrefix(objectName).withMaxResults(1);
    VersionListing listing = s3.listVersions(lv);
    if (listing.getVersionSummaries().size() < 1) {
        throw new DistributionException("Object not found: " + objectName);
    }
    String versionId = listing.getVersionSummaries().get(0).getVersionId();
    // Verify that this is in fact a delete marker
    GetObjectMetadataRequest metadata = new GetObjectMetadataRequest(bucketName, objectName, versionId);
    // Ok, so there's no way of asking AWS directly if the object is deleted in this version of the SDK
    // So instead, we ask for its metadata
    // If it's deleted, then there *isn't* any metadata and we get a 404, which throws the exception
    // This, imo, is an incredibly boneheaded omission from the AWS SDK, and implies we should look for something which
    // sucks less
    // FIXME: This section should be refactored with a simple s3.doesObjectExist(bucketName, objectName) once we update
    // the AWS SDK
    boolean isDeleted = false;
    try {
        s3.getObjectMetadata(metadata);
    } catch (AmazonServiceException e) {
        // Note: This exception is actually a 405, not a 404.
        // This is expected, but very confusing if you're thinking it should be a 'file not found', rather than a 'method
        // not allowed on stuff that's deleted'
        // It's unclear what the expected behaviour is for things which have never existed...
        isDeleted = true;
    }
    if (isDeleted) {
        // Delete the delete marker
        DeleteVersionRequest delete = new DeleteVersionRequest(bucketName, objectName, versionId);
        s3.deleteVersion(delete);
    }
    return mediaPackage.getElementById(elementId);
}
Also used : GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) VersionListing(com.amazonaws.services.s3.model.VersionListing) AmazonServiceException(com.amazonaws.AmazonServiceException) DistributionException(org.opencastproject.distribution.api.DistributionException) ListVersionsRequest(com.amazonaws.services.s3.model.ListVersionsRequest) DeleteVersionRequest(com.amazonaws.services.s3.model.DeleteVersionRequest)

Example 7 with DistributionException

use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.

the class DownloadDistributionServiceImpl method distribute.

@Override
public Job distribute(String channelId, MediaPackage mediapackage, Set<String> elementIds, boolean checkAvailability, boolean preserveReference) throws DistributionException, MediaPackageException {
    notNull(mediapackage, "mediapackage");
    notNull(elementIds, "elementIds");
    notNull(channelId, "channelId");
    try {
        return serviceRegistry.createJob(JOB_TYPE, Operation.Distribute.toString(), Arrays.asList(channelId, MediaPackageParser.getAsXml(mediapackage), gson.toJson(elementIds), Boolean.toString(checkAvailability), Boolean.toString(preserveReference)), distributeJobLoad);
    } catch (ServiceRegistryException e) {
        throw new DistributionException("Unable to create a job", e);
    }
}
Also used : DistributionException(org.opencastproject.distribution.api.DistributionException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 8 with DistributionException

use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.

the class DownloadDistributionServiceImpl method distributeElement.

/**
 * Distribute a Mediapackage element to the download distribution service.
 *
 * @param channelId
 *   #          The id of the publication channel to be distributed to.
 * @param mediapackage
 *          The media package that contains the element to be distributed.
 * @param element
 *          The the element that should be distributed contained within the media package.
 * @param checkAvailability
 *          Check the availability of the distributed element via http.
 * @param preserveReference
 *           Copy existing Track-Reference to the new distributed Track
 * @return A reference to the MediaPackageElement that has been distributed.
 * @throws DistributionException
 *           Thrown if the parent directory of the MediaPackageElement cannot be created, if the MediaPackageElement
 *           cannot be copied or another unexpected exception occurs.
 */
public MediaPackageElement distributeElement(String channelId, MediaPackage mediapackage, MediaPackageElement element, boolean checkAvailability, boolean preserveReference) throws DistributionException {
    final String mediapackageId = mediapackage.getIdentifier().compact();
    final String elementId = element.getIdentifier();
    try {
        File source;
        try {
            source = workspace.get(element.getURI());
        } catch (NotFoundException e) {
            throw new DistributionException("Unable to find " + element.getURI() + " in the workspace", e);
        } catch (IOException e) {
            throw new DistributionException("Error loading " + element.getURI() + " from the workspace", e);
        }
        // Try to find a duplicated element source
        try {
            source = findDuplicatedElementSource(source, mediapackageId);
        } catch (IOException e) {
            logger.warn("Unable to find duplicated source {}: {}", source, ExceptionUtils.getMessage(e));
        }
        File destination = getDistributionFile(channelId, mediapackage, element);
        if (!destination.equals(source)) {
            // Put the file in place if sourcesfile differs destinationfile
            try {
                FileUtils.forceMkdir(destination.getParentFile());
            } catch (IOException e) {
                throw new DistributionException("Unable to create " + destination.getParentFile(), e);
            }
            logger.debug("Distributing element {} of media package {} to publication channel {} ({})", elementId, mediapackageId, channelId, destination);
            try {
                FileSupport.link(source, destination, true);
            } catch (IOException e) {
                throw new DistributionException(format("Unable to copy %s to %s", source, destination), e);
            }
        }
        // Create a media package element representation of the distributed file
        MediaPackageElement distributedElement = (MediaPackageElement) element.clone();
        try {
            distributedElement.setURI(getDistributionUri(channelId, mediapackageId, element));
            if (preserveReference) {
                distributedElement.setReference(element.getReference());
            }
        } catch (URISyntaxException e) {
            throw new DistributionException("Distributed element produces an invalid URI", e);
        }
        logger.debug("Finished distributing element {} of media package {} to publication channel {}", elementId, mediapackageId, channelId);
        final URI uri = distributedElement.getURI();
        if (checkAvailability) {
            logger.debug("Checking availability of distributed artifact {} at {}", distributedElement, uri);
            waitForResource(trustedHttpClient, uri, HttpServletResponse.SC_OK, TIMEOUT, INTERVAL).fold(Misc.<Exception, Void>chuck(), new Effect.X<Integer>() {

                @Override
                public void xrun(Integer status) throws Exception {
                    if (ne(status, HttpServletResponse.SC_OK)) {
                        logger.warn("Attempt to access distributed file {} returned code {}", uri, status);
                        throw new DistributionException("Unable to load distributed file " + uri.toString());
                    }
                }
            });
        }
        return distributedElement;
    } catch (Exception e) {
        logger.warn("Error distributing " + element, e);
        if (e instanceof DistributionException) {
            throw (DistributionException) e;
        } else {
            throw new DistributionException(e);
        }
    }
}
Also used : NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) DistributionException(org.opencastproject.distribution.api.DistributionException) Effect(org.opencastproject.util.data.Effect) File(java.io.File)

Example 9 with DistributionException

use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.

the class StreamingDistributionServiceImpl method retractElement.

/**
 * Retracts the mediapackage with the given identifier from the distribution channel.
 *
 * @param channelId
 *          the channel id
 * @param mp
 *          the mediapackage
 * @param mpeId
 *          the element identifier
 * @return the retracted element or <code>null</code> if the element was not retracted
 */
private MediaPackageElement retractElement(final String channelId, final MediaPackage mp, final String mpeId) throws DistributionException {
    RequireUtil.notNull(channelId, "channelId");
    RequireUtil.notNull(mp, "mp");
    RequireUtil.notNull(mpeId, "elementId");
    // Make sure the element exists
    final MediaPackageElement mpe = mp.getElementById(mpeId);
    if (mpe == null) {
        throw new IllegalStateException("No element " + mpeId + " found in media package");
    }
    try {
        for (final File mpeFile : locations.get().getDistributionFileFrom(mpe.getURI())) {
            logger.info("Retracting element {} from {}", mpe, mpeFile);
            // or has been removed otherwise
            if (mpeFile.exists()) {
                // Try to remove the file and - if possible - the parent folder
                final File parentDir = mpeFile.getParentFile();
                FileUtils.forceDelete(mpeFile);
                FileSupport.deleteHierarchyIfEmpty(new File(locations.get().getBaseDir()), parentDir);
                logger.info("Finished retracting element {} of media package {}", mpeId, mp);
                return mpe;
            } else {
                logger.info(format("Element %s@%s has already been removed from publication channel %s", mpeId, mp.getIdentifier(), channelId));
                return mpe;
            }
        }
        // could not extract a file from the element's URI
        logger.info(format("Element %s has not been published to publication channel %s", mpe.getURI(), channelId));
        return mpe;
    } catch (Exception e) {
        logger.warn(format("Error retracting element %s of media package %s", mpeId, mp), e);
        if (e instanceof DistributionException) {
            throw (DistributionException) e;
        } else {
            throw new DistributionException(e);
        }
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) DistributionException(org.opencastproject.distribution.api.DistributionException) File(java.io.File) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 10 with DistributionException

use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.

the class StreamingDistributionServiceRemoteImpl method retract.

@Override
public Job retract(String channelId, MediaPackage mediaPackage, Set<String> elementIds) throws DistributionException {
    logger.info(format("Retracting %s elements from %s@%s", elementIds.size(), channelId, distributionChannel));
    final HttpPost req = post("/retract", param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)), param(PARAM_ELEMENT_IDS, gson.toJson(elementIds)), param(PARAM_CHANNEL_ID, channelId));
    for (Job job : join(runRequest(req, jobFromHttpResponse))) {
        return job;
    }
    throw new DistributionException(format("Unable to retract '%s' elements of " + "mediapackage '%s' using a remote destribution service proxy", elementIds.size(), mediaPackage.getIdentifier().toString()));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job)

Aggregations

DistributionException (org.opencastproject.distribution.api.DistributionException)35 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)17 Job (org.opencastproject.job.api.Job)16 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)14 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 NotFoundException (org.opencastproject.util.NotFoundException)11 IOException (java.io.IOException)9 File (java.io.File)7 URISyntaxException (java.net.URISyntaxException)7 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)7 HttpPost (org.apache.http.client.methods.HttpPost)7 MediaPackage (org.opencastproject.mediapackage.MediaPackage)7 URI (java.net.URI)6 ConfigurationException (org.osgi.service.cm.ConfigurationException)6 Publication (org.opencastproject.mediapackage.Publication)4 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3