Search in sources :

Example 36 with MediaPackageException

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

the class TestRestService method newAuthorizationService.

private static AuthorizationService newAuthorizationService() {
    AccessControlList acl = new AccessControlList();
    Attachment attachment = new AttachmentImpl();
    MediaPackage mediapackage;
    try {
        mediapackage = new MediaPackageBuilderImpl().createNew();
    } catch (MediaPackageException e) {
        throw new RuntimeException(e);
    }
    AuthorizationService authorizationService = EasyMock.createNiceMock(AuthorizationService.class);
    EasyMock.expect(authorizationService.getActiveAcl((MediaPackage) EasyMock.anyObject())).andReturn(Tuple.tuple(acl, AclScope.Series)).anyTimes();
    EasyMock.expect(authorizationService.setAcl((MediaPackage) EasyMock.anyObject(), (AclScope) EasyMock.anyObject(), (AccessControlList) EasyMock.anyObject())).andReturn(Tuple.tuple(mediapackage, attachment));
    EasyMock.replay(authorizationService);
    return authorizationService;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackageBuilderImpl(org.opencastproject.mediapackage.MediaPackageBuilderImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Attachment(org.opencastproject.mediapackage.Attachment) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl)

Example 37 with MediaPackageException

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

the class IngestRestService method addMediaPackageElement.

protected Response addMediaPackageElement(HttpServletRequest request, MediaPackageElement.Type type) {
    MediaPackageElementFlavor flavor = null;
    InputStream in = null;
    try {
        String fileName = null;
        MediaPackage mp = null;
        Long startTime = null;
        String[] tags = null;
        /* Only accept multipart/form-data */
        if (!ServletFileUpload.isMultipartContent(request)) {
            logger.trace("request isn't multipart-form-data");
            return Response.serverError().status(Status.BAD_REQUEST).build();
        }
        boolean isDone = false;
        for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
            FileItemStream item = iter.next();
            String fieldName = item.getFieldName();
            if (item.isFormField()) {
                if ("flavor".equals(fieldName)) {
                    String flavorString = Streams.asString(item.openStream(), "UTF-8");
                    logger.trace("flavor: {}", flavorString);
                    if (flavorString != null) {
                        flavor = MediaPackageElementFlavor.parseFlavor(flavorString);
                    }
                } else if ("tags".equals(fieldName)) {
                    String tagsString = Streams.asString(item.openStream(), "UTF-8");
                    logger.trace("tags: {}", tagsString);
                    tags = tagsString.split(",");
                } else if ("mediaPackage".equals(fieldName)) {
                    try {
                        String mediaPackageString = Streams.asString(item.openStream(), "UTF-8");
                        logger.trace("mediaPackage: {}", mediaPackageString);
                        mp = factory.newMediaPackageBuilder().loadFromXml(mediaPackageString);
                    } catch (MediaPackageException e) {
                        logger.debug("Unable to parse the 'mediaPackage' parameter: {}", ExceptionUtils.getMessage(e));
                        return Response.serverError().status(Status.BAD_REQUEST).build();
                    }
                } else if ("startTime".equals(fieldName) && "/addPartialTrack".equals(request.getPathInfo())) {
                    String startTimeString = Streams.asString(item.openStream(), "UTF-8");
                    logger.trace("startTime: {}", startTime);
                    try {
                        startTime = Long.parseLong(startTimeString);
                    } catch (Exception e) {
                        logger.debug("Unable to parse the 'startTime' parameter: {}", ExceptionUtils.getMessage(e));
                        return Response.serverError().status(Status.BAD_REQUEST).build();
                    }
                }
            } else {
                if (flavor == null) {
                    /* A flavor has to be specified in the request prior the video file */
                    logger.debug("A flavor has to be specified in the request prior to the content BODY");
                    return Response.serverError().status(Status.BAD_REQUEST).build();
                }
                fileName = item.getName();
                in = item.openStream();
                isDone = true;
            }
            if (isDone) {
                break;
            }
        }
        /*
       * Check if we actually got a valid request including a message body and a valid mediapackage to attach the
       * element to
       */
        if (in == null || mp == null || MediaPackageSupport.sanityCheck(mp).isSome()) {
            return Response.serverError().status(Status.BAD_REQUEST).build();
        }
        switch(type) {
            case Attachment:
                mp = ingestService.addAttachment(in, fileName, flavor, tags, mp);
                break;
            case Catalog:
                mp = ingestService.addCatalog(in, fileName, flavor, tags, mp);
                break;
            case Track:
                if (startTime == null) {
                    mp = ingestService.addTrack(in, fileName, flavor, tags, mp);
                } else {
                    mp = ingestService.addPartialTrack(in, fileName, flavor, startTime, mp);
                }
                break;
            default:
                throw new IllegalStateException("Type must be one of track, catalog, or attachment");
        }
        return Response.ok(MediaPackageParser.getAsXml(mp)).build();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IngestException(org.opencastproject.ingest.api.IngestException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) FileItemIterator(org.apache.commons.fileupload.FileItemIterator)

Example 38 with MediaPackageException

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

the class IngestRestService method ingestZippedMediaPackage.

private Response ingestZippedMediaPackage(HttpServletRequest request, String wdID, String wiID) {
    if (isIngestLimitEnabled()) {
        setIngestLimit(getIngestLimit() - 1);
        logger.debug("An ingest has started so remaining ingest limit is " + getIngestLimit());
    }
    InputStream in = null;
    Date started = new Date();
    logger.info("Received new request from {} to ingest a zipped mediapackage", request.getRemoteHost());
    try {
        String workflowDefinitionId = wdID;
        String workflowIdAsString = wiID;
        Long workflowInstanceIdAsLong = null;
        Map<String, String> workflowConfig = new HashMap<>();
        if (ServletFileUpload.isMultipartContent(request)) {
            boolean isDone = false;
            for (FileItemIterator iter = new ServletFileUpload().getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String value = Streams.asString(item.openStream(), "UTF-8");
                    logger.trace("{}: {}", fieldName, value);
                    if (WORKFLOW_INSTANCE_ID_PARAM.equals(fieldName)) {
                        workflowIdAsString = value;
                        continue;
                    } else if (WORKFLOW_DEFINITION_ID_PARAM.equals(fieldName)) {
                        workflowDefinitionId = value;
                        continue;
                    } else {
                        logger.debug("Processing form field: " + fieldName);
                        workflowConfig.put(fieldName, value);
                    }
                } else {
                    logger.debug("Processing file item");
                    // once the body gets read iter.hasNext must not be invoked or the stream can not be read
                    // MH-9579
                    in = item.openStream();
                    isDone = true;
                }
                if (isDone)
                    break;
            }
        } else {
            logger.debug("Processing file item");
            in = request.getInputStream();
        }
        // Adding ingest start time to workflow configuration
        DateFormat formatter = new SimpleDateFormat(IngestService.UTC_DATE_FORMAT);
        workflowConfig.put(IngestService.START_DATE_KEY, formatter.format(started));
        /* Legacy support: Try to convert the workflowId to integer */
        if (!StringUtils.isBlank(workflowIdAsString)) {
            try {
                workflowInstanceIdAsLong = Long.parseLong(workflowIdAsString);
            } catch (NumberFormatException e) {
                // The workflowId is not a long value and might be the media package identifier
                workflowConfig.put(IngestServiceImpl.LEGACY_MEDIAPACKAGE_ID_KEY, workflowIdAsString);
            }
        }
        if (StringUtils.isBlank(workflowDefinitionId)) {
            workflowDefinitionId = defaultWorkflowDefinitionId;
        }
        WorkflowInstance workflow;
        if (workflowInstanceIdAsLong != null) {
            workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig, workflowInstanceIdAsLong);
        } else {
            workflow = ingestService.addZippedMediaPackage(in, workflowDefinitionId, workflowConfig);
        }
        return Response.ok(WorkflowParser.toXml(workflow)).build();
    } catch (NotFoundException e) {
        logger.info(e.getMessage());
        return Response.status(Status.NOT_FOUND).build();
    } catch (MediaPackageException e) {
        logger.warn(e.getMessage());
        return Response.serverError().status(Status.BAD_REQUEST).build();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        IOUtils.closeQuietly(in);
        if (isIngestLimitEnabled()) {
            setIngestLimit(getIngestLimit() + 1);
            logger.debug("An ingest has finished so increased ingest limit to " + getIngestLimit());
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) Date(java.util.Date) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IngestException(org.opencastproject.ingest.api.IngestException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) SimpleDateFormat(java.text.SimpleDateFormat)

Example 39 with MediaPackageException

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

the class IngestServiceImpl method createMediaPackage.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#createMediaPackage()
 */
@Override
public MediaPackage createMediaPackage() throws MediaPackageException, ConfigurationException, HandleException {
    MediaPackage mediaPackage;
    try {
        mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew();
    } catch (MediaPackageException e) {
        logger.error("INGEST:Failed to create media package " + e.getLocalizedMessage());
        throw e;
    }
    mediaPackage.setDate(new Date());
    logger.info("Created mediapackage {}", mediaPackage);
    return mediaPackage;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Date(java.util.Date)

Example 40 with MediaPackageException

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

the class IngestServiceImpl method createMediaPackage.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#createMediaPackage()
 */
@Override
public MediaPackage createMediaPackage(String mediaPackageId) throws MediaPackageException, ConfigurationException, HandleException {
    MediaPackage mediaPackage;
    try {
        mediaPackage = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(new UUIDIdBuilderImpl().fromString(mediaPackageId));
    } catch (MediaPackageException e) {
        logger.error("INGEST:Failed to create media package " + e.getLocalizedMessage());
        throw e;
    }
    mediaPackage.setDate(new Date());
    logger.info("Created mediapackage {}", mediaPackage);
    return mediaPackage;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Date(java.util.Date) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)

Aggregations

MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)72 Job (org.opencastproject.job.api.Job)42 IOException (java.io.IOException)34 MediaPackage (org.opencastproject.mediapackage.MediaPackage)33 NotFoundException (org.opencastproject.util.NotFoundException)31 URI (java.net.URI)25 ArrayList (java.util.ArrayList)25 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)21 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)20 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)19 Track (org.opencastproject.mediapackage.Track)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)15 File (java.io.File)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 HttpResponse (org.apache.http.HttpResponse)12 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.http.client.methods.HttpPost)12 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)12 EncoderException (org.opencastproject.composer.api.EncoderException)12