Search in sources :

Example 1 with AssetManagerException

use of org.opencastproject.assetmanager.api.AssetManagerException in project opencast by opencast.

the class AbstractAssetManager method storeAssets.

/**
 * Store all elements of <code>pmp</code> under the given version.
 */
private void storeAssets(final PartialMediaPackage pmp, final Version version) throws Exception {
    final String mpId = pmp.getMediaPackage().getIdentifier().toString();
    final String orgId = getCurrentOrgId();
    for (final MediaPackageElement e : pmp.getElements()) {
        logger.debug(format("Archiving %s %s %s", e.getFlavor(), e.getMimeType(), e.getURI()));
        final StoragePath storagePath = StoragePath.mk(orgId, mpId, version, e.getIdentifier());
        final Opt<StoragePath> existingAssetOpt = findAssetInVersions(e.getChecksum().toString());
        if (existingAssetOpt.isSome()) {
            final StoragePath existingAsset = existingAssetOpt.get();
            logger.debug("Content of asset {} with checksum {} has been archived before", existingAsset.getMediaPackageElementId(), e.getChecksum());
            if (!getAssetStore().copy(existingAsset, storagePath)) {
                throw new AssetManagerException(format("An asset with checksum %s has already been archived but trying to copy or link asset %s to it failed", e.getChecksum(), existingAsset));
            }
        } else {
            final Opt<Long> size = e.getSize() > 0 ? Opt.some(e.getSize()) : Opt.<Long>none();
            getAssetStore().put(storagePath, Source.mk(e.getURI(), size, Opt.nul(e.getMimeType())));
        }
    }
}
Also used : StoragePath(org.opencastproject.assetmanager.impl.storage.StoragePath) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException)

Example 2 with AssetManagerException

use of org.opencastproject.assetmanager.api.AssetManagerException in project opencast by opencast.

the class AbstractAssetManager method addInternal.

/**
 * Mutates mp and its elements, so make sure to work on a copy.
 */
private SnapshotDto addInternal(String owner, final MediaPackage mp) throws Exception {
    final Date now = new Date();
    // claim a new version for the media package
    final String mpId = mp.getIdentifier().toString();
    final VersionImpl version = getDb().claimVersion(mpId);
    logger.info("Creating new version {} of media package {}", version, mp);
    final PartialMediaPackage pmp = assetsOnly(mp);
    // make sure they have a checksum
    calcChecksumsForMediaPackageElements(pmp);
    // download and archive elements
    storeAssets(pmp, version);
    // store mediapackage in db
    final SnapshotDto snapshotDto;
    try {
        rewriteUrisForArchival(pmp, version);
        snapshotDto = getDb().saveSnapshot(getCurrentOrgId(), pmp, now, version, Availability.ONLINE, owner);
    } catch (AssetManagerException e) {
        logger.error("Could not take snapshot {}: {}", mpId, e);
        throw new AssetManagerException(e);
    }
    // save manifest to element store
    // this is done at the end after the media package element ids have been rewritten to neutral URNs
    storeManifest(pmp, version);
    return snapshotDto;
}
Also used : SnapshotDto(org.opencastproject.assetmanager.impl.persistence.SnapshotDto) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Date(java.util.Date)

Example 3 with AssetManagerException

use of org.opencastproject.assetmanager.api.AssetManagerException in project opencast by opencast.

the class ToolsEndpoint method addSmilToArchive.

/**
 * Adds the SMIL file as {@link Catalog} to the media package and sends the updated media package to the archive.
 *
 * @param mediaPackage
 *          the media package to at the SMIL catalog
 * @param smil
 *          the SMIL catalog
 * @return the updated media package
 * @throws IOException
 *           if the SMIL catalog cannot be read or not be written to the archive
 */
