Search in sources :

Example 1 with FileUploadException

use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.

the class FileUploadRestService method getNewJob.

// </editor-fold>
@POST
@Produces(MediaType.TEXT_PLAIN)
@Path("newjob")
@RestQuery(name = "newjob", description = "Creates a new upload job and returns the jobs ID.", restParameters = { @RestParameter(description = "The name of the file that will be uploaded", isRequired = false, name = REQUESTFIELD_FILENAME, type = RestParameter.Type.STRING), @RestParameter(description = "The size of the file that will be uploaded", isRequired = false, name = REQUESTFIELD_FILESIZE, type = RestParameter.Type.STRING), @RestParameter(description = "The size of the chunks that will be uploaded", isRequired = false, name = REQUESTFIELD_CHUNKSIZE, type = RestParameter.Type.STRING), @RestParameter(description = "The flavor of this track", isRequired = false, name = REQUESTFIELD_FLAVOR, type = RestParameter.Type.STRING), @RestParameter(description = "The mediapackage the file should belong to", isRequired = false, name = REQUESTFIELD_MEDIAPACKAGE, type = RestParameter.Type.TEXT) }, reponses = { @RestResponse(description = "job was successfully created", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "upload service gave an error", responseCode = HttpServletResponse.SC_NO_CONTENT) }, returnDescription = "The ID of the newly created upload job")
public Response getNewJob(@FormParam(REQUESTFIELD_FILENAME) String filename, @FormParam(REQUESTFIELD_FILESIZE) long filesize, @FormParam(REQUESTFIELD_CHUNKSIZE) int chunksize, @FormParam(REQUESTFIELD_MEDIAPACKAGE) String mediapackage, @FormParam(REQUESTFIELD_FLAVOR) String flav) {
    try {
        if (StringUtils.isBlank(filename)) {
            filename = "john.doe";
        }
        if (filesize < 1) {
            filesize = -1;
        }
        if (chunksize < 1) {
            chunksize = -1;
        }
        MediaPackage mp = null;
        if (StringUtils.isNotBlank(mediapackage)) {
            mp = factory.newMediaPackageBuilder().loadFromXml(mediapackage);
        }
        MediaPackageElementFlavor flavor = null;
        if (StringUtils.isNotBlank(flav)) {
            flavor = new MediaPackageElementFlavor(flav.split("/")[0], flav.split("/")[1]);
        }
        FileUploadJob job = uploadService.createJob(filename, filesize, chunksize, mp, flavor);
        return Response.ok(job.getId()).build();
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.NO_CONTENT).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) MediaPackage(org.opencastproject.mediapackage.MediaPackage) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 2 with FileUploadException

use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.

the class FileUploadRestService method postPayload.

@POST
@Produces(MediaType.APPLICATION_XML)
@Path("job/{jobID}")
@RestQuery(name = "newjob", description = "Appends the next chunk of data to the file on the server.", pathParameters = { @RestParameter(description = "The ID of the upload job", isRequired = false, name = "jobID", type = RestParameter.Type.STRING) }, restParameters = { @RestParameter(description = "The number of the current chunk", isRequired = false, name = "chunknumber", type = RestParameter.Type.STRING), @RestParameter(description = "The payload", isRequired = false, name = "filedata", type = RestParameter.Type.FILE) }, reponses = { @RestResponse(description = "the chunk data was successfully appended to file on server", responseCode = HttpServletResponse.SC_OK), @RestResponse(description = "the upload job was not found", responseCode = HttpServletResponse.SC_NOT_FOUND), @RestResponse(description = "the request was malformed", responseCode = HttpServletResponse.SC_BAD_REQUEST) }, returnDescription = "The XML representation of the updated upload job")
public Response postPayload(@PathParam("jobID") String jobId, @Context HttpServletRequest request) {
    try {
        if (!ServletFileUpload.isMultipartContent(request)) {
            // make sure request is "multipart/form-data"
            throw new FileUploadException("Request is not of type multipart/form-data");
        }
        if (uploadService.hasJob(jobId)) {
            // testing for existence of job here already so we can generate a 404 early
            long chunkNum = 0;
            FileUploadJob job = uploadService.getJob(jobId);
            ServletFileUpload upload = new ServletFileUpload();
            for (FileItemIterator iter = upload.getItemIterator(request); iter.hasNext(); ) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (REQUESTFIELD_CHUNKNUM.equalsIgnoreCase(name)) {
                        chunkNum = Long.parseLong(Streams.asString(item.openStream()));
                    }
                } else if (REQUESTFIELD_DATA.equalsIgnoreCase(item.getFieldName())) {
                    uploadService.acceptChunk(job, chunkNum, item.openStream());
                    return Response.ok(job).build();
                }
            }
            throw new FileUploadException("No payload!");
        } else {
            log.warn("Upload job not found: " + jobId);
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.serverError().entity(buildUnexpectedErrorMessage(e)).build();
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 3 with FileUploadException

use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.

the class FileUploadServiceImpl method getJob.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.fileupload.api.FileUploadService#getJob(String id)
 */
@Override
public FileUploadJob getJob(String id) throws FileUploadException {
    if (jobCache.containsKey(id)) {
        // job already cached?
        return jobCache.get(id);
    } else {
        // job not in cache?
        try {
            // try to load job from filesystem
            synchronized (this) {
                File jobFile = getJobFile(id);
                FileUploadJob job = (FileUploadJob) jobUnmarshaller.unmarshal(jobFile);
                // get last modified time from job file
                job.setLastModified(jobFile.lastModified());
                return job;
            }
        // if loading from fs also fails
        } catch (Exception e) {
            // we could not find the job and throw an Exception
            throw fileUploadException(Severity.warn, "Failed to load job " + id + " from file.", e);
        }
    }
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) File(java.io.File) ConfigurationException(org.osgi.service.cm.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException)

Example 4 with FileUploadException

use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.

the class FileUploadServiceImpl method createJob.

// </editor-fold>
/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.fileupload.api.FileUploadService#createJob(String, long, int,
 *      org.opencastproject.mediapackage.MediaPackage, org.opencastproject.mediapackage.MediaPackageElementFlavor)
 */
