Search in sources :

Example 6 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException 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 7 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException 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)

Example 8 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class AttachmentTest method testFromURL.

/**
 * Test method for {@link org.opencastproject.mediapackage.attachment.AttachmentImpl#fromURI(java.net.URI)}.
 */
@Test
public void testFromURL() {
    MediaPackageElementBuilderFactory factory = MediaPackageElementBuilderFactory.newInstance();
    MediaPackageElementBuilder builder = factory.newElementBuilder();
    MediaPackageElement packageElement = null;
    // Create the element
    try {
        packageElement = builder.elementFromURI(coverFile.toURI());
    } catch (UnsupportedElementException e) {
        fail("Attachment is unsupported: " + e.getMessage());
    }
    // Type test
    assertTrue("Type mismatch", packageElement instanceof Attachment);
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaPackageElementBuilderFactory(org.opencastproject.mediapackage.MediaPackageElementBuilderFactory) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Attachment(org.opencastproject.mediapackage.Attachment) MediaPackageBuilderTest(org.opencastproject.mediapackage.MediaPackageBuilderTest) Test(org.junit.Test)

Example 9 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class MediaInspector method inspectTrack.

/**
 * Inspects the element that is passed in as uri.
 *
 * @param trackURI
 *          the element uri
 * @return the inspected track
 * @throws org.opencastproject.inspection.api.MediaInspectionException
 *           if inspection fails
 */
public Track inspectTrack(URI trackURI, Map<String, String> options) throws MediaInspectionException {
    logger.debug("inspect(" + trackURI + ") called, using workspace " + workspace);
    throwExceptionIfInvalid(options);
    try {
        // Get the file from the URL (runtime exception if invalid)
        File file = null;
        try {
            file = workspace.get(trackURI);
        } catch (NotFoundException notFound) {
            throw new MediaInspectionException("Unable to find resource " + trackURI, notFound);
        } catch (IOException ioe) {
            throw new MediaInspectionException("Error reading " + trackURI + " from workspace", ioe);
        }
        // TODO: Try to guess the extension from the container's metadata
        if ("".equals(FilenameUtils.getExtension(file.getName()))) {
            throw new MediaInspectionException("Can not inspect files without a filename extension");
        }
        MediaContainerMetadata metadata = getFileMetadata(file, getAccurateFrameCount(options));
        if (metadata == null) {
            throw new MediaInspectionException("Media analyzer returned no metadata from " + file);
        } else {
            MediaPackageElementBuilder elementBuilder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
            TrackImpl track;
            MediaPackageElement element;
            try {
                element = elementBuilder.elementFromURI(trackURI, MediaPackageElement.Type.Track, null);
            } catch (UnsupportedElementException e) {
                throw new MediaInspectionException("Unable to create track element from " + file, e);
            }
            track = (TrackImpl) element;
            // Duration
            if (metadata.getDuration() != null && metadata.getDuration() > 0)
                track.setDuration(metadata.getDuration());
            // Checksum
            try {
                track.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
            } catch (IOException e) {
                throw new MediaInspectionException("Unable to read " + file, e);
            }
            // Mimetype
            InputStream is = null;
            try {
                // Try to get the Mimetype from Apache Tika
                is = new FileInputStream(file);
                MimeType mimeType = extractContentType(is);
                // If Mimetype could not be extracted try to get it from opencast
                if (mimeType == null) {
                    mimeType = MimeTypes.fromURL(file.toURI().toURL());
                    // The mimetype library doesn't know about audio/video metadata, so the type might be wrong.
                    if ("audio".equals(mimeType.getType()) && metadata.hasVideoStreamMetadata()) {
                        mimeType = MimeTypes.parseMimeType("video/" + mimeType.getSubtype());
                    } else if ("video".equals(mimeType.getType()) && !metadata.hasVideoStreamMetadata()) {
                        mimeType = MimeTypes.parseMimeType("audio/" + mimeType.getSubtype());
                    }
                }
                track.setMimeType(mimeType);
            } catch (Exception e) {
                logger.error("Unable to find mimetype for {}", file.getAbsolutePath());
            } finally {
                IoSupport.closeQuietly(is);
            }
            // Audio metadata
            try {
                addAudioStreamMetadata(track, metadata);
            } catch (Exception e) {
                throw new MediaInspectionException("Unable to extract audio metadata from " + file, e);
            }
            // Videometadata
            try {
                addVideoStreamMetadata(track, metadata);
            } catch (Exception e) {
                throw new MediaInspectionException("Unable to extract video metadata from " + file, e);
            }
            return track;
        }
    } catch (Exception e) {
        logger.warn("Error inspecting " + trackURI, e);
        if (e instanceof MediaInspectionException) {
            throw (MediaInspectionException) e;
        } else {
            throw new MediaInspectionException(e);
        }
    }
}
Also used : MediaContainerMetadata(org.opencastproject.inspection.ffmpeg.api.MediaContainerMetadata) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MimeType(org.opencastproject.util.MimeType) MediaAnalyzerException(org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) File(java.io.File)

Example 10 with UnsupportedElementException

use of org.opencastproject.mediapackage.UnsupportedElementException in project opencast by opencast.

the class EventCommentParser method replyFromManifest.

private static EventCommentReply replyFromManifest(Node commentReplyNode, UserDirectoryService userDirectoryService) throws UnsupportedElementException {
    try {
        // id
        Long id = null;
        Double idAsDouble = ((Number) xpath.evaluate("@id", commentReplyNode, XPathConstants.NUMBER)).doubleValue();
        if (!idAsDouble.isNaN())
            id = idAsDouble.longValue();
        // text
        String text = (String) xpath.evaluate("text/text()", commentReplyNode, XPathConstants.STRING);
        // Author
        Node authorNode = (Node) xpath.evaluate("author", commentReplyNode, XPathConstants.NODE);
        User author = userFromManifest(authorNode, userDirectoryService);
        // CreationDate
        String creationDateString = (String) xpath.evaluate("creationDate/text()", commentReplyNode, XPathConstants.STRING);
        Date creationDate = new Date(DateTimeSupport.fromUTC(creationDateString));
        // ModificationDate
        String modificationDateString = (String) xpath.evaluate("modificationDate/text()", commentReplyNode, XPathConstants.STRING);
        Date modificationDate = new Date(DateTimeSupport.fromUTC(modificationDateString));
        // Create reply
        return EventCommentReply.create(Option.option(id), text.trim(), author, creationDate, modificationDate);
    } catch (XPathExpressionException e) {
        throw new UnsupportedElementException("Error while reading comment reply information from manifest", e);
    } catch (Exception e) {
        if (e instanceof UnsupportedElementException)
            throw (UnsupportedElementException) e;
        throw new UnsupportedElementException("Error while reading comment reply creation or modification date information from manifest", e);
    }
}
Also used : User(org.opencastproject.security.api.User) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Date(java.util.Date) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

UnsupportedElementException (org.opencastproject.mediapackage.UnsupportedElementException)11 URI (java.net.URI)6 XPathExpressionException (javax.xml.xpath.XPathExpressionException)6 MimeType (org.opencastproject.util.MimeType)6 NodeList (org.w3c.dom.NodeList)5 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)4 Checksum (org.opencastproject.util.Checksum)4 FileInputStream (java.io.FileInputStream)3 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)3 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)3 NotFoundException (org.opencastproject.util.NotFoundException)3 Node (org.w3c.dom.Node)3 File (java.io.File)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 MediaAnalyzerException (org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException)2