Search in sources :

Example 1 with Checksum

use of org.opencastproject.util.Checksum in project opencast by opencast.

the class MediaInspectionServiceImplTest method testInspection.

@Test
public void testInspection() throws Exception {
    final URI trackUri = getResource("/test.mp4");
    for (MediaInspector mi : init(trackUri)) {
        Track track = mi.inspectTrack(trackUri, Options.NO_OPTION);
        // test the returned values
        Checksum cs = Checksum.create(ChecksumType.fromString("md5"), "cc72b7a4f1a68b84fba6f0fb895da395");
        assertEquals(cs, track.getChecksum());
        assertEquals("video", track.getMimeType().getType());
        assertEquals("mp4", track.getMimeType().getSubtype());
        assertNotNull(track.getDuration());
        assertTrue(track.getDuration() > 0);
    }
}
Also used : Checksum(org.opencastproject.util.Checksum) URI(java.net.URI) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 2 with Checksum

use of org.opencastproject.util.Checksum in project opencast by opencast.

the class PublicationBuilderPlugin method elementFromManifest.

@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
    String id = null;
    MimeType mimeType = null;
    MediaPackageElementFlavor flavor = null;
    String reference = null;
    String channel = null;
    URI url = null;
    long size = -1;
    Checksum checksum = null;
    try {
        // id
        id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
        if (StringUtils.isEmpty(id)) {
            throw new UnsupportedElementException("Unvalid or missing id argument!");
        }
        // url
        url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
        // channel
        channel = xpath.evaluate("@channel", elementNode).trim();
        if (StringUtils.isEmpty(channel)) {
            throw new UnsupportedElementException("Unvalid or missing channel argument!");
        }
        // reference
        reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
        // size
        String trackSize = xpath.evaluate("size/text()", elementNode).trim();
        if (!"".equals(trackSize))
            size = Long.parseLong(trackSize);
        // flavor
        String flavorValue = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(flavorValue))
            flavor = MediaPackageElementFlavor.parseFlavor(flavorValue);
        // checksum
        String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
        String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
            checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
        // mimetype
        String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(mimeTypeValue)) {
            mimeType = MimeTypes.parseMimeType(mimeTypeValue);
        } else {
            throw new UnsupportedElementException("Unvalid or missing mimetype argument!");
        }
        // Build the publication element
        PublicationImpl publication = new PublicationImpl(id, channel, url, mimeType);
        if (StringUtils.isNotBlank(id))
            publication.setIdentifier(id);
        // Add url
        publication.setURI(url);
        // Add reference
        if (StringUtils.isNotEmpty(reference))
            publication.referTo(MediaPackageReferenceImpl.fromString(reference));
        // Set size
        if (size > 0)
            publication.setSize(size);
        // Set checksum
        if (checksum != null)
            publication.setChecksum(checksum);
        // Set mimetpye
        if (mimeType != null)
            publication.setMimeType(mimeType);
        if (flavor != null)
            publication.setFlavor(flavor);
        // description
        String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotBlank(description))
            publication.setElementDescription(description.trim());
        // tags
        NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
        for (int i = 0; i < tagNodes.getLength(); i++) {
            publication.addTag(tagNodes.item(i).getTextContent());
        }
        return publication;
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading track information from manifest: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new UnsupportedElementException("Error while reading presenter track " + url + ": " + e.getMessage());
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MimeType(org.opencastproject.util.MimeType) PublicationImpl(org.opencastproject.mediapackage.PublicationImpl) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) Checksum(org.opencastproject.util.Checksum)

Example 3 with Checksum

use of org.opencastproject.util.Checksum in project opencast by opencast.

the class SchedulerUtil method calculateChecksum.