MediaPackage addSmilToArchive(MediaPackage mediaPackage, final Smil smil) throws IOException {
    MediaPackageElementFlavor mediaPackageElementFlavor = adminUIConfiguration.getSmilCatalogFlavor();
    // set default catalog Id if there is none existing
    String catalogId = smil.getId();
    Catalog[] catalogs = mediaPackage.getCatalogs();
    // get the first smil/cutting  catalog-ID to overwrite it with new smil info
    for (Catalog p : catalogs) {
        if (p.getFlavor().matches(mediaPackageElementFlavor)) {
            logger.debug("Set Idendifier for Smil-Catalog to: " + p.getIdentifier());
            catalogId = p.getIdentifier();
            break;
        }
    }
    Catalog catalog = mediaPackage.getCatalog(catalogId);
    URI smilURI;
    try (InputStream is = IOUtils.toInputStream(smil.toXML(), "UTF-8")) {
        smilURI = workspace.put(mediaPackage.getIdentifier().compact(), catalogId, TARGET_FILE_NAME, is);
    } catch (SAXException e) {
        logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
        throw new IOException(e);
    } catch (JAXBException e) {
        logger.error("Error while serializing the SMIL catalog to XML: {}", e.getMessage());
        throw new IOException(e);
    }
    if (catalog == null) {
        MediaPackageElementBuilder mpeBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
        catalog = (Catalog) mpeBuilder.elementFromURI(smilURI, MediaPackageElement.Type.Catalog, adminUIConfiguration.getSmilCatalogFlavor());
        mediaPackage.add(catalog);
    }
    catalog.setURI(smilURI);
    catalog.setIdentifier(catalogId);
    catalog.setMimeType(MimeTypes.XML);
    for (String tag : adminUIConfiguration.getSmilCatalogTags()) {
        catalog.addTag(tag);
    }
    // setting the URI to a new source so the checksum will most like be invalid
    catalog.setChecksum(null);
    try {
        // FIXME SWITCHP-333: Start in new thread
        assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
    } catch (AssetManagerException e) {
        logger.error("Error while adding the updated media package ({}) to the archive: {}", mediaPackage.getIdentifier(), e.getMessage());
        throw new IOException(e);
    }
    return mediaPackage;
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) SAXException(org.xml.sax.SAXException)

Example 4 with AssetManagerException

use of org.opencastproject.assetmanager.api.AssetManagerException in project opencast by opencast.

the class ToolsEndpoint method editVideo.

@POST
@Path("{mediapackageid}/editor.json")
@Consumes(MediaType.APPLICATION_JSON)
@RestQuery(name = "editVideo", description = "Takes editing information from the client side and processes it", returnDescription = "", pathParameters = { @RestParameter(name = "mediapackageid", description = "The id of the media package", isRequired = true, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "Editing information saved and processed", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "Media package not found", responseCode = HttpServletResponse.SC_NOT_FOUND), @RestResponse(description = "The editing information cannot be parsed", responseCode = HttpServletResponse.SC_BAD_REQUEST) })
public Response editVideo(@PathParam("mediapackageid") final String mediaPackageId, @Context HttpServletRequest request) throws IndexServiceException, NotFoundException {
    String details;
    try (InputStream is = request.getInputStream()) {
        details = IOUtils.toString(is);
    } catch (IOException e) {
        logger.error("Error reading request body: {}", getStackTrace(e));
        return R.serverError();
    }
    JSONParser parser = new JSONParser();
    EditingInfo editingInfo;
    try {
        JSONObject detailsJSON = (JSONObject) parser.parse(details);
        editingInfo = EditingInfo.parse(detailsJSON);
    } catch (Exception e) {
        logger.warn("Unable to parse concat information ({}): {}", details, ExceptionUtils.getStackTrace(e));
        return R.badRequest("Unable to parse details");
    }
    final Opt<Event> optEvent = getEvent(mediaPackageId);
    if (optEvent.isNone()) {
        return R.notFound();
    } else {
        MediaPackage mediaPackage = index.getEventMediapackage(optEvent.get());
        Smil smil;
        try {
            smil = createSmilCuttingCatalog(editingInfo, mediaPackage);
        } catch (Exception e) {
            logger.warn("Unable to create a SMIL cutting catalog ({}): {}", details, getStackTrace(e));
            return R.badRequest("Unable to create SMIL cutting catalog");
        }
        try {
            addSmilToArchive(mediaPackage, smil);
        } catch (IOException e) {
            logger.warn("Unable to add SMIL cutting catalog to archive: {}", getStackTrace(e));
            return R.serverError();
        }
        if (editingInfo.getPostProcessingWorkflow().isSome()) {
            final String workflowId = editingInfo.getPostProcessingWorkflow().get();
            try {
                final Workflows workflows = new Workflows(assetManager, workspace, workflowService);
                workflows.applyWorkflowToLatestVersion($(mediaPackage.getIdentifier().toString()), ConfiguredWorkflow.workflow(workflowService.getWorkflowDefinitionById(workflowId))).run();
            } catch (AssetManagerException e) {
                logger.warn("Unable to start workflow '{}' on archived media package '{}': {}", workflowId, mediaPackage, getStackTrace(e));
                return R.serverError();
            } catch (WorkflowDatabaseException e) {
                logger.warn("Unable to load workflow '{}' from workflow service: {}", workflowId, getStackTrace(e));
                return R.serverError();
            } catch (NotFoundException e) {
                logger.warn("Workflow '{}' not found", workflowId);
                return R.badRequest("Workflow not found");
            }
        }
    }
    return R.ok();
}
Also used : Workflows(org.opencastproject.assetmanager.util.Workflows) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) SmilException(org.opencastproject.smil.api.SmilException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) JAXBException(javax.xml.bind.JAXBException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) SAXException(org.xml.sax.SAXException) UrlSigningException(org.opencastproject.security.urlsigning.exception.UrlSigningException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) JSONObject(org.json.simple.JSONObject) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) JSONParser(org.json.simple.parser.JSONParser) Smil(org.opencastproject.smil.entity.api.Smil) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 5 with AssetManagerException

use of org.opencastproject.assetmanager.api.AssetManagerException in project opencast by opencast.

the class AbstractAssetManagerBasicTest method testUnwrapException.

@Test
public void testUnwrapException() {
    assertTrue(AbstractAssetManager.unwrapExceptionUntil(Exception.class, new AssetManagerException()).isSome());
    assertThat(AbstractAssetManager.unwrapExceptionUntil(Exception.class, new AssetManagerException()).get(), instanceOf(AssetManagerException.class));
    assertEquals("error", AbstractAssetManager.unwrapExceptionUntil(AssetManagerException.class, new AssetManagerException("error")).get().getMessage());
    assertEquals("error", AbstractAssetManager.unwrapExceptionUntil(AssetManagerException.class, new Exception(new AssetManagerException("error"))).get().getMessage());
    assertEquals("wrapper", AbstractAssetManager.unwrapExceptionUntil(AssetManagerException.class, new AssetManagerException("wrapper", new AssetManagerException("error"))).get().getMessage());
}
Also used : AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Test(org.junit.Test)

Aggregations

AssetManagerException (org.opencastproject.assetmanager.api.AssetManagerException)8 NotFoundException (org.opencastproject.util.NotFoundException)4 IOException (java.io.IOException)3 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)3 InputStream (java.io.InputStream)2 URI (java.net.URI)2 JAXBException (javax.xml.bind.JAXBException)2 JSONObject (org.json.simple.JSONObject)2 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)2 AResult (org.opencastproject.assetmanager.api.query.AResult)2 Workflows (org.opencastproject.assetmanager.util.Workflows)2 IndexServiceException (org.opencastproject.index.service.exception.IndexServiceException)2 Catalog (org.opencastproject.mediapackage.Catalog)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)2 WorkflowQuery (org.opencastproject.workflow.api.WorkflowQuery)2 WorkflowSet (org.opencastproject.workflow.api.WorkflowSet)2 SAXException (org.xml.sax.SAXException)2 URISyntaxException (java.net.URISyntaxException)1