@Override
public FileUploadJob createJob(String filename, long filesize, int chunksize, MediaPackage mp, MediaPackageElementFlavor flavor) throws FileUploadException {
    FileUploadJob job = new FileUploadJob(filename, filesize, chunksize, mp, flavor);
    logger.info("Creating new upload job: {}", job);
    try {
        // create working dir
        File jobDir = getJobDir(job.getId());
        FileUtils.forceMkdir(jobDir);
        // create empty payload file
        ensureExists(getPayloadFile(job.getId()));
        // create job file
        storeJob(job);
    } catch (FileUploadException e) {
        deleteJob(job.getId());
        throw fileUploadException(Severity.error, "Could not create job file in " + workRoot.getAbsolutePath(), e);
    } catch (IOException e) {
        deleteJob(job.getId());
        throw fileUploadException(Severity.error, "Could not create upload job directory in " + workRoot.getAbsolutePath(), e);
    }
    return job;
}
Also used : FileUploadJob(org.opencastproject.fileupload.api.job.FileUploadJob) IOException(java.io.IOException) File(java.io.File) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException)

Example 5 with FileUploadException

use of org.opencastproject.fileupload.api.exception.FileUploadException in project opencast by opencast.

the class FileUploadServiceImpl method putPayloadIntoMediaPackage.

/**
 * Puts the payload of an upload job into a MediaPackage in the WFR, adds the files as a track to the MediaPackage and
 * returns the files URL in the WFR.
 *
 * @param job
 * @return URL of the file in the WFR
 * @throws FileUploadException
 */
private URL putPayloadIntoMediaPackage(FileUploadJob job) throws FileUploadException {
    MediaPackage mediaPackage = job.getPayload().getMediaPackage();
    MediaPackageElementFlavor flavor = job.getPayload().getFlavor();
    List<Track> excludeTracks = Arrays.asList(mediaPackage.getTracks(flavor));
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(getPayloadFile(job.getId()));
        MediaPackage mp = ingestService.addTrack(fileInputStream, job.getPayload().getFilename(), job.getPayload().getFlavor(), mediaPackage);
        List<Track> tracks = new ArrayList<Track>(Arrays.asList(mp.getTracks(flavor)));
        tracks.removeAll(excludeTracks);
        if (tracks.size() != 1)
            throw new FileUploadException("Ingested track not found");
        return tracks.get(0).getURI().toURL();
    } catch (Exception e) {
        throw fileUploadException(Severity.error, "Failed to add payload to MediaPackage.", e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) ArrayList(java.util.ArrayList) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Track(org.opencastproject.mediapackage.Track) FileInputStream(java.io.FileInputStream) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) ConfigurationException(org.osgi.service.cm.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException)

Aggregations

FileUploadException (org.opencastproject.fileupload.api.exception.FileUploadException)8 IOException (java.io.IOException)6 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)5 MalformedURLException (java.net.MalformedURLException)5 ConfigurationException (org.osgi.service.cm.ConfigurationException)5 FileUploadJob (org.opencastproject.fileupload.api.job.FileUploadJob)4 FileInputStream (java.io.FileInputStream)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 MediaPackage (org.opencastproject.mediapackage.MediaPackage)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1 FileItemStream (org.apache.commons.fileupload.FileItemStream)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1