public static String calculateChecksum(Workspace workspace, List<MediaPackageElementFlavor> eventCatalogUIAdapterFlavors, Date startDateTime, Date endDateTime, String captureAgentId, Set<String> userIds, MediaPackage mediaPackage, Opt<DublinCoreCatalog> episodeDublincore, Map<String, String> wfProperties, Map<String, String> finalCaProperties, boolean optOut, AccessControlList acl) {
    List<String> userIdsList = new ArrayList<>(userIds);
    Collections.sort(userIdsList);
    final MessageDigest messageDigest = mkMd5MessageDigest();
    messageDigest.update(mkChecksumInput(startDateTime));
    messageDigest.update(mkChecksumInput(endDateTime));
    messageDigest.update(mkChecksumInput(captureAgentId));
    for (String user : userIdsList) {
        messageDigest.update(mkChecksumInput(user));
    }
    if (episodeDublincore.isSome()) {
        Catalog episodeCatalog = $(mediaPackage.getCatalogs()).filter(MediaPackageSupport.Filters.isEpisodeDublinCore.toFn()).head2();
        Checksum checksum = episodeCatalog.getChecksum();
        if (checksum == null) {
            checksum = DublinCoreUtil.calculateChecksum(episodeDublincore.get());
            episodeCatalog.setChecksum(checksum);
        }
        messageDigest.update(mkChecksumInput(checksum.toString()));
    }
    // Add extended metadata to calculation
    for (Catalog c : $(mediaPackage.getCatalogs()).sort(sortCatalogById)) {
        if (eventCatalogUIAdapterFlavors.contains(c.getFlavor())) {
            Checksum checksum = c.getChecksum();
            if (checksum == null) {
                DublinCoreCatalog dublinCore = DublinCoreUtil.loadDublinCore(workspace, c);
                checksum = DublinCoreUtil.calculateChecksum(dublinCore);
                c.setChecksum(checksum);
            }
            messageDigest.update(mkChecksumInput(checksum.toString()));
        }
    }
    messageDigest.update(mkChecksumInput(AccessControlUtil.calculateChecksum(acl).toString()));
    for (Entry<String, String> entry : new TreeMap<>(wfProperties).entrySet()) {
        messageDigest.update(mkChecksumInput(entry.getKey()));
        messageDigest.update(mkChecksumInput(entry.getValue()));
    }
    for (Entry<String, String> entry : new TreeMap<>(finalCaProperties).entrySet()) {
        messageDigest.update(mkChecksumInput(entry.getKey()));
        messageDigest.update(mkChecksumInput(entry.getValue()));
    }
    messageDigest.update(mkChecksumInput(optOut));
    return Checksum.convertToHex(messageDigest.digest());
}
Also used : Checksum(org.opencastproject.util.Checksum) ArrayList(java.util.ArrayList) MessageDigest(java.security.MessageDigest) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 4 with Checksum

use of org.opencastproject.util.Checksum in project opencast by opencast.

the class AbstractAttachmentBuilderPlugin method elementFromManifest.

/**
 * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
 *      org.opencastproject.mediapackage.MediaPackageSerializer)
 */
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
    String id = null;
    String attachmentFlavor = null;
    String reference = null;
    URI uri = null;
    long size = -1;
    Checksum checksum = null;
    MimeType mimeType = null;
    try {
        // id
        id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
        // flavor
        attachmentFlavor = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
        // reference
        reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
        // url
        uri = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
        // size
        String attachmentSize = xpath.evaluate("size/text()", elementNode).trim();
        if (!"".equals(attachmentSize))
            size = Long.parseLong(attachmentSize);
        // checksum
        String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
        String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
            checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
        // mimetype
        String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(mimeTypeValue))
            mimeType = MimeTypes.parseMimeType(mimeTypeValue);
        // create the attachment
        AttachmentImpl attachment = (AttachmentImpl) AttachmentImpl.fromURI(uri);
        if (StringUtils.isNotEmpty(id))
            attachment.setIdentifier(id);
        // Add url
        attachment.setURI(uri);
        // Add reference
        if (StringUtils.isNotEmpty(reference))
            attachment.referTo(MediaPackageReferenceImpl.fromString(reference));
        // Add type/flavor information
        if (StringUtils.isNotEmpty(attachmentFlavor)) {
            try {
                MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor(attachmentFlavor);
                attachment.setFlavor(flavor);
            } catch (IllegalArgumentException e) {
                logger.warn("Unable to read attachment flavor: " + e.getMessage());
            }
        }
        // Set the size
        if (size > 0)
            attachment.setSize(size);
        // Set checksum
        if (checksum != null)
            attachment.setChecksum(checksum);
        // Set mimetype
        if (mimeType != null)
            attachment.setMimeType(mimeType);
        // Set the description
        String description = xpath.evaluate("description/text()", elementNode);
        if (StringUtils.isNotEmpty(description))
            attachment.setElementDescription(description.trim());
        // Set tags
        NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
        for (int i = 0; i < tagNodes.getLength(); i++) {
            attachment.addTag(tagNodes.item(i).getTextContent());
        }
        return specializeAttachment(attachment);
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading attachment from manifest: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new UnsupportedElementException("Error while reading attachment file " + uri + ": " + e.getMessage());
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MimeType(org.opencastproject.util.MimeType) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) Checksum(org.opencastproject.util.Checksum) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl)

Example 5 with Checksum

use of org.opencastproject.util.Checksum in project opencast by opencast.

the class TrackBuilderPlugin method elementFromManifest.

/**
 * @see org.opencastproject.mediapackage.elementbuilder.MediaPackageElementBuilderPlugin#elementFromManifest(org.w3c.dom.Node,
 *      org.opencastproject.mediapackage.MediaPackageSerializer)
 */
@Override
public MediaPackageElement elementFromManifest(Node elementNode, MediaPackageSerializer serializer) throws UnsupportedElementException {
    String id = null;
    MimeType mimeType = null;
    MediaPackageElementFlavor flavor = null;
    TrackImpl.StreamingProtocol transport = null;
    String reference = null;
    URI url = null;
    long size = -1;
    Checksum checksum = null;
    try {
        // id
        id = (String) xpath.evaluate("@id", elementNode, XPathConstants.STRING);
        // url
        url = serializer.decodeURI(new URI(xpath.evaluate("url/text()", elementNode).trim()));
        // reference
        reference = (String) xpath.evaluate("@ref", elementNode, XPathConstants.STRING);
        // size
        String trackSize = xpath.evaluate("size/text()", elementNode).trim();
        if (!"".equals(trackSize))
            size = Long.parseLong(trackSize);
        // flavor
        String flavorValue = (String) xpath.evaluate("@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(flavorValue))
            flavor = MediaPackageElementFlavor.parseFlavor(flavorValue);
        // transport
        String transportValue = (String) xpath.evaluate("@transport", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(transportValue))
            transport = TrackImpl.StreamingProtocol.valueOf(transportValue);
        // checksum
        String checksumValue = (String) xpath.evaluate("checksum/text()", elementNode, XPathConstants.STRING);
        String checksumType = (String) xpath.evaluate("checksum/@type", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(checksumValue) && checksumType != null)
            checksum = Checksum.create(checksumType.trim(), checksumValue.trim());
        // mimetype
        String mimeTypeValue = (String) xpath.evaluate("mimetype/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(mimeTypeValue))
            mimeType = MimeTypes.parseMimeType(mimeTypeValue);
        // 
        // Build the track
        TrackImpl track = TrackImpl.fromURI(url);
        if (StringUtils.isNotBlank(id))
            track.setIdentifier(id);
        // Add url
        track.setURI(url);
        // Add reference
        if (StringUtils.isNotEmpty(reference))
            track.referTo(MediaPackageReferenceImpl.fromString(reference));
        // Set size
        if (size > 0)
            track.setSize(size);
        // Set checksum
        if (checksum != null)
            track.setChecksum(checksum);
        // Set mimetpye
        if (mimeType != null)
            track.setMimeType(mimeType);
        if (flavor != null)
            track.setFlavor(flavor);
        // set transport
        if (transport != null)
            track.setTransport(transport);
        // description
        String description = (String) xpath.evaluate("description/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotBlank(description))
            track.setElementDescription(description.trim());
        // tags
        NodeList tagNodes = (NodeList) xpath.evaluate("tags/tag", elementNode, XPathConstants.NODESET);
        for (int i = 0; i < tagNodes.getLength(); i++) {
            track.addTag(tagNodes.item(i).getTextContent());
        }
        // duration
        try {
            String strDuration = (String) xpath.evaluate("duration/text()", elementNode, XPathConstants.STRING);
            if (StringUtils.isNotEmpty(strDuration)) {
                long duration = Long.parseLong(strDuration.trim());
                track.setDuration(duration);
            }
        } catch (NumberFormatException e) {
            throw new UnsupportedElementException("Duration of track " + url + " is malformatted");
        }
        // is live
        String strLive = (String) xpath.evaluate("live/text()", elementNode, XPathConstants.STRING);
        if (StringUtils.isNotEmpty(strLive)) {
            boolean live = Boolean.parseBoolean(strLive.trim());
            track.setLive(live);
        }
        // audio settings
        Node audioSettingsNode = (Node) xpath.evaluate("audio", elementNode, XPathConstants.NODE);
        if (audioSettingsNode != null && audioSettingsNode.hasChildNodes()) {
            try {
                AudioStreamImpl as = AudioStreamImpl.fromManifest(createStreamID(track), audioSettingsNode, xpath);
                track.addStream(as);
            } catch (IllegalStateException e) {
                throw new UnsupportedElementException("Illegal state encountered while reading audio settings from " + url + ": " + e.getMessage());
            } catch (XPathException e) {
                throw new UnsupportedElementException("Error while parsing audio settings from " + url + ": " + e.getMessage());
            }
        }
        // video settings
        Node videoSettingsNode = (Node) xpath.evaluate("video", elementNode, XPathConstants.NODE);
        if (videoSettingsNode != null && videoSettingsNode.hasChildNodes()) {
            try {
                VideoStreamImpl vs = VideoStreamImpl.fromManifest(createStreamID(track), videoSettingsNode, xpath);
                track.addStream(vs);
            } catch (IllegalStateException e) {
                throw new UnsupportedElementException("Illegal state encountered while reading video settings from " + url + ": " + e.getMessage());
            } catch (XPathException e) {
                throw new UnsupportedElementException("Error while parsing video settings from " + url + ": " + e.getMessage());
            }
        }
        return track;
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading track information from manifest: " + e.getMessage());
    } catch (NoSuchAlgorithmException e) {
        throw new UnsupportedElementException("Unsupported digest algorithm: " + e.getMessage());
    } catch (URISyntaxException e) {
        throw new UnsupportedElementException("Error while reading presenter track " + url + ": " + e.getMessage());
    }
}
Also used : XPathException(javax.xml.xpath.XPathException) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AudioStreamImpl(org.opencastproject.mediapackage.track.AudioStreamImpl) VideoStreamImpl(org.opencastproject.mediapackage.track.VideoStreamImpl) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URISyntaxException(java.net.URISyntaxException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MimeType(org.opencastproject.util.MimeType) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) Checksum(org.opencastproject.util.Checksum)

Aggregations

Checksum (org.opencastproject.util.Checksum)8 URI (java.net.URI)7 MimeType (org.opencastproject.util.MimeType)6 URISyntaxException (java.net.URISyntaxException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)4 UnsupportedElementException (org.opencastproject.mediapackage.UnsupportedElementException)4 NodeList (org.w3c.dom.NodeList)4 Test (org.junit.Test)3 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)3 Track (org.opencastproject.mediapackage.Track)3 Catalog (org.opencastproject.mediapackage.Catalog)2 MessageDigest (java.security.MessageDigest)1 ArrayList (java.util.ArrayList)1 XPathException (javax.xml.xpath.XPathException)1 AudioStream (org.opencastproject.mediapackage.AudioStream)1 PublicationImpl (org.opencastproject.mediapackage.PublicationImpl)1 VideoStream (org.opencastproject.mediapackage.VideoStream)1 AttachmentImpl (org.opencastproject.mediapackage.attachment.AttachmentImpl)1 AudioStreamImpl (org.opencastproject.mediapackage.track.AudioStreamImpl